Brian Silverman | f7bd1c2 | 2015-12-24 16:07:11 -0800 | [diff] [blame^] | 1 | //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the SmallVector class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_ADT_SMALLVECTOR_H |
| 15 | #define LLVM_ADT_SMALLVECTOR_H |
| 16 | |
| 17 | #include "llvm/iterator_range.h" |
| 18 | #include "llvm/AlignOf.h" |
| 19 | #include "llvm/Compiler.h" |
| 20 | #include "llvm/MathExtras.h" |
| 21 | #include "llvm/type_traits.h" |
| 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | #include <cstddef> |
| 25 | #include <cstdlib> |
| 26 | #include <cstring> |
| 27 | #include <initializer_list> |
| 28 | #include <iterator> |
| 29 | #include <memory> |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
| 33 | /// This is all the non-templated stuff common to all SmallVectors. |
| 34 | class SmallVectorBase { |
| 35 | protected: |
| 36 | void *BeginX, *EndX, *CapacityX; |
| 37 | |
| 38 | protected: |
| 39 | SmallVectorBase(void *FirstEl, size_t Size) |
| 40 | : BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl+Size) {} |
| 41 | |
| 42 | /// This is an implementation of the grow() method which only works |
| 43 | /// on POD-like data types and is out of line to reduce code duplication. |
| 44 | void grow_pod(void *FirstEl, size_t MinSizeInBytes, size_t TSize); |
| 45 | |
| 46 | public: |
| 47 | /// This returns size()*sizeof(T). |
| 48 | size_t size_in_bytes() const { |
| 49 | return size_t((char*)EndX - (char*)BeginX); |
| 50 | } |
| 51 | |
| 52 | /// capacity_in_bytes - This returns capacity()*sizeof(T). |
| 53 | size_t capacity_in_bytes() const { |
| 54 | return size_t((char*)CapacityX - (char*)BeginX); |
| 55 | } |
| 56 | |
| 57 | bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return BeginX == EndX; } |
| 58 | }; |
| 59 | |
| 60 | template <typename T, unsigned N> struct SmallVectorStorage; |
| 61 | |
| 62 | /// This is the part of SmallVectorTemplateBase which does not depend on whether |
| 63 | /// the type T is a POD. The extra dummy template argument is used by ArrayRef |
| 64 | /// to avoid unnecessarily requiring T to be complete. |
| 65 | template <typename T, typename = void> |
| 66 | class SmallVectorTemplateCommon : public SmallVectorBase { |
| 67 | private: |
| 68 | template <typename, unsigned> friend struct SmallVectorStorage; |
| 69 | |
| 70 | // Allocate raw space for N elements of type T. If T has a ctor or dtor, we |
| 71 | // don't want it to be automatically run, so we need to represent the space as |
| 72 | // something else. Use an array of char of sufficient alignment. |
| 73 | typedef llvm::AlignedCharArrayUnion<T> U; |
| 74 | U FirstEl; |
| 75 | // Space after 'FirstEl' is clobbered, do not add any instance vars after it. |
| 76 | |
| 77 | protected: |
| 78 | SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {} |
| 79 | |
| 80 | void grow_pod(size_t MinSizeInBytes, size_t TSize) { |
| 81 | SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize); |
| 82 | } |
| 83 | |
| 84 | /// Return true if this is a smallvector which has not had dynamic |
| 85 | /// memory allocated for it. |
| 86 | bool isSmall() const { |
| 87 | return BeginX == static_cast<const void*>(&FirstEl); |
| 88 | } |
| 89 | |
| 90 | /// Put this vector in a state of being small. |
| 91 | void resetToSmall() { |
| 92 | BeginX = EndX = CapacityX = &FirstEl; |
| 93 | } |
| 94 | |
| 95 | void setEnd(T *P) { this->EndX = P; } |
| 96 | public: |
| 97 | typedef size_t size_type; |
| 98 | typedef ptrdiff_t difference_type; |
| 99 | typedef T value_type; |
| 100 | typedef T *iterator; |
| 101 | typedef const T *const_iterator; |
| 102 | |
| 103 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 104 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 105 | |
| 106 | typedef T &reference; |
| 107 | typedef const T &const_reference; |
| 108 | typedef T *pointer; |
| 109 | typedef const T *const_pointer; |
| 110 | |
| 111 | // forward iterator creation methods. |
| 112 | iterator begin() { return (iterator)this->BeginX; } |
| 113 | const_iterator begin() const { return (const_iterator)this->BeginX; } |
| 114 | iterator end() { return (iterator)this->EndX; } |
| 115 | const_iterator end() const { return (const_iterator)this->EndX; } |
| 116 | protected: |
| 117 | iterator capacity_ptr() { return (iterator)this->CapacityX; } |
| 118 | const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;} |
| 119 | public: |
| 120 | |
| 121 | // reverse iterator creation methods. |
| 122 | reverse_iterator rbegin() { return reverse_iterator(end()); } |
| 123 | const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); } |
| 124 | reverse_iterator rend() { return reverse_iterator(begin()); } |
| 125 | const_reverse_iterator rend() const { return const_reverse_iterator(begin());} |
| 126 | |
| 127 | size_type size() const { return end()-begin(); } |
| 128 | size_type max_size() const { return size_type(-1) / sizeof(T); } |
| 129 | |
| 130 | /// Return the total number of elements in the currently allocated buffer. |
| 131 | size_t capacity() const { return capacity_ptr() - begin(); } |
| 132 | |
| 133 | /// Return a pointer to the vector's buffer, even if empty(). |
| 134 | pointer data() { return pointer(begin()); } |
| 135 | /// Return a pointer to the vector's buffer, even if empty(). |
| 136 | const_pointer data() const { return const_pointer(begin()); } |
| 137 | |
| 138 | reference operator[](size_type idx) { |
| 139 | assert(idx < size()); |
| 140 | return begin()[idx]; |
| 141 | } |
| 142 | const_reference operator[](size_type idx) const { |
| 143 | assert(idx < size()); |
| 144 | return begin()[idx]; |
| 145 | } |
| 146 | |
| 147 | reference front() { |
| 148 | assert(!empty()); |
| 149 | return begin()[0]; |
| 150 | } |
| 151 | const_reference front() const { |
| 152 | assert(!empty()); |
| 153 | return begin()[0]; |
| 154 | } |
| 155 | |
| 156 | reference back() { |
| 157 | assert(!empty()); |
| 158 | return end()[-1]; |
| 159 | } |
| 160 | const_reference back() const { |
| 161 | assert(!empty()); |
| 162 | return end()[-1]; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method |
| 167 | /// implementations that are designed to work with non-POD-like T's. |
| 168 | template <typename T, bool isPodLike> |
| 169 | class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> { |
| 170 | protected: |
| 171 | SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {} |
| 172 | |
| 173 | static void destroy_range(T *S, T *E) { |
| 174 | while (S != E) { |
| 175 | --E; |
| 176 | E->~T(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// Use move-assignment to move the range [I, E) onto the |
| 181 | /// objects starting with "Dest". This is just <memory>'s |
| 182 | /// std::move, but not all stdlibs actually provide that. |
| 183 | template<typename It1, typename It2> |
| 184 | static It2 move(It1 I, It1 E, It2 Dest) { |
| 185 | for (; I != E; ++I, ++Dest) |
| 186 | *Dest = ::std::move(*I); |
| 187 | return Dest; |
| 188 | } |
| 189 | |
| 190 | /// Use move-assignment to move the range |
| 191 | /// [I, E) onto the objects ending at "Dest", moving objects |
| 192 | /// in reverse order. This is just <algorithm>'s |
| 193 | /// std::move_backward, but not all stdlibs actually provide that. |
| 194 | template<typename It1, typename It2> |
| 195 | static It2 move_backward(It1 I, It1 E, It2 Dest) { |
| 196 | while (I != E) |
| 197 | *--Dest = ::std::move(*--E); |
| 198 | return Dest; |
| 199 | } |
| 200 | |
| 201 | /// Move the range [I, E) into the uninitialized memory starting with "Dest", |
| 202 | /// constructing elements as needed. |
| 203 | template<typename It1, typename It2> |
| 204 | static void uninitialized_move(It1 I, It1 E, It2 Dest) { |
| 205 | for (; I != E; ++I, ++Dest) |
| 206 | ::new ((void*) &*Dest) T(::std::move(*I)); |
| 207 | } |
| 208 | |
| 209 | /// Copy the range [I, E) onto the uninitialized memory starting with "Dest", |
| 210 | /// constructing elements as needed. |
| 211 | template<typename It1, typename It2> |
| 212 | static void uninitialized_copy(It1 I, It1 E, It2 Dest) { |
| 213 | std::uninitialized_copy(I, E, Dest); |
| 214 | } |
| 215 | |
| 216 | /// Grow the allocated memory (without initializing new elements), doubling |
| 217 | /// the size of the allocated memory. Guarantees space for at least one more |
| 218 | /// element, or MinSize more elements if specified. |
| 219 | void grow(size_t MinSize = 0); |
| 220 | |
| 221 | public: |
| 222 | void push_back(const T &Elt) { |
| 223 | if (LLVM_UNLIKELY(this->EndX >= this->CapacityX)) |
| 224 | this->grow(); |
| 225 | ::new ((void*) this->end()) T(Elt); |
| 226 | this->setEnd(this->end()+1); |
| 227 | } |
| 228 | |
| 229 | void push_back(T &&Elt) { |
| 230 | if (LLVM_UNLIKELY(this->EndX >= this->CapacityX)) |
| 231 | this->grow(); |
| 232 | ::new ((void*) this->end()) T(::std::move(Elt)); |
| 233 | this->setEnd(this->end()+1); |
| 234 | } |
| 235 | |
| 236 | void pop_back() { |
| 237 | this->setEnd(this->end()-1); |
| 238 | this->end()->~T(); |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | // Define this out-of-line to dissuade the C++ compiler from inlining it. |
| 243 | template <typename T, bool isPodLike> |
| 244 | void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) { |
| 245 | size_t CurCapacity = this->capacity(); |
| 246 | size_t CurSize = this->size(); |
| 247 | // Always grow, even from zero. |
| 248 | size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2)); |
| 249 | if (NewCapacity < MinSize) |
| 250 | NewCapacity = MinSize; |
| 251 | T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T))); |
| 252 | |
| 253 | // Move the elements over. |
| 254 | this->uninitialized_move(this->begin(), this->end(), NewElts); |
| 255 | |
| 256 | // Destroy the original elements. |
| 257 | destroy_range(this->begin(), this->end()); |
| 258 | |
| 259 | // If this wasn't grown from the inline copy, deallocate the old space. |
| 260 | if (!this->isSmall()) |
| 261 | free(this->begin()); |
| 262 | |
| 263 | this->setEnd(NewElts+CurSize); |
| 264 | this->BeginX = NewElts; |
| 265 | this->CapacityX = this->begin()+NewCapacity; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method |
| 270 | /// implementations that are designed to work with POD-like T's. |
| 271 | template <typename T> |
| 272 | class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> { |
| 273 | protected: |
| 274 | SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {} |
| 275 | |
| 276 | // No need to do a destroy loop for POD's. |
| 277 | static void destroy_range(T *, T *) {} |
| 278 | |
| 279 | /// Use move-assignment to move the range [I, E) onto the |
| 280 | /// objects starting with "Dest". For PODs, this is just memcpy. |
| 281 | template<typename It1, typename It2> |
| 282 | static It2 move(It1 I, It1 E, It2 Dest) { |
| 283 | return ::std::copy(I, E, Dest); |
| 284 | } |
| 285 | |
| 286 | /// Use move-assignment to move the range [I, E) onto the objects ending at |
| 287 | /// "Dest", moving objects in reverse order. |
| 288 | template<typename It1, typename It2> |
| 289 | static It2 move_backward(It1 I, It1 E, It2 Dest) { |
| 290 | return ::std::copy_backward(I, E, Dest); |
| 291 | } |
| 292 | |
| 293 | /// Move the range [I, E) onto the uninitialized memory |
| 294 | /// starting with "Dest", constructing elements into it as needed. |
| 295 | template<typename It1, typename It2> |
| 296 | static void uninitialized_move(It1 I, It1 E, It2 Dest) { |
| 297 | // Just do a copy. |
| 298 | uninitialized_copy(I, E, Dest); |
| 299 | } |
| 300 | |
| 301 | /// Copy the range [I, E) onto the uninitialized memory |
| 302 | /// starting with "Dest", constructing elements into it as needed. |
| 303 | template<typename It1, typename It2> |
| 304 | static void uninitialized_copy(It1 I, It1 E, It2 Dest) { |
| 305 | // Arbitrary iterator types; just use the basic implementation. |
| 306 | std::uninitialized_copy(I, E, Dest); |
| 307 | } |
| 308 | |
| 309 | /// Copy the range [I, E) onto the uninitialized memory |
| 310 | /// starting with "Dest", constructing elements into it as needed. |
| 311 | template <typename T1, typename T2> |
| 312 | static void uninitialized_copy( |
| 313 | T1 *I, T1 *E, T2 *Dest, |
| 314 | typename std::enable_if<std::is_same<typename std::remove_const<T1>::type, |
| 315 | T2>::value>::type * = nullptr) { |
| 316 | // Use memcpy for PODs iterated by pointers (which includes SmallVector |
| 317 | // iterators): std::uninitialized_copy optimizes to memmove, but we can |
| 318 | // use memcpy here. |
| 319 | memcpy(Dest, I, (E-I)*sizeof(T)); |
| 320 | } |
| 321 | |
| 322 | /// Double the size of the allocated memory, guaranteeing space for at |
| 323 | /// least one more element or MinSize if specified. |
| 324 | void grow(size_t MinSize = 0) { |
| 325 | this->grow_pod(MinSize*sizeof(T), sizeof(T)); |
| 326 | } |
| 327 | public: |
| 328 | void push_back(const T &Elt) { |
| 329 | if (LLVM_UNLIKELY(this->EndX >= this->CapacityX)) |
| 330 | this->grow(); |
| 331 | memcpy(this->end(), &Elt, sizeof(T)); |
| 332 | this->setEnd(this->end()+1); |
| 333 | } |
| 334 | |
| 335 | void pop_back() { |
| 336 | this->setEnd(this->end()-1); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | |
| 341 | /// This class consists of common code factored out of the SmallVector class to |
| 342 | /// reduce code duplication based on the SmallVector 'N' template parameter. |
| 343 | template <typename T> |
| 344 | class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> { |
| 345 | typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass; |
| 346 | |
| 347 | SmallVectorImpl(const SmallVectorImpl&) = delete; |
| 348 | public: |
| 349 | typedef typename SuperClass::iterator iterator; |
| 350 | typedef typename SuperClass::size_type size_type; |
| 351 | |
| 352 | protected: |
| 353 | // Default ctor - Initialize to empty. |
| 354 | explicit SmallVectorImpl(unsigned N) |
| 355 | : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) { |
| 356 | } |
| 357 | |
| 358 | public: |
| 359 | ~SmallVectorImpl() { |
| 360 | // Destroy the constructed elements in the vector. |
| 361 | this->destroy_range(this->begin(), this->end()); |
| 362 | |
| 363 | // If this wasn't grown from the inline copy, deallocate the old space. |
| 364 | if (!this->isSmall()) |
| 365 | free(this->begin()); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | void clear() { |
| 370 | this->destroy_range(this->begin(), this->end()); |
| 371 | this->EndX = this->BeginX; |
| 372 | } |
| 373 | |
| 374 | void resize(size_type N) { |
| 375 | if (N < this->size()) { |
| 376 | this->destroy_range(this->begin()+N, this->end()); |
| 377 | this->setEnd(this->begin()+N); |
| 378 | } else if (N > this->size()) { |
| 379 | if (this->capacity() < N) |
| 380 | this->grow(N); |
| 381 | for (auto I = this->end(), E = this->begin() + N; I != E; ++I) |
| 382 | new (&*I) T(); |
| 383 | this->setEnd(this->begin()+N); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void resize(size_type N, const T &NV) { |
| 388 | if (N < this->size()) { |
| 389 | this->destroy_range(this->begin()+N, this->end()); |
| 390 | this->setEnd(this->begin()+N); |
| 391 | } else if (N > this->size()) { |
| 392 | if (this->capacity() < N) |
| 393 | this->grow(N); |
| 394 | std::uninitialized_fill(this->end(), this->begin()+N, NV); |
| 395 | this->setEnd(this->begin()+N); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void reserve(size_type N) { |
| 400 | if (this->capacity() < N) |
| 401 | this->grow(N); |
| 402 | } |
| 403 | |
| 404 | T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() { |
| 405 | T Result = ::std::move(this->back()); |
| 406 | this->pop_back(); |
| 407 | return Result; |
| 408 | } |
| 409 | |
| 410 | void swap(SmallVectorImpl &RHS); |
| 411 | |
| 412 | /// Add the specified range to the end of the SmallVector. |
| 413 | template<typename in_iter> |
| 414 | void append(in_iter in_start, in_iter in_end) { |
| 415 | size_type NumInputs = std::distance(in_start, in_end); |
| 416 | // Grow allocated space if needed. |
| 417 | if (NumInputs > size_type(this->capacity_ptr()-this->end())) |
| 418 | this->grow(this->size()+NumInputs); |
| 419 | |
| 420 | // Copy the new elements over. |
| 421 | this->uninitialized_copy(in_start, in_end, this->end()); |
| 422 | this->setEnd(this->end() + NumInputs); |
| 423 | } |
| 424 | |
| 425 | /// Add the specified range to the end of the SmallVector. |
| 426 | void append(size_type NumInputs, const T &Elt) { |
| 427 | // Grow allocated space if needed. |
| 428 | if (NumInputs > size_type(this->capacity_ptr()-this->end())) |
| 429 | this->grow(this->size()+NumInputs); |
| 430 | |
| 431 | // Copy the new elements over. |
| 432 | std::uninitialized_fill_n(this->end(), NumInputs, Elt); |
| 433 | this->setEnd(this->end() + NumInputs); |
| 434 | } |
| 435 | |
| 436 | void append(std::initializer_list<T> IL) { |
| 437 | append(IL.begin(), IL.end()); |
| 438 | } |
| 439 | |
| 440 | void assign(size_type NumElts, const T &Elt) { |
| 441 | clear(); |
| 442 | if (this->capacity() < NumElts) |
| 443 | this->grow(NumElts); |
| 444 | this->setEnd(this->begin()+NumElts); |
| 445 | std::uninitialized_fill(this->begin(), this->end(), Elt); |
| 446 | } |
| 447 | |
| 448 | void assign(std::initializer_list<T> IL) { |
| 449 | clear(); |
| 450 | append(IL); |
| 451 | } |
| 452 | |
| 453 | iterator erase(iterator I) { |
| 454 | assert(I >= this->begin() && "Iterator to erase is out of bounds."); |
| 455 | assert(I < this->end() && "Erasing at past-the-end iterator."); |
| 456 | |
| 457 | iterator N = I; |
| 458 | // Shift all elts down one. |
| 459 | this->move(I+1, this->end(), I); |
| 460 | // Drop the last elt. |
| 461 | this->pop_back(); |
| 462 | return(N); |
| 463 | } |
| 464 | |
| 465 | iterator erase(iterator S, iterator E) { |
| 466 | assert(S >= this->begin() && "Range to erase is out of bounds."); |
| 467 | assert(S <= E && "Trying to erase invalid range."); |
| 468 | assert(E <= this->end() && "Trying to erase past the end."); |
| 469 | |
| 470 | iterator N = S; |
| 471 | // Shift all elts down. |
| 472 | iterator I = this->move(E, this->end(), S); |
| 473 | // Drop the last elts. |
| 474 | this->destroy_range(I, this->end()); |
| 475 | this->setEnd(I); |
| 476 | return(N); |
| 477 | } |
| 478 | |
| 479 | iterator insert(iterator I, T &&Elt) { |
| 480 | if (I == this->end()) { // Important special case for empty vector. |
| 481 | this->push_back(::std::move(Elt)); |
| 482 | return this->end()-1; |
| 483 | } |
| 484 | |
| 485 | assert(I >= this->begin() && "Insertion iterator is out of bounds."); |
| 486 | assert(I <= this->end() && "Inserting past the end of the vector."); |
| 487 | |
| 488 | if (this->EndX >= this->CapacityX) { |
| 489 | size_t EltNo = I-this->begin(); |
| 490 | this->grow(); |
| 491 | I = this->begin()+EltNo; |
| 492 | } |
| 493 | |
| 494 | ::new ((void*) this->end()) T(::std::move(this->back())); |
| 495 | // Push everything else over. |
| 496 | this->move_backward(I, this->end()-1, this->end()); |
| 497 | this->setEnd(this->end()+1); |
| 498 | |
| 499 | // If we just moved the element we're inserting, be sure to update |
| 500 | // the reference. |
| 501 | T *EltPtr = &Elt; |
| 502 | if (I <= EltPtr && EltPtr < this->EndX) |
| 503 | ++EltPtr; |
| 504 | |
| 505 | *I = ::std::move(*EltPtr); |
| 506 | return I; |
| 507 | } |
| 508 | |
| 509 | iterator insert(iterator I, const T &Elt) { |
| 510 | if (I == this->end()) { // Important special case for empty vector. |
| 511 | this->push_back(Elt); |
| 512 | return this->end()-1; |
| 513 | } |
| 514 | |
| 515 | assert(I >= this->begin() && "Insertion iterator is out of bounds."); |
| 516 | assert(I <= this->end() && "Inserting past the end of the vector."); |
| 517 | |
| 518 | if (this->EndX >= this->CapacityX) { |
| 519 | size_t EltNo = I-this->begin(); |
| 520 | this->grow(); |
| 521 | I = this->begin()+EltNo; |
| 522 | } |
| 523 | ::new ((void*) this->end()) T(std::move(this->back())); |
| 524 | // Push everything else over. |
| 525 | this->move_backward(I, this->end()-1, this->end()); |
| 526 | this->setEnd(this->end()+1); |
| 527 | |
| 528 | // If we just moved the element we're inserting, be sure to update |
| 529 | // the reference. |
| 530 | const T *EltPtr = &Elt; |
| 531 | if (I <= EltPtr && EltPtr < this->EndX) |
| 532 | ++EltPtr; |
| 533 | |
| 534 | *I = *EltPtr; |
| 535 | return I; |
| 536 | } |
| 537 | |
| 538 | iterator insert(iterator I, size_type NumToInsert, const T &Elt) { |
| 539 | // Convert iterator to elt# to avoid invalidating iterator when we reserve() |
| 540 | size_t InsertElt = I - this->begin(); |
| 541 | |
| 542 | if (I == this->end()) { // Important special case for empty vector. |
| 543 | append(NumToInsert, Elt); |
| 544 | return this->begin()+InsertElt; |
| 545 | } |
| 546 | |
| 547 | assert(I >= this->begin() && "Insertion iterator is out of bounds."); |
| 548 | assert(I <= this->end() && "Inserting past the end of the vector."); |
| 549 | |
| 550 | // Ensure there is enough space. |
| 551 | reserve(this->size() + NumToInsert); |
| 552 | |
| 553 | // Uninvalidate the iterator. |
| 554 | I = this->begin()+InsertElt; |
| 555 | |
| 556 | // If there are more elements between the insertion point and the end of the |
| 557 | // range than there are being inserted, we can use a simple approach to |
| 558 | // insertion. Since we already reserved space, we know that this won't |
| 559 | // reallocate the vector. |
| 560 | if (size_t(this->end()-I) >= NumToInsert) { |
| 561 | T *OldEnd = this->end(); |
| 562 | append(std::move_iterator<iterator>(this->end() - NumToInsert), |
| 563 | std::move_iterator<iterator>(this->end())); |
| 564 | |
| 565 | // Copy the existing elements that get replaced. |
| 566 | this->move_backward(I, OldEnd-NumToInsert, OldEnd); |
| 567 | |
| 568 | std::fill_n(I, NumToInsert, Elt); |
| 569 | return I; |
| 570 | } |
| 571 | |
| 572 | // Otherwise, we're inserting more elements than exist already, and we're |
| 573 | // not inserting at the end. |
| 574 | |
| 575 | // Move over the elements that we're about to overwrite. |
| 576 | T *OldEnd = this->end(); |
| 577 | this->setEnd(this->end() + NumToInsert); |
| 578 | size_t NumOverwritten = OldEnd-I; |
| 579 | this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten); |
| 580 | |
| 581 | // Replace the overwritten part. |
| 582 | std::fill_n(I, NumOverwritten, Elt); |
| 583 | |
| 584 | // Insert the non-overwritten middle part. |
| 585 | std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt); |
| 586 | return I; |
| 587 | } |
| 588 | |
| 589 | template<typename ItTy> |
| 590 | iterator insert(iterator I, ItTy From, ItTy To) { |
| 591 | // Convert iterator to elt# to avoid invalidating iterator when we reserve() |
| 592 | size_t InsertElt = I - this->begin(); |
| 593 | |
| 594 | if (I == this->end()) { // Important special case for empty vector. |
| 595 | append(From, To); |
| 596 | return this->begin()+InsertElt; |
| 597 | } |
| 598 | |
| 599 | assert(I >= this->begin() && "Insertion iterator is out of bounds."); |
| 600 | assert(I <= this->end() && "Inserting past the end of the vector."); |
| 601 | |
| 602 | size_t NumToInsert = std::distance(From, To); |
| 603 | |
| 604 | // Ensure there is enough space. |
| 605 | reserve(this->size() + NumToInsert); |
| 606 | |
| 607 | // Uninvalidate the iterator. |
| 608 | I = this->begin()+InsertElt; |
| 609 | |
| 610 | // If there are more elements between the insertion point and the end of the |
| 611 | // range than there are being inserted, we can use a simple approach to |
| 612 | // insertion. Since we already reserved space, we know that this won't |
| 613 | // reallocate the vector. |
| 614 | if (size_t(this->end()-I) >= NumToInsert) { |
| 615 | T *OldEnd = this->end(); |
| 616 | append(std::move_iterator<iterator>(this->end() - NumToInsert), |
| 617 | std::move_iterator<iterator>(this->end())); |
| 618 | |
| 619 | // Copy the existing elements that get replaced. |
| 620 | this->move_backward(I, OldEnd-NumToInsert, OldEnd); |
| 621 | |
| 622 | std::copy(From, To, I); |
| 623 | return I; |
| 624 | } |
| 625 | |
| 626 | // Otherwise, we're inserting more elements than exist already, and we're |
| 627 | // not inserting at the end. |
| 628 | |
| 629 | // Move over the elements that we're about to overwrite. |
| 630 | T *OldEnd = this->end(); |
| 631 | this->setEnd(this->end() + NumToInsert); |
| 632 | size_t NumOverwritten = OldEnd-I; |
| 633 | this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten); |
| 634 | |
| 635 | // Replace the overwritten part. |
| 636 | for (T *J = I; NumOverwritten > 0; --NumOverwritten) { |
| 637 | *J = *From; |
| 638 | ++J; ++From; |
| 639 | } |
| 640 | |
| 641 | // Insert the non-overwritten middle part. |
| 642 | this->uninitialized_copy(From, To, OldEnd); |
| 643 | return I; |
| 644 | } |
| 645 | |
| 646 | void insert(iterator I, std::initializer_list<T> IL) { |
| 647 | insert(I, IL.begin(), IL.end()); |
| 648 | } |
| 649 | |
| 650 | template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) { |
| 651 | if (LLVM_UNLIKELY(this->EndX >= this->CapacityX)) |
| 652 | this->grow(); |
| 653 | ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...); |
| 654 | this->setEnd(this->end() + 1); |
| 655 | } |
| 656 | |
| 657 | SmallVectorImpl &operator=(const SmallVectorImpl &RHS); |
| 658 | |
| 659 | SmallVectorImpl &operator=(SmallVectorImpl &&RHS); |
| 660 | |
| 661 | bool operator==(const SmallVectorImpl &RHS) const { |
| 662 | if (this->size() != RHS.size()) return false; |
| 663 | return std::equal(this->begin(), this->end(), RHS.begin()); |
| 664 | } |
| 665 | bool operator!=(const SmallVectorImpl &RHS) const { |
| 666 | return !(*this == RHS); |
| 667 | } |
| 668 | |
| 669 | bool operator<(const SmallVectorImpl &RHS) const { |
| 670 | return std::lexicographical_compare(this->begin(), this->end(), |
| 671 | RHS.begin(), RHS.end()); |
| 672 | } |
| 673 | |
| 674 | /// Set the array size to \p N, which the current array must have enough |
| 675 | /// capacity for. |
| 676 | /// |
| 677 | /// This does not construct or destroy any elements in the vector. |
| 678 | /// |
| 679 | /// Clients can use this in conjunction with capacity() to write past the end |
| 680 | /// of the buffer when they know that more elements are available, and only |
| 681 | /// update the size later. This avoids the cost of value initializing elements |
| 682 | /// which will only be overwritten. |
| 683 | void set_size(size_type N) { |
| 684 | assert(N <= this->capacity()); |
| 685 | this->setEnd(this->begin() + N); |
| 686 | } |
| 687 | }; |
| 688 | |
| 689 | |
| 690 | template <typename T> |
| 691 | void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) { |
| 692 | if (this == &RHS) return; |
| 693 | |
| 694 | // We can only avoid copying elements if neither vector is small. |
| 695 | if (!this->isSmall() && !RHS.isSmall()) { |
| 696 | std::swap(this->BeginX, RHS.BeginX); |
| 697 | std::swap(this->EndX, RHS.EndX); |
| 698 | std::swap(this->CapacityX, RHS.CapacityX); |
| 699 | return; |
| 700 | } |
| 701 | if (RHS.size() > this->capacity()) |
| 702 | this->grow(RHS.size()); |
| 703 | if (this->size() > RHS.capacity()) |
| 704 | RHS.grow(this->size()); |
| 705 | |
| 706 | // Swap the shared elements. |
| 707 | size_t NumShared = this->size(); |
| 708 | if (NumShared > RHS.size()) NumShared = RHS.size(); |
| 709 | for (size_type i = 0; i != NumShared; ++i) |
| 710 | std::swap((*this)[i], RHS[i]); |
| 711 | |
| 712 | // Copy over the extra elts. |
| 713 | if (this->size() > RHS.size()) { |
| 714 | size_t EltDiff = this->size() - RHS.size(); |
| 715 | this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end()); |
| 716 | RHS.setEnd(RHS.end()+EltDiff); |
| 717 | this->destroy_range(this->begin()+NumShared, this->end()); |
| 718 | this->setEnd(this->begin()+NumShared); |
| 719 | } else if (RHS.size() > this->size()) { |
| 720 | size_t EltDiff = RHS.size() - this->size(); |
| 721 | this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end()); |
| 722 | this->setEnd(this->end() + EltDiff); |
| 723 | this->destroy_range(RHS.begin()+NumShared, RHS.end()); |
| 724 | RHS.setEnd(RHS.begin()+NumShared); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | template <typename T> |
| 729 | SmallVectorImpl<T> &SmallVectorImpl<T>:: |
| 730 | operator=(const SmallVectorImpl<T> &RHS) { |
| 731 | // Avoid self-assignment. |
| 732 | if (this == &RHS) return *this; |
| 733 | |
| 734 | // If we already have sufficient space, assign the common elements, then |
| 735 | // destroy any excess. |
| 736 | size_t RHSSize = RHS.size(); |
| 737 | size_t CurSize = this->size(); |
| 738 | if (CurSize >= RHSSize) { |
| 739 | // Assign common elements. |
| 740 | iterator NewEnd; |
| 741 | if (RHSSize) |
| 742 | NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin()); |
| 743 | else |
| 744 | NewEnd = this->begin(); |
| 745 | |
| 746 | // Destroy excess elements. |
| 747 | this->destroy_range(NewEnd, this->end()); |
| 748 | |
| 749 | // Trim. |
| 750 | this->setEnd(NewEnd); |
| 751 | return *this; |
| 752 | } |
| 753 | |
| 754 | // If we have to grow to have enough elements, destroy the current elements. |
| 755 | // This allows us to avoid copying them during the grow. |
| 756 | // FIXME: don't do this if they're efficiently moveable. |
| 757 | if (this->capacity() < RHSSize) { |
| 758 | // Destroy current elements. |
| 759 | this->destroy_range(this->begin(), this->end()); |
| 760 | this->setEnd(this->begin()); |
| 761 | CurSize = 0; |
| 762 | this->grow(RHSSize); |
| 763 | } else if (CurSize) { |
| 764 | // Otherwise, use assignment for the already-constructed elements. |
| 765 | std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin()); |
| 766 | } |
| 767 | |
| 768 | // Copy construct the new elements in place. |
| 769 | this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(), |
| 770 | this->begin()+CurSize); |
| 771 | |
| 772 | // Set end. |
| 773 | this->setEnd(this->begin()+RHSSize); |
| 774 | return *this; |
| 775 | } |
| 776 | |
| 777 | template <typename T> |
| 778 | SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) { |
| 779 | // Avoid self-assignment. |
| 780 | if (this == &RHS) return *this; |
| 781 | |
| 782 | // If the RHS isn't small, clear this vector and then steal its buffer. |
| 783 | if (!RHS.isSmall()) { |
| 784 | this->destroy_range(this->begin(), this->end()); |
| 785 | if (!this->isSmall()) free(this->begin()); |
| 786 | this->BeginX = RHS.BeginX; |
| 787 | this->EndX = RHS.EndX; |
| 788 | this->CapacityX = RHS.CapacityX; |
| 789 | RHS.resetToSmall(); |
| 790 | return *this; |
| 791 | } |
| 792 | |
| 793 | // If we already have sufficient space, assign the common elements, then |
| 794 | // destroy any excess. |
| 795 | size_t RHSSize = RHS.size(); |
| 796 | size_t CurSize = this->size(); |
| 797 | if (CurSize >= RHSSize) { |
| 798 | // Assign common elements. |
| 799 | iterator NewEnd = this->begin(); |
| 800 | if (RHSSize) |
| 801 | NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd); |
| 802 | |
| 803 | // Destroy excess elements and trim the bounds. |
| 804 | this->destroy_range(NewEnd, this->end()); |
| 805 | this->setEnd(NewEnd); |
| 806 | |
| 807 | // Clear the RHS. |
| 808 | RHS.clear(); |
| 809 | |
| 810 | return *this; |
| 811 | } |
| 812 | |
| 813 | // If we have to grow to have enough elements, destroy the current elements. |
| 814 | // This allows us to avoid copying them during the grow. |
| 815 | // FIXME: this may not actually make any sense if we can efficiently move |
| 816 | // elements. |
| 817 | if (this->capacity() < RHSSize) { |
| 818 | // Destroy current elements. |
| 819 | this->destroy_range(this->begin(), this->end()); |
| 820 | this->setEnd(this->begin()); |
| 821 | CurSize = 0; |
| 822 | this->grow(RHSSize); |
| 823 | } else if (CurSize) { |
| 824 | // Otherwise, use assignment for the already-constructed elements. |
| 825 | this->move(RHS.begin(), RHS.begin()+CurSize, this->begin()); |
| 826 | } |
| 827 | |
| 828 | // Move-construct the new elements in place. |
| 829 | this->uninitialized_move(RHS.begin()+CurSize, RHS.end(), |
| 830 | this->begin()+CurSize); |
| 831 | |
| 832 | // Set end. |
| 833 | this->setEnd(this->begin()+RHSSize); |
| 834 | |
| 835 | RHS.clear(); |
| 836 | return *this; |
| 837 | } |
| 838 | |
| 839 | /// Storage for the SmallVector elements which aren't contained in |
| 840 | /// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1' |
| 841 | /// element is in the base class. This is specialized for the N=1 and N=0 cases |
| 842 | /// to avoid allocating unnecessary storage. |
| 843 | template <typename T, unsigned N> |
| 844 | struct SmallVectorStorage { |
| 845 | typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1]; |
| 846 | }; |
| 847 | template <typename T> struct SmallVectorStorage<T, 1> {}; |
| 848 | template <typename T> struct SmallVectorStorage<T, 0> {}; |
| 849 | |
| 850 | /// This is a 'vector' (really, a variable-sized array), optimized |
| 851 | /// for the case when the array is small. It contains some number of elements |
| 852 | /// in-place, which allows it to avoid heap allocation when the actual number of |
| 853 | /// elements is below that threshold. This allows normal "small" cases to be |
| 854 | /// fast without losing generality for large inputs. |
| 855 | /// |
| 856 | /// Note that this does not attempt to be exception safe. |
| 857 | /// |
| 858 | template <typename T, unsigned N> |
| 859 | class SmallVector : public SmallVectorImpl<T> { |
| 860 | /// Inline space for elements which aren't stored in the base class. |
| 861 | SmallVectorStorage<T, N> Storage; |
| 862 | public: |
| 863 | SmallVector() : SmallVectorImpl<T>(N) { |
| 864 | } |
| 865 | |
| 866 | explicit SmallVector(size_t Size, const T &Value = T()) |
| 867 | : SmallVectorImpl<T>(N) { |
| 868 | this->assign(Size, Value); |
| 869 | } |
| 870 | |
| 871 | template<typename ItTy> |
| 872 | SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) { |
| 873 | this->append(S, E); |
| 874 | } |
| 875 | |
| 876 | template <typename RangeTy> |
| 877 | explicit SmallVector(const llvm::iterator_range<RangeTy> R) |
| 878 | : SmallVectorImpl<T>(N) { |
| 879 | this->append(R.begin(), R.end()); |
| 880 | } |
| 881 | |
| 882 | SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) { |
| 883 | this->assign(IL); |
| 884 | } |
| 885 | |
| 886 | SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) { |
| 887 | if (!RHS.empty()) |
| 888 | SmallVectorImpl<T>::operator=(RHS); |
| 889 | } |
| 890 | |
| 891 | const SmallVector &operator=(const SmallVector &RHS) { |
| 892 | SmallVectorImpl<T>::operator=(RHS); |
| 893 | return *this; |
| 894 | } |
| 895 | |
| 896 | SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) { |
| 897 | if (!RHS.empty()) |
| 898 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 899 | } |
| 900 | |
| 901 | const SmallVector &operator=(SmallVector &&RHS) { |
| 902 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 903 | return *this; |
| 904 | } |
| 905 | |
| 906 | SmallVector(SmallVectorImpl<T> &&RHS) : SmallVectorImpl<T>(N) { |
| 907 | if (!RHS.empty()) |
| 908 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 909 | } |
| 910 | |
| 911 | const SmallVector &operator=(SmallVectorImpl<T> &&RHS) { |
| 912 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 913 | return *this; |
| 914 | } |
| 915 | |
| 916 | const SmallVector &operator=(std::initializer_list<T> IL) { |
| 917 | this->assign(IL); |
| 918 | return *this; |
| 919 | } |
| 920 | }; |
| 921 | |
| 922 | template<typename T, unsigned N> |
| 923 | static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) { |
| 924 | return X.capacity_in_bytes(); |
| 925 | } |
| 926 | |
| 927 | } // namespace llvm |
| 928 | |
| 929 | namespace std { |
| 930 | /// Implement std::swap in terms of SmallVector swap. |
| 931 | template<typename T> |
| 932 | inline void |
| 933 | swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) { |
| 934 | LHS.swap(RHS); |
| 935 | } |
| 936 | |
| 937 | /// Implement std::swap in terms of SmallVector swap. |
| 938 | template<typename T, unsigned N> |
| 939 | inline void |
| 940 | swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) { |
| 941 | LHS.swap(RHS); |
| 942 | } |
| 943 | } // namespace std |
| 944 | |
| 945 | #endif |