Commit 13d65bfb authored by Anton Bikineev's avatar Anton Bikineev Committed by Commit Bot

wtf: Add erase(iterator, iterator) overload to support erase-remove idiom

Bug: 1029379
Change-Id: I4642e31fd3f39469ee1f94045228027da736a61a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2066921Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarOmer Katz <omerkatz@chromium.org>
Reviewed-by: default avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Anton Bikineev <bikineev@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743503}
parent 3864f083
......@@ -1237,6 +1237,9 @@ class Vector
void EraseAt(wtf_size_t position);
void EraseAt(wtf_size_t position, wtf_size_t length);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
// This is to prevent compilation of deprecated calls like 'vector.erase(0)'.
void erase(std::nullptr_t) = delete;
// Remove the last element. Unlike remove(), (1) this function is fast, and
// (2) only iterators pointing to the last element will be invalidated. Other
......@@ -1326,9 +1329,6 @@ class Vector
void ReallocateBuffer(wtf_size_t);
// This is to prevent compilation of deprecated calls like 'vector.erase(0)'.
void erase(std::nullptr_t) = delete;
using Base::size_;
using Base::Buffer;
using Base::SwapVectorBuffer;
......@@ -1921,6 +1921,17 @@ inline auto Vector<T, inlineCapacity, Allocator>::erase(iterator position)
return begin() + index;
}
template <typename T, wtf_size_t inlineCapacity, typename Allocator>
inline auto Vector<T, inlineCapacity, Allocator>::erase(iterator first,
iterator last)
-> iterator {
DCHECK_LE(first, last);
const wtf_size_t index = static_cast<wtf_size_t>(first - begin());
const wtf_size_t diff = std::distance(first, last);
EraseAt(index, diff);
return begin() + index;
}
template <typename T, wtf_size_t inlineCapacity, typename Allocator>
inline void Vector<T, inlineCapacity, Allocator>::EraseAt(wtf_size_t position,
wtf_size_t length) {
......
......@@ -101,22 +101,34 @@ TEST(VectorTest, EraseAtIndex) {
}
TEST(VectorTest, Erase) {
Vector<int> int_vector({0, 1, 2, 3});
Vector<int> int_vector({0, 1, 2, 3, 4, 5});
EXPECT_EQ(4u, int_vector.size());
EXPECT_EQ(6u, int_vector.size());
EXPECT_EQ(0, int_vector[0]);
EXPECT_EQ(1, int_vector[1]);
EXPECT_EQ(2, int_vector[2]);
EXPECT_EQ(3, int_vector[3]);
EXPECT_EQ(4, int_vector[4]);
EXPECT_EQ(5, int_vector[5]);
auto* first = int_vector.erase(int_vector.begin());
EXPECT_EQ(3u, int_vector.size());
EXPECT_EQ(5u, int_vector.size());
EXPECT_EQ(1, *first);
EXPECT_EQ(int_vector.begin(), first);
auto* last = std::lower_bound(int_vector.begin(), int_vector.end(), 3);
auto* last = std::lower_bound(int_vector.begin(), int_vector.end(), 5);
auto* end = int_vector.erase(last);
EXPECT_EQ(4u, int_vector.size());
EXPECT_EQ(int_vector.end(), end);
auto* item2 = std::lower_bound(int_vector.begin(), int_vector.end(), 2);
auto* item4 = int_vector.erase(item2, item2 + 2);
EXPECT_EQ(2u, int_vector.size());
EXPECT_EQ(4, *item4);
last = std::lower_bound(int_vector.begin(), int_vector.end(), 4);
end = int_vector.erase(last, int_vector.end());
EXPECT_EQ(1u, int_vector.size());
EXPECT_EQ(int_vector.end(), end);
}
......
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