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