Commit 8d3a4298 authored by Kei Nakashima's avatar Kei Nakashima Committed by Commit Bot

Fixed a bug in |VectorBackedLinkedList::insert|

In the current code, |insert| doesn't work correctly since it does not update prev/next indices when free list is not empty.
This CL fixes it.

Change-Id: I6355bfd61e4a7b8f928a462e45fb98702f005f03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2094491Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Kei Nakashima <keinakashima@google.com>
Cr-Commit-Position: refs/heads/master@{#749007}
parent 7f31c1d3
...@@ -408,14 +408,10 @@ typename VectorBackedLinkedList<T>::iterator VectorBackedLinkedList<T>::insert( ...@@ -408,14 +408,10 @@ typename VectorBackedLinkedList<T>::iterator VectorBackedLinkedList<T>::insert(
IncomingValueType&& value) { IncomingValueType&& value) {
wtf_size_t position_index = position.GetIndex(); wtf_size_t position_index = position.GetIndex();
wtf_size_t prev_index = nodes_[position_index].prev_index_; wtf_size_t prev_index = nodes_[position_index].prev_index_;
Node& prev = nodes_[prev_index];
Node& next = nodes_[position_index];
wtf_size_t new_entry_index; wtf_size_t new_entry_index;
if (IsFreeListEmpty()) { if (IsFreeListEmpty()) {
new_entry_index = nodes_.size(); new_entry_index = nodes_.size();
prev.next_index_ = new_entry_index;
next.prev_index_ = new_entry_index;
nodes_.push_back(Node(prev_index, position_index, nodes_.push_back(Node(prev_index, position_index,
std::forward<IncomingValueType>(value))); std::forward<IncomingValueType>(value)));
} else { } else {
...@@ -425,6 +421,8 @@ typename VectorBackedLinkedList<T>::iterator VectorBackedLinkedList<T>::insert( ...@@ -425,6 +421,8 @@ typename VectorBackedLinkedList<T>::iterator VectorBackedLinkedList<T>::insert(
free_head = Node(prev_index, position_index, free_head = Node(prev_index, position_index,
std::forward<IncomingValueType>(value)); std::forward<IncomingValueType>(value));
} }
nodes_[prev_index].next_index_ = new_entry_index;
nodes_[position_index].prev_index_ = new_entry_index;
size_++; size_++;
return iterator(new_entry_index, this); return iterator(new_entry_index, this);
} }
......
...@@ -143,6 +143,12 @@ TEST(VectorBackedLinkedList, Erase) { ...@@ -143,6 +143,12 @@ TEST(VectorBackedLinkedList, Erase) {
list.push_back(5); list.push_back(5);
EXPECT_EQ(list.size(), 5u); EXPECT_EQ(list.size(), 5u);
int i = 1;
for (auto element : list) {
EXPECT_EQ(element, i);
i++;
}
List::iterator target = list.begin(); List::iterator target = list.begin();
++target; ++target;
it = list.erase(target); // list = {1, 3, 4, 5} it = list.erase(target); // list = {1, 3, 4, 5}
...@@ -162,6 +168,10 @@ TEST(VectorBackedLinkedList, Erase) { ...@@ -162,6 +168,10 @@ TEST(VectorBackedLinkedList, Erase) {
EXPECT_EQ(*it, 5); EXPECT_EQ(*it, 5);
++it; ++it;
EXPECT_TRUE(it == list.end()); EXPECT_TRUE(it == list.end());
list.push_back(6);
EXPECT_EQ(list.front(), 3);
EXPECT_EQ(list.back(), 6);
} }
TEST(VectorBackedLinkedList, PopFront) { TEST(VectorBackedLinkedList, PopFront) {
......
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