Commit 331e28ef authored by Kei Nakashima's avatar Kei Nakashima Committed by Commit Bot

Added functions |find| and |Contains| to NewLinkedHashSet

Implemented |NewLinkedHashSet::find| and |NewLinkedHashSet::Contains| and added tests

Change-Id: I2488db864e2f618899a3a4fda49a9440f1264c6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2100558
Commit-Queue: Kei Nakashima <keinakashima@google.com>
Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#750474}
parent 424c1c64
......@@ -1082,7 +1082,8 @@ class NewLinkedHashSet {
// TODO(keinakashima): implement front/back
// TODO(keinakashima): implement find, Contains after implementing iterator
const_iterator find(ValuePeekInType) const;
bool Contains(ValuePeekInType) const;
// TODO(keinakahsima): implement functions related to insert
template <typename IncomingValueType>
......@@ -1140,6 +1141,21 @@ inline void NewLinkedHashSet<T>::Swap(NewLinkedHashSet& other) {
list_.swap(other.list_);
}
template <typename T>
typename NewLinkedHashSet<T>::const_iterator NewLinkedHashSet<T>::find(
ValuePeekInType value) const {
typename Map::const_iterator it = value_to_index_.find(value);
if (it == value_to_index_.end())
return end();
return list_.MakeConstIterator(it->value);
}
template <typename T>
bool NewLinkedHashSet<T>::Contains(ValuePeekInType value) const {
return value_to_index_.Contains(value);
}
template <typename T>
template <typename IncomingValueType>
typename NewLinkedHashSet<T>::AddResult NewLinkedHashSet<T>::insert(
......
......@@ -108,4 +108,36 @@ TEST(NewLinkedHashSetTest, PrependOrMoveToFirst) {
EXPECT_EQ(*it, 2);
}
TEST(NewLinkedHashSetTest, FindAndContains) {
using Set = NewLinkedHashSet<int>;
Set set;
set.insert(2);
set.AppendOrMoveToLast(2);
set.PrependOrMoveToFirst(1);
set.insert(3);
set.AppendOrMoveToLast(4);
set.insert(5);
int i = 1;
for (auto element : set) {
EXPECT_EQ(element, i);
i++;
}
Set::const_iterator it = set.find(2);
EXPECT_EQ(*it, 2);
it = set.find(3);
EXPECT_EQ(*it, 3);
it = set.find(10);
EXPECT_TRUE(it == set.end());
EXPECT_TRUE(set.Contains(1));
EXPECT_TRUE(set.Contains(2));
EXPECT_TRUE(set.Contains(3));
EXPECT_TRUE(set.Contains(4));
EXPECT_TRUE(set.Contains(5));
EXPECT_FALSE(set.Contains(10));
}
} // namespace WTF
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