Commit 5efb8291 authored by Kei Nakashima's avatar Kei Nakashima Committed by Commit Bot

Added a function |clear| to VectorBackedLinkedList

Implemented a functoin |clear|, which removes all elements in a linked list.

Change-Id: I94fc761eb7f779a532ccd7846254aff4f703c909
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2094897
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@{#748525}
parent fdabed16
......@@ -140,6 +140,15 @@ class VectorBackedLinkedList {
erase(--cend());
}
// Removes all elements in a linked list.
void clear() {
nodes_.clear();
// Reinserts anchor so that we can insert elements after this operation.
nodes_.push_back(Node(anchor_index_, anchor_index_));
free_head_index_ = anchor_index_;
size_ = 0;
}
private:
bool IsFreeListEmpty() const { return free_head_index_ == anchor_index_; }
......
......@@ -193,6 +193,27 @@ TEST(VectorBackedLinkedList, PopBack) {
EXPECT_TRUE(list.empty());
}
TEST(VectorBackedLinkedList, Clear) {
using List = VectorBackedLinkedList<int>;
List list;
list.push_back(1);
list.push_back(2);
list.push_back(3);
EXPECT_EQ(list.size(), 3u);
list.clear();
EXPECT_EQ(list.size(), 0u);
EXPECT_TRUE(list.empty());
EXPECT_TRUE(list.begin() == list.end());
list.push_back(1);
EXPECT_EQ(list.front(), 1);
EXPECT_EQ(list.back(), 1);
EXPECT_EQ(list.size(), 1u);
}
TEST(VectorBackedLinkedList, Iterator) {
using List = VectorBackedLinkedList<int>;
List list;
......
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