ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
Similarity_Table Class Reference

Represents a similarity graph between functions (paths). More...

#include <similarity_table.hpp>

Public Types

enum class  Cache_Compatibility { Success , Fail }
 

Public Member Functions

 Similarity_Table (double _similarity_threshold)
 Constructs with custom similarity threshold. More...
 
 Similarity_Table ()
 Constructs with default similarity threshold. More...
 
Cache_Compatibility load ()
 
void update_similarity (double new_similarity_threshold)
 Updates similarity threshold. More...
 
double get_similarity (const Path &path1, const Path &path2)
 Gets similarity between two paths. More...
 
double get_threshold_similarity () const
 Gets the similarity threshold. More...
 
const Path & get_path (PathId id) const
 
bool is_similar (const Path &path1, const Path &path2)
 Checks if two paths are similar. More...
 
void normalize_pair (PathId &id1, PathId &id2) const
 
const std::vector< Path > & get_path_list () const
 Gets list of all known paths. More...
 
int get_number_lines_in_pair (const Path &path1, const Path &path2)
 
std::vector< Path > get_similar_path_to_the_reference (const Path &reference)
 Gets paths similar to reference path. More...
 
std::vector< SimilarPair > get_all_similar_pairs ()
 Gets all similar path pairs with scores, sorted. More...
 
bool compare_paths (const SimilarPair &a, const SimilarPair &b) const
 
void sort_pairs_by_line_number (std::vector< SimilarPair > &similar_pairs) const
 Sorts path pairs by line count. More...
 
void sort_pairs_by_similarity (std::vector< SimilarPair > &similar_pairs) const
 Sorts path pairs by similarity. More...
 
std::vector< Cluster > get_clusters ()
 Generate clusters of similar functions using DFS on the similarity graph. More...
 
std::vector< ClusterInfo > get_clusters_info (bool sorted)
 Returns detailed information about all clusters found in the similarity table. More...
 

Detailed Description

Represents a similarity graph between functions (paths).

Each node corresponds to a function (identified by a Path). Stores pairs of similar functions with their similarity scores.

Internally:

  • paths stores all known functions.
  • path_id maps a Path to its unique node ID.
  • similarity_graph is an adjacency list representation: node -> [(neighbor_id, similarity), ...]
  • similarity_table stores pairwise similarity for fast lookup.

Graph interpretation:

  • Nodes = functions
  • Edges = similarity >= threshold (filtered at query time)
Note
The graph is undirected.

Definition at line 104 of file similarity_table.hpp.

Member Enumeration Documentation

◆ Cache_Compatibility

Enumerator
Success 
Fail 

Definition at line 107 of file similarity_table.hpp.

Constructor & Destructor Documentation

◆ Similarity_Table() [1/2]

Similarity_Table::Similarity_Table ( double  _similarity_threshold)
explicit

Constructs with custom similarity threshold.

Parameters
_similarity_thresholdInitial threshold value

Definition at line 67 of file similarity_table.cpp.

◆ Similarity_Table() [2/2]

Similarity_Table::Similarity_Table ( )
explicit

Constructs with default similarity threshold.

Definition at line 70 of file similarity_table.cpp.

Member Function Documentation

◆ compare_paths()

bool Similarity_Table::compare_paths ( const SimilarPair &  a,
const SimilarPair &  b 
) const

Definition at line 150 of file similarity_table.cpp.

◆ get_all_similar_pairs()

std::vector< SimilarPair > Similarity_Table::get_all_similar_pairs ( )

Gets all similar path pairs with scores, sorted.

Returns
vector<SimilarPair> Similar pairs with scores

Definition at line 128 of file similarity_table.cpp.

◆ get_clusters()

std::vector< Cluster > Similarity_Table::get_clusters ( )

Generate clusters of similar functions using DFS on the similarity graph.

Only edges with similarity >= threshold are considered.

Returns
vector of Cluster objects

Definition at line 191 of file similarity_table.cpp.

◆ get_clusters_info()

std::vector< ClusterInfo > Similarity_Table::get_clusters_info ( bool  sorted)

Returns detailed information about all clusters found in the similarity table.

Returns
std::vector<ClusterInfo>

Definition at line 228 of file similarity_table.cpp.

◆ get_number_lines_in_pair()

int Similarity_Table::get_number_lines_in_pair ( const Path &  path1,
const Path &  path2 
)

Definition at line 184 of file similarity_table.cpp.

◆ get_path()

const Path & Similarity_Table::get_path ( PathId  id) const

Definition at line 96 of file similarity_table.cpp.

◆ get_path_list()

const std::vector< Path > & Similarity_Table::get_path_list ( ) const

Gets list of all known paths.

Returns
vector<Path> All paths in table

Definition at line 113 of file similarity_table.cpp.

◆ get_similar_path_to_the_reference()

std::vector< Path > Similarity_Table::get_similar_path_to_the_reference ( const Path &  reference)

Gets paths similar to reference path.

Parameters
referencePath to compare against
Returns
vector<Path> Similar paths

Definition at line 117 of file similarity_table.cpp.

◆ get_similarity()

double Similarity_Table::get_similarity ( const Path &  path1,
const Path &  path2 
)

Gets similarity between two paths.

Parameters
path1First path to compare
path2Second path to compare
Returns
double Similarity score

Definition at line 81 of file similarity_table.cpp.

◆ get_threshold_similarity()

double Similarity_Table::get_threshold_similarity ( ) const

Gets the similarity threshold.

Returns
double Similarity threshold value.

Definition at line 104 of file similarity_table.cpp.

◆ is_similar()

bool Similarity_Table::is_similar ( const Path &  path1,
const Path &  path2 
)

Checks if two paths are similar.

Parameters
path1First path to compare
path2Second path to compare
Returns
bool True if paths are similar

Definition at line 108 of file similarity_table.cpp.

◆ load()

Similarity_Table::Cache_Compatibility Similarity_Table::load ( )

Definition at line 73 of file similarity_table.cpp.

◆ normalize_pair()

void Similarity_Table::normalize_pair ( PathId &  id1,
PathId &  id2 
) const

Definition at line 8 of file similarity_table.cpp.

◆ sort_pairs_by_line_number()

void Similarity_Table::sort_pairs_by_line_number ( std::vector< SimilarPair > &  similar_pairs) const

Sorts path pairs by line count.

Parameters
similar_path_pairsPairs to sort
Returns
Sorted pairs with line counts

Definition at line 172 of file similarity_table.cpp.

◆ sort_pairs_by_similarity()

void Similarity_Table::sort_pairs_by_similarity ( std::vector< SimilarPair > &  similar_pairs) const

Sorts path pairs by similarity.

Parameters
similar_path_pairsPairs to sort
Returns
Sorted pairs with similarity

Definition at line 160 of file similarity_table.cpp.

◆ update_similarity()

void Similarity_Table::update_similarity ( double  new_similarity_threshold)

Updates similarity threshold.

Parameters
new_similarity_thresholdNew threshold value

Definition at line 77 of file similarity_table.cpp.


The documentation for this class was generated from the following files: