Commit 1787a34a authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook GitHub Bot

Implement contains() in sorted vector types

Summary: Implement C++20 `contains()`.

Reviewed By: yfeldblum

Differential Revision: D29556436

fbshipit-source-id: 1c3a8e6e9a07f6e47491b063f88df6bf9da2d87b
parent ee8d0a53
......@@ -530,6 +530,13 @@ class sorted_vector_set : detail::growth_policy_wrapper<GrowthPolicy> {
return find(key) == end() ? 0 : 1;
}
bool contains(const key_type& key) const { return find(key) != end(); }
template <typename K>
if_is_transparent<K, bool> contains(const K& key) const {
return find(key) != end();
}
iterator lower_bound(const key_type& key) {
return std::lower_bound(begin(), end(), key, key_comp());
}
......@@ -1017,6 +1024,13 @@ class sorted_vector_map : detail::growth_policy_wrapper<GrowthPolicy> {
return find(key) == end() ? 0 : 1;
}
bool contains(const key_type& key) const { return find(key) != end(); }
template <typename K>
if_is_transparent<K, bool> contains(const K& key) const {
return find(key) != end();
}
iterator lower_bound(const key_type& key) { return lower_bound(*this, key); }
const_iterator lower_bound(const key_type& key) const {
......
......@@ -188,6 +188,7 @@ TEST(SortedVectorTypes, SimpleSetTest) {
EXPECT_TRUE(range.second != cs2.end());
EXPECT_TRUE(cs2.count(32) == 1);
EXPECT_FALSE(cs2.find(32) == cs2.end());
EXPECT_TRUE(cs2.contains(32));
// Bad insert hint.
s2.insert(s2.begin() + 3, 33);
......@@ -200,6 +201,7 @@ TEST(SortedVectorTypes, SimpleSetTest) {
it = s2.find(32);
EXPECT_FALSE(it == s2.end());
s2.erase(it);
EXPECT_FALSE(cs2.contains(32));
EXPECT_TRUE(s2.size() == oldSz);
check_invariant(s2);
......@@ -255,6 +257,13 @@ TEST(SortedVectorTypes, TransparentSetTest) {
EXPECT_EQ(1, s.count(world));
EXPECT_EQ(0, s.count(zebra));
// contains
EXPECT_FALSE(s.contains(buddy));
EXPECT_TRUE(s.contains(hello));
EXPECT_FALSE(s.contains(stake));
EXPECT_TRUE(s.contains(world));
EXPECT_FALSE(s.contains(zebra));
// lower_bound
EXPECT_TRUE(s.find(hello) == s.lower_bound(buddy));
EXPECT_TRUE(s.find(hello) == s.lower_bound(hello));
......@@ -305,8 +314,10 @@ TEST(SortedVectorTypes, SimpleMapTest) {
EXPECT_TRUE(m.count(32) == 1);
EXPECT_DOUBLE_EQ(100.0, m.at(32));
EXPECT_FALSE(m.find(32) == m.end());
EXPECT_TRUE(m.contains(32));
m.erase(32);
EXPECT_TRUE(m.find(32) == m.end());
EXPECT_FALSE(m.contains(32));
check_invariant(m);
EXPECT_THROW(m.at(32), std::out_of_range);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment