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:
Karl Wiberg
2020-03-02 21:49:20 +01:00
committed by Commit Bot
parent c126937564
commit d084ea93b6
3 changed files with 42 additions and 0 deletions

View File

@ -34,6 +34,7 @@ class BoundedInlineVector {
static_assert(fixed_capacity > 0, "Capacity must be strictly positive");
public:
using size_type = int;
using value_type = T;
using const_iterator = const T*;
@ -108,6 +109,22 @@ class BoundedInlineVector {
int size() const { return storage_.size; }
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; }
T* data() { return storage_.data; }