Skip to content

Commit

Permalink
Merge branch 'pull-request/#990-Add-contains-method-to-etl-unordered_…
Browse files Browse the repository at this point in the history
…map-and-etl-unordered_set' into development
  • Loading branch information
jwellbelove committed Dec 19, 2024
2 parents 2cb15ca + 191eaae commit 4cbef7f
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/etl/unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
bool contains(const K& key) const
{
return find(key) != end();
}
#endif

protected:

//*********************************************************************
Expand Down
19 changes: 19 additions & 0 deletions include/etl/unordered_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
bool contains(const K& key) const
{
return find(key) != end();
}
#endif

protected:

//*********************************************************************
Expand Down
19 changes: 19 additions & 0 deletions include/etl/unordered_multiset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
bool contains(const K& key) const
{
return find(key) != end();
}
#endif

protected:

//*********************************************************************
Expand Down
19 changes: 19 additions & 0 deletions include/etl/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
bool contains(const K& key) const
{
return find(key) != end();
}
#endif

protected:

//*********************************************************************
Expand Down
22 changes: 22 additions & 0 deletions test/test_unordered_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
}
22 changes: 22 additions & 0 deletions test/test_unordered_multimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
}
24 changes: 24 additions & 0 deletions test/test_unordered_multiset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*, 8> 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));
}
};
}
24 changes: 24 additions & 0 deletions test/test_unordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*, 8> 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));
}
};
}

0 comments on commit 4cbef7f

Please sign in to comment.