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 <cstddef>
40 #include <cstdlib>
41 #include <cstring>
42 #include <initializer_list>
43 #include <iterator>
44 #include <memory>
45 #include <type_traits>
46 #include <utility>
47
48 #include "absl/algorithm/algorithm.h"
49 #include "absl/base/internal/throw_delegate.h"
50 #include "absl/base/macros.h"
51 #include "absl/base/optimization.h"
52 #include "absl/base/port.h"
53 #include "absl/container/internal/inlined_vector.h"
54 #include "absl/memory/memory.h"
55 #include "absl/meta/type_traits.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 IsMoveAssignOk = inlined_vector_internal::IsMoveAssignOk<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 using MemcpyPolicy = typename Storage::MemcpyPolicy;
99 using ElementwiseAssignPolicy = typename Storage::ElementwiseAssignPolicy;
100 using ElementwiseConstructPolicy =
101 typename Storage::ElementwiseConstructPolicy;
102 using MoveAssignmentPolicy = typename Storage::MoveAssignmentPolicy;
103
104 public:
105 using allocator_type = A;
106 using value_type = inlined_vector_internal::ValueType<A>;
107 using pointer = inlined_vector_internal::Pointer<A>;
108 using const_pointer = inlined_vector_internal::ConstPointer<A>;
109 using size_type = inlined_vector_internal::SizeType<A>;
110 using difference_type = inlined_vector_internal::DifferenceType<A>;
111 using reference = inlined_vector_internal::Reference<A>;
112 using const_reference = inlined_vector_internal::ConstReference<A>;
113 using iterator = inlined_vector_internal::Iterator<A>;
114 using const_iterator = inlined_vector_internal::ConstIterator<A>;
115 using reverse_iterator = inlined_vector_internal::ReverseIterator<A>;
116 using const_reverse_iterator =
117 inlined_vector_internal::ConstReverseIterator<A>;
118
119 // ---------------------------------------------------------------------------
120 // InlinedVector Constructors and Destructor
121 // ---------------------------------------------------------------------------
122
123 // Creates an empty inlined vector with a value-initialized allocator.
noexcept(noexcept (allocator_type ()))124 InlinedVector() noexcept(noexcept(allocator_type())) : storage_() {}
125
126 // Creates an empty inlined vector with a copy of `allocator`.
InlinedVector(const allocator_type & allocator)127 explicit InlinedVector(const allocator_type& allocator) noexcept
128 : storage_(allocator) {}
129
130 // Creates an inlined vector with `n` copies of `value_type()`.
131 explicit InlinedVector(size_type n,
132 const allocator_type& allocator = allocator_type())
storage_(allocator)133 : storage_(allocator) {
134 storage_.Initialize(DefaultValueAdapter<A>(), n);
135 }
136
137 // Creates an inlined vector with `n` copies of `v`.
138 InlinedVector(size_type n, const_reference v,
139 const allocator_type& allocator = allocator_type())
storage_(allocator)140 : storage_(allocator) {
141 storage_.Initialize(CopyValueAdapter<A>(std::addressof(v)), n);
142 }
143
144 // Creates an inlined vector with copies of the elements of `list`.
145 InlinedVector(std::initializer_list<value_type> list,
146 const allocator_type& allocator = allocator_type())
147 : InlinedVector(list.begin(), list.end(), allocator) {}
148
149 // Creates an inlined vector with elements constructed from the provided
150 // forward iterator range [`first`, `last`).
151 //
152 // NOTE: the `enable_if` prevents ambiguous interpretation between a call to
153 // this constructor with two integral arguments and a call to the above
154 // `InlinedVector(size_type, const_reference)` constructor.
155 template <typename ForwardIterator,
156 EnableIfAtLeastForwardIterator<ForwardIterator> = 0>
157 InlinedVector(ForwardIterator first, ForwardIterator last,
158 const allocator_type& allocator = allocator_type())
storage_(allocator)159 : storage_(allocator) {
160 storage_.Initialize(IteratorValueAdapter<A, ForwardIterator>(first),
161 static_cast<size_t>(std::distance(first, last)));
162 }
163
164 // Creates an inlined vector with elements constructed from the provided input
165 // iterator range [`first`, `last`).
166 template <typename InputIterator,
167 DisableIfAtLeastForwardIterator<InputIterator> = 0>
168 InlinedVector(InputIterator first, InputIterator last,
169 const allocator_type& allocator = allocator_type())
storage_(allocator)170 : storage_(allocator) {
171 std::copy(first, last, std::back_inserter(*this));
172 }
173
174 // Creates an inlined vector by copying the contents of `other` using
175 // `other`'s allocator.
InlinedVector(const InlinedVector & other)176 InlinedVector(const InlinedVector& other)
177 : InlinedVector(other, other.storage_.GetAllocator()) {}
178
179 // Creates an inlined vector by copying the contents of `other` using the
180 // provided `allocator`.
InlinedVector(const InlinedVector & other,const allocator_type & allocator)181 InlinedVector(const InlinedVector& other, const allocator_type& allocator)
182 : storage_(allocator) {
183 // Fast path: if the other vector is empty, there's nothing for us to do.
184 if (other.empty()) {
185 return;
186 }
187
188 // Fast path: if the value type is trivially copy constructible, we know the
189 // allocator doesn't do anything fancy, and there is nothing on the heap
190 // then we know it is legal for us to simply memcpy the other vector's
191 // inlined bytes to form our copy of its elements.
192 if (absl::is_trivially_copy_constructible<value_type>::value &&
193 std::is_same<A, std::allocator<value_type>>::value &&
194 !other.storage_.GetIsAllocated()) {
195 storage_.MemcpyFrom(other.storage_);
196 return;
197 }
198
199 storage_.InitFrom(other.storage_);
200 }
201
202 // Creates an inlined vector by moving in the contents of `other` without
203 // allocating. If `other` contains allocated memory, the newly-created inlined
204 // vector will take ownership of that memory. However, if `other` does not
205 // contain allocated memory, the newly-created inlined vector will perform
206 // element-wise move construction of the contents of `other`.
207 //
208 // NOTE: since no allocation is performed for the inlined vector in either
209 // case, the `noexcept(...)` specification depends on whether moving the
210 // underlying objects can throw. It is assumed assumed that...
211 // a) move constructors should only throw due to allocation failure.
212 // b) if `value_type`'s move constructor allocates, it uses the same
213 // allocation function as the inlined vector's allocator.
214 // Thus, the move constructor is non-throwing if the allocator is non-throwing
215 // or `value_type`'s move constructor is specified as `noexcept`.
216 InlinedVector(InlinedVector&& other) noexcept(
217 absl::allocator_is_nothrow<allocator_type>::value ||
218 std::is_nothrow_move_constructible<value_type>::value)
219 : storage_(other.storage_.GetAllocator()) {
220 // Fast path: if the value type can be trivially relocated (i.e. moved from
221 // and destroyed), and we know the allocator doesn't do anything fancy, then
222 // it's safe for us to simply adopt the contents of the storage for `other`
223 // and remove its own reference to them. It's as if we had individually
224 // move-constructed each value and then destroyed the original.
225 if (absl::is_trivially_relocatable<value_type>::value &&
226 std::is_same<A, std::allocator<value_type>>::value) {
227 storage_.MemcpyFrom(other.storage_);
228 other.storage_.SetInlinedSize(0);
229 return;
230 }
231
232 // Fast path: if the other vector is on the heap, we can simply take over
233 // its allocation.
234 if (other.storage_.GetIsAllocated()) {
235 storage_.SetAllocation({other.storage_.GetAllocatedData(),
236 other.storage_.GetAllocatedCapacity()});
237 storage_.SetAllocatedSize(other.storage_.GetSize());
238
239 other.storage_.SetInlinedSize(0);
240 return;
241 }
242
243 // Otherwise we must move each element individually.
244 IteratorValueAdapter<A, MoveIterator<A>> other_values(
245 MoveIterator<A>(other.storage_.GetInlinedData()));
246
247 inlined_vector_internal::ConstructElements<A>(
248 storage_.GetAllocator(), storage_.GetInlinedData(), other_values,
249 other.storage_.GetSize());
250
251 storage_.SetInlinedSize(other.storage_.GetSize());
252 }
253
254 // Creates an inlined vector by moving in the contents of `other` with a copy
255 // of `allocator`.
256 //
257 // NOTE: if `other`'s allocator is not equal to `allocator`, even if `other`
258 // contains allocated memory, this move constructor will still allocate. Since
259 // allocation is performed, this constructor can only be `noexcept` if the
260 // specified allocator is also `noexcept`.
InlinedVector(InlinedVector && other,const allocator_type & allocator)261 InlinedVector(
262 InlinedVector&& other,
263 const allocator_type&
264 allocator) noexcept(absl::allocator_is_nothrow<allocator_type>::value)
265 : storage_(allocator) {
266 // Fast path: if the value type can be trivially relocated (i.e. moved from
267 // and destroyed), and we know the allocator doesn't do anything fancy, then
268 // it's safe for us to simply adopt the contents of the storage for `other`
269 // and remove its own reference to them. It's as if we had individually
270 // move-constructed each value and then destroyed the original.
271 if (absl::is_trivially_relocatable<value_type>::value &&
272 std::is_same<A, std::allocator<value_type>>::value) {
273 storage_.MemcpyFrom(other.storage_);
274 other.storage_.SetInlinedSize(0);
275 return;
276 }
277
278 // Fast path: if the other vector is on the heap and shared the same
279 // allocator, we can simply take over its allocation.
280 if ((storage_.GetAllocator() == other.storage_.GetAllocator()) &&
281 other.storage_.GetIsAllocated()) {
282 storage_.SetAllocation({other.storage_.GetAllocatedData(),
283 other.storage_.GetAllocatedCapacity()});
284 storage_.SetAllocatedSize(other.storage_.GetSize());
285
286 other.storage_.SetInlinedSize(0);
287 return;
288 }
289
290 // Otherwise we must move each element individually.
291 storage_.Initialize(
292 IteratorValueAdapter<A, MoveIterator<A>>(MoveIterator<A>(other.data())),
293 other.size());
294 }
295
~InlinedVector()296 ~InlinedVector() {}
297
298 // ---------------------------------------------------------------------------
299 // InlinedVector Member Accessors
300 // ---------------------------------------------------------------------------
301
302 // `InlinedVector::empty()`
303 //
304 // Returns whether the inlined vector contains no elements.
empty()305 bool empty() const noexcept { return !size(); }
306
307 // `InlinedVector::size()`
308 //
309 // Returns the number of elements in the inlined vector.
size()310 size_type size() const noexcept { return storage_.GetSize(); }
311
312 // `InlinedVector::max_size()`
313 //
314 // Returns the maximum number of elements the inlined vector can hold.
max_size()315 size_type max_size() const noexcept {
316 // One bit of the size storage is used to indicate whether the inlined
317 // vector contains allocated memory. As a result, the maximum size that the
318 // inlined vector can express is the minimum of the limit of how many
319 // objects we can allocate and std::numeric_limits<size_type>::max() / 2.
320 return (std::min)(AllocatorTraits<A>::max_size(storage_.GetAllocator()),
321 (std::numeric_limits<size_type>::max)() / 2);
322 }
323
324 // `InlinedVector::capacity()`
325 //
326 // Returns the number of elements that could be stored in the inlined vector
327 // without requiring a reallocation.
328 //
329 // NOTE: for most inlined vectors, `capacity()` should be equal to the
330 // template parameter `N`. For inlined vectors which exceed this capacity,
331 // they will no longer be inlined and `capacity()` will equal the capactity of
332 // the allocated memory.
capacity()333 size_type capacity() const noexcept {
334 return storage_.GetIsAllocated() ? storage_.GetAllocatedCapacity()
335 : storage_.GetInlinedCapacity();
336 }
337
338 // `InlinedVector::data()`
339 //
340 // Returns a `pointer` to the elements of the inlined vector. This pointer
341 // can be used to access and modify the contained elements.
342 //
343 // NOTE: only elements within [`data()`, `data() + size()`) are valid.
data()344 pointer data() noexcept {
345 return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
346 : storage_.GetInlinedData();
347 }
348
349 // Overload of `InlinedVector::data()` that returns a `const_pointer` to the
350 // elements of the inlined vector. This pointer can be used to access but not
351 // modify the contained elements.
352 //
353 // NOTE: only elements within [`data()`, `data() + size()`) are valid.
data()354 const_pointer data() const noexcept {
355 return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
356 : storage_.GetInlinedData();
357 }
358
359 // `InlinedVector::operator[](...)`
360 //
361 // Returns a `reference` to the `i`th element of the inlined vector.
362 reference operator[](size_type i) {
363 ABSL_HARDENING_ASSERT(i < size());
364 return data()[i];
365 }
366
367 // Overload of `InlinedVector::operator[](...)` that returns a
368 // `const_reference` to the `i`th element of the inlined vector.
369 const_reference operator[](size_type i) const {
370 ABSL_HARDENING_ASSERT(i < size());
371 return data()[i];
372 }
373
374 // `InlinedVector::at(...)`
375 //
376 // Returns a `reference` to the `i`th element of the inlined vector.
377 //
378 // NOTE: if `i` is not within the required range of `InlinedVector::at(...)`,
379 // in both debug and non-debug builds, `std::out_of_range` will be thrown.
at(size_type i)380 reference at(size_type i) {
381 if (ABSL_PREDICT_FALSE(i >= size())) {
382 base_internal::ThrowStdOutOfRange(
383 "`InlinedVector::at(size_type)` failed bounds check");
384 }
385 return data()[i];
386 }
387
388 // Overload of `InlinedVector::at(...)` that returns a `const_reference` to
389 // the `i`th element of the inlined vector.
390 //
391 // NOTE: if `i` is not within the required range of `InlinedVector::at(...)`,
392 // in both debug and non-debug builds, `std::out_of_range` will be thrown.
at(size_type i)393 const_reference at(size_type i) const {
394 if (ABSL_PREDICT_FALSE(i >= size())) {
395 base_internal::ThrowStdOutOfRange(
396 "`InlinedVector::at(size_type) const` failed bounds check");
397 }
398 return data()[i];
399 }
400
401 // `InlinedVector::front()`
402 //
403 // Returns a `reference` to the first element of the inlined vector.
front()404 reference front() {
405 ABSL_HARDENING_ASSERT(!empty());
406 return data()[0];
407 }
408
409 // Overload of `InlinedVector::front()` that returns a `const_reference` to
410 // the first element of the inlined vector.
front()411 const_reference front() const {
412 ABSL_HARDENING_ASSERT(!empty());
413 return data()[0];
414 }
415
416 // `InlinedVector::back()`
417 //
418 // Returns a `reference` to the last element of the inlined vector.
back()419 reference back() {
420 ABSL_HARDENING_ASSERT(!empty());
421 return data()[size() - 1];
422 }
423
424 // Overload of `InlinedVector::back()` that returns a `const_reference` to the
425 // last element of the inlined vector.
back()426 const_reference back() const {
427 ABSL_HARDENING_ASSERT(!empty());
428 return data()[size() - 1];
429 }
430
431 // `InlinedVector::begin()`
432 //
433 // Returns an `iterator` to the beginning of the inlined vector.
begin()434 iterator begin() noexcept { return data(); }
435
436 // Overload of `InlinedVector::begin()` that returns a `const_iterator` to
437 // the beginning of the inlined vector.
begin()438 const_iterator begin() const noexcept { return data(); }
439
440 // `InlinedVector::end()`
441 //
442 // Returns an `iterator` to the end of the inlined vector.
end()443 iterator end() noexcept { return data() + size(); }
444
445 // Overload of `InlinedVector::end()` that returns a `const_iterator` to the
446 // end of the inlined vector.
end()447 const_iterator end() const noexcept { return data() + size(); }
448
449 // `InlinedVector::cbegin()`
450 //
451 // Returns a `const_iterator` to the beginning of the inlined vector.
cbegin()452 const_iterator cbegin() const noexcept { return begin(); }
453
454 // `InlinedVector::cend()`
455 //
456 // Returns a `const_iterator` to the end of the inlined vector.
cend()457 const_iterator cend() const noexcept { return end(); }
458
459 // `InlinedVector::rbegin()`
460 //
461 // Returns a `reverse_iterator` from the end of the inlined vector.
rbegin()462 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
463
464 // Overload of `InlinedVector::rbegin()` that returns a
465 // `const_reverse_iterator` from the end of the inlined vector.
rbegin()466 const_reverse_iterator rbegin() const noexcept {
467 return const_reverse_iterator(end());
468 }
469
470 // `InlinedVector::rend()`
471 //
472 // Returns a `reverse_iterator` from the beginning of the inlined vector.
rend()473 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
474
475 // Overload of `InlinedVector::rend()` that returns a `const_reverse_iterator`
476 // from the beginning of the inlined vector.
rend()477 const_reverse_iterator rend() const noexcept {
478 return const_reverse_iterator(begin());
479 }
480
481 // `InlinedVector::crbegin()`
482 //
483 // Returns a `const_reverse_iterator` from the end of the inlined vector.
crbegin()484 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
485
486 // `InlinedVector::crend()`
487 //
488 // Returns a `const_reverse_iterator` from the beginning of the inlined
489 // vector.
crend()490 const_reverse_iterator crend() const noexcept { return rend(); }
491
492 // `InlinedVector::get_allocator()`
493 //
494 // Returns a copy of the inlined vector's allocator.
get_allocator()495 allocator_type get_allocator() const { return storage_.GetAllocator(); }
496
497 // ---------------------------------------------------------------------------
498 // InlinedVector Member Mutators
499 // ---------------------------------------------------------------------------
500
501 // `InlinedVector::operator=(...)`
502 //
503 // Replaces the elements of the inlined vector with copies of the elements of
504 // `list`.
505 InlinedVector& operator=(std::initializer_list<value_type> list) {
506 assign(list.begin(), list.end());
507
508 return *this;
509 }
510
511 // Overload of `InlinedVector::operator=(...)` that replaces the elements of
512 // the inlined vector with copies of the elements of `other`.
513 InlinedVector& operator=(const InlinedVector& other) {
514 if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
515 const_pointer other_data = other.data();
516 assign(other_data, other_data + other.size());
517 }
518
519 return *this;
520 }
521
522 // Overload of `InlinedVector::operator=(...)` that moves the elements of
523 // `other` into the inlined vector.
524 //
525 // NOTE: as a result of calling this overload, `other` is left in a valid but
526 // unspecified state.
527 InlinedVector& operator=(InlinedVector&& other) {
528 if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
529 MoveAssignment(MoveAssignmentPolicy{}, std::move(other));
530 }
531
532 return *this;
533 }
534
535 // `InlinedVector::assign(...)`
536 //
537 // Replaces the contents of the inlined vector with `n` copies of `v`.
assign(size_type n,const_reference v)538 void assign(size_type n, const_reference v) {
539 storage_.Assign(CopyValueAdapter<A>(std::addressof(v)), n);
540 }
541
542 // Overload of `InlinedVector::assign(...)` that replaces the contents of the
543 // inlined vector with copies of the elements of `list`.
assign(std::initializer_list<value_type> list)544 void assign(std::initializer_list<value_type> list) {
545 assign(list.begin(), list.end());
546 }
547
548 // Overload of `InlinedVector::assign(...)` to replace the contents of the
549 // inlined vector with the range [`first`, `last`).
550 //
551 // NOTE: this overload is for iterators that are "forward" category or better.
552 template <typename ForwardIterator,
553 EnableIfAtLeastForwardIterator<ForwardIterator> = 0>
assign(ForwardIterator first,ForwardIterator last)554 void assign(ForwardIterator first, ForwardIterator last) {
555 storage_.Assign(IteratorValueAdapter<A, ForwardIterator>(first),
556 static_cast<size_t>(std::distance(first, last)));
557 }
558
559 // Overload of `InlinedVector::assign(...)` to replace the contents of the
560 // inlined vector with the range [`first`, `last`).
561 //
562 // NOTE: this overload is for iterators that are "input" category.
563 template <typename InputIterator,
564 DisableIfAtLeastForwardIterator<InputIterator> = 0>
assign(InputIterator first,InputIterator last)565 void assign(InputIterator first, InputIterator last) {
566 size_type i = 0;
567 for (; i < size() && first != last; ++i, static_cast<void>(++first)) {
568 data()[i] = *first;
569 }
570
571 erase(data() + i, data() + size());
572 std::copy(first, last, std::back_inserter(*this));
573 }
574
575 // `InlinedVector::resize(...)`
576 //
577 // Resizes the inlined vector to contain `n` elements.
578 //
579 // NOTE: If `n` is smaller than `size()`, extra elements are destroyed. If `n`
580 // is larger than `size()`, new elements are value-initialized.
resize(size_type n)581 void resize(size_type n) {
582 ABSL_HARDENING_ASSERT(n <= max_size());
583 storage_.Resize(DefaultValueAdapter<A>(), n);
584 }
585
586 // Overload of `InlinedVector::resize(...)` that resizes the inlined vector to
587 // contain `n` elements.
588 //
589 // NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n`
590 // is larger than `size()`, new elements are copied-constructed from `v`.
resize(size_type n,const_reference v)591 void resize(size_type n, const_reference v) {
592 ABSL_HARDENING_ASSERT(n <= max_size());
593 storage_.Resize(CopyValueAdapter<A>(std::addressof(v)), n);
594 }
595
596 // `InlinedVector::insert(...)`
597 //
598 // Inserts a copy of `v` at `pos`, returning an `iterator` to the newly
599 // inserted element.
insert(const_iterator pos,const_reference v)600 iterator insert(const_iterator pos, const_reference v) {
601 return emplace(pos, v);
602 }
603
604 // Overload of `InlinedVector::insert(...)` that inserts `v` at `pos` using
605 // move semantics, returning an `iterator` to the newly inserted element.
insert(const_iterator pos,value_type && v)606 iterator insert(const_iterator pos, value_type&& v) {
607 return emplace(pos, std::move(v));
608 }
609
610 // Overload of `InlinedVector::insert(...)` that inserts `n` contiguous copies
611 // of `v` starting at `pos`, returning an `iterator` pointing to the first of
612 // the newly inserted elements.
insert(const_iterator pos,size_type n,const_reference v)613 iterator insert(const_iterator pos, size_type n, const_reference v) {
614 ABSL_HARDENING_ASSERT(pos >= begin());
615 ABSL_HARDENING_ASSERT(pos <= end());
616
617 if (ABSL_PREDICT_TRUE(n != 0)) {
618 value_type dealias = v;
619 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102329#c2
620 // It appears that GCC thinks that since `pos` is a const pointer and may
621 // point to uninitialized memory at this point, a warning should be
622 // issued. But `pos` is actually only used to compute an array index to
623 // write to.
624 #if !defined(__clang__) && defined(__GNUC__)
625 #pragma GCC diagnostic push
626 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
627 #endif
628 return storage_.Insert(pos, CopyValueAdapter<A>(std::addressof(dealias)),
629 n);
630 #if !defined(__clang__) && defined(__GNUC__)
631 #pragma GCC diagnostic pop
632 #endif
633 } else {
634 return const_cast<iterator>(pos);
635 }
636 }
637
638 // Overload of `InlinedVector::insert(...)` that inserts copies of the
639 // elements of `list` starting at `pos`, returning an `iterator` pointing to
640 // the first of the newly inserted elements.
insert(const_iterator pos,std::initializer_list<value_type> list)641 iterator insert(const_iterator pos, std::initializer_list<value_type> list) {
642 return insert(pos, list.begin(), list.end());
643 }
644
645 // Overload of `InlinedVector::insert(...)` that inserts the range [`first`,
646 // `last`) starting at `pos`, returning an `iterator` pointing to the first
647 // of the newly inserted elements.
648 //
649 // NOTE: this overload is for iterators that are "forward" category or better.
650 template <typename ForwardIterator,
651 EnableIfAtLeastForwardIterator<ForwardIterator> = 0>
insert(const_iterator pos,ForwardIterator first,ForwardIterator last)652 iterator insert(const_iterator pos, ForwardIterator first,
653 ForwardIterator last) {
654 ABSL_HARDENING_ASSERT(pos >= begin());
655 ABSL_HARDENING_ASSERT(pos <= end());
656
657 if (ABSL_PREDICT_TRUE(first != last)) {
658 return storage_.Insert(
659 pos, IteratorValueAdapter<A, ForwardIterator>(first),
660 static_cast<size_type>(std::distance(first, last)));
661 } else {
662 return const_cast<iterator>(pos);
663 }
664 }
665
666 // Overload of `InlinedVector::insert(...)` that inserts the range [`first`,
667 // `last`) starting at `pos`, returning an `iterator` pointing to the first
668 // of the newly inserted elements.
669 //
670 // NOTE: this overload is for iterators that are "input" category.
671 template <typename InputIterator,
672 DisableIfAtLeastForwardIterator<InputIterator> = 0>
insert(const_iterator pos,InputIterator first,InputIterator last)673 iterator insert(const_iterator pos, InputIterator first, InputIterator last) {
674 ABSL_HARDENING_ASSERT(pos >= begin());
675 ABSL_HARDENING_ASSERT(pos <= end());
676
677 size_type index = static_cast<size_type>(std::distance(cbegin(), pos));
678 for (size_type i = index; first != last; ++i, static_cast<void>(++first)) {
679 insert(data() + i, *first);
680 }
681
682 return iterator(data() + index);
683 }
684
685 // `InlinedVector::emplace(...)`
686 //
687 // Constructs and inserts an element using `args...` in the inlined vector at
688 // `pos`, returning an `iterator` pointing to the newly emplaced element.
689 template <typename... Args>
emplace(const_iterator pos,Args &&...args)690 iterator emplace(const_iterator pos, Args&&... args) {
691 ABSL_HARDENING_ASSERT(pos >= begin());
692 ABSL_HARDENING_ASSERT(pos <= end());
693
694 value_type dealias(std::forward<Args>(args)...);
695 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102329#c2
696 // It appears that GCC thinks that since `pos` is a const pointer and may
697 // point to uninitialized memory at this point, a warning should be
698 // issued. But `pos` is actually only used to compute an array index to
699 // write to.
700 #if !defined(__clang__) && defined(__GNUC__)
701 #pragma GCC diagnostic push
702 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
703 #endif
704 return storage_.Insert(pos,
705 IteratorValueAdapter<A, MoveIterator<A>>(
706 MoveIterator<A>(std::addressof(dealias))),
707 1);
708 #if !defined(__clang__) && defined(__GNUC__)
709 #pragma GCC diagnostic pop
710 #endif
711 }
712
713 // `InlinedVector::emplace_back(...)`
714 //
715 // Constructs and inserts an element using `args...` in the inlined vector at
716 // `end()`, returning a `reference` to the newly emplaced element.
717 template <typename... Args>
emplace_back(Args &&...args)718 reference emplace_back(Args&&... args) {
719 return storage_.EmplaceBack(std::forward<Args>(args)...);
720 }
721
722 // `InlinedVector::push_back(...)`
723 //
724 // Inserts a copy of `v` in the inlined vector at `end()`.
push_back(const_reference v)725 void push_back(const_reference v) { static_cast<void>(emplace_back(v)); }
726
727 // Overload of `InlinedVector::push_back(...)` for inserting `v` at `end()`
728 // using move semantics.
push_back(value_type && v)729 void push_back(value_type&& v) {
730 static_cast<void>(emplace_back(std::move(v)));
731 }
732
733 // `InlinedVector::pop_back()`
734 //
735 // Destroys the element at `back()`, reducing the size by `1`.
pop_back()736 void pop_back() noexcept {
737 ABSL_HARDENING_ASSERT(!empty());
738
739 AllocatorTraits<A>::destroy(storage_.GetAllocator(), data() + (size() - 1));
740 storage_.SubtractSize(1);
741 }
742
743 // `InlinedVector::erase(...)`
744 //
745 // Erases the element at `pos`, returning an `iterator` pointing to where the
746 // erased element was located.
747 //
748 // NOTE: may return `end()`, which is not dereferencable.
erase(const_iterator pos)749 iterator erase(const_iterator pos) {
750 ABSL_HARDENING_ASSERT(pos >= begin());
751 ABSL_HARDENING_ASSERT(pos < end());
752
753 return storage_.Erase(pos, pos + 1);
754 }
755
756 // Overload of `InlinedVector::erase(...)` that erases every element in the
757 // range [`from`, `to`), returning an `iterator` pointing to where the first
758 // erased element was located.
759 //
760 // NOTE: may return `end()`, which is not dereferencable.
erase(const_iterator from,const_iterator to)761 iterator erase(const_iterator from, const_iterator to) {
762 ABSL_HARDENING_ASSERT(from >= begin());
763 ABSL_HARDENING_ASSERT(from <= to);
764 ABSL_HARDENING_ASSERT(to <= end());
765
766 if (ABSL_PREDICT_TRUE(from != to)) {
767 return storage_.Erase(from, to);
768 } else {
769 return const_cast<iterator>(from);
770 }
771 }
772
773 // `InlinedVector::clear()`
774 //
775 // Destroys all elements in the inlined vector, setting the size to `0` and
776 // deallocating any held memory.
clear()777 void clear() noexcept {
778 inlined_vector_internal::DestroyAdapter<A>::DestroyElements(
779 storage_.GetAllocator(), data(), size());
780 storage_.DeallocateIfAllocated();
781
782 storage_.SetInlinedSize(0);
783 }
784
785 // `InlinedVector::reserve(...)`
786 //
787 // Ensures that there is enough room for at least `n` elements.
reserve(size_type n)788 void reserve(size_type n) { storage_.Reserve(n); }
789
790 // `InlinedVector::shrink_to_fit()`
791 //
792 // Attempts to reduce memory usage by moving elements to (or keeping elements
793 // in) the smallest available buffer sufficient for containing `size()`
794 // elements.
795 //
796 // If `size()` is sufficiently small, the elements will be moved into (or kept
797 // in) the inlined space.
shrink_to_fit()798 void shrink_to_fit() {
799 if (storage_.GetIsAllocated()) {
800 storage_.ShrinkToFit();
801 }
802 }
803
804 // `InlinedVector::swap(...)`
805 //
806 // Swaps the contents of the inlined vector with `other`.
swap(InlinedVector & other)807 void swap(InlinedVector& other) {
808 if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
809 storage_.Swap(std::addressof(other.storage_));
810 }
811 }
812
813 private:
814 template <typename H, typename TheT, size_t TheN, typename TheA>
815 friend H AbslHashValue(H h, const absl::InlinedVector<TheT, TheN, TheA>& a);
816
MoveAssignment(MemcpyPolicy,InlinedVector && other)817 void MoveAssignment(MemcpyPolicy, InlinedVector&& other) {
818 // Assumption check: we shouldn't be told to use memcpy to implement move
819 // asignment unless we have trivially destructible elements and an allocator
820 // that does nothing fancy.
821 static_assert(absl::is_trivially_destructible<value_type>::value, "");
822 static_assert(std::is_same<A, std::allocator<value_type>>::value, "");
823
824 // Throw away our existing heap allocation, if any. There is no need to
825 // destroy the existing elements one by one because we know they are
826 // trivially destructible.
827 storage_.DeallocateIfAllocated();
828
829 // Adopt the other vector's inline elements or heap allocation.
830 storage_.MemcpyFrom(other.storage_);
831 other.storage_.SetInlinedSize(0);
832 }
833
834 // Destroy our existing elements, if any, and adopt the heap-allocated
835 // elements of the other vector.
836 //
837 // REQUIRES: other.storage_.GetIsAllocated()
DestroyExistingAndAdopt(InlinedVector && other)838 void DestroyExistingAndAdopt(InlinedVector&& other) {
839 ABSL_HARDENING_ASSERT(other.storage_.GetIsAllocated());
840
841 inlined_vector_internal::DestroyAdapter<A>::DestroyElements(
842 storage_.GetAllocator(), data(), size());
843 storage_.DeallocateIfAllocated();
844
845 storage_.MemcpyFrom(other.storage_);
846 other.storage_.SetInlinedSize(0);
847 }
848
MoveAssignment(ElementwiseAssignPolicy,InlinedVector && other)849 void MoveAssignment(ElementwiseAssignPolicy, InlinedVector&& other) {
850 // Fast path: if the other vector is on the heap then we don't worry about
851 // actually move-assigning each element. Instead we only throw away our own
852 // existing elements and adopt the heap allocation of the other vector.
853 if (other.storage_.GetIsAllocated()) {
854 DestroyExistingAndAdopt(std::move(other));
855 return;
856 }
857
858 storage_.Assign(IteratorValueAdapter<A, MoveIterator<A>>(
859 MoveIterator<A>(other.storage_.GetInlinedData())),
860 other.size());
861 }
862
MoveAssignment(ElementwiseConstructPolicy,InlinedVector && other)863 void MoveAssignment(ElementwiseConstructPolicy, InlinedVector&& other) {
864 // Fast path: if the other vector is on the heap then we don't worry about
865 // actually move-assigning each element. Instead we only throw away our own
866 // existing elements and adopt the heap allocation of the other vector.
867 if (other.storage_.GetIsAllocated()) {
868 DestroyExistingAndAdopt(std::move(other));
869 return;
870 }
871
872 inlined_vector_internal::DestroyAdapter<A>::DestroyElements(
873 storage_.GetAllocator(), data(), size());
874 storage_.DeallocateIfAllocated();
875
876 IteratorValueAdapter<A, MoveIterator<A>> other_values(
877 MoveIterator<A>(other.storage_.GetInlinedData()));
878 inlined_vector_internal::ConstructElements<A>(
879 storage_.GetAllocator(), storage_.GetInlinedData(), other_values,
880 other.storage_.GetSize());
881 storage_.SetInlinedSize(other.storage_.GetSize());
882 }
883
884 Storage storage_;
885 };
886
887 // -----------------------------------------------------------------------------
888 // InlinedVector Non-Member Functions
889 // -----------------------------------------------------------------------------
890
891 // `swap(...)`
892 //
893 // Swaps the contents of two inlined vectors.
894 template <typename T, size_t N, typename A>
swap(absl::InlinedVector<T,N,A> & a,absl::InlinedVector<T,N,A> & b)895 void swap(absl::InlinedVector<T, N, A>& a,
896 absl::InlinedVector<T, N, A>& b) noexcept(noexcept(a.swap(b))) {
897 a.swap(b);
898 }
899
900 // `operator==(...)`
901 //
902 // Tests for value-equality of two inlined vectors.
903 template <typename T, size_t N, typename A>
904 bool operator==(const absl::InlinedVector<T, N, A>& a,
905 const absl::InlinedVector<T, N, A>& b) {
906 auto a_data = a.data();
907 auto b_data = b.data();
908 return std::equal(a_data, a_data + a.size(), b_data, b_data + b.size());
909 }
910
911 // `operator!=(...)`
912 //
913 // Tests for value-inequality of two inlined vectors.
914 template <typename T, size_t N, typename A>
915 bool operator!=(const absl::InlinedVector<T, N, A>& a,
916 const absl::InlinedVector<T, N, A>& b) {
917 return !(a == b);
918 }
919
920 // `operator<(...)`
921 //
922 // Tests whether the value of an inlined vector is less than the value of
923 // another inlined vector using a lexicographical comparison algorithm.
924 template <typename T, size_t N, typename A>
925 bool operator<(const absl::InlinedVector<T, N, A>& a,
926 const absl::InlinedVector<T, N, A>& b) {
927 auto a_data = a.data();
928 auto b_data = b.data();
929 return std::lexicographical_compare(a_data, a_data + a.size(), b_data,
930 b_data + b.size());
931 }
932
933 // `operator>(...)`
934 //
935 // Tests whether the value of an inlined vector is greater than the value of
936 // another inlined vector using a lexicographical comparison algorithm.
937 template <typename T, size_t N, typename A>
938 bool operator>(const absl::InlinedVector<T, N, A>& a,
939 const absl::InlinedVector<T, N, A>& b) {
940 return b < a;
941 }
942
943 // `operator<=(...)`
944 //
945 // Tests whether the value of an inlined vector is less than or equal to the
946 // value of another inlined vector using a lexicographical comparison algorithm.
947 template <typename T, size_t N, typename A>
948 bool operator<=(const absl::InlinedVector<T, N, A>& a,
949 const absl::InlinedVector<T, N, A>& b) {
950 return !(b < a);
951 }
952
953 // `operator>=(...)`
954 //
955 // Tests whether the value of an inlined vector is greater than or equal to the
956 // value of another inlined vector using a lexicographical comparison algorithm.
957 template <typename T, size_t N, typename A>
958 bool operator>=(const absl::InlinedVector<T, N, A>& a,
959 const absl::InlinedVector<T, N, A>& b) {
960 return !(a < b);
961 }
962
963 // `AbslHashValue(...)`
964 //
965 // Provides `absl::Hash` support for `absl::InlinedVector`. It is uncommon to
966 // call this directly.
967 template <typename H, typename T, size_t N, typename A>
AbslHashValue(H h,const absl::InlinedVector<T,N,A> & a)968 H AbslHashValue(H h, const absl::InlinedVector<T, N, A>& a) {
969 auto size = a.size();
970 return H::combine(H::combine_contiguous(std::move(h), a.data(), size), size);
971 }
972
973 ABSL_NAMESPACE_END
974 } // namespace absl
975
976 #endif // ABSL_CONTAINER_INLINED_VECTOR_H_
977