Commit 8793226f authored by Kei Nakashima's avatar Kei Nakashima Committed by Commit Bot

Added iterators to NewLinkedHashSet

Implemented iterators of NewLinkedHashSet using those of VectorBackedLinkedList.
Also made some iterator-related APIs public to use them in NewLinkedHashSet.

Change-Id: Ifa4a7c60bfbd3673a1f3923f911cde3eebca612f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2097904
Commit-Queue: Kei Nakashima <keinakashima@google.com>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749587}
parent 7347301c
......@@ -1045,6 +1045,10 @@ class NewLinkedHashSet {
using Map = HashMap<Value, wtf_size_t>;
public:
using const_iterator = typename VectorBackedLinkedList<Value>::const_iterator;
using const_reverse_iterator =
typename VectorBackedLinkedList<Value>::const_reverse_iterator;
// TODO(keinakashima): add security check
struct AddResult final {
STACK_ALLOCATED();
......@@ -1070,7 +1074,11 @@ class NewLinkedHashSet {
// TODO(keinakashima): implement size-related functions
// TODO(keinakashima): implement begin/end, rbegin/rend
const_iterator begin() const { return list_.cbegin(); }
const_iterator end() const { return list_.cend(); }
const_reverse_iterator rbegin() const { return list_.crbegin(); }
const_reverse_iterator rend() const { return list_.crend(); }
// TODO(keinakashima): implement front/back
......
......@@ -14,4 +14,11 @@ TEST(NewLinkedHashSetTest, Construct) {
NewLinkedHashSet<int> test;
}
TEST(NewLinkedHashSetTest, Iterator) {
using Set = NewLinkedHashSet<int>;
Set set;
EXPECT_TRUE(set.begin() == set.end());
EXPECT_TRUE(set.rbegin() == set.rend());
}
} // namespace WTF
......@@ -182,6 +182,9 @@ class VectorBackedLinkedList {
// terminator.
wtf_size_t free_head_index_ = anchor_index_;
wtf_size_t size_ = 0;
template <typename T>
friend class NewLinkedHashSet;
};
template <typename VectorBackedLinkedListType>
......@@ -241,9 +244,18 @@ class VectorBackedLinkedListConstIterator {
using Node = typename VectorBackedLinkedListType::Node;
public:
PointerType Get() const {
DCHECK(container_->IsIndexValid(index_));
DCHECK(!container_->IsAnchor(index_));
const Node& node = container_->nodes_[index_];
return &node.value_;
}
ReferenceType operator*() const { return *Get(); }
PointerType operator->() const { return Get(); }
wtf_size_t GetIndex() const { return index_; }
VectorBackedLinkedListConstIterator& operator++() {
DCHECK(container_->IsIndexValid(index_));
index_ = container_->nodes_[index_].next_index_;
......@@ -271,19 +283,12 @@ class VectorBackedLinkedListConstIterator {
}
protected:
PointerType Get() const {
DCHECK(container_->IsIndexValid(index_));
DCHECK(!container_->IsAnchor(index_));
const Node& node = container_->nodes_[index_];
return &node.value_;
}
VectorBackedLinkedListConstIterator(
wtf_size_t index,
const VectorBackedLinkedListType* container)
: index_(index), container_(container) {}
wtf_size_t GetIndex() const { return index_; }
: index_(index), container_(container) {
DCHECK(container_->IsIndexValid(index_));
}
private:
wtf_size_t index_;
......
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