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