| 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. |
| 112 | InlinedVector() noexcept(noexcept(allocator_type())) : storage_() {} |
| 113 |
| 114 | // Creates an empty inlined vector with a copy of `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()) |
| 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()) |
| 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()) |
| 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()) |
| 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. |
| 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`. |
| 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`. |
| 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 |
| 246 | ~InlinedVector() {} |
| 247 |
| 248 | // --------------------------------------------------------------------------- |
| 249 | // InlinedVector Member Accessors |
| 250 | // --------------------------------------------------------------------------- |
| 251 |
| 252 | // `InlinedVector::empty()` |
| 253 | // |
| 254 | // Returns whether the inlined vector contains no elements. |
| 255 | bool empty() const noexcept { return !size(); } |
| 256 |
| 257 | // `InlinedVector::size()` |
| 258 | // |
| 259 | // Returns the number of elements in the inlined vector. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 352 | reference front() { |
| 353 | ABSL_HARDENING_ASSERT(!empty()); |
| 354 | return at(0); |
| 355 | } |
| 356 |
| 357 | // Overload of `InlinedVector::front()` that returns a `const_reference` to |
| 358 | // the first element of the inlined vector. |
| 359 | const_reference front() const { |
| 360 | ABSL_HARDENING_ASSERT(!empty()); |
| 361 | return at(0); |
| 362 | } |
| 363 |
| 364 | // `InlinedVector::back()` |
| 365 | // |
| 366 | // Returns a `reference` to the last element of the inlined vector. |
| 367 | reference back() { |
| 368 | ABSL_HARDENING_ASSERT(!empty()); |
| 369 | return at(size() - 1); |
| 370 | } |
| 371 |
| 372 | // Overload of `InlinedVector::back()` that returns a `const_reference` to the |
| 373 | // last element of the inlined vector. |
| 374 | const_reference back() const { |
| 375 | ABSL_HARDENING_ASSERT(!empty()); |
| 376 | return at(size() - 1); |
| 377 | } |
| 378 |
| 379 | // `InlinedVector::begin()` |
| 380 | // |
| 381 | // Returns an `iterator` to the beginning of the inlined vector. |
| 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. |
| 386 | const_iterator begin() const noexcept { return data(); } |
| 387 |
| 388 | // `InlinedVector::end()` |
| 389 | // |
| 390 | // Returns an `iterator` to the end of the inlined vector. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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`. |
| 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`. |
| 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> |
| 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> |
| 524 | void assign(InputIterator first, InputIterator last) { |
| 525 | size_type i = 0; |
| 526 | for (; i < size() && first != last; ++i, static_cast<void>(++first)) { |
| 527 | at(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. |
| 540 | void resize(size_type n) { storage_.Resize(DefaultValueAdapter(), n); } |
| 541 |
| 542 | // Overload of `InlinedVector::resize(...)` that resizes the inlined vector to |
| 543 | // contain `n` elements. |
| 544 | // |
| 545 | // NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n` |
| 546 | // is larger than `size()`, new elements are copied-constructed from `v`. |
| 547 | void resize(size_type n, const_reference v) { |
| 548 | storage_.Resize(CopyValueAdapter(v), n); |
| 549 | } |
| 550 |
| 551 | // `InlinedVector::insert(...)` |
| 552 | // |
| 553 | // Inserts a copy of `v` at `pos`, returning an `iterator` to the newly |
| 554 | // inserted element. |
| 555 | iterator insert(const_iterator pos, const_reference v) { |
| 556 | return emplace(pos, v); |
| 557 | } |
| 558 |
| 559 | // Overload of `InlinedVector::insert(...)` that inserts `v` at `pos` using |
| 560 | // move semantics, returning an `iterator` to the newly inserted element. |
| 561 | iterator insert(const_iterator pos, RValueReference v) { |
| 562 | return emplace(pos, std::move(v)); |
| 563 | } |
| 564 |
| 565 | // Overload of `InlinedVector::insert(...)` that inserts `n` contiguous copies |
| 566 | // of `v` starting at `pos`, returning an `iterator` pointing to the first of |
| 567 | // the newly inserted elements. |
| 568 | iterator insert(const_iterator pos, size_type n, const_reference v) { |
| 569 | ABSL_HARDENING_ASSERT(pos >= begin()); |
| 570 | ABSL_HARDENING_ASSERT(pos <= end()); |
| 571 |
| 572 | if (ABSL_PREDICT_TRUE(n != 0)) { |
| 573 | value_type dealias = v; |
| 574 | return storage_.Insert(pos, CopyValueAdapter(dealias), n); |
| 575 | } else { |
| 576 | return const_cast<iterator>(pos); |
| 577 | } |
| 578 | } |
| 579 |
| 580 | // Overload of `InlinedVector::insert(...)` that inserts copies of the |
| 581 | // elements of `list` starting at `pos`, returning an `iterator` pointing to |
| 582 | // the first of the newly inserted elements. |
| 583 | iterator insert(const_iterator pos, std::initializer_list<value_type> list) { |
| 584 | return insert(pos, list.begin(), list.end()); |
| 585 | } |
| 586 |
| 587 | // Overload of `InlinedVector::insert(...)` that inserts the range [`first`, |
| 588 | // `last`) starting at `pos`, returning an `iterator` pointing to the first |
| 589 | // of the newly inserted elements. |
| 590 | // |
| 591 | // NOTE: this overload is for iterators that are "forward" category or better. |
| 592 | template <typename ForwardIterator, |
| 593 | EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr> |
| 594 | iterator insert(const_iterator pos, ForwardIterator first, |
| 595 | ForwardIterator last) { |
| 596 | ABSL_HARDENING_ASSERT(pos >= begin()); |
| 597 | ABSL_HARDENING_ASSERT(pos <= end()); |
| 598 |
| 599 | if (ABSL_PREDICT_TRUE(first != last)) { |
| 600 | return storage_.Insert(pos, IteratorValueAdapter<ForwardIterator>(first), |
| 601 | std::distance(first, last)); |
| 602 | } else { |
| 603 | return const_cast<iterator>(pos); |
| 604 | } |
| 605 | } |
| 606 |
| 607 | // Overload of `InlinedVector::insert(...)` that inserts the range [`first`, |
| 608 | // `last`) starting at `pos`, returning an `iterator` pointing to the first |
| 609 | // of the newly inserted elements. |
| 610 | // |
| 611 | // NOTE: this overload is for iterators that are "input" category. |
| 612 | template <typename InputIterator, |
| 613 | DisableIfAtLeastForwardIterator<InputIterator>* = nullptr> |
| 614 | iterator insert(const_iterator pos, InputIterator first, InputIterator last) { |
| 615 | ABSL_HARDENING_ASSERT(pos >= begin()); |
| 616 | ABSL_HARDENING_ASSERT(pos <= end()); |
| 617 |
| 618 | size_type index = std::distance(cbegin(), pos); |
| 619 | for (size_type i = index; first != last; ++i, static_cast<void>(++first)) { |
| 620 | insert(data() + i, *first); |
| 621 | } |
| 622 |
| 623 | return iterator(data() + index); |
| 624 | } |
| 625 |
| 626 | // `InlinedVector::emplace(...)` |
| 627 | // |
| 628 | // Constructs and inserts an element using `args...` in the inlined vector at |
| 629 | // `pos`, returning an `iterator` pointing to the newly emplaced element. |
| 630 | template <typename... Args> |
| 631 | iterator emplace(const_iterator pos, Args&&... args) { |
| 632 | ABSL_HARDENING_ASSERT(pos >= begin()); |
| 633 | ABSL_HARDENING_ASSERT(pos <= end()); |
| 634 |
| 635 | value_type dealias(std::forward<Args>(args)...); |
| 636 | return storage_.Insert(pos, |
| 637 | IteratorValueAdapter<MoveIterator>( |
| 638 | MoveIterator(std::addressof(dealias))), |
| 639 | 1); |
| 640 | } |
| 641 |
| 642 | // `InlinedVector::emplace_back(...)` |
| 643 | // |
| 644 | // Constructs and inserts an element using `args...` in the inlined vector at |
| 645 | // `end()`, returning a `reference` to the newly emplaced element. |
| 646 | template <typename... Args> |
| 647 | reference emplace_back(Args&&... args) { |
| 648 | return storage_.EmplaceBack(std::forward<Args>(args)...); |
| 649 | } |
| 650 |
| 651 | // `InlinedVector::push_back(...)` |
| 652 | // |
| 653 | // Inserts a copy of `v` in the inlined vector at `end()`. |
| 654 | void push_back(const_reference v) { static_cast<void>(emplace_back(v)); } |
| 655 |
| 656 | // Overload of `InlinedVector::push_back(...)` for inserting `v` at `end()` |
| 657 | // using move semantics. |
| 658 | void push_back(RValueReference v) { |
| 659 | static_cast<void>(emplace_back(std::move(v))); |
| 660 | } |
| 661 |
| 662 | // `InlinedVector::pop_back()` |
| 663 | // |
| 664 | // Destroys the element at `back()`, reducing the size by `1`. |
| 665 | void pop_back() noexcept { |
| 666 | ABSL_HARDENING_ASSERT(!empty()); |
| 667 |
| 668 | AllocatorTraits::destroy(*storage_.GetAllocPtr(), data() + (size() - 1)); |
| 669 | storage_.SubtractSize(1); |
| 670 | } |
| 671 |
| 672 | // `InlinedVector::erase(...)` |
| 673 | // |
| 674 | // Erases the element at `pos`, returning an `iterator` pointing to where the |
| 675 | // erased element was located. |
| 676 | // |
| 677 | // NOTE: may return `end()`, which is not dereferencable. |
| 678 | iterator erase(const_iterator pos) { |
| 679 | ABSL_HARDENING_ASSERT(pos >= begin()); |
| 680 | ABSL_HARDENING_ASSERT(pos < end()); |
| 681 |
| 682 | return storage_.Erase(pos, pos + 1); |
| 683 | } |
| 684 |
| 685 | // Overload of `InlinedVector::erase(...)` that erases every element in the |
| 686 | // range [`from`, `to`), returning an `iterator` pointing to where the first |
| 687 | // erased element was located. |
| 688 | // |
| 689 | // NOTE: may return `end()`, which is not dereferencable. |
| 690 | iterator erase(const_iterator from, const_iterator to) { |
| 691 | ABSL_HARDENING_ASSERT(from >= begin()); |
| 692 | ABSL_HARDENING_ASSERT(from <= to); |
| 693 | ABSL_HARDENING_ASSERT(to <= end()); |
| 694 |
| 695 | if (ABSL_PREDICT_TRUE(from != to)) { |
| 696 | return storage_.Erase(from, to); |
| 697 | } else { |
| 698 | return const_cast<iterator>(from); |
| 699 | } |
| 700 | } |
| 701 |
| 702 | // `InlinedVector::clear()` |
| 703 | // |
| 704 | // Destroys all elements in the inlined vector, setting the size to `0` and |
| 705 | // deallocating any held memory. |
| 706 | void clear() noexcept { |
| 707 | inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(), |
| 708 | size()); |
| 709 | storage_.DeallocateIfAllocated(); |
| 710 |
| 711 | storage_.SetInlinedSize(0); |
| 712 | } |
| 713 |
| 714 | // `InlinedVector::reserve(...)` |
| 715 | // |
| 716 | // Ensures that there is enough room for at least `n` elements. |
| 717 | void reserve(size_type n) { storage_.Reserve(n); } |
| 718 |
| 719 | // `InlinedVector::shrink_to_fit()` |
| 720 | // |
| 721 | // Reduces memory usage by freeing unused memory. After being called, calls to |
| 722 | // `capacity()` will be equal to `max(N, size())`. |
| 723 | // |
| 724 | // If `size() <= N` and the inlined vector contains allocated memory, the |
| 725 | // elements will all be moved to the inlined space and the allocated memory |
| 726 | // will be deallocated. |
| 727 | // |
| 728 | // If `size() > N` and `size() < capacity()`, the elements will be moved to a |
| 729 | // smaller allocation. |
| 730 | void shrink_to_fit() { |
| 731 | if (storage_.GetIsAllocated()) { |
| 732 | storage_.ShrinkToFit(); |
| 733 | } |
| 734 | } |
| 735 |
| 736 | // `InlinedVector::swap(...)` |
| 737 | // |
| 738 | // Swaps the contents of the inlined vector with `other`. |
| 739 | void swap(InlinedVector& other) { |
| 740 | if (ABSL_PREDICT_TRUE(this != std::addressof(other))) { |
| 741 | storage_.Swap(std::addressof(other.storage_)); |
| 742 | } |
| 743 | } |
| 744 |
| 745 | private: |
| 746 | template <typename H, typename TheT, size_t TheN, typename TheA> |
| 747 | friend H AbslHashValue(H h, const absl::InlinedVector<TheT, TheN, TheA>& a); |
| 748 |
| 749 | Storage storage_; |
| 750 | }; |
| 751 |
| 752 | // ----------------------------------------------------------------------------- |
| 753 | // InlinedVector Non-Member Functions |
| 754 | // ----------------------------------------------------------------------------- |
| 755 |
| 756 | // `swap(...)` |
| 757 | // |
| 758 | // Swaps the contents of two inlined vectors. |
| 759 | template <typename T, size_t N, typename A> |
| 760 | void swap(absl::InlinedVector<T, N, A>& a, |
| 761 | absl::InlinedVector<T, N, A>& b) noexcept(noexcept(a.swap(b))) { |
| 762 | a.swap(b); |
| 763 | } |
| 764 |
| 765 | // `operator==(...)` |
| 766 | // |
| 767 | // Tests for value-equality of two inlined vectors. |
| 768 | template <typename T, size_t N, typename A> |
| 769 | bool operator==(const absl::InlinedVector<T, N, A>& a, |
| 770 | const absl::InlinedVector<T, N, A>& b) { |
| 771 | auto a_data = a.data(); |
| 772 | auto b_data = b.data(); |
| 773 | return absl::equal(a_data, a_data + a.size(), b_data, b_data + b.size()); |
| 774 | } |
| 775 |
| 776 | // `operator!=(...)` |
| 777 | // |
| 778 | // Tests for value-inequality of two inlined vectors. |
| 779 | template <typename T, size_t N, typename A> |
| 780 | bool operator!=(const absl::InlinedVector<T, N, A>& a, |
| 781 | const absl::InlinedVector<T, N, A>& b) { |
| 782 | return !(a == b); |
| 783 | } |
| 784 |
| 785 | // `operator<(...)` |
| 786 | // |
| 787 | // Tests whether the value of an inlined vector is less than the value of |
| 788 | // another inlined vector using a lexicographical comparison algorithm. |
| 789 | template <typename T, size_t N, typename A> |
| 790 | bool operator<(const absl::InlinedVector<T, N, A>& a, |
| 791 | const absl::InlinedVector<T, N, A>& b) { |
| 792 | auto a_data = a.data(); |
| 793 | auto b_data = b.data(); |
| 794 | return std::lexicographical_compare(a_data, a_data + a.size(), b_data, |
| 795 | b_data + b.size()); |
| 796 | } |
| 797 |
| 798 | // `operator>(...)` |
| 799 | // |
| 800 | // Tests whether the value of an inlined vector is greater than the value of |
| 801 | // another inlined vector using a lexicographical comparison algorithm. |
| 802 | template <typename T, size_t N, typename A> |
| 803 | bool operator>(const absl::InlinedVector<T, N, A>& a, |
| 804 | const absl::InlinedVector<T, N, A>& b) { |
| 805 | return b < a; |
| 806 | } |
| 807 |
| 808 | // `operator<=(...)` |
| 809 | // |
| 810 | // Tests whether the value of an inlined vector is less than or equal to the |
| 811 | // value of another inlined vector using a lexicographical comparison algorithm. |
| 812 | template <typename T, size_t N, typename A> |
| 813 | bool operator<=(const absl::InlinedVector<T, N, A>& a, |
| 814 | const absl::InlinedVector<T, N, A>& b) { |
| 815 | return !(b < a); |
| 816 | } |
| 817 |
| 818 | // `operator>=(...)` |
| 819 | // |
| 820 | // Tests whether the value of an inlined vector is greater than or equal to the |
| 821 | // value of another inlined vector using a lexicographical comparison algorithm. |
| 822 | template <typename T, size_t N, typename A> |
| 823 | bool operator>=(const absl::InlinedVector<T, N, A>& a, |
| 824 | const absl::InlinedVector<T, N, A>& b) { |
| 825 | return !(a < b); |
| 826 | } |
| 827 |
| 828 | // `AbslHashValue(...)` |
| 829 | // |
| 830 | // Provides `absl::Hash` support for `absl::InlinedVector`. It is uncommon to |
| 831 | // call this directly. |
| 832 | template <typename H, typename T, size_t N, typename A> |
| 833 | H AbslHashValue(H h, const absl::InlinedVector<T, N, A>& a) { |
| 834 | auto size = a.size(); |
| 835 | return H::combine(H::combine_contiguous(std::move(h), a.data(), size), size); |
| 836 | } |
| 837 |
| 838 | ABSL_NAMESPACE_END |
| 839 | } // namespace absl |
| 840 |
| 841 | #endif // ABSL_CONTAINER_INLINED_VECTOR_H_ |
| 842 |