Skip to content

Commit

Permalink
Sort pairs by value if frequencies are equal in Cords
Browse files Browse the repository at this point in the history
Sort pairs by value if frequencies are equal in Cords'
FrequencyHandler::InitFrequencyHandler
  • Loading branch information
p-senichenkov committed Dec 19, 2024
1 parent f398aaf commit 9bc09e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/core/algorithms/fd/sfd/frequency_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <cstddef>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -30,7 +31,9 @@ void FrequencyHandler::InitFrequencyHandler(std::vector<model::TypedColumnData>

auto cmp = [](std::pair<std::string, size_t> const &left,
std::pair<std::string, size_t> const &right) {
return left.second > right.second;
// Compare frequencies.
// If frequencies are equal, compare values lexicographically.
return std::tie(left.second, left.first) > std::tie(right.second, right.first);
};

std::sort(values_ordered_by_frequencies.begin(), values_ordered_by_frequencies.end(), cmp);
Expand Down
62 changes: 31 additions & 31 deletions src/tests/test_sfd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,41 @@ namespace tests {

TEST(TestCordsUtils, FrequenciesOfIris) {
std::vector<std::vector<std::pair<std::string, size_t>>> expected = {
{{"7.400000", 34}, {"7.600000", 33}, {"4.300000", 32}, {"7.100000", 31},
{"6.100000", 11}, {"5.400000", 8}, {"6.600000", 25}, {"6.400000", 7},
{"5.800000", 5}, {"4.800000", 14}, {"5.200000", 18}, {"4.900000", 9},
{"5.500000", 6}, {"5.700000", 4}, {"4.600000", 16}, {"5.100000", 2},
{"6.000000", 10}, {"5.600000", 12}, {"4.500000", 27}, {"6.700000", 3},
{"6.300000", 1}, {"6.500000", 13}, {"6.200000", 15}, {"7.300000", 30},
{"7.900000", 29}, {"6.900000", 17}, {"6.800000", 20}, {"7.000000", 28},
{"5.900000", 21}, {"4.700000", 24}, {"5.000000", 0}, {"4.400000", 22},
{"7.700000", 19}, {"7.200000", 23}, {"5.300000", 26}},

{{"4.200000", 21}, {"4.400000", 19}, {"4.000000", 18}, {"2.400000", 16},
{"3.700000", 15}, {"4.100000", 20}, {"3.600000", 14}, {"2.800000", 1},
{{"4.300000", 34}, {"4.500000", 33}, {"5.300000", 32}, {"7.100000", 30},
{"5.400000", 11}, {"6.600000", 24}, {"6.100000", 8}, {"7.600000", 27},
{"5.800000", 6}, {"4.800000", 14}, {"5.200000", 18}, {"6.400000", 5},
{"4.900000", 12}, {"5.500000", 7}, {"5.700000", 4}, {"4.600000", 19},
{"5.100000", 2}, {"6.000000", 9}, {"5.600000", 10}, {"6.700000", 3},
{"6.300000", 1}, {"6.500000", 13}, {"6.200000", 17}, {"7.700000", 15},
{"7.200000", 20}, {"7.300000", 29}, {"6.900000", 16}, {"7.900000", 26},
{"6.800000", 21}, {"7.000000", 31}, {"5.900000", 22}, {"4.700000", 25},
{"5.000000", 0}, {"4.400000", 23}, {"7.400000", 28}},

{{"4.000000", 21}, {"4.200000", 19}, {"4.400000", 18}, {"2.400000", 15},
{"4.100000", 20}, {"3.600000", 14}, {"3.700000", 13}, {"2.800000", 1},
{"3.800000", 8}, {"3.200000", 2}, {"3.900000", 17}, {"3.100000", 4},
{"3.300000", 10}, {"2.900000", 5}, {"2.000000", 22}, {"2.500000", 7},
{"3.400000", 3}, {"3.500000", 9}, {"2.300000", 12}, {"2.200000", 13},
{"3.400000", 3}, {"3.500000", 9}, {"2.300000", 12}, {"2.200000", 16},
{"3.000000", 0}, {"2.700000", 6}, {"2.600000", 11}},

{{"6.900000", 42}, {"6.300000", 41}, {"6.600000", 40}, {"1.000000", 39},
{"1.100000", 38}, {"3.000000", 37}, {"3.600000", 34}, {"5.400000", 32},
{"6.700000", 31}, {"5.300000", 30}, {"5.900000", 29}, {"4.900000", 7},
{"5.800000", 15}, {"3.700000", 36}, {"5.200000", 27}, {"4.800000", 10},
{"1.900000", 28}, {"4.500000", 3}, {"1.300000", 4}, {"5.100000", 2},
{"6.000000", 23}, {"1.600000", 5}, {"4.400000", 12}, {"5.000000", 11},
{"6.400000", 35}, {"3.500000", 24}, {"1.500000", 0}, {"4.200000", 13},
{"6.100000", 17}, {"3.800000", 33}, {"1.400000", 1}, {"5.700000", 16},
{"4.600000", 19}, {"5.500000", 18}, {"1.200000", 22}, {"4.100000", 20},
{"4.000000", 8}, {"5.600000", 6}, {"3.900000", 21}, {"4.700000", 9},
{"1.700000", 14}, {"3.300000", 25}, {"4.300000", 26}},

{{"0.600000", 20}, {"1.700000", 19}, {"1.100000", 18}, {"1.900000", 13},
{"0.200000", 0}, {"2.400000", 16}, {"1.300000", 1}, {"2.100000", 10},
{"1.800000", 2}, {"2.200000", 15}, {"0.400000", 6}, {"1.500000", 3},
{"0.100000", 9}, {"1.400000", 4}, {"2.300000", 5}, {"0.300000", 7},
{"0.500000", 21}, {"2.500000", 17}, {"1.600000", 14}, {"2.000000", 11},
{"1.200000", 12}, {"1.000000", 8}},
{{"1.000000", 42}, {"1.100000", 41}, {"3.000000", 40}, {"3.600000", 39},
{"6.300000", 36}, {"6.600000", 34}, {"6.900000", 33}, {"5.900000", 24},
{"4.900000", 7}, {"4.500000", 3}, {"1.300000", 5}, {"1.900000", 31},
{"4.800000", 11}, {"5.800000", 16}, {"3.700000", 38}, {"5.200000", 27},
{"5.400000", 25}, {"5.100000", 2}, {"6.000000", 23}, {"1.600000", 4},
{"4.400000", 12}, {"5.000000", 10}, {"6.700000", 22}, {"6.400000", 35},
{"3.500000", 29}, {"1.500000", 0}, {"4.200000", 13}, {"6.100000", 15},
{"3.300000", 30}, {"4.700000", 8}, {"1.700000", 14}, {"3.800000", 37},
{"1.400000", 1}, {"5.700000", 17}, {"4.600000", 19}, {"5.500000", 18},
{"1.200000", 32}, {"4.100000", 20}, {"4.000000", 9}, {"5.600000", 6},
{"3.900000", 21}, {"5.300000", 26}, {"4.300000", 28}},

{{"0.600000", 20}, {"1.700000", 19}, {"1.100000", 18}, {"1.200000", 13},
{"0.200000", 0}, {"2.400000", 16}, {"1.300000", 1}, {"2.100000", 9},
{"1.800000", 2}, {"2.200000", 17}, {"0.400000", 7}, {"1.500000", 3},
{"0.100000", 11}, {"2.300000", 4}, {"1.400000", 5}, {"1.000000", 6},
{"0.300000", 8}, {"0.500000", 21}, {"2.500000", 15}, {"1.600000", 14},
{"2.000000", 10}, {"1.900000", 12}},

{{"Iris-setosa", 2}, {"Iris-versicolor", 1}, {"Iris-virginica", 0}}};

Expand Down

0 comments on commit 9bc09e3

Please sign in to comment.