Commit 7a395b15 authored by Edvard Thörnros's avatar Edvard Thörnros Committed by Commit Bot

Add InsertAt to WTF::Vector

The WTF::Vector datatype has not had a way to
insert with an iterator, which is the preferred way
to iterate through all elements. This new method
takes an iterator and saves you the trouble of thinking
and making silly iterator-math errors.

Bug: 981522
Change-Id: I4850606e4072668406e55b97ee65208aaf35d33d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1690961
Auto-Submit: Edvard Thörnros <edvardt@opera.com>
Commit-Queue: Edvard Thörnros <edvardt@opera.com>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#676364}
parent 09341806
......@@ -1176,15 +1176,24 @@ class Vector : private VectorBuffer<T, INLINE_CAPACITY, Allocator> {
// pointing to an element after |position| will be invalidated.
//
// insert(position, value)
// Insert a single element at |position|.
// Insert a single element at |position|, where |position| is an index.
// insert(position, buffer, size)
// InsertVector(position, vector)
// Insert multiple elements represented by either |buffer| and |size|
// or |vector| at |position|. The elements will be copied.
// InsertAt(position, value)
// Insert a single element at |position|, where |position| is an iterator.
// InsertAt(position, buffer, size)
// Insert multiple elements represented by either |buffer| and |size|
// or |vector| at |position|. The elements will be copied.
template <typename U>
void insert(wtf_size_t position, U&&);
template <typename U>
void insert(wtf_size_t position, const U*, wtf_size_t);
template <typename U>
void InsertAt(iterator position, U&&);
template <typename U>
void InsertAt(iterator position, const U*, wtf_size_t);
template <typename U, wtf_size_t otherCapacity, typename OtherAllocator>
void InsertVector(wtf_size_t position,
const Vector<U, otherCapacity, OtherAllocator>&);
......@@ -1856,6 +1865,20 @@ void Vector<T, inlineCapacity, Allocator>::insert(wtf_size_t position,
size_ = new_size;
}
template <typename T, wtf_size_t inlineCapacity, typename Allocator>
template <typename U>
void Vector<T, inlineCapacity, Allocator>::InsertAt(T* position, U&& val) {
insert(position - begin(), val);
}
template <typename T, wtf_size_t inlineCapacity, typename Allocator>
template <typename U>
void Vector<T, inlineCapacity, Allocator>::InsertAt(T* position,
const U* data,
wtf_size_t data_size) {
insert(position - begin(), data, data_size);
}
template <typename T, wtf_size_t inlineCapacity, typename Allocator>
template <typename U, wtf_size_t otherCapacity, typename OtherAllocator>
inline void Vector<T, inlineCapacity, Allocator>::InsertVector(
......
......@@ -27,6 +27,7 @@
#include <memory>
#include "base/optional.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
......@@ -662,6 +663,28 @@ TEST(VectorTest, UninitializedFill) {
EXPECT_EQ(42, v[2]);
}
TEST(VectorTest, IteratorSingleInsertion) {
Vector<int> v;
v.InsertAt(v.begin(), 1);
EXPECT_EQ(1, v[0]);
for (int i : {9, 5, 2, 3, 3, 7, 7, 8, 2, 4, 6})
v.InsertAt(std::lower_bound(v.begin(), v.end(), i), i);
EXPECT_TRUE(std::is_sorted(v.begin(), v.end()));
}
TEST(VectorTest, IteratorMultipleInsertion) {
Vector<int> v = {0, 0, 0, 3, 3, 3};
Vector<int> q = {1, 1, 1, 1};
v.InsertAt(std::lower_bound(v.begin(), v.end(), q[0]), &q[0], q.size());
EXPECT_THAT(v, testing::ElementsAre(0, 0, 0, 1, 1, 1, 1, 3, 3, 3));
EXPECT_TRUE(std::is_sorted(v.begin(), v.end()));
}
static_assert(VectorTraits<int>::kCanCopyWithMemcpy,
"int should be copied with memcopy.");
static_assert(VectorTraits<char>::kCanCopyWithMemcpy,
......
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