mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 05:40:42 +01:00
BoundedInlineVector: Add resize() method
Bug: webrtc:11391 Change-Id: I34d659d0e295617e9058393d4d1b510111a78b83 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/169520 Commit-Queue: Karl Wiberg <kwiberg@webrtc.org> Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Cr-Commit-Position: refs/heads/master@{#30664}
This commit is contained in:
parent
c126937564
commit
d084ea93b6
3 changed files with 42 additions and 0 deletions
|
@ -34,6 +34,7 @@ class BoundedInlineVector {
|
||||||
static_assert(fixed_capacity > 0, "Capacity must be strictly positive");
|
static_assert(fixed_capacity > 0, "Capacity must be strictly positive");
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
using size_type = int;
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
using const_iterator = const T*;
|
using const_iterator = const T*;
|
||||||
|
|
||||||
|
@ -108,6 +109,22 @@ class BoundedInlineVector {
|
||||||
int size() const { return storage_.size; }
|
int size() const { return storage_.size; }
|
||||||
constexpr int capacity() const { return fixed_capacity; }
|
constexpr int capacity() const { return fixed_capacity; }
|
||||||
|
|
||||||
|
// Resizes the BoundedInlineVector to the given size, which must not exceed
|
||||||
|
// its constant capacity. If the size is increased, the added elements are
|
||||||
|
// default constructed.
|
||||||
|
void resize(int new_size) {
|
||||||
|
RTC_DCHECK_GE(new_size, 0);
|
||||||
|
RTC_DCHECK_LE(new_size, fixed_capacity);
|
||||||
|
if (new_size > storage_.size) {
|
||||||
|
bounded_inline_vector_impl::DefaultInitializeElements(
|
||||||
|
storage_.data + storage_.size, new_size - storage_.size);
|
||||||
|
} else if (new_size < storage_.size) {
|
||||||
|
bounded_inline_vector_impl::DestroyElements(storage_.data + new_size,
|
||||||
|
storage_.size - new_size);
|
||||||
|
}
|
||||||
|
storage_.size = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
const T* data() const { return storage_.data; }
|
const T* data() const { return storage_.data; }
|
||||||
T* data() { return storage_.data; }
|
T* data() { return storage_.data; }
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,16 @@ void InitializeElements(T* data, U&& element, Us&&... elements) {
|
||||||
InitializeElements(data + 1, std::forward<Us>(elements)...);
|
InitializeElements(data + 1, std::forward<Us>(elements)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Default initializes uninitialized array elements.
|
||||||
|
// TODO(kwiberg): Replace with std::uninitialized_default_construct_n() (C++17).
|
||||||
|
template <typename T>
|
||||||
|
void DefaultInitializeElements(T* data, int size) {
|
||||||
|
for (int i = 0; i < size; ++i) {
|
||||||
|
// Placement new, because we construct a new object in uninitialized memory.
|
||||||
|
::new (&data[i]) T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Copies from source to uninitialized destination. Caller is responsible for
|
// Copies from source to uninitialized destination. Caller is responsible for
|
||||||
// ensuring that there is enough space in `dst_data`.
|
// ensuring that there is enough space in `dst_data`.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
|
|
@ -114,5 +114,20 @@ TYPED_TEST(BoundedInlineVectorTestAllTypes, Return) {
|
||||||
EXPECT_EQ((Returns<TypeParam, 3>(TypeParam(), TypeParam()).size()), 2);
|
EXPECT_EQ((Returns<TypeParam, 3>(TypeParam(), TypeParam()).size()), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TYPED_TEST(BoundedInlineVectorTestAllTypes, Resize) {
|
||||||
|
BoundedInlineVector<TypeParam, 17> x;
|
||||||
|
EXPECT_EQ(x.size(), 0);
|
||||||
|
x.resize(17);
|
||||||
|
EXPECT_EQ(x.size(), 17);
|
||||||
|
// Test one arbitrary element, mostly to give MSan a chance to scream. But if
|
||||||
|
// the type has a trivial default constructor we can't, because the element
|
||||||
|
// won't be initialized.
|
||||||
|
if (!std::is_trivially_default_constructible<TypeParam>::value) {
|
||||||
|
EXPECT_EQ(x[4], TypeParam());
|
||||||
|
}
|
||||||
|
x.resize(2);
|
||||||
|
EXPECT_EQ(x.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
|
Loading…
Reference in a new issue