Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
random_selector.cpp
Go to the documentation of this file.
2#include "random_selector.hpp"
4
5using fm = FormatterManager;
6
7bool RandomSelector::is_valid_pair(tuple<double, Path, Path> path_pair) {
8 auto [similarity, path1, path2] = path_pair;
9 if (similarity < minimum_similarity)
10 return false;
11 if (similarity > maximum_similarity)
12 return false;
13 return true;
14}
15
16vector<tuple<double, Path, Path>> RandomSelector::get_similarity_pairs_filtered() {
17 auto path_pairs = similarity_table->get_all_path_pairs_and_similarity_sorted_by_similarity();
18 vector<tuple<double, Path, Path>> ret;
19 for (auto path_pair : path_pairs) {
20 if (is_valid_pair(path_pair)) {
21 ret.push_back(path_pair);
22 }
23 }
24 return ret;
25}
26
27vector<tuple<double, Path, Path>> RandomSelector::make_random_selection(vector<tuple<double, Path, Path>> path_pairs) {
28 shuffle(path_pairs.begin(), path_pairs.end(), rng);
29 while (int(path_pairs.size()) > maximum_quantity) {
30 path_pairs.pop_back();
31 }
32 return path_pairs;
33}
34
35void RandomSelector::print_path_pairs(vector<tuple<double, Path, Path>> path_pairs) {
36 std::vector<RandomSelectorEntry> vector_entry = {};
37 for (const auto& [similarity, path1, path2] : path_pairs) {
38 vector_entry.push_back(RandomSelectorEntry{
39 path1.format_path_message_in_pair(),
40 path2.format_path_message_in_pair(),
41 similarity
42 });
43 }
44 if (vector_entry.size() <= 0) return;
45
46 fm::write(TEMPLATE_RANDOM_ENTRY, vector_entry, Format::AUTO, [](size_t i) {
47 return (i % 2 == 0)
48 ? fm::get_formatter()->style().at("row_even")
49 : fm::get_formatter()->style().at("row_odd");
50 });
51}
52
53RandomSelector::RandomSelector(Similarity_Table* _similarity_table) {
54 similarity_table = _similarity_table;
55 similarity_table->update_similarity(0);
56 minimum_similarity = 0;
57 maximum_similarity = 0;
58 maximum_quantity = 0;
59}
60
62 if (options.args.count("help") > 0)
63 return true;
64
65 if (options.extra_args.size() <= 2) {
66 throw CLIError("Random expect three parameters, but less was given");
67 return false;
68 }
69
70 return true;
71}
72
73bool RandomSelector::run(const ParsedOptions& options) {
74 minimum_similarity = stod(options.extra_args[0]);
75 maximum_similarity = stod(options.extra_args[1]);
76 maximum_quantity = stod(options.extra_args[2]);
77
78 auto path_pairs = get_similarity_pairs_filtered();
79 path_pairs = make_random_selection(path_pairs);
80 print_path_pairs(path_pairs);
81
82 return true;
83}
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()
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< std::tuple< double, Path, Path > > get_all_path_pairs_and_similarity_sorted_by_similarity()
Gets all similar path pairs with scores, sorted.
Random selection of similar code pairs.