From 3bc0c75a1c4608ee87854d70a733e79f88b90ee8 Mon Sep 17 00:00:00 2001 From: Aleksander Dejewski Date: Sat, 7 Dec 2024 20:01:19 +0100 Subject: [PATCH] Add contains() method to etl::unordered_map and etl::unordered_set --- include/etl/unordered_map.h | 8 ++++++++ include/etl/unordered_set.h | 8 ++++++++ test/test_unordered_map.cpp | 9 +++++++++ test/test_unordered_set.cpp | 9 +++++++++ 4 files changed, 34 insertions(+) diff --git a/include/etl/unordered_map.h b/include/etl/unordered_map.h index d8ed62a5b..b3a22d84f 100644 --- a/include/etl/unordered_map.h +++ b/include/etl/unordered_map.h @@ -1361,6 +1361,14 @@ namespace etl } #endif + //************************************************************************* + /// Check if the unordered_map contains the key. + //************************************************************************* + bool contains(const TKey& key) const + { + return find(key) != end(); + } + protected: //********************************************************************* diff --git a/include/etl/unordered_set.h b/include/etl/unordered_set.h index 2b426ac59..79ec1b57e 100644 --- a/include/etl/unordered_set.h +++ b/include/etl/unordered_set.h @@ -166,6 +166,14 @@ namespace etl return !(lhs == rhs); } + //************************************************************************* + /// Check if the unordered_set contains the key. + //************************************************************************* + bool contains(const TKey& key) const + { + return find(key) != end(); + } + protected: typedef etl::intrusive_forward_list bucket_t; diff --git a/test/test_unordered_map.cpp b/test/test_unordered_map.cpp index 93f664574..c627b60fc 100644 --- a/test/test_unordered_map.cpp +++ b/test/test_unordered_map.cpp @@ -1207,5 +1207,14 @@ namespace CHECK_TRUE(map1 == map2a); CHECK_FALSE(map1 == map2b); } + + //************************************************************************* + TEST(test_contains) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + CHECK(data.contains(std::string("FF"))); + CHECK(!data.contains(std::string("ZZ"))); + } }; } diff --git a/test/test_unordered_set.cpp b/test/test_unordered_set.cpp index 7a4210cc0..9fb78a5a9 100644 --- a/test/test_unordered_set.cpp +++ b/test/test_unordered_set.cpp @@ -909,5 +909,14 @@ namespace CHECK_TRUE(set1 == set2a); CHECK_FALSE(set1 == set2b); } + + //************************************************************************* + TEST(test_contains) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + CHECK_TRUE(data.contains(NDC("FF"))); + CHECK_FALSE(data.contains(NDC("ZZ"))); + } }; }