diff --git a/include/etl/map.h b/include/etl/map.h index 785e566f..e5529ce3 100644 --- a/include/etl/map.h +++ b/include/etl/map.h @@ -474,26 +474,18 @@ namespace etl typedef const value_type* const_pointer; typedef size_t size_type; - //************************************************************************* - /// How to compare two key elements. - //************************************************************************* - struct key_comp + class value_compare { - bool operator ()(const key_type& key1, const key_type& key2) const - { - return compare(key1, key2); - } - }; + public: - //************************************************************************* - /// How to compare two value elements. - //************************************************************************* - struct value_comp - { - bool operator ()(const value_type& value1, const value_type& value2) const + bool operator()(const value_type& lhs, const value_type& rhs) const { - return compare(value1.first, value2.first); + return (kcompare(lhs.first, rhs.first)); } + + private: + + key_compare kcompare; }; protected: @@ -524,15 +516,15 @@ namespace etl //************************************************************************* bool node_comp(const Data_Node& node1, const Data_Node& node2) const { - return compare(node1.value.first, node2.value.first); + return kcompare(node1.value.first, node2.value.first); } bool node_comp(const Data_Node& node, key_parameter_t key) const { - return compare(node.value.first, key); + return kcompare(node.value.first, key); } bool node_comp(key_parameter_t key, const Data_Node& node) const { - return compare(key, node.value.first); + return kcompare(key, node.value.first); } private: @@ -540,7 +532,8 @@ namespace etl /// The pool of data nodes used in the map. ipool* p_node_pool; - key_compare compare; + key_compare kcompare; + value_compare vcompare; //************************************************************************* /// Downcast a Node* to a Data_Node* @@ -1236,6 +1229,22 @@ namespace etl return *this; } + //************************************************************************* + /// How to compare two key elements. + //************************************************************************* + key_compare key_comp() const + { + return kcompare; + }; + + //************************************************************************* + /// How to compare two value elements. + //************************************************************************* + value_compare value_comp() const + { + return vcompare; + }; + protected: //************************************************************************* diff --git a/include/etl/multimap.h b/include/etl/multimap.h index 8f151884..a50f0aa3 100644 --- a/include/etl/multimap.h +++ b/include/etl/multimap.h @@ -631,26 +631,18 @@ namespace etl typedef const value_type* const_pointer; typedef size_t size_type; - //************************************************************************* - /// How to compare two key elements. - //************************************************************************* - struct key_comp + class value_compare { - bool operator ()(const key_type& key1, const key_type& key2) const - { - return compare(key1, key2); - } - }; + public: - //************************************************************************* - /// How to compare two value elements. - //************************************************************************* - struct value_comp - { - bool operator ()(const value_type& value1, const value_type& value2) const + bool operator()(const value_type& lhs, const value_type& rhs) const { - return compare(value1.first, value2.first); + return (kcompare(lhs.first, rhs.first)); } + + private: + + key_compare kcompare; }; protected: @@ -676,17 +668,17 @@ namespace etl //************************************************************************* bool node_comp(const Data_Node& node1, const Data_Node& node2) const { - return compare(node1.value.first, node2.value.first); + return kcompare(node1.value.first, node2.value.first); } bool node_comp(const Data_Node& node, key_parameter_t key) const { - return compare(node.value.first, key); + return kcompare(node.value.first, key); } bool node_comp(key_parameter_t key, const Data_Node& node) const { - return compare(key, node.value.first); + return kcompare(key, node.value.first); } private: @@ -694,7 +686,8 @@ namespace etl /// The pool of data nodes used in the multimap. ipool* p_node_pool; - key_compare compare; + key_compare kcompare; + value_compare vcompare; //************************************************************************* /// Downcast a Node* to a Data_Node* @@ -1330,6 +1323,22 @@ namespace etl return *this; } + //************************************************************************* + /// How to compare two key elements. + //************************************************************************* + key_compare key_comp() const + { + return kcompare; + }; + + //************************************************************************* + /// How to compare two value elements. + //************************************************************************* + value_compare value_comp() const + { + return vcompare; + }; + protected: //************************************************************************* diff --git a/include/etl/multiset.h b/include/etl/multiset.h index 8f78dc7f..cf79c76e 100644 --- a/include/etl/multiset.h +++ b/include/etl/multiset.h @@ -629,28 +629,6 @@ namespace etl typedef value_type* const_pointer; typedef size_t size_type; - //************************************************************************* - /// How to compare two key elements. - //************************************************************************* - struct key_comp - { - bool operator ()(key_type& key1, key_type& key2) const - { - return compare(key1, key2); - } - }; - - //************************************************************************* - /// How to compare two value elements. - //************************************************************************* - struct value_comp - { - bool operator ()(value_type& value1, value_type& value2) const - { - return compare(value1, value2); - } - }; - protected: //************************************************************************* @@ -1311,6 +1289,22 @@ namespace etl return *this; } + //************************************************************************* + /// How to compare two key elements. + //************************************************************************* + key_compare key_comp() const + { + return compare; + }; + + //************************************************************************* + /// How to compare two value elements. + //************************************************************************* + value_compare value_comp() const + { + return compare; + }; + protected: //************************************************************************* diff --git a/include/etl/set.h b/include/etl/set.h index 9736b005..26a172ce 100644 --- a/include/etl/set.h +++ b/include/etl/set.h @@ -467,28 +467,6 @@ namespace etl typedef value_type* const_pointer; typedef size_t size_type; - //************************************************************************* - /// How to compare two key elements. - //************************************************************************* - struct key_comp - { - bool operator ()(key_type& key1, key_type& key2) const - { - return compare(key1, key2); - } - }; - - //************************************************************************* - /// How to compare two value elements. - //************************************************************************* - struct value_comp - { - bool operator ()(value_type& value1, value_type& value2) const - { - return compare(value1, value2); - } - }; - protected: //************************************************************************* @@ -1159,6 +1137,22 @@ namespace etl return const_iterator(*this, find_upper_node(root_node, key)); } + //************************************************************************* + /// How to compare two key elements. + //************************************************************************* + key_compare key_comp() const + { + return compare; + }; + + //************************************************************************* + /// How to compare two value elements. + //************************************************************************* + value_compare value_comp() const + { + return compare; + }; + protected: //************************************************************************* diff --git a/test/test_map.cpp b/test/test_map.cpp index c1cdedec..686089e1 100644 --- a/test/test_map.cpp +++ b/test/test_map.cpp @@ -1055,5 +1055,42 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_key_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::key_compare compare = data.key_comp(); + + Data::key_type a("A"); + Data::key_type b("B"); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_value_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::value_compare compare = data.value_comp(); + + Data::value_type a(std::string("A"), 0); + Data::value_type b(std::string("B"), 1); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } }; } diff --git a/test/test_multimap.cpp b/test/test_multimap.cpp index 018ff277..c221a34d 100644 --- a/test/test_multimap.cpp +++ b/test/test_multimap.cpp @@ -1010,5 +1010,42 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_key_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::key_compare compare = data.key_comp(); + + Data::key_type a("A"); + Data::key_type b("B"); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_value_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::value_compare compare = data.value_comp(); + + Data::value_type a(std::string("A"), 0); + Data::value_type b(std::string("B"), 1); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } }; } diff --git a/test/test_multiset.cpp b/test/test_multiset.cpp index 14982819..28397153 100644 --- a/test/test_multiset.cpp +++ b/test/test_multiset.cpp @@ -968,5 +968,42 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_key_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::key_compare compare = data.key_comp(); + + Data::key_type a(1); + Data::key_type b(2); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_value_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::value_compare compare = data.value_comp(); + + Data::value_type a(1); + Data::value_type b(2); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } }; } diff --git a/test/test_set.cpp b/test/test_set.cpp index 57bfc7ea..7892c8b5 100644 --- a/test/test_set.cpp +++ b/test/test_set.cpp @@ -945,5 +945,42 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_key_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::key_compare compare = data.key_comp(); + + Data::key_type a(1); + Data::key_type b(2); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_value_compare) + { + const Data data(initial_data.begin(), initial_data.end()); + + Data::value_compare compare = data.value_comp(); + + Data::value_type a(1); + Data::value_type b(2); + +#ifdef TEST_GREATER_THAN + CHECK(!compare(a, b)); + CHECK(compare(b, a)); +#else + CHECK(compare(a, b)); + CHECK(!compare(b, a)); +#endif + } }; }