diff --git a/include/etl/unordered_map.h b/include/etl/unordered_map.h index a4488101f..54d0bafaa 100644 --- a/include/etl/unordered_map.h +++ b/include/etl/unordered_map.h @@ -1681,6 +1681,25 @@ namespace etl } #endif + //************************************************************************* + /// Check if the unordered_map contains the key. + //************************************************************************* + bool contains(const_key_reference key) const + { + 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 122868b0e..3db0d1f5c 100644 --- a/include/etl/unordered_multimap.h +++ b/include/etl/unordered_multimap.h @@ -1442,6 +1442,25 @@ namespace etl } #endif + //************************************************************************* + /// Check if the unordered_multimap contains the key. + //************************************************************************* + bool contains(const_key_reference key) const + { + 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 f2b28a585..e0cf15909 100644 --- a/include/etl/unordered_multiset.h +++ b/include/etl/unordered_multiset.h @@ -1493,6 +1493,25 @@ namespace etl } #endif + //************************************************************************* + /// Check if the unordered_multiset contains the key. + //************************************************************************* + bool contains(key_parameter_t key) const + { + 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 b08bd7552..795eabfd6 100644 --- a/include/etl/unordered_set.h +++ b/include/etl/unordered_set.h @@ -1502,6 +1502,25 @@ namespace etl } #endif + //************************************************************************* + /// Check if the unordered_set contains the key. + //************************************************************************* + bool contains(key_parameter_t key) const + { + 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 382653372..c38fb0dd9 100644 --- a/test/test_unordered_map.cpp +++ b/test/test_unordered_map.cpp @@ -1409,5 +1409,27 @@ namespace CHECK_TRUE(map1 == map2a); CHECK_FALSE(map1 == map2b); } + + //************************************************************************* + TEST(test_contains) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + const char* not_inserted = "ZZ"; + + 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 a5fccb25c..716636e0e 100644 --- a/test/test_unordered_multimap.cpp +++ b/test/test_unordered_multimap.cpp @@ -1226,5 +1226,27 @@ namespace CHECK_TRUE(map1 == map2a); CHECK_FALSE(map1 == map2b); } + + //************************************************************************* + TEST(test_contains) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + const char* not_inserted = "ZZ"; + + 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 3e58dd6c6..27bfe310e 100644 --- a/test/test_unordered_multiset.cpp +++ b/test/test_unordered_multiset.cpp @@ -1079,5 +1079,29 @@ namespace CHECK_TRUE(set1 == set2a); CHECK_FALSE(set1 == set2b); } + + //************************************************************************* + TEST(test_contains) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + NDC not_inserted = NDC("ZZ"); + + 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 33eecb889..189f97615 100644 --- a/test/test_unordered_set.cpp +++ b/test/test_unordered_set.cpp @@ -1044,5 +1044,29 @@ namespace CHECK_TRUE(set1 == set2a); CHECK_FALSE(set1 == set2b); } + + //************************************************************************* + TEST(test_contains) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + NDC not_inserted = NDC("ZZ"); + + 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)); + } }; }