From 191eaae22502e28edde2a86a5ce05d15fb3838a5 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 19 Dec 2024 16:20:48 +0000 Subject: [PATCH] Added transparent comparator overloads of contains() --- include/etl/unordered_map.h | 11 +++++++++++ include/etl/unordered_multimap.h | 11 +++++++++++ include/etl/unordered_multiset.h | 11 +++++++++++ include/etl/unordered_set.h | 11 +++++++++++ test/test_unordered_map.cpp | 15 +++++++++++++-- test/test_unordered_multimap.cpp | 15 +++++++++++++-- test/test_unordered_multiset.cpp | 13 +++++++++++++ test/test_unordered_set.cpp | 13 +++++++++++++ 8 files changed, 96 insertions(+), 4 deletions(-) diff --git a/include/etl/unordered_map.h b/include/etl/unordered_map.h index b9d73d2f4..54d0bafaa 100644 --- a/include/etl/unordered_map.h +++ b/include/etl/unordered_map.h @@ -1689,6 +1689,17 @@ namespace etl return find(key) != end(); } +#if ETL_USING_CPP11 + //************************************************************************* + /// Check if the unordered_map contains the key. + //************************************************************************* + template ::value, int> = 0> + bool contains(const K& key) const + { + return find(key) != end(); + } +#endif + protected: //********************************************************************* diff --git a/include/etl/unordered_multimap.h b/include/etl/unordered_multimap.h index b75eb84b3..3db0d1f5c 100644 --- a/include/etl/unordered_multimap.h +++ b/include/etl/unordered_multimap.h @@ -1450,6 +1450,17 @@ namespace etl return find(key) != end(); } +#if ETL_USING_CPP11 + //************************************************************************* + /// Check if the unordered_map contains the key. + //************************************************************************* + template ::value, int> = 0> + bool contains(const K& key) const + { + return find(key) != end(); + } +#endif + protected: //********************************************************************* diff --git a/include/etl/unordered_multiset.h b/include/etl/unordered_multiset.h index a71ae1099..e0cf15909 100644 --- a/include/etl/unordered_multiset.h +++ b/include/etl/unordered_multiset.h @@ -1501,6 +1501,17 @@ namespace etl return find(key) != end(); } +#if ETL_USING_CPP11 + //************************************************************************* + /// Check if the unordered_map contains the key. + //************************************************************************* + template ::value, int> = 0> + bool contains(const K& key) const + { + return find(key) != end(); + } +#endif + protected: //********************************************************************* diff --git a/include/etl/unordered_set.h b/include/etl/unordered_set.h index fd365102b..795eabfd6 100644 --- a/include/etl/unordered_set.h +++ b/include/etl/unordered_set.h @@ -1510,6 +1510,17 @@ namespace etl return find(key) != end(); } +#if ETL_USING_CPP11 + //************************************************************************* + /// Check if the unordered_map contains the key. + //************************************************************************* + template ::value, int> = 0> + bool contains(const K& key) const + { + return find(key) != end(); + } +#endif + protected: //********************************************************************* diff --git a/test/test_unordered_map.cpp b/test/test_unordered_map.cpp index 1c3ac5320..c38fb0dd9 100644 --- a/test/test_unordered_map.cpp +++ b/test/test_unordered_map.cpp @@ -1417,8 +1417,19 @@ namespace const char* not_inserted = "ZZ"; - CHECK(data.contains(std::string(K0))); - CHECK(!data.contains(std::string(not_inserted))); + CHECK_TRUE(data.contains(K0)); + CHECK_FALSE(data.contains(std::string(not_inserted))); + } + + //************************************************************************* + TEST(test_contains_with_transparent_comparator) + { + DataNDCTransparent data(initial_data.begin(), initial_data.end()); + + const char* not_inserted = "ZZ"; + + CHECK_TRUE(data.contains("FF")); + CHECK_FALSE(data.contains(not_inserted)); } }; } diff --git a/test/test_unordered_multimap.cpp b/test/test_unordered_multimap.cpp index f072d54a1..716636e0e 100644 --- a/test/test_unordered_multimap.cpp +++ b/test/test_unordered_multimap.cpp @@ -1234,8 +1234,19 @@ namespace const char* not_inserted = "ZZ"; - CHECK(data.contains(K0)); - CHECK(!data.contains(not_inserted)); + CHECK_TRUE(data.contains(K0)); + CHECK_FALSE(data.contains(not_inserted)); + } + + //************************************************************************* + TEST(test_contains_with_transparent_comparator) + { + DataNDCTransparent data(initial_data.begin(), initial_data.end()); + + const char* not_inserted = "ZZ"; + + CHECK_TRUE(data.contains("FF")); + CHECK_FALSE(data.contains(not_inserted)); } }; } diff --git a/test/test_unordered_multiset.cpp b/test/test_unordered_multiset.cpp index ad65e3642..27bfe310e 100644 --- a/test/test_unordered_multiset.cpp +++ b/test/test_unordered_multiset.cpp @@ -1090,5 +1090,18 @@ namespace CHECK_TRUE(data.contains(N0)); CHECK_FALSE(data.contains(not_inserted)); } + + //************************************************************************* + TEST(test_contains_with_transparent_comparator) + { + std::array initial = { "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH" }; + + DataTransparent data(initial.begin(), initial.end()); + + const char* not_inserted = "ZZ"; + + CHECK_TRUE(data.contains("FF")); + CHECK_FALSE(data.contains(not_inserted)); + } }; } diff --git a/test/test_unordered_set.cpp b/test/test_unordered_set.cpp index 0bf4b4315..189f97615 100644 --- a/test/test_unordered_set.cpp +++ b/test/test_unordered_set.cpp @@ -1055,5 +1055,18 @@ namespace CHECK_TRUE(data.contains(N0)); CHECK_FALSE(data.contains(not_inserted)); } + + //************************************************************************* + TEST(test_contains_with_transparent_comparator) + { + std::array initial = { "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH" }; + + DataTransparent data(initial.begin(), initial.end()); + + const char* not_inserted = "ZZ"; + + CHECK_TRUE(data.contains("FF")); + CHECK_FALSE(data.contains(not_inserted)); + } }; }