2019-03-19 19:14:01 +01:00
|
|
|
// Copyright 2019 The Abseil Authors.
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2019-03-08 16:27:53 +01:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// File: inlined_vector.h
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This header file contains the declaration and definition of an "inlined
|
|
|
|
// vector" which behaves in an equivalent fashion to a `std::vector`, except
|
|
|
|
// that storage for small sequences of the vector are provided inline without
|
|
|
|
// requiring any heap allocation.
|
2018-11-13 22:22:00 +01:00
|
|
|
//
|
|
|
|
// An `absl::InlinedVector<T, N>` specifies the default capacity `N` as one of
|
|
|
|
// its template parameters. Instances where `size() <= N` hold contained
|
|
|
|
// elements in inline space. Typically `N` is very small so that sequences that
|
|
|
|
// are expected to be short do not require allocations.
|
|
|
|
//
|
|
|
|
// An `absl::InlinedVector` does not usually require a specific allocator. If
|
2017-09-19 22:54:40 +02:00
|
|
|
// the inlined vector grows beyond its initial constraints, it will need to
|
2018-11-13 22:22:00 +01:00
|
|
|
// allocate (as any normal `std::vector` would). This is usually performed with
|
|
|
|
// the default allocator (defined as `std::allocator<T>`). Optionally, a custom
|
|
|
|
// allocator type may be specified as `A` in `absl::InlinedVector<T, N, A>`.
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
#ifndef ABSL_CONTAINER_INLINED_VECTOR_H_
|
|
|
|
#define ABSL_CONTAINER_INLINED_VECTOR_H_
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "absl/algorithm/algorithm.h"
|
|
|
|
#include "absl/base/internal/throw_delegate.h"
|
|
|
|
#include "absl/base/optimization.h"
|
|
|
|
#include "absl/base/port.h"
|
2019-03-19 19:14:01 +01:00
|
|
|
#include "absl/container/internal/inlined_vector.h"
|
2017-09-19 22:54:40 +02:00
|
|
|
#include "absl/memory/memory.h"
|
|
|
|
|
|
|
|
namespace absl {
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// InlinedVector
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// An `absl::InlinedVector` is designed to be a drop-in replacement for
|
|
|
|
// `std::vector` for use cases where the vector's size is sufficiently small
|
|
|
|
// that it can be inlined. If the inlined vector does grow beyond its estimated
|
2018-11-13 22:22:00 +01:00
|
|
|
// capacity, it will trigger an initial allocation on the heap, and will behave
|
|
|
|
// as a `std:vector`. The API of the `absl::InlinedVector` within this file is
|
2017-09-19 22:54:40 +02:00
|
|
|
// designed to cover the same API footprint as covered by `std::vector`.
|
2018-11-06 22:01:25 +01:00
|
|
|
template <typename T, size_t N, typename A = std::allocator<T>>
|
2017-09-19 22:54:40 +02:00
|
|
|
class InlinedVector {
|
2019-03-22 20:20:05 +01:00
|
|
|
static_assert(
|
|
|
|
N > 0, "InlinedVector cannot be instantiated with `0` inlined elements.");
|
|
|
|
|
2019-05-08 23:20:43 +02:00
|
|
|
using Storage = inlined_vector_internal::Storage<T, N, A>;
|
2019-06-18 22:05:50 +02:00
|
|
|
using rvalue_reference = typename Storage::rvalue_reference;
|
|
|
|
using MoveIterator = typename Storage::MoveIterator;
|
2019-04-05 18:05:22 +02:00
|
|
|
using AllocatorTraits = typename Storage::AllocatorTraits;
|
2019-06-18 22:05:50 +02:00
|
|
|
using IsMemcpyOk = typename Storage::IsMemcpyOk;
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
using IteratorValueAdapter =
|
|
|
|
typename Storage::template IteratorValueAdapter<Iterator>;
|
|
|
|
using CopyValueAdapter = typename Storage::CopyValueAdapter;
|
|
|
|
using DefaultValueAdapter = typename Storage::DefaultValueAdapter;
|
2018-11-06 22:01:25 +01:00
|
|
|
|
2019-01-11 19:16:39 +01:00
|
|
|
template <typename Iterator>
|
2019-04-16 21:11:35 +02:00
|
|
|
using EnableIfAtLeastForwardIterator = absl::enable_if_t<
|
|
|
|
inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value>;
|
2019-01-11 19:16:39 +01:00
|
|
|
|
|
|
|
template <typename Iterator>
|
2019-04-16 21:11:35 +02:00
|
|
|
using DisableIfAtLeastForwardIterator = absl::enable_if_t<
|
|
|
|
!inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value>;
|
2018-11-06 22:01:25 +01:00
|
|
|
|
2017-09-19 22:54:40 +02:00
|
|
|
public:
|
2019-03-19 19:14:01 +01:00
|
|
|
using allocator_type = typename Storage::allocator_type;
|
|
|
|
using value_type = typename Storage::value_type;
|
|
|
|
using pointer = typename Storage::pointer;
|
|
|
|
using const_pointer = typename Storage::const_pointer;
|
|
|
|
using reference = typename Storage::reference;
|
|
|
|
using const_reference = typename Storage::const_reference;
|
|
|
|
using size_type = typename Storage::size_type;
|
|
|
|
using difference_type = typename Storage::difference_type;
|
|
|
|
using iterator = typename Storage::iterator;
|
|
|
|
using const_iterator = typename Storage::const_iterator;
|
|
|
|
using reverse_iterator = typename Storage::reverse_iterator;
|
|
|
|
using const_reverse_iterator = typename Storage::const_reverse_iterator;
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// InlinedVector Constructors and Destructor
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-06-05 17:26:58 +02:00
|
|
|
// Creates an empty inlined vector with a value-initialized allocator.
|
|
|
|
InlinedVector() noexcept(noexcept(allocator_type())) : storage_() {}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Creates an empty inlined vector with a specified allocator.
|
2017-09-19 22:54:40 +02:00
|
|
|
explicit InlinedVector(const allocator_type& alloc) noexcept
|
2019-03-19 19:14:01 +01:00
|
|
|
: storage_(alloc) {}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Creates an inlined vector with `n` copies of `value_type()`.
|
2018-06-21 21:55:12 +02:00
|
|
|
explicit InlinedVector(size_type n,
|
|
|
|
const allocator_type& alloc = allocator_type())
|
2019-03-19 19:14:01 +01:00
|
|
|
: storage_(alloc) {
|
2019-06-18 22:05:50 +02:00
|
|
|
storage_.Initialize(DefaultValueAdapter(), n);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Creates an inlined vector with `n` copies of `v`.
|
|
|
|
InlinedVector(size_type n, const_reference v,
|
2017-09-19 22:54:40 +02:00
|
|
|
const allocator_type& alloc = allocator_type())
|
2019-03-19 19:14:01 +01:00
|
|
|
: storage_(alloc) {
|
2019-06-18 22:05:50 +02:00
|
|
|
storage_.Initialize(CopyValueAdapter(v), n);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 22:54:06 +01:00
|
|
|
// Creates an inlined vector of copies of the values in `list`.
|
|
|
|
InlinedVector(std::initializer_list<value_type> list,
|
2018-11-06 22:01:25 +01:00
|
|
|
const allocator_type& alloc = allocator_type())
|
2019-06-05 17:26:58 +02:00
|
|
|
: InlinedVector(list.begin(), list.end(), alloc) {}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-13 22:22:00 +01:00
|
|
|
// Creates an inlined vector with elements constructed from the provided
|
2019-01-25 22:54:06 +01:00
|
|
|
// forward iterator range [`first`, `last`).
|
2018-11-06 22:01:25 +01:00
|
|
|
//
|
|
|
|
// NOTE: The `enable_if` prevents ambiguous interpretation between a call to
|
2018-11-13 22:22:00 +01:00
|
|
|
// this constructor with two integral arguments and a call to the above
|
|
|
|
// `InlinedVector(size_type, const_reference)` constructor.
|
2019-01-25 22:54:06 +01:00
|
|
|
template <typename ForwardIterator,
|
|
|
|
EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr>
|
|
|
|
InlinedVector(ForwardIterator first, ForwardIterator last,
|
|
|
|
const allocator_type& alloc = allocator_type())
|
2019-03-19 19:14:01 +01:00
|
|
|
: storage_(alloc) {
|
2019-06-18 22:05:50 +02:00
|
|
|
storage_.Initialize(IteratorValueAdapter<ForwardIterator>(first),
|
|
|
|
std::distance(first, last));
|
2019-01-25 22:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates an inlined vector with elements constructed from the provided input
|
|
|
|
// iterator range [`first`, `last`).
|
2019-01-24 16:23:40 +01:00
|
|
|
template <typename InputIterator,
|
2019-01-25 22:54:06 +01:00
|
|
|
DisableIfAtLeastForwardIterator<InputIterator>* = nullptr>
|
2018-11-06 22:01:25 +01:00
|
|
|
InlinedVector(InputIterator first, InputIterator last,
|
2017-09-19 22:54:40 +02:00
|
|
|
const allocator_type& alloc = allocator_type())
|
2019-03-19 19:14:01 +01:00
|
|
|
: storage_(alloc) {
|
2019-01-30 19:59:43 +01:00
|
|
|
std::copy(first, last, std::back_inserter(*this));
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2019-02-25 20:52:11 +01:00
|
|
|
// Creates a copy of an `other` inlined vector using `other`'s allocator.
|
2018-12-06 21:44:49 +01:00
|
|
|
InlinedVector(const InlinedVector& other)
|
2019-05-22 14:06:50 +02:00
|
|
|
: InlinedVector(other, *other.storage_.GetAllocPtr()) {}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2019-02-25 20:52:11 +01:00
|
|
|
// Creates a copy of an `other` inlined vector using a specified allocator.
|
2018-12-06 21:44:49 +01:00
|
|
|
InlinedVector(const InlinedVector& other, const allocator_type& alloc)
|
2019-03-19 19:14:01 +01:00
|
|
|
: storage_(alloc) {
|
2019-06-18 22:05:50 +02:00
|
|
|
if (IsMemcpyOk::value && !other.storage_.GetIsAllocated()) {
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.MemcpyFrom(other.storage_);
|
2018-12-06 21:44:49 +01:00
|
|
|
} else {
|
2019-06-18 22:05:50 +02:00
|
|
|
storage_.Initialize(IteratorValueAdapter<const_pointer>(other.data()),
|
|
|
|
other.size());
|
2018-12-06 21:44:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 22:01:25 +01:00
|
|
|
|
2019-02-25 20:52:11 +01:00
|
|
|
// Creates an inlined vector by moving in the contents of an `other` inlined
|
|
|
|
// vector without performing any allocations. If `other` contains allocated
|
|
|
|
// memory, the newly-created instance will take ownership of that memory
|
2019-04-23 21:04:13 +02:00
|
|
|
// (leaving `other` empty). However, if `other` does not contain allocated
|
|
|
|
// memory (i.e. is inlined), the new inlined vector will perform element-wise
|
|
|
|
// move construction of `other`'s elements.
|
2018-11-06 22:01:25 +01:00
|
|
|
//
|
2019-02-25 20:52:11 +01:00
|
|
|
// NOTE: since no allocation is performed for the inlined vector in either
|
|
|
|
// case, the `noexcept(...)` specification depends on whether moving the
|
|
|
|
// underlying objects can throw. We assume:
|
|
|
|
// a) Move constructors should only throw due to allocation failure.
|
|
|
|
// b) If `value_type`'s move constructor allocates, it uses the same
|
|
|
|
// allocation function as the `InlinedVector`'s allocator. Thus, the move
|
2017-10-05 22:33:44 +02:00
|
|
|
// constructor is non-throwing if the allocator is non-throwing or
|
|
|
|
// `value_type`'s move constructor is specified as `noexcept`.
|
2018-12-06 21:44:49 +01:00
|
|
|
InlinedVector(InlinedVector&& other) noexcept(
|
2017-09-19 22:54:40 +02:00
|
|
|
absl::allocator_is_nothrow<allocator_type>::value ||
|
2018-12-06 21:44:49 +01:00
|
|
|
std::is_nothrow_move_constructible<value_type>::value)
|
2019-05-22 14:06:50 +02:00
|
|
|
: storage_(*other.storage_.GetAllocPtr()) {
|
2019-06-18 22:05:50 +02:00
|
|
|
if (IsMemcpyOk::value) {
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.MemcpyFrom(other.storage_);
|
2019-06-18 22:05:50 +02:00
|
|
|
other.storage_.SetInlinedSize(0);
|
|
|
|
} else if (other.storage_.GetIsAllocated()) {
|
2019-06-05 17:26:58 +02:00
|
|
|
storage_.SetAllocatedData(other.storage_.GetAllocatedData(),
|
|
|
|
other.storage_.GetAllocatedCapacity());
|
2019-06-18 22:05:50 +02:00
|
|
|
storage_.SetAllocatedSize(other.storage_.GetSize());
|
2019-04-04 17:13:57 +02:00
|
|
|
other.storage_.SetInlinedSize(0);
|
2018-12-06 21:44:49 +01:00
|
|
|
} else {
|
2019-06-18 22:05:50 +02:00
|
|
|
IteratorValueAdapter<MoveIterator> other_values(
|
|
|
|
MoveIterator(other.storage_.GetInlinedData()));
|
|
|
|
inlined_vector_internal::ConstructElements(
|
|
|
|
storage_.GetAllocPtr(), storage_.GetInlinedData(), &other_values,
|
|
|
|
other.storage_.GetSize());
|
|
|
|
storage_.SetInlinedSize(other.storage_.GetSize());
|
2018-12-06 21:44:49 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 22:33:44 +02:00
|
|
|
|
2019-02-25 20:52:11 +01:00
|
|
|
// Creates an inlined vector by moving in the contents of an `other` inlined
|
|
|
|
// vector, performing allocations with the specified `alloc` allocator. If
|
|
|
|
// `other`'s allocator is not equal to `alloc` and `other` contains allocated
|
|
|
|
// memory, this move constructor will create a new allocation.
|
2018-11-06 22:01:25 +01:00
|
|
|
//
|
2019-02-25 20:52:11 +01:00
|
|
|
// NOTE: since allocation is performed in this case, this constructor can
|
|
|
|
// only be `noexcept` if the specified allocator is also `noexcept`. If this
|
|
|
|
// is the case, or if `other` contains allocated memory, this constructor
|
|
|
|
// performs element-wise move construction of its contents.
|
|
|
|
//
|
|
|
|
// Only in the case where `other`'s allocator is equal to `alloc` and `other`
|
|
|
|
// contains allocated memory will the newly created inlined vector take
|
|
|
|
// ownership of `other`'s allocated memory.
|
2018-12-06 21:44:49 +01:00
|
|
|
InlinedVector(InlinedVector&& other, const allocator_type& alloc) noexcept(
|
|
|
|
absl::allocator_is_nothrow<allocator_type>::value)
|
2019-03-19 19:14:01 +01:00
|
|
|
: storage_(alloc) {
|
2019-06-18 22:05:50 +02:00
|
|
|
if (IsMemcpyOk::value) {
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.MemcpyFrom(other.storage_);
|
2019-06-18 22:05:50 +02:00
|
|
|
other.storage_.SetInlinedSize(0);
|
|
|
|
} else if ((*storage_.GetAllocPtr() == *other.storage_.GetAllocPtr()) &&
|
|
|
|
other.storage_.GetIsAllocated()) {
|
|
|
|
storage_.SetAllocatedData(other.storage_.GetAllocatedData(),
|
|
|
|
other.storage_.GetAllocatedCapacity());
|
|
|
|
storage_.SetAllocatedSize(other.storage_.GetSize());
|
|
|
|
other.storage_.SetInlinedSize(0);
|
2018-12-06 21:44:49 +01:00
|
|
|
} else {
|
2019-06-18 22:05:50 +02:00
|
|
|
storage_.Initialize(
|
|
|
|
IteratorValueAdapter<MoveIterator>(MoveIterator(other.data())),
|
|
|
|
other.size());
|
2018-12-06 21:44:49 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2019-06-05 17:26:58 +02:00
|
|
|
~InlinedVector() {}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// InlinedVector Member Accessors
|
|
|
|
// ---------------------------------------------------------------------------
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::empty()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Checks if the inlined vector has no elements.
|
|
|
|
bool empty() const noexcept { return !size(); }
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::size()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Returns the number of elements in the inlined vector.
|
2019-04-04 17:13:57 +02:00
|
|
|
size_type size() const noexcept { return storage_.GetSize(); }
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::max_size()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Returns the maximum number of elements the vector can hold.
|
|
|
|
size_type max_size() const noexcept {
|
|
|
|
// One bit of the size storage is used to indicate whether the inlined
|
2018-11-06 22:01:25 +01:00
|
|
|
// vector is allocated. As a result, the maximum size of the container that
|
|
|
|
// we can express is half of the max for `size_type`.
|
2018-10-29 23:53:34 +01:00
|
|
|
return (std::numeric_limits<size_type>::max)() / 2;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::capacity()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns the number of elements that can be stored in the inlined vector
|
|
|
|
// without requiring a reallocation of underlying memory.
|
|
|
|
//
|
2019-01-28 21:10:41 +01:00
|
|
|
// NOTE: For most inlined vectors, `capacity()` should equal the template
|
|
|
|
// parameter `N`. For inlined vectors which exceed this capacity, they
|
2018-11-06 22:01:25 +01:00
|
|
|
// will no longer be inlined and `capacity()` will equal its capacity on the
|
|
|
|
// allocated heap.
|
|
|
|
size_type capacity() const noexcept {
|
2019-04-04 17:13:57 +02:00
|
|
|
return storage_.GetIsAllocated() ? storage_.GetAllocatedCapacity()
|
2019-07-25 13:03:57 +02:00
|
|
|
: storage_.GetInlinedCapacity();
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::data()`
|
|
|
|
//
|
|
|
|
// Returns a `pointer` to elements of the inlined vector. This pointer can be
|
|
|
|
// used to access and modify the contained elements.
|
|
|
|
// Only results within the range [`0`, `size()`) are defined.
|
2017-09-19 22:54:40 +02:00
|
|
|
pointer data() noexcept {
|
2019-04-04 17:13:57 +02:00
|
|
|
return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
|
|
|
|
: storage_.GetInlinedData();
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::data()` to return a `const_pointer` to elements
|
|
|
|
// of the inlined vector. This pointer can be used to access (but not modify)
|
|
|
|
// the contained elements.
|
|
|
|
const_pointer data() const noexcept {
|
2019-04-04 17:13:57 +02:00
|
|
|
return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
|
|
|
|
: storage_.GetInlinedData();
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::operator[]()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a `reference` to the `i`th element of the inlined vector using the
|
|
|
|
// array operator.
|
|
|
|
reference operator[](size_type i) {
|
|
|
|
assert(i < size());
|
2017-09-19 22:54:40 +02:00
|
|
|
return data()[i];
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::operator[]()` to return a `const_reference` to
|
|
|
|
// the `i`th element of the inlined vector.
|
|
|
|
const_reference operator[](size_type i) const {
|
2017-09-19 22:54:40 +02:00
|
|
|
assert(i < size());
|
|
|
|
return data()[i];
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::at()`
|
|
|
|
//
|
|
|
|
// Returns a `reference` to the `i`th element of the inlined vector.
|
|
|
|
reference at(size_type i) {
|
|
|
|
if (ABSL_PREDICT_FALSE(i >= size())) {
|
2017-09-19 22:54:40 +02:00
|
|
|
base_internal::ThrowStdOutOfRange(
|
2018-12-06 21:44:49 +01:00
|
|
|
"`InlinedVector::at(size_type)` failed bounds check");
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
return data()[i];
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::at()` to return a `const_reference` to the
|
|
|
|
// `i`th element of the inlined vector.
|
|
|
|
const_reference at(size_type i) const {
|
|
|
|
if (ABSL_PREDICT_FALSE(i >= size())) {
|
|
|
|
base_internal::ThrowStdOutOfRange(
|
2018-12-06 21:44:49 +01:00
|
|
|
"`InlinedVector::at(size_type) const` failed bounds check");
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
2017-09-19 22:54:40 +02:00
|
|
|
return data()[i];
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::front()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a `reference` to the first element of the inlined vector.
|
|
|
|
reference front() {
|
2017-09-19 22:54:40 +02:00
|
|
|
assert(!empty());
|
|
|
|
return at(0);
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::front()` returns a `const_reference` to the
|
|
|
|
// first element of the inlined vector.
|
|
|
|
const_reference front() const {
|
2017-09-19 22:54:40 +02:00
|
|
|
assert(!empty());
|
|
|
|
return at(0);
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::back()`
|
2017-12-13 21:02:15 +01:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a `reference` to the last element of the inlined vector.
|
|
|
|
reference back() {
|
|
|
|
assert(!empty());
|
|
|
|
return at(size() - 1);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::back()` to return a `const_reference` to the
|
|
|
|
// last element of the inlined vector.
|
|
|
|
const_reference back() const {
|
2017-09-19 22:54:40 +02:00
|
|
|
assert(!empty());
|
2018-11-06 22:01:25 +01:00
|
|
|
return at(size() - 1);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::begin()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns an `iterator` to the beginning of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
iterator begin() noexcept { return data(); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::begin()` to return a `const_iterator` to
|
|
|
|
// the beginning of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
const_iterator begin() const noexcept { return data(); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::end()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns an `iterator` to the end of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
iterator end() noexcept { return data() + size(); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::end()` to return a `const_iterator` to the
|
|
|
|
// end of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
const_iterator end() const noexcept { return data() + size(); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::cbegin()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a `const_iterator` to the beginning of the inlined vector.
|
|
|
|
const_iterator cbegin() const noexcept { return begin(); }
|
|
|
|
|
|
|
|
// `InlinedVector::cend()`
|
|
|
|
//
|
|
|
|
// Returns a `const_iterator` to the end of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
const_iterator cend() const noexcept { return end(); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::rbegin()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a `reverse_iterator` from the end of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::rbegin()` to return a
|
|
|
|
// `const_reverse_iterator` from the end of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
const_reverse_iterator rbegin() const noexcept {
|
|
|
|
return const_reverse_iterator(end());
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::rend()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a `reverse_iterator` from the beginning of the inlined vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::rend()` to return a `const_reverse_iterator`
|
2017-09-19 22:54:40 +02:00
|
|
|
// from the beginning of the inlined vector.
|
|
|
|
const_reverse_iterator rend() const noexcept {
|
|
|
|
return const_reverse_iterator(begin());
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::crbegin()`
|
|
|
|
//
|
|
|
|
// Returns a `const_reverse_iterator` from the end of the inlined vector.
|
|
|
|
const_reverse_iterator crbegin() const noexcept { return rbegin(); }
|
|
|
|
|
|
|
|
// `InlinedVector::crend()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a `const_reverse_iterator` from the beginning of the inlined
|
|
|
|
// vector.
|
2017-09-19 22:54:40 +02:00
|
|
|
const_reverse_iterator crend() const noexcept { return rend(); }
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::get_allocator()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// Returns a copy of the allocator of the inlined vector.
|
2019-05-22 14:06:50 +02:00
|
|
|
allocator_type get_allocator() const { return *storage_.GetAllocPtr(); }
|
2018-11-06 22:01:25 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// InlinedVector Member Mutators
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// `InlinedVector::operator=()`
|
|
|
|
//
|
|
|
|
// Replaces the contents of the inlined vector with copies of the elements in
|
|
|
|
// the provided `std::initializer_list`.
|
2019-01-25 22:54:06 +01:00
|
|
|
InlinedVector& operator=(std::initializer_list<value_type> list) {
|
2019-05-20 14:29:52 +02:00
|
|
|
assign(list.begin(), list.end());
|
2018-11-06 22:01:25 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::operator=()` to replace the contents of the
|
|
|
|
// inlined vector with the contents of `other`.
|
|
|
|
InlinedVector& operator=(const InlinedVector& other) {
|
2019-05-20 14:29:52 +02:00
|
|
|
if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
|
|
|
|
const_pointer other_data = other.data();
|
|
|
|
assign(other_data, other_data + other.size());
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::operator=()` to replace the contents of the
|
|
|
|
// inlined vector with the contents of `other`.
|
|
|
|
//
|
|
|
|
// NOTE: As a result of calling this overload, `other` may be empty or it's
|
|
|
|
// contents may be left in a moved-from state.
|
|
|
|
InlinedVector& operator=(InlinedVector&& other) {
|
2019-03-27 16:05:41 +01:00
|
|
|
if (ABSL_PREDICT_FALSE(this == std::addressof(other))) return *this;
|
2018-11-06 22:01:25 +01:00
|
|
|
|
2019-06-21 22:11:42 +02:00
|
|
|
if (IsMemcpyOk::value || other.storage_.GetIsAllocated()) {
|
|
|
|
inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(),
|
|
|
|
size());
|
2019-06-25 03:35:20 +02:00
|
|
|
storage_.DeallocateIfAllocated();
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.MemcpyFrom(other.storage_);
|
2019-04-04 17:13:57 +02:00
|
|
|
other.storage_.SetInlinedSize(0);
|
2018-11-06 22:01:25 +01:00
|
|
|
} else {
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.Assign(IteratorValueAdapter<MoveIterator>(
|
|
|
|
MoveIterator(other.storage_.GetInlinedData())),
|
|
|
|
other.size());
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
2019-06-21 22:11:42 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// `InlinedVector::assign()`
|
|
|
|
//
|
|
|
|
// Replaces the contents of the inlined vector with `n` copies of `v`.
|
|
|
|
void assign(size_type n, const_reference v) {
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.Assign(CopyValueAdapter(v), n);
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::assign()` to replace the contents of the
|
|
|
|
// inlined vector with copies of the values in the provided
|
|
|
|
// `std::initializer_list`.
|
2019-01-25 22:54:06 +01:00
|
|
|
void assign(std::initializer_list<value_type> list) {
|
2019-05-20 14:29:52 +02:00
|
|
|
assign(list.begin(), list.end());
|
2019-01-25 22:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::assign()` to replace the contents of the
|
|
|
|
// inlined vector with the forward iterator range [`first`, `last`).
|
|
|
|
template <typename ForwardIterator,
|
|
|
|
EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr>
|
|
|
|
void assign(ForwardIterator first, ForwardIterator last) {
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.Assign(IteratorValueAdapter<ForwardIterator>(first),
|
|
|
|
std::distance(first, last));
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::assign()` to replace the contents of the
|
2019-01-25 22:54:06 +01:00
|
|
|
// inlined vector with the input iterator range [`first`, `last`).
|
2019-01-24 16:23:40 +01:00
|
|
|
template <typename InputIterator,
|
2019-01-25 22:54:06 +01:00
|
|
|
DisableIfAtLeastForwardIterator<InputIterator>* = nullptr>
|
2018-11-06 22:01:25 +01:00
|
|
|
void assign(InputIterator first, InputIterator last) {
|
2019-06-25 21:32:44 +02:00
|
|
|
size_type i = 0;
|
|
|
|
for (; i < size() && first != last; ++i, static_cast<void>(++first)) {
|
|
|
|
at(i) = *first;
|
2019-01-30 19:59:43 +01:00
|
|
|
}
|
2019-06-25 21:32:44 +02:00
|
|
|
|
|
|
|
erase(data() + i, data() + size());
|
|
|
|
|
2019-01-30 19:59:43 +01:00
|
|
|
std::copy(first, last, std::back_inserter(*this));
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// `InlinedVector::resize()`
|
|
|
|
//
|
|
|
|
// Resizes the inlined vector to contain `n` elements. If `n` is smaller than
|
|
|
|
// the inlined vector's current size, extra elements are destroyed. If `n` is
|
|
|
|
// larger than the initial size, new elements are value-initialized.
|
2019-06-25 03:35:20 +02:00
|
|
|
void resize(size_type n) { storage_.Resize(DefaultValueAdapter(), n); }
|
2018-11-06 22:01:25 +01:00
|
|
|
|
|
|
|
// Overload of `InlinedVector::resize()` to resize the inlined vector to
|
|
|
|
// contain `n` elements where, if `n` is larger than `size()`, the new values
|
|
|
|
// will be copy-constructed from `v`.
|
2018-12-06 21:44:49 +01:00
|
|
|
void resize(size_type n, const_reference v) {
|
2019-06-25 03:35:20 +02:00
|
|
|
storage_.Resize(CopyValueAdapter(v), n);
|
2018-12-06 21:44:49 +01:00
|
|
|
}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::insert()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2019-01-25 22:54:06 +01:00
|
|
|
// Copies `v` into `pos`, returning an `iterator` pointing to the newly
|
2018-11-06 22:01:25 +01:00
|
|
|
// inserted element.
|
2019-01-25 22:54:06 +01:00
|
|
|
iterator insert(const_iterator pos, const_reference v) {
|
|
|
|
return emplace(pos, v);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 22:54:06 +01:00
|
|
|
// Overload of `InlinedVector::insert()` for moving `v` into `pos`, returning
|
|
|
|
// an iterator pointing to the newly inserted element.
|
|
|
|
iterator insert(const_iterator pos, rvalue_reference v) {
|
|
|
|
return emplace(pos, std::move(v));
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::insert()` for inserting `n` contiguous copies
|
2019-01-25 22:54:06 +01:00
|
|
|
// of `v` starting at `pos`. Returns an `iterator` pointing to the first of
|
|
|
|
// the newly inserted elements.
|
|
|
|
iterator insert(const_iterator pos, size_type n, const_reference v) {
|
2019-07-23 21:42:09 +02:00
|
|
|
assert(pos >= begin());
|
|
|
|
assert(pos <= end());
|
|
|
|
|
|
|
|
if (ABSL_PREDICT_TRUE(n != 0)) {
|
|
|
|
value_type dealias = v;
|
|
|
|
return storage_.Insert(pos, CopyValueAdapter(dealias), n);
|
|
|
|
} else {
|
2019-06-21 22:11:42 +02:00
|
|
|
return const_cast<iterator>(pos);
|
|
|
|
}
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::insert()` for copying the contents of the
|
2019-01-25 22:54:06 +01:00
|
|
|
// `std::initializer_list` into the vector starting at `pos`. Returns an
|
2018-11-06 22:01:25 +01:00
|
|
|
// `iterator` pointing to the first of the newly inserted elements.
|
2019-01-25 22:54:06 +01:00
|
|
|
iterator insert(const_iterator pos, std::initializer_list<value_type> list) {
|
|
|
|
return insert(pos, list.begin(), list.end());
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::insert()` for inserting elements constructed
|
2019-01-25 22:54:06 +01:00
|
|
|
// from the forward iterator range [`first`, `last`). Returns an `iterator`
|
|
|
|
// pointing to the first of the newly inserted elements.
|
2018-11-06 22:01:25 +01:00
|
|
|
//
|
|
|
|
// NOTE: The `enable_if` is intended to disambiguate the two three-argument
|
|
|
|
// overloads of `insert()`.
|
2019-01-25 22:54:06 +01:00
|
|
|
template <typename ForwardIterator,
|
|
|
|
EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr>
|
|
|
|
iterator insert(const_iterator pos, ForwardIterator first,
|
|
|
|
ForwardIterator last) {
|
2019-07-23 21:42:09 +02:00
|
|
|
assert(pos >= begin());
|
|
|
|
assert(pos <= end());
|
|
|
|
|
|
|
|
if (ABSL_PREDICT_TRUE(first != last)) {
|
|
|
|
return storage_.Insert(pos, IteratorValueAdapter<ForwardIterator>(first),
|
|
|
|
std::distance(first, last));
|
|
|
|
} else {
|
2019-06-21 22:11:42 +02:00
|
|
|
return const_cast<iterator>(pos);
|
|
|
|
}
|
2019-01-25 22:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::insert()` for inserting elements constructed
|
|
|
|
// from the input iterator range [`first`, `last`). Returns an `iterator`
|
|
|
|
// pointing to the first of the newly inserted elements.
|
2017-09-19 22:54:40 +02:00
|
|
|
template <typename InputIterator,
|
2019-01-25 22:54:06 +01:00
|
|
|
DisableIfAtLeastForwardIterator<InputIterator>* = nullptr>
|
|
|
|
iterator insert(const_iterator pos, InputIterator first, InputIterator last) {
|
2019-06-25 21:32:44 +02:00
|
|
|
assert(pos >= begin());
|
|
|
|
assert(pos <= end());
|
|
|
|
|
|
|
|
size_type index = std::distance(cbegin(), pos);
|
|
|
|
for (size_type i = index; first != last; ++i, static_cast<void>(++first)) {
|
|
|
|
insert(data() + i, *first);
|
2019-01-30 19:59:43 +01:00
|
|
|
}
|
2019-06-25 21:32:44 +02:00
|
|
|
|
|
|
|
return iterator(data() + index);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::emplace()`
|
|
|
|
//
|
2019-01-25 22:54:06 +01:00
|
|
|
// Constructs and inserts an object in the inlined vector at the given `pos`,
|
|
|
|
// returning an `iterator` pointing to the newly emplaced element.
|
2018-11-06 22:01:25 +01:00
|
|
|
template <typename... Args>
|
2019-01-25 22:54:06 +01:00
|
|
|
iterator emplace(const_iterator pos, Args&&... args) {
|
|
|
|
assert(pos >= begin());
|
|
|
|
assert(pos <= end());
|
2018-12-06 21:44:49 +01:00
|
|
|
|
2019-07-23 21:42:09 +02:00
|
|
|
value_type dealias(std::forward<Args>(args)...);
|
|
|
|
return storage_.Insert(pos,
|
|
|
|
IteratorValueAdapter<MoveIterator>(
|
|
|
|
MoveIterator(std::addressof(dealias))),
|
|
|
|
1);
|
2018-12-06 21:44:49 +01:00
|
|
|
}
|
2018-11-06 22:01:25 +01:00
|
|
|
|
|
|
|
// `InlinedVector::emplace_back()`
|
|
|
|
//
|
|
|
|
// Constructs and appends a new element to the end of the inlined vector,
|
|
|
|
// returning a `reference` to the emplaced element.
|
|
|
|
template <typename... Args>
|
|
|
|
reference emplace_back(Args&&... args) {
|
2019-06-28 02:24:26 +02:00
|
|
|
return storage_.EmplaceBack(std::forward<Args>(args)...);
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// `InlinedVector::push_back()`
|
|
|
|
//
|
|
|
|
// Appends a copy of `v` to the end of the inlined vector.
|
|
|
|
void push_back(const_reference v) { static_cast<void>(emplace_back(v)); }
|
|
|
|
|
|
|
|
// Overload of `InlinedVector::push_back()` for moving `v` into a newly
|
|
|
|
// appended element.
|
|
|
|
void push_back(rvalue_reference v) {
|
|
|
|
static_cast<void>(emplace_back(std::move(v)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// `InlinedVector::pop_back()`
|
|
|
|
//
|
|
|
|
// Destroys the element at the end of the inlined vector and shrinks the size
|
|
|
|
// by `1` (unless the inlined vector is empty, in which case this is a no-op).
|
|
|
|
void pop_back() noexcept {
|
|
|
|
assert(!empty());
|
2019-06-25 21:32:44 +02:00
|
|
|
|
2019-06-18 22:05:50 +02:00
|
|
|
AllocatorTraits::destroy(*storage_.GetAllocPtr(), data() + (size() - 1));
|
2019-06-21 22:11:42 +02:00
|
|
|
storage_.SubtractSize(1);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::erase()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
2019-01-25 22:54:06 +01:00
|
|
|
// Erases the element at `pos` of the inlined vector, returning an `iterator`
|
|
|
|
// pointing to the first element following the erased element.
|
2018-11-06 22:01:25 +01:00
|
|
|
//
|
|
|
|
// NOTE: May return the end iterator, which is not dereferencable.
|
2019-01-25 22:54:06 +01:00
|
|
|
iterator erase(const_iterator pos) {
|
|
|
|
assert(pos >= begin());
|
|
|
|
assert(pos < end());
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2019-06-28 02:24:26 +02:00
|
|
|
return storage_.Erase(pos, pos + 1);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// Overload of `InlinedVector::erase()` for erasing all elements in the
|
|
|
|
// range [`from`, `to`) in the inlined vector. Returns an `iterator` pointing
|
|
|
|
// to the first element following the range erased or the end iterator if `to`
|
|
|
|
// was the end iterator.
|
2018-12-06 21:44:49 +01:00
|
|
|
iterator erase(const_iterator from, const_iterator to) {
|
2019-06-28 02:24:26 +02:00
|
|
|
assert(from >= begin());
|
2018-12-06 21:44:49 +01:00
|
|
|
assert(from <= to);
|
|
|
|
assert(to <= end());
|
|
|
|
|
2019-06-28 02:24:26 +02:00
|
|
|
if (ABSL_PREDICT_TRUE(from != to)) {
|
|
|
|
return storage_.Erase(from, to);
|
|
|
|
} else {
|
|
|
|
return const_cast<iterator>(from);
|
2018-12-06 21:44:49 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::clear()`
|
|
|
|
//
|
|
|
|
// Destroys all elements in the inlined vector, sets the size of `0` and
|
|
|
|
// deallocates the heap allocation if the inlined vector was allocated.
|
|
|
|
void clear() noexcept {
|
2019-06-25 21:32:44 +02:00
|
|
|
inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(),
|
|
|
|
size());
|
|
|
|
storage_.DeallocateIfAllocated();
|
2019-05-22 14:06:50 +02:00
|
|
|
storage_.SetInlinedSize(0);
|
2018-11-06 22:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// `InlinedVector::reserve()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Enlarges the underlying representation of the inlined vector so it can hold
|
|
|
|
// at least `n` elements. This method does not change `size()` or the actual
|
|
|
|
// contents of the vector.
|
|
|
|
//
|
2018-11-06 22:01:25 +01:00
|
|
|
// NOTE: If `n` does not exceed `capacity()`, `reserve()` will have no
|
|
|
|
// effects. Otherwise, `reserve()` will reallocate, performing an n-time
|
|
|
|
// element-wise move of everything contained.
|
2019-06-25 03:35:20 +02:00
|
|
|
void reserve(size_type n) { storage_.Reserve(n); }
|
2017-09-19 22:54:40 +02:00
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::shrink_to_fit()`
|
|
|
|
//
|
|
|
|
// Reduces memory usage by freeing unused memory. After this call, calls to
|
2019-03-22 20:20:05 +01:00
|
|
|
// `capacity()` will be equal to `max(N, size())`.
|
2018-01-10 20:46:09 +01:00
|
|
|
//
|
2019-03-22 20:20:05 +01:00
|
|
|
// If `size() <= N` and the elements are currently stored on the heap, they
|
|
|
|
// will be moved to the inlined storage and the heap memory will be
|
|
|
|
// deallocated.
|
2018-01-10 20:46:09 +01:00
|
|
|
//
|
2019-03-22 20:20:05 +01:00
|
|
|
// If `size() > N` and `size() < capacity()` the elements will be moved to a
|
|
|
|
// smaller heap allocation.
|
2018-01-10 20:46:09 +01:00
|
|
|
void shrink_to_fit() {
|
2019-06-21 22:11:42 +02:00
|
|
|
if (storage_.GetIsAllocated()) {
|
|
|
|
storage_.ShrinkToFit();
|
2018-01-10 20:46:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `InlinedVector::swap()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Swaps the contents of this inlined vector with the contents of `other`.
|
2018-12-06 21:44:49 +01:00
|
|
|
void swap(InlinedVector& other) {
|
2019-06-28 02:24:26 +02:00
|
|
|
if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
|
|
|
|
storage_.Swap(std::addressof(other.storage_));
|
2019-06-21 22:11:42 +02:00
|
|
|
}
|
2018-10-02 21:09:18 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 22:54:40 +02:00
|
|
|
private:
|
2019-01-17 18:48:22 +01:00
|
|
|
template <typename H, typename TheT, size_t TheN, typename TheA>
|
2019-03-28 19:12:19 +01:00
|
|
|
friend H AbslHashValue(H h, const absl::InlinedVector<TheT, TheN, TheA>& a);
|
2018-12-06 21:44:49 +01:00
|
|
|
|
2019-03-19 19:14:01 +01:00
|
|
|
Storage storage_;
|
2017-09-19 22:54:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// InlinedVector Non-Member Functions
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `swap()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Swaps the contents of two inlined vectors. This convenience function
|
2018-11-06 22:01:25 +01:00
|
|
|
// simply calls `InlinedVector::swap()`.
|
2017-09-19 22:54:40 +02:00
|
|
|
template <typename T, size_t N, typename A>
|
2019-03-28 19:12:19 +01:00
|
|
|
void swap(absl::InlinedVector<T, N, A>& a,
|
|
|
|
absl::InlinedVector<T, N, A>& b) noexcept(noexcept(a.swap(b))) {
|
2017-09-19 22:54:40 +02:00
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `operator==()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Tests the equivalency of the contents of two inlined vectors.
|
|
|
|
template <typename T, size_t N, typename A>
|
2019-03-28 19:12:19 +01:00
|
|
|
bool operator==(const absl::InlinedVector<T, N, A>& a,
|
|
|
|
const absl::InlinedVector<T, N, A>& b) {
|
|
|
|
auto a_data = a.data();
|
|
|
|
auto a_size = a.size();
|
|
|
|
auto b_data = b.data();
|
|
|
|
auto b_size = b.size();
|
|
|
|
return absl::equal(a_data, a_data + a_size, b_data, b_data + b_size);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `operator!=()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Tests the inequality of the contents of two inlined vectors.
|
|
|
|
template <typename T, size_t N, typename A>
|
2019-03-28 19:12:19 +01:00
|
|
|
bool operator!=(const absl::InlinedVector<T, N, A>& a,
|
|
|
|
const absl::InlinedVector<T, N, A>& b) {
|
2017-09-19 22:54:40 +02:00
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `operator<()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Tests whether the contents of one inlined vector are less than the contents
|
|
|
|
// of another through a lexicographical comparison operation.
|
|
|
|
template <typename T, size_t N, typename A>
|
2019-03-28 19:12:19 +01:00
|
|
|
bool operator<(const absl::InlinedVector<T, N, A>& a,
|
|
|
|
const absl::InlinedVector<T, N, A>& b) {
|
|
|
|
auto a_data = a.data();
|
|
|
|
auto a_size = a.size();
|
|
|
|
auto b_data = b.data();
|
|
|
|
auto b_size = b.size();
|
|
|
|
return std::lexicographical_compare(a_data, a_data + a_size, b_data,
|
|
|
|
b_data + b_size);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `operator>()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Tests whether the contents of one inlined vector are greater than the
|
|
|
|
// contents of another through a lexicographical comparison operation.
|
|
|
|
template <typename T, size_t N, typename A>
|
2019-03-28 19:12:19 +01:00
|
|
|
bool operator>(const absl::InlinedVector<T, N, A>& a,
|
|
|
|
const absl::InlinedVector<T, N, A>& b) {
|
2017-09-19 22:54:40 +02:00
|
|
|
return b < a;
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `operator<=()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Tests whether the contents of one inlined vector are less than or equal to
|
|
|
|
// the contents of another through a lexicographical comparison operation.
|
|
|
|
template <typename T, size_t N, typename A>
|
2019-03-28 19:12:19 +01:00
|
|
|
bool operator<=(const absl::InlinedVector<T, N, A>& a,
|
|
|
|
const absl::InlinedVector<T, N, A>& b) {
|
2017-09-19 22:54:40 +02:00
|
|
|
return !(b < a);
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
// `operator>=()`
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Tests whether the contents of one inlined vector are greater than or equal to
|
|
|
|
// the contents of another through a lexicographical comparison operation.
|
|
|
|
template <typename T, size_t N, typename A>
|
2019-03-28 19:12:19 +01:00
|
|
|
bool operator>=(const absl::InlinedVector<T, N, A>& a,
|
|
|
|
const absl::InlinedVector<T, N, A>& b) {
|
2017-09-19 22:54:40 +02:00
|
|
|
return !(a < b);
|
|
|
|
}
|
|
|
|
|
2019-04-01 23:06:28 +02:00
|
|
|
// `AbslHashValue()`
|
2019-01-15 17:49:10 +01:00
|
|
|
//
|
2019-04-01 23:06:28 +02:00
|
|
|
// Provides `absl::Hash` support for `absl::InlinedVector`. You do not normally
|
|
|
|
// call this function directly.
|
2019-01-17 18:48:22 +01:00
|
|
|
template <typename H, typename TheT, size_t TheN, typename TheA>
|
2019-03-28 19:12:19 +01:00
|
|
|
H AbslHashValue(H h, const absl::InlinedVector<TheT, TheN, TheA>& a) {
|
|
|
|
auto a_data = a.data();
|
|
|
|
auto a_size = a.size();
|
|
|
|
return H::combine(H::combine_contiguous(std::move(h), a_data, a_size),
|
|
|
|
a_size);
|
2018-12-06 21:44:49 +01:00
|
|
|
}
|
2019-03-28 19:12:19 +01:00
|
|
|
|
2019-01-30 19:59:43 +01:00
|
|
|
} // namespace absl
|
2018-12-06 21:44:49 +01:00
|
|
|
|
2017-09-19 22:54:40 +02:00
|
|
|
#endif // ABSL_CONTAINER_INLINED_VECTOR_H_
|