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