ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
similarity_table.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <fstream>
16#include <map>
17#include <string>
18#include <tuple>
19#include <utility>
20#include <vector>
21
22#include <arkanjo/base/path.hpp>
24
25struct PathId {
26 int value;
27
28 explicit PathId(int v = -1) : value(v) {}
29
30 bool operator==(const PathId& other) const {
31 return value == other.value;
32 }
33
34 bool operator!=(const PathId& other) const {
35 return value != other.value;
36 }
37
38 bool operator<(const PathId& other) const {
39 return value < other.value;
40 }
41
42 operator int() const { return value; }
43};
44
46 double similarity;
47 uint32_t number_lines = 0;
50};
51
60template<typename Key, typename Weight>
61using AdjacencyList = std::vector<std::vector<std::pair<Key, Weight>>>;
62
67struct Cluster {
68 std::vector<int> members;
69};
70
72 std::vector<Path> paths;
73 int total_lines = 0;
74 int total_pairs = 0;
75
76 double score() const {
77 double w_files = 0.3;
78 double w_lines = 0.4;
79 double w_density = 0.3;
80 double d = total_pairs > 0 ? (double)total_lines / total_pairs : 0;
81 return w_files * paths.size() + w_lines * total_lines + w_density * d;
82 }
83};
84
105
106 public:
108 Success,
109 Fail
110 };
111
112 private:
113 static constexpr const double DEFAULT_SIMILARITY = 100.00;
114 static constexpr const double EPS_ERROR_MARGIN = 1e-6;
115 static constexpr const double MAXIMUM_SIMILARITY = 100.00;
116 static constexpr const double MINIMUM_SIMILARITY = 0.00;
117
118 double similarity_threshold;
119 std::vector<Path> paths;
120 std::map<Path, PathId> path_id;
121
125 AdjacencyList<PathId, double> similarity_graph;
126 std::map<std::pair<PathId, PathId>, double> similarity_table;
127
133 PathId find_id_path(const Path& path);
134
142 void read_comparation(std::ifstream& table_file);
143
148 void read_file_table(std::ifstream& table_file);
149
153 Cache_Compatibility init_similarity_table();
154
160 bool is_above_threshold(double similarity) const;
161
162 public:
167 explicit Similarity_Table(double _similarity_threshold);
168
172 explicit Similarity_Table();
173
175
180 void update_similarity(double new_similarity_threshold);
181
188 double get_similarity(const Path& path1, const Path& path2);
189
194 double get_threshold_similarity() const;
195
196 const Path& get_path(PathId id) const;
197
204 bool is_similar(const Path& path1, const Path& path2);
205
206 void normalize_pair(PathId& id1, PathId& id2) const;
207
212 const std::vector<Path>& get_path_list() const;
213
214 int get_number_lines_in_pair(const Path& path1, const Path& path2);
215
221 std::vector<Path> get_similar_path_to_the_reference(const Path& reference);
222
227 std::vector<SimilarPair> get_all_similar_pairs();
228
229 bool compare_paths(const SimilarPair& a, const SimilarPair& b) const;
230
236 void sort_pairs_by_line_number(std::vector<SimilarPair>& similar_pairs) const;
237
243 void sort_pairs_by_similarity(std::vector<SimilarPair>& similar_pairs) const;
244
250 std::vector<Cluster> get_clusters();
251
257 std::vector<ClusterInfo> get_clusters_info(bool sorted);
258};
Represents a logical path to a function in the codebase.
Definition: path.hpp:26
Represents a similarity graph between functions (paths).
void update_similarity(double new_similarity_threshold)
Updates similarity threshold.
std::vector< SimilarPair > get_all_similar_pairs()
Gets all similar path pairs with scores, sorted.
bool compare_paths(const SimilarPair &a, const SimilarPair &b) const
std::vector< ClusterInfo > get_clusters_info(bool sorted)
Returns detailed information about all clusters found in the similarity table.
int get_number_lines_in_pair(const Path &path1, const Path &path2)
const Path & get_path(PathId id) const
void sort_pairs_by_line_number(std::vector< SimilarPair > &similar_pairs) const
Sorts path pairs by line count.
Similarity_Table()
Constructs with default similarity threshold.
void sort_pairs_by_similarity(std::vector< SimilarPair > &similar_pairs) const
Sorts path pairs by similarity.
double get_similarity(const Path &path1, const Path &path2)
Gets similarity between two paths.
std::vector< Cluster > get_clusters()
Generate clusters of similar functions using DFS on the similarity graph.
void normalize_pair(PathId &id1, PathId &id2) const
const std::vector< Path > & get_path_list() const
Gets list of all known paths.
Cache_Compatibility load()
bool is_similar(const Path &path1, const Path &path2)
Checks if two paths are similar.
std::vector< Path > get_similar_path_to_the_reference(const Path &reference)
Gets paths similar to reference path.
double get_threshold_similarity() const
Gets the similarity threshold.
Configuration management interface.
Logical path abstraction for source code elements.
std::vector< std::vector< std::pair< Key, Weight > > > AdjacencyList
{ 1: [{ key, weight }, { key, weight }] 2: [{ key, weight }] 3: [] ... }
std::vector< Path > paths
double score() const
Represents a cluster of similar functions in the similarity graph.
std::vector< int > members
List of node indices (Path IDs) in the cluster.
bool operator==(const PathId &other) const
bool operator!=(const PathId &other) const
PathId(int v=-1)
bool operator<(const PathId &other) const
uint32_t number_lines