1 // Copyright 2019 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: inlined_vector.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header file contains the declaration and definition of an "inlined
20 // vector" which behaves in an equivalent fashion to a `std::vector`, except
21 // that storage for small sequences of the vector are provided inline without
22 // requiring any heap allocation.
23 //
24 // An `absl::InlinedVector<T, N>` specifies the default capacity `N` as one of
25 // its template parameters. Instances where `size() <= N` hold contained
26 // elements in inline space. Typically `N` is very small so that sequences that
27 // are expected to be short do not require allocations.
28 //
29 // An `absl::InlinedVector` does not usually require a specific allocator. If
30 // the inlined vector grows beyond its initial constraints, it will need to
31 // allocate (as any normal `std::vector` would). This is usually performed with
32 // the default allocator (defined as `std::allocator<T>`). Optionally, a custom
33 // allocator type may be specified as `A` in `absl::InlinedVector<T, N, A>`.
34
35 #ifndef ABSL_CONTAINER_INLINED_VECTOR_H_
36 #define ABSL_CONTAINER_INLINED_VECTOR_H_
37
38 #include <algorithm>
39 #include <cassert>
40 #include <cstddef>
41 #include <cstdlib>
42 #include <cstring>
43 #include <initializer_list>
44 #include <iterator>
45 #include <memory>
46 #include <type_traits>
47 #include <utility>
48
49 #include "absl/algorithm/algorithm.h"
50 #include "absl/base/internal/throw_delegate.h"
51 #include "absl/base/macros.h"
52 #include "absl/base/optimization.h"
53 #include "absl/base/port.h"
54 #include "absl/container/internal/inlined_vector.h"
55 #include "absl/memory/memory.h"
56
57 namespace absl {
58 ABSL_NAMESPACE_BEGIN
59 // -----------------------------------------------------------------------------
60 // InlinedVector
61 // -----------------------------------------------------------------------------
62 //
63 // An `absl::InlinedVector` is designed to be a drop-in replacement for
64 // `std::vector` for use cases where the vector's size is sufficiently small
65 // that it can be inlined. If the inlined vector does grow beyond its estimated
66 // capacity, it will trigger an initial allocation on the heap, and will behave
67 // as a `std::vector`. The API of the `absl::InlinedVector` within this file is
68 // designed to cover the same API footprint as covered by `std::vector`.
69 template <typename T, size_t N, typename A = std::allocator<T>>
70 class InlinedVector {
71 static_assert(N > 0, "`absl::InlinedVector` requires an inlined capacity.");
72
73 using Storage = inlined_vector_internal::Storage<T, N, A>;
74
75 template <typename TheA>
76 using AllocatorTraits = inlined_vector_internal::AllocatorTraits<TheA>;
77 template <typename TheA>
78 using MoveIterator = inlined_vector_internal::MoveIterator<TheA>;
79 template <typename TheA>
80 using IsMemcpyOk = inlined_vector_internal::IsMemcpyOk<TheA>;
81
82 template <typename TheA, typename Iterator>
83 using IteratorValueAdapter =
84 inlined_vector_internal::IteratorValueAdapter<TheA, Iterator>;
85 template <typename TheA>
86 using CopyValueAdapter = inlined_vector_internal::CopyValueAdapter<TheA>;
87 template <typename TheA>
88 using DefaultValueAdapter =
89 inlined_vector_internal::DefaultValueAdapter<TheA>;
90
91 template <typename Iterator>
92 using EnableIfAtLeastForwardIterator = absl::enable_if_t<
93 inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value, int>;
94 template <typename Iterator>
95 using DisableIfAtLeastForwardIterator = absl::enable_if_t<
96 !inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value, int>;
97
98 public:
99 using allocator_type = A;
100 using value_type = inlined_vector_internal::ValueType<A>;
101 using pointer = inlined_vector_internal::Pointer<A>;
102 using const_pointer = inlined_vector_internal::ConstPointer<A>;
103 using size_type = inlined_vector_internal::SizeType<A>;
104 using difference_type = inlined_vector_internal::DifferenceType<A>;
105 using reference = inlined_vector_internal::Reference<A>;
106 using const_reference = inlined_vector_internal::ConstReference<A>;
107 using iterator = inlined_vector_internal::Iterator<A>;
108 using const_iterator = inlined_vector_internal::ConstIterator<A>;
109 using reverse_iterator = inlined_vector_internal::ReverseIterator<A>;
110 using const_reverse_iterator =
111 inlined_vector_internal::ConstReverseIterator<A>;
112
113 // ---------------------------------------------------------------------------
114 // InlinedVector Constructors and Destructor
115 // ---------------------------------------------------------------------------
116
117 // Creates an empty inlined vector with a value-initialized allocator.
noexcept(noexcept (allocator_type ()))118 InlinedVector() noexcept(noexcept(allocator_type())) : storage_() {}
119
120 // Creates an empty inlined vector with a copy of `allocator`.
InlinedVector(const allocator_type & allocator)121 explicit InlinedVector(const allocator_type& allocator) noexcept
122 : storage_(allocator) {}
123
124 // Creates an inlined vector with `n` copies of `value_type()`.
125 explicit InlinedVector(size_type n,
126 const allocator_type& allocator = allocator_type())
storage_(allocator)127 : storage_(allocator) {
128 storage_.Initialize(DefaultValueAdapter<A>(), n);
129 }
130
131 // Creates an inlined vector with `n` copies of `v`.
132 InlinedVector(size_type n, const_reference v,
133 const allocator_type& allocator = allocator_type())
storage_(allocator)134 : storage_(allocator) {
135 storage_.Initialize(CopyValueAdapter<A>(std::addressof(v)), n);
136 }
137
138 // Creates an inlined vector with copies of the elements of `list`.
139 InlinedVector(std::initializer_list<value_type> list,
140 const allocator_type& allocator = allocator_type())
141 : InlinedVector(list.begin(), list.end(), allocator) {}
142
143 // Creates an inlined vector with elements constructed from the provided
144 // forward iterator range [`first`, `last`).
145 //
146 // NOTE: the `enable_if` prevents ambiguous interpretation between a call to
147 // this constructor with two integral arguments and a call to the above
148 // `InlinedVector(size_type, const_reference)` constructor.
149 template <typename ForwardIterator,
150 EnableIfAtLeastForwardIterator<ForwardIterator> = 0>
151 InlinedVector(ForwardIterator first, ForwardIterator last,
152 const allocator_type& allocator = allocator_type())
storage_(allocator)153 : storage_(allocator) {
154 storage_.Initialize(IteratorValueAdapter<A, ForwardIterator>(first),
155 std::distance(first, last));
156 }
157
158 // Creates an inlined vector with elements constructed from the provided input
159 // iterator range [`first`, `last`).
160 template <typename InputIterator,
161 DisableIfAtLeastForwardIterator<InputIterator> = 0>
162 InlinedVector(InputIterator first, InputIterator last,
163 const allocator_type& allocator = allocator_type())
storage_(allocator)164 : storage_(allocator) {
165 std::copy(first, last, std::back_inserter(*this));
166 }
167
168 // Creates an inlined vector by copying the contents of `other` using
169 // `other`'s allocator.
InlinedVector(const InlinedVector & other)170 InlinedVector(const InlinedVector& other)
171 : InlinedVector(other, other.storage_.GetAllocator()) {}
172
173 // Creates an inlined vector by copying the contents of `other` using the
174 // provided `allocator`.
InlinedVector(const InlinedVector & other,const allocator_type & allocator)175 InlinedVector(const InlinedVector& other, const allocator_type& allocator)
176 : storage_(allocator) {
177 if (other.empty()) {
178 // Empty; nothing to do.
179 } else if (IsMemcpyOk<A>::value && !other.storage_.GetIsAllocated()) {
180 // Memcpy-able and do not need allocation.
181 storage_.MemcpyFrom(other.storage_);
182 } else {
183 storage_.InitFrom(other.storage_);
184 }
185 }
186
187 // Creates an inlined vector by moving in the contents of `other` without
188 // allocating. If `other` contains allocated memory, the newly-created inlined
189 // vector will take ownership of that memory. However, if `other` does not
190 // contain allocated memory, the newly-created inlined vector will perform
191 // element-wise move construction of the contents of `other`.
192 //
193 // NOTE: since no allocation is performed for the inlined vector in either
194 // case, the `noexcept(...)` specification depends on whether moving the
195 // underlying objects can throw. It is assumed assumed that...
196 // a) move constructors should only throw due to allocation failure.
197 // b) if `value_type`'s move constructor allocates, it uses the same
198 // allocation function as the inlined vector's allocator.
199 // Thus, the move constructor is non-throwing if the allocator is non-throwing
200 // or `value_type`'s move constructor is specified as `noexcept`.
201 InlinedVector(InlinedVector&& other) noexcept(
202 absl::allocator_is_nothrow<allocator_type>::value ||
203 std::is_nothrow_move_constructible<value_type>::value)
204 : storage_(other.storage_.GetAllocator()) {
205 if (IsMemcpyOk<A>::value) {
206 storage_.MemcpyFrom(other.storage_);
207
208 other.storage_.SetInlinedSize(0);
209 } else if (other.storage_.GetIsAllocated()) {
210 storage_.SetAllocation({other.storage_.GetAllocatedData(),
211 other.storage_.GetAllocatedCapacity()});
212 storage_.SetAllocatedSize(other.storage_.GetSize());
213
214 other.storage_.SetInlinedSize(0);
215 } else {
216 IteratorValueAdapter<A, MoveIterator<A>> other_values(
217 MoveIterator<A>(other.storage_.GetInlinedData()));
218
219 inlined_vector_internal::ConstructElements<A>(
220 storage_.GetAllocator(), storage_.GetInlinedData(), other_values,
221 other.storage_.GetSize());
222
223 storage_.SetInlinedSize(other.storage_.GetSize());
224 }
225 }
226
227 // Creates an inlined vector by moving in the contents of `other` with a copy
228 // of `allocator`.
229 //
230 // NOTE: if `other`'s allocator is not equal to `allocator`, even if `other`
231 // contains allocated memory, this move constructor will still allocate. Since
232 // allocation is performed, this constructor can only be `noexcept` if the
233 // specified allocator is also `noexcept`.
InlinedVector(InlinedVector && other,const allocator_type & allocator)234 InlinedVector(
235 InlinedVector&& other,
236 const allocator_type& allocator)
237 noexcept(absl::allocator_is_nothrow<allocator_type>::value)
238 : storage_(allocator) {
239 if (IsMemcpyOk<A>::value) {
240 storage_.MemcpyFrom(other.storage_);
241
242 other.storage_.SetInlinedSize(0);
243 } else if ((storage_.GetAllocator() == other.storage_.GetAllocator()) &&
244 other.storage_.GetIsAllocated()) {
245 storage_.SetAllocation({other.storage_.GetAllocatedData(),
246 other.storage_.GetAllocatedCapacity()});
247 storage_.SetAllocatedSize(other.storage_.GetSize());
248
249 other.storage_.SetInlinedSize(0);
250 } else {
251 storage_.Initialize(IteratorValueAdapter<A, MoveIterator<A>>(
252 MoveIterator<A>(other.data())),
253 other.size());
254 }
255 }
256
~InlinedVector()257 ~InlinedVector() {}
258
259 // ---------------------------------------------------------------------------
260 // InlinedVector Member Accessors
261 // ---------------------------------------------------------------------------
262
263 // `InlinedVector::empty()`
264 //
265 // Returns whether the inlined vector contains no elements.
empty()266 bool empty() const noexcept { return !size(); }
267
268 // `InlinedVector::size()`
269 //
270 // Returns the number of elements in the inlined vector.
size()271 size_type size() const noexcept { return storage_.GetSize(); }
272
273 // `InlinedVector::max_size()`
274 //
275 // Returns the maximum number of elements the inlined vector can hold.
max_size()276 size_type max_size() const noexcept {
277 // One bit of the size storage is used to indicate whether the inlined
278 // vector contains allocated memory. As a result, the maximum size that the
279 // inlined vector can express is half of the max for `size_type`.
280 return (std::numeric_limits<size_type>::max)() / 2;
281 }
282
283 // `InlinedVector::capacity()`
284 //
285 // Returns the number of elements that could be stored in the inlined vector
286 // without requiring a reallocation.
287 //
288 // NOTE: for most inlined vectors, `capacity()` should be equal to the
289 // template parameter `N`. For inlined vectors which exceed this capacity,
290 // they will no longer be inlined and `capacity()` will equal the capactity of
291 // the allocated memory.
capacity()292 size_type capacity() const noexcept {
293 return storage_.GetIsAllocated() ? storage_.GetAllocatedCapacity()
294 : storage_.GetInlinedCapacity();
295 }
296
297 // `InlinedVector::data()`
298 //
299 // Returns a `pointer` to the elements of the inlined vector. This pointer
300 // can be used to access and modify the contained elements.
301 //
302 // NOTE: only elements within [`data()`, `data() + size()`) are valid.
data()303 pointer data() noexcept {
304 return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
305 : storage_.GetInlinedData();
306 }
307
308 // Overload of `InlinedVector::data()` that returns a `const_pointer` to the
309 // elements of the inlined vector. This pointer can be used to access but not
310 // modify the contained elements.
311 //
312 // NOTE: only elements within [`data()`, `data() + size()`) are valid.
data()313 const_pointer data() const noexcept {
314 return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
315 : storage_.GetInlinedData();
316 }
317
318 // `InlinedVector::operator[](...)`
319 //
320 // Returns a `reference` to the `i`th element of the inlined vector.
321 reference operator[](size_type i) {
322 ABSL_HARDENING_ASSERT(i < size());
323 return data()[i];
324 }
325
326 // Overload of `InlinedVector::operator[](...)` that returns a
327 // `const_reference` to the `i`th element of the inlined vector.
328 const_reference operator[](size_type i) const {
329 ABSL_HARDENING_ASSERT(i < size());
330 return data()[i];
331 }
332
333 // `InlinedVector::at(...)`
334 //
335 // Returns a `reference` to the `i`th element of the inlined vector.
336 //
337 // NOTE: if `i` is not within the required range of `InlinedVector::at(...)`,
338 // in both debug and non-debug builds, `std::out_of_range` will be thrown.
at(size_type i)339 reference at(size_type i) {
340 if (ABSL_PREDICT_FALSE(i >= size())) {
341 base_internal::ThrowStdOutOfRange(
342 "`InlinedVector::at(size_type)` failed bounds check");
343 }
344 return data()[i];
345 }
346
347 // Overload of `InlinedVector::at(...)` that returns a `const_reference` to
348 // the `i`th element of the inlined vector.
349 //
350 // NOTE: if `i` is not within the required range of `InlinedVector::at(...)`,
351 // in both debug and non-debug builds, `std::out_of_range` will be thrown.
at(size_type i)352 const_reference at(size_type i) const {
353 if (ABSL_PREDICT_FALSE(i >= size())) {
354 base_internal::ThrowStdOutOfRange(
355 "`InlinedVector::at(size_type) const` failed bounds check");
356 }
357 return data()[i];
358 }
359
360 // `InlinedVector::front()`
361 //
362 // Returns a `reference` to the first element of the inlined vector.
front()363 reference front() {
364 ABSL_HARDENING_ASSERT(!empty());
365 return data()[0];
366 }
367
368 // Overload of `InlinedVector::front()` that returns a `const_reference` to
369 // the first element of the inlined vector.
front()370 const_reference front() const {
371 ABSL_HARDENING_ASSERT(!empty());
372 return data()[0];
373 }
374
375 // `InlinedVector::back()`
376 //
377 // Returns a `reference` to the last element of the inlined vector.
back()378 reference back() {
379 ABSL_HARDENING_ASSERT(!empty());
380 return data()[size() - 1];
381 }
382
383 // Overload of `InlinedVector::back()` that returns a `const_reference` to the
384 // last element of the inlined vector.
back()385 const_reference back() const {
386 ABSL_HARDENING_ASSERT(!empty());
387 return data()[size() - 1];
388 }
389
390 // `InlinedVector::begin()`
391 //
392 // Returns an `iterator` to the beginning of the inlined vector.
begin()393 iterator begin() noexcept { return data(); }
394
395 // Overload of `InlinedVector::begin()` that returns a `const_iterator` to
396 // the beginning of the inlined vector.
begin()397 const_iterator begin() const noexcept { return data(); }
398
399 // `InlinedVector::end()`
400 //
401 // Returns an `iterator` to the end of the inlined vector.
end()402 iterator end() noexcept { return data() + size(); }
403
404 // Overload of `InlinedVector::end()` that returns a `const_iterator` to the
405 // end of the inlined vector.
end()406 const_iterator end() const noexcept { return data() + size(); }
407
408 // `InlinedVector::cbegin()`
409 //
410 // Returns a `const_iterator` to the beginning of the inlined vector.
cbegin()411 const_iterator cbegin() const noexcept { return begin(); }
412
413 // `InlinedVector::cend()`
414 //
415 // Returns a `const_iterator` to the end of the inlined vector.
cend()416 const_iterator cend() const noexcept { return end(); }
417
418 // `InlinedVector::rbegin()`
419 //
420 // Returns a `reverse_iterator` from the end of the inlined vector.
rbegin()421 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
422
423 // Overload of `InlinedVector::rbegin()` that returns a
424 // `const_reverse_iterator` from the end of the inlined vector.
rbegin()425 const_reverse_iterator rbegin() const noexcept {
426 return const_reverse_iterator(end());
427 }
428
429 // `InlinedVector::rend()`
430 //
431 // Returns a `reverse_iterator` from the beginning of the inlined vector.
rend()432 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
433
434 // Overload of `InlinedVector::rend()` that returns a `const_reverse_iterator`
435 // from the beginning of the inlined vector.
rend()436 const_reverse_iterator rend() const noexcept {
437 return const_reverse_iterator(begin());
438 }
439
440 // `InlinedVector::crbegin()`
441 //
442 // Returns a `const_reverse_iterator` from the end of the inlined vector.
crbegin()443 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
444
445 // `InlinedVector::crend()`
446 //
447 // Returns a `const_reverse_iterator` from the beginning of the inlined
448 // vector.
crend()449 const_reverse_iterator crend() const noexcept { return rend(); }
450
451 // `InlinedVector::get_allocator()`
452 //
453 // Returns a copy of the inlined vector's allocator.
get_allocator()454 allocator_type get_allocator() const { return storage_.GetAllocator(); }
455
456 // ---------------------------------------------------------------------------
457 // InlinedVector Member Mutators
458 // ---------------------------------------------------------------------------
459
460 // `InlinedVector::operator=(...)`
461 //
462 // Replaces the elements of the inlined vector with copies of the elements of
463 // `list`.
464 InlinedVector& operator=(std::initializer_list<value_type> list) {
465 assign(list.begin(), list.end());
466
467 return *this;
468 }
469
470 // Overload of `InlinedVector::operator=(...)` that replaces the elements of
471 // the inlined vector with copies of the elements of `other`.
472 InlinedVector& operator=(const InlinedVector& other) {
473 if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
474 const_pointer other_data = other.data();
475 assign(other_data, other_data + other.size());
476 }
477
478 return *this;
479 }
480
481 // Overload of `InlinedVector::operator=(...)` that moves the elements of
482 // `other` into the inlined vector.
483 //
484 // NOTE: as a result of calling this overload, `other` is left in a valid but
485 // unspecified state.
486 InlinedVector& operator=(InlinedVector&& other) {
487 if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
488 if (IsMemcpyOk<A>::value || other.storage_.GetIsAllocated()) {
489 inlined_vector_internal::DestroyElements<A>(storage_.GetAllocator(),
490 data(), size());
491 storage_.DeallocateIfAllocated();
492 storage_.MemcpyFrom(other.storage_);
493
494 other.storage_.SetInlinedSize(0);
495 } else {
496 storage_.Assign(IteratorValueAdapter<A, MoveIterator<A>>(
497 MoveIterator<A>(other.storage_.GetInlinedData())),
498 other.size());
499 }
500 }
501
502 return *this;
503 }
504
505 // `InlinedVector::assign(...)`
506 //
507 // Replaces the contents of the inlined vector with `n` copies of `v`.
assign(size_type n,const_reference v)508 void assign(size_type n, const_reference v) {
509 storage_.Assign(CopyValueAdapter<A>(std::addressof(v)), n);
510 }
511
512 // Overload of `InlinedVector::assign(...)` that replaces the contents of the
513 // inlined vector with copies of the elements of `list`.
assign(std::initializer_list<value_type> list)514 void assign(std::initializer_list<value_type> list) {
515 assign(list.begin(), list.end());
516 }
517
518 // Overload of `InlinedVector::assign(...)` to replace the contents of the
519 // inlined vector with the range [`first`, `last`).
520 //
521 // NOTE: this overload is for iterators that are "forward" category or better.
522 template <typename ForwardIterator,
523 EnableIfAtLeastForwardIterator<ForwardIterator> = 0>
assign(ForwardIterator first,ForwardIterator last)524 void assign(ForwardIterator first, ForwardIterator last) {
525 storage_.Assign(IteratorValueAdapter<A, ForwardIterator>(first),
526 std::distance(first, last));
527 }
528
529 // Overload of `InlinedVector::assign(...)` to replace the contents of the
530 // inlined vector with the range [`first`, `last`).
531 //
532 // NOTE: this overload is for iterators that are "input" category.
533 template <typename InputIterator,
534 DisableIfAtLeastForwardIterator<InputIterator> = 0>
assign(InputIterator first,InputIterator last)535 void assign(InputIterator first, InputIterator last) {
536 size_type i = 0;
537 for (; i < size() && first != last; ++i, static_cast<void>(++first)) {
538 data()[i] = *first;
539 }
540
541 erase(data() + i, data() + size());
542 std::copy(first, last, std::back_inserter(*this));
543 }
544
545 // `InlinedVector::resize(...)`
546 //
547 // Resizes the inlined vector to contain `n` elements.
548 //
549 // NOTE: If `n` is smaller than `size()`, extra elements are destroyed. If `n`
550 // is larger than `size()`, new elements are value-initialized.
resize(size_type n)551 void resize(size_type n) {
552 ABSL_HARDENING_ASSERT(n <= max_size());
553 storage_.Resize(DefaultValueAdapter<A>(), n);
554 }
555
556 // Overload of `InlinedVector::resize(...)` that resizes the inlined vector to
557 // contain `n` elements.
558 //
559 // NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n`
560 // is larger than `size()`, new elements are copied-constructed from `v`.
resize(size_type n,const_reference v)561 void resize(size_type n, const_reference v) {
562 ABSL_HARDENING_ASSERT(n <= max_size());
563 storage_.Resize(CopyValueAdapter<A>(std::addressof(v)), n);
564 }
565
566 // `InlinedVector::insert(...)`
567 //
568 // Inserts a copy of `v` at `pos`, returning an `iterator` to the newly
569 // inserted element.
insert(const_iterator pos,const_reference v)570 iterator insert(const_iterator pos, const_reference v) {
571 return emplace(pos, v);
572 }
573
574 // Overload of `InlinedVector::insert(...)` that inserts `v` at `pos` using
575 // move semantics, returning an `iterator` to the newly inserted element.
insert(const_iterator pos,value_type && v)576 iterator insert(const_iterator pos, value_type&& v) {
577 return emplace(pos, std::move(v));
578 }
579
580 // Overload of `InlinedVector::insert(...)` that inserts `n` contiguous copies
581 // of `v` starting at `pos`, returning an `iterator` pointing to the first of
582 // the newly inserted elements.
insert(const_iterator pos,size_type n,const_reference v)583 iterator insert(const_iterator pos, size_type n, const_reference v) {
584 ABSL_HARDENING_ASSERT(pos >= begin());
585 ABSL_HARDENING_ASSERT(pos <= end());
586
587 if (ABSL_PREDICT_TRUE(n != 0)) {
588 value_type dealias = v;
589 return storage_.Insert(pos, CopyValueAdapter<A>(std::addressof(dealias)),
590 n);
591 } else {
592 return const_cast<iterator>(pos);
593 }
594 }
595
596 // Overload of `InlinedVector::insert(...)` that inserts copies of the
597 // elements of `list` starting at `pos`, returning an `iterator` pointing to
598 // the first of the newly inserted elements.
insert(const_iterator pos,std::initializer_list<value_type> list)599 iterator insert(const_iterator pos, std::initializer_list<value_type> list) {
600 return insert(pos, list.begin(), list.end());
601 }
602
603 // Overload of `InlinedVector::insert(...)` that inserts the range [`first`,
604 // `last`) starting at `pos`, returning an `iterator` pointing to the first
605 // of the newly inserted elements.
606 //
607 // NOTE: this overload is for iterators that are "forward" category or better.
608 template <typename ForwardIterator,
609 EnableIfAtLeastForwardIterator<ForwardIterator> = 0>
insert(const_iterator pos,ForwardIterator first,ForwardIterator last)610 iterator insert(const_iterator pos, ForwardIterator first,
611 ForwardIterator last) {
612 ABSL_HARDENING_ASSERT(pos >= begin());
613 ABSL_HARDENING_ASSERT(pos <= end());
614
615 if (ABSL_PREDICT_TRUE(first != last)) {
616 return storage_.Insert(pos,
617 IteratorValueAdapter<A, ForwardIterator>(first),
618 std::distance(first, last));
619 } else {
620 return const_cast<iterator>(pos);
621 }
622 }
623
624 // Overload of `InlinedVector::insert(...)` that inserts the range [`first`,
625 // `last`) starting at `pos`, returning an `iterator` pointing to the first
626 // of the newly inserted elements.
627 //
628 // NOTE: this overload is for iterators that are "input" category.
629 template <typename InputIterator,
630 DisableIfAtLeastForwardIterator<InputIterator> = 0>
insert(const_iterator pos,InputIterator first,InputIterator last)631 iterator insert(const_iterator pos, InputIterator first, InputIterator last) {
632 ABSL_HARDENING_ASSERT(pos >= begin());
633 ABSL_HARDENING_ASSERT(pos <= end());
634
635 size_type index = std::distance(cbegin(), pos);
636 for (size_type i = index; first != last; ++i, static_cast<void>(++first)) {
637 insert(data() + i, *first);
638 }
639
640 return iterator(data() + index);
641 }
642
643 // `InlinedVector::emplace(...)`
644 //
645 // Constructs and inserts an element using `args...` in the inlined vector at
646 // `pos`, returning an `iterator` pointing to the newly emplaced element.
647 template <typename... Args>
emplace(const_iterator pos,Args &&...args)648 iterator emplace(const_iterator pos, Args&&... args) {
649 ABSL_HARDENING_ASSERT(pos >= begin());
650 ABSL_HARDENING_ASSERT(pos <= end());
651
652 value_type dealias(std::forward<Args>(args)...);
653 return storage_.Insert(pos,
654 IteratorValueAdapter<A, MoveIterator<A>>(
655 MoveIterator<A>(std::addressof(dealias))),
656 1);
657 }
658
659 // `InlinedVector::emplace_back(...)`
660 //
661 // Constructs and inserts an element using `args...` in the inlined vector at
662 // `end()`, returning a `reference` to the newly emplaced element.
663 template <typename... Args>
emplace_back(Args &&...args)664 reference emplace_back(Args&&... args) {
665 return storage_.EmplaceBack(std::forward<Args>(args)...);
666 }
667
668 // `InlinedVector::push_back(...)`
669 //
670 // Inserts a copy of `v` in the inlined vector at `end()`.
push_back(const_reference v)671 void push_back(const_reference v) { static_cast<void>(emplace_back(v)); }
672
673 // Overload of `InlinedVector::push_back(...)` for inserting `v` at `end()`
674 // using move semantics.
push_back(value_type && v)675 void push_back(value_type&& v) {
676 static_cast<void>(emplace_back(std::move(v)));
677 }
678
679 // `InlinedVector::pop_back()`
680 //
681 // Destroys the element at `back()`, reducing the size by `1`.
pop_back()682 void pop_back() noexcept {
683 ABSL_HARDENING_ASSERT(!empty());
684
685 AllocatorTraits<A>::destroy(storage_.GetAllocator(), data() + (size() - 1));
686 storage_.SubtractSize(1);
687 }
688
689 // `InlinedVector::erase(...)`
690 //
691 // Erases the element at `pos`, returning an `iterator` pointing to where the
692 // erased element was located.
693 //
694 // NOTE: may return `end()`, which is not dereferencable.
erase(const_iterator pos)695 iterator erase(const_iterator pos) {
696 ABSL_HARDENING_ASSERT(pos >= begin());
697 ABSL_HARDENING_ASSERT(pos < end());
698
699 return storage_.Erase(pos, pos + 1);
700 }
701
702 // Overload of `InlinedVector::erase(...)` that erases every element in the
703 // range [`from`, `to`), returning an `iterator` pointing to where the first
704 // erased element was located.
705 //
706 // NOTE: may return `end()`, which is not dereferencable.
erase(const_iterator from,const_iterator to)707 iterator erase(const_iterator from, const_iterator to) {
708 ABSL_HARDENING_ASSERT(from >= begin());
709 ABSL_HARDENING_ASSERT(from <= to);
710 ABSL_HARDENING_ASSERT(to <= end());
711
712 if (ABSL_PREDICT_TRUE(from != to)) {
713 return storage_.Erase(from, to);
714 } else {
715 return const_cast<iterator>(from);
716 }
717 }
718
719 // `InlinedVector::clear()`
720 //
721 // Destroys all elements in the inlined vector, setting the size to `0` and
722 // deallocating any held memory.
clear()723 void clear() noexcept {
724 inlined_vector_internal::DestroyElements<A>(storage_.GetAllocator(), data(),
725 size());
726 storage_.DeallocateIfAllocated();
727
728 storage_.SetInlinedSize(0);
729 }
730
731 // `InlinedVector::reserve(...)`
732 //
733 // Ensures that there is enough room for at least `n` elements.
reserve(size_type n)734 void reserve(size_type n) { storage_.Reserve(n); }
735
736 // `InlinedVector::shrink_to_fit()`
737 //
738 // Attempts to reduce memory usage by moving elements to (or keeping elements
739 // in) the smallest available buffer sufficient for containing `size()`
740 // elements.
741 //
742 // If `size()` is sufficiently small, the elements will be moved into (or kept
743 // in) the inlined space.
shrink_to_fit()744 void shrink_to_fit() {
745 if (storage_.GetIsAllocated()) {
746 storage_.ShrinkToFit();
747 }
748 }
749
750 // `InlinedVector::swap(...)`
751 //
752 // Swaps the contents of the inlined vector with `other`.
swap(InlinedVector & other)753 void swap(InlinedVector& other) {
754 if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
755 storage_.Swap(std::addressof(other.storage_));
756 }
757 }
758
759 private:
760 template <typename H, typename TheT, size_t TheN, typename TheA>
761 friend H AbslHashValue(H h, const absl::InlinedVector<TheT, TheN, TheA>& a);
762
763 Storage storage_;
764 };
765
766 // -----------------------------------------------------------------------------
767 // InlinedVector Non-Member Functions
768 // -----------------------------------------------------------------------------
769
770 // `swap(...)`
771 //
772 // Swaps the contents of two inlined vectors.
773 template <typename T, size_t N, typename A>
swap(absl::InlinedVector<T,N,A> & a,absl::InlinedVector<T,N,A> & b)774 void swap(absl::InlinedVector<T, N, A>& a,
775 absl::InlinedVector<T, N, A>& b) noexcept(noexcept(a.swap(b))) {
776 a.swap(b);
777 }
778
779 // `operator==(...)`
780 //
781 // Tests for value-equality of two inlined vectors.
782 template <typename T, size_t N, typename A>
783 bool operator==(const absl::InlinedVector<T, N, A>& a,
784 const absl::InlinedVector<T, N, A>& b) {
785 auto a_data = a.data();
786 auto b_data = b.data();
787 return absl::equal(a_data, a_data + a.size(), b_data, b_data + b.size());
788 }
789
790 // `operator!=(...)`
791 //
792 // Tests for value-inequality of two inlined vectors.
793 template <typename T, size_t N, typename A>
794 bool operator!=(const absl::InlinedVector<T, N, A>& a,
795 const absl::InlinedVector<T, N, A>& b) {
796 return !(a == b);
797 }
798
799 // `operator<(...)`
800 //
801 // Tests whether the value of an inlined vector is less than the value of
802 // another inlined vector using a lexicographical comparison algorithm.
803 template <typename T, size_t N, typename A>
804 bool operator<(const absl::InlinedVector<T, N, A>& a,
805 const absl::InlinedVector<T, N, A>& b) {
806 auto a_data = a.data();
807 auto b_data = b.data();
808 return std::lexicographical_compare(a_data, a_data + a.size(), b_data,
809 b_data + b.size());
810 }
811
812 // `operator>(...)`
813 //
814 // Tests whether the value of an inlined vector is greater than the value of
815 // another inlined vector using a lexicographical comparison algorithm.
816 template <typename T, size_t N, typename A>
817 bool operator>(const absl::InlinedVector<T, N, A>& a,
818 const absl::InlinedVector<T, N, A>& b) {
819 return b < a;
820 }
821
822 // `operator<=(...)`
823 //
824 // Tests whether the value of an inlined vector is less than or equal to the
825 // value of another inlined vector using a lexicographical comparison algorithm.
826 template <typename T, size_t N, typename A>
827 bool operator<=(const absl::InlinedVector<T, N, A>& a,
828 const absl::InlinedVector<T, N, A>& b) {
829 return !(b < a);
830 }
831
832 // `operator>=(...)`
833 //
834 // Tests whether the value of an inlined vector is greater than or equal to the
835 // value of another inlined vector using a lexicographical comparison algorithm.
836 template <typename T, size_t N, typename A>
837 bool operator>=(const absl::InlinedVector<T, N, A>& a,
838 const absl::InlinedVector<T, N, A>& b) {
839 return !(a < b);
840 }
841
842 // `AbslHashValue(...)`
843 //
844 // Provides `absl::Hash` support for `absl::InlinedVector`. It is uncommon to
845 // call this directly.
846 template <typename H, typename T, size_t N, typename A>
AbslHashValue(H h,const absl::InlinedVector<T,N,A> & a)847 H AbslHashValue(H h, const absl::InlinedVector<T, N, A>& a) {
848 auto size = a.size();
849 return H::combine(H::combine_contiguous(std::move(h), a.data(), size), size);
850 }
851
852 ABSL_NAMESPACE_END
853 } // namespace absl
854
855 #endif // ABSL_CONTAINER_INLINED_VECTOR_H_
856