Skip to content

Commit

Permalink
Added transparent comparator overloads of contains()
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed Dec 19, 2024
1 parent 1daa345 commit 191eaae
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
11 changes: 11 additions & 0 deletions include/etl/unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,17 @@ namespace etl
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
11 changes: 11 additions & 0 deletions include/etl/unordered_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,17 @@ namespace etl
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
11 changes: 11 additions & 0 deletions include/etl/unordered_multiset.h
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,17 @@ namespace etl
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
11 changes: 11 additions & 0 deletions include/etl/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,17 @@ namespace etl
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
15 changes: 13 additions & 2 deletions test/test_unordered_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
}
15 changes: 13 additions & 2 deletions test/test_unordered_multimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
}
13 changes: 13 additions & 0 deletions test/test_unordered_multiset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,5 +1090,18 @@ namespace
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));
}
};
}
13 changes: 13 additions & 0 deletions test/test_unordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,5 +1055,18 @@ namespace
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 191eaae

Please sign in to comment.