ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
random_selector.cpp
Go to the documentation of this file.
1#include "random_selector.hpp"
6
8
9bool RandomSelector::is_valid_pair(SimilarPair path_pair) {
10 if (path_pair.similarity < minimum_similarity)
11 return false;
12 if (path_pair.similarity > maximum_similarity)
13 return false;
14 return true;
15}
16
17std::vector<SimilarPair> RandomSelector::get_similarity_pairs_filtered() {
18 auto path_pairs = similarity_table->get_all_similar_pairs();
19 similarity_table->sort_pairs_by_similarity(path_pairs);
20 std::vector<SimilarPair> ret;
21 for (auto path_pair : path_pairs) {
22 if (is_valid_pair(path_pair)) {
23 ret.push_back(path_pair);
24 }
25 }
26 return ret;
27}
28
29std::vector<SimilarPair> RandomSelector::make_random_selection(std::vector<SimilarPair> path_pairs) {
30 shuffle(path_pairs.begin(), path_pairs.end(), rng);
31 while (int(path_pairs.size()) > maximum_quantity) {
32 path_pairs.pop_back();
33 }
34 return path_pairs;
35}
36
37void RandomSelector::print_path_pairs(std::vector<SimilarPair> path_pairs) {
38 std::vector<RandomSelectorEntry> vector_entry = {};
39 FunctionLoader loader;
40 for (const auto& path_pair : path_pairs) {
41 const Path& path1 = similarity_table->get_path(path_pair.id1);
42 const Path& path2 = similarity_table->get_path(path_pair.id2);
43
44 auto function1 = loader.load_metadata(path1);
45 auto function2 = loader.load_metadata(path2);
46
47 vector_entry.push_back(RandomSelectorEntry{
50 path_pair.similarity
51 });
52 }
53 if (vector_entry.size() <= 0) return;
54
55 fm::write(TEMPLATE_RANDOM_ENTRY, vector_entry, Format::AUTO, [](size_t i) {
56 return (i % 2 == 0)
57 ? fm::get_formatter()->style().at("row_even")
58 : fm::get_formatter()->style().at("row_odd");
59 });
60}
61
62RandomSelector::RandomSelector(Similarity_Table* _similarity_table) {
63 similarity_table = _similarity_table;
64 similarity_table->update_similarity(0);
65 minimum_similarity = 0;
66 maximum_similarity = 0;
67 maximum_quantity = 0;
68}
69
71 if (options.extra_args.size() <= 2) {
72 throw CLIError("Random expect three parameters, but less was given");
73 return false;
74 }
75
76 return true;
77}
78
79bool RandomSelector::run(const ParsedOptions& options) {
80 minimum_similarity = stod(options.extra_args[0]);
81 maximum_similarity = stod(options.extra_args[1]);
82 maximum_quantity = stod(options.extra_args[2]);
83
84 auto path_pairs = get_similarity_pairs_filtered();
85 path_pairs = make_random_selection(path_pairs);
86 print_path_pairs(path_pairs);
87
88 return true;
89}
Base class for CLI-related errors.
Definition: cli_error.hpp:10
const CliOption * options() const final
static void write(const std::string &template_str, const std::vector< T > &data, enum Format effective=Format::AUTO, RowColorFn color_fn=nullptr, std::ostream &out=std::cout)
static std::shared_ptr< IFormatter > get_formatter()
Function load_metadata(const Path &path)
Loads function metadata from a path.
static std::string format_path_message_in_pair(const Function &function)
Formats path for display.
Represents a logical path to a function in the codebase.
Definition: path.hpp:26
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
bool run(const ParsedOptions &options) override
Handles random selection command.
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.
const Path & get_path(PathId id) const
void sort_pairs_by_similarity(std::vector< SimilarPair > &similar_pairs) const
Sorts path pairs by similarity.
Provides functionality to load Function objects from external sources.
Provides utilities for printing function information and source code.
Random selection of similar code pairs.