Commit 39afe003 authored by jdoerrie's avatar jdoerrie Committed by Commit Bot

[base] Implement base::flat_tree::contains()

This change implements base::flat_tree::contains(). This mirrors C++20,
which introduces contains() to the associative containers in the STL.
See P0458R2 [1] for further details.

[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0458r2.html

Bug: 682254
Change-Id: I4af8ce4fadd4647f7021d374bee4316c110c277d
Reviewed-on: https://chromium-review.googlesource.com/c/1288434Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601196}
parent 9046a417
......@@ -131,6 +131,7 @@ struct GetKeyFromValuePairFirst {
// template <typename K> size_t count(const K&) const;
// template <typename K> iterator find(const K&);
// template <typename K> const_iterator find(const K&) const;
// template <typename K> bool contains(const K&) const;
// template <typename K> pair<iterator, iterator> equal_range(const K&);
// template <typename K> iterator lower_bound(const K&);
// template <typename K> const_iterator lower_bound(const K&) const;
......
......@@ -111,6 +111,7 @@ namespace base {
// template <typename K> size_t count(const K&) const;
// template <typename K> iterator find(const K&);
// template <typename K> const_iterator find(const K&) const;
// template <typename K> bool contains(const K&) const;
// template <typename K> pair<iterator, iterator> equal_range(K&);
// template <typename K> iterator lower_bound(const K&);
// template <typename K> const_iterator lower_bound(const K&) const;
......
......@@ -272,6 +272,9 @@ class flat_tree {
template <typename K>
const_iterator find(const K& key) const;
template <typename K>
bool contains(const K& key) const;
template <typename K>
std::pair<iterator, iterator> equal_range(const K& key);
......@@ -862,6 +865,14 @@ auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::find(
return (eq_range.first == eq_range.second) ? end() : eq_range.first;
}
template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
template <typename K>
bool flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::contains(
const K& key) const {
auto lower = lower_bound(key);
return lower != end() && !key_comp()(key, GetKeyFromValue()(*lower));
}
template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
template <typename K>
auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::equal_range(
......
......@@ -1112,6 +1112,22 @@ TEST(FlatTree, Find) {
}
}
// bool contains(const key_type& key) const
TEST(FlatTree, Contains) {
const IntTree cont({5, 6, 7, 8, 9, 10, 11, 12});
EXPECT_TRUE(cont.contains(5));
EXPECT_TRUE(cont.contains(6));
EXPECT_TRUE(cont.contains(7));
EXPECT_TRUE(cont.contains(8));
EXPECT_TRUE(cont.contains(9));
EXPECT_TRUE(cont.contains(10));
EXPECT_TRUE(cont.contains(11));
EXPECT_TRUE(cont.contains(12));
EXPECT_FALSE(cont.contains(4));
}
// pair<iterator, iterator> equal_range(const key_type& key)
// pair<const_iterator, const_iterator> equal_range(const key_type& key) const
......
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