1 //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SmallVector class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SMALLVECTOR_H
15 #define LLVM_ADT_SMALLVECTOR_H
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/Support/AlignOf.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/type_traits.h"
27 #include <initializer_list>
33 /// This is all the non-templated stuff common to all SmallVectors.
34 class SmallVectorBase {
36 void *BeginX, *EndX, *CapacityX;
39 SmallVectorBase(void *FirstEl, size_t Size)
40 : BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl+Size) {}
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);
47 /// This returns size()*sizeof(T).
48 size_t size_in_bytes() const {
49 return size_t((char*)EndX - (char*)BeginX);
52 /// capacity_in_bytes - This returns capacity()*sizeof(T).
53 size_t capacity_in_bytes() const {
54 return size_t((char*)CapacityX - (char*)BeginX);
57 bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return BeginX == EndX; }
60 template <typename T, unsigned N> struct SmallVectorStorage;
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 {
68 template <typename, unsigned> friend struct SmallVectorStorage;
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;
75 // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
78 SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
80 void grow_pod(size_t MinSizeInBytes, size_t TSize) {
81 SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
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);
90 /// Put this vector in a state of being small.
92 BeginX = EndX = CapacityX = &FirstEl;
95 void setEnd(T *P) { this->EndX = P; }
97 typedef size_t size_type;
98 typedef ptrdiff_t difference_type;
101 typedef const T *const_iterator;
103 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
104 typedef std::reverse_iterator<iterator> reverse_iterator;
106 typedef T &reference;
107 typedef const T &const_reference;
109 typedef const T *const_pointer;
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; }
117 iterator capacity_ptr() { return (iterator)this->CapacityX; }
118 const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
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());}
127 size_type size() const { return end()-begin(); }
128 size_type max_size() const { return size_type(-1) / sizeof(T); }
130 /// Return the total number of elements in the currently allocated buffer.
131 size_t capacity() const { return capacity_ptr() - begin(); }
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()); }
138 reference operator[](size_type idx) {
139 assert(idx < size());
142 const_reference operator[](size_type idx) const {
143 assert(idx < size());
151 const_reference front() const {
160 const_reference back() const {
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> {
171 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
173 static void destroy_range(T *S, T *E) {
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);
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) {
197 *--Dest = ::std::move(*--E);
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));
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);
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);
222 void push_back(const T &Elt) {
223 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
225 ::new ((void*) this->end()) T(Elt);
226 this->setEnd(this->end()+1);
229 void push_back(T &&Elt) {
230 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
232 ::new ((void*) this->end()) T(::std::move(Elt));
233 this->setEnd(this->end()+1);
237 this->setEnd(this->end()-1);
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)));
253 // Move the elements over.
254 this->uninitialized_move(this->begin(), this->end(), NewElts);
256 // Destroy the original elements.
257 destroy_range(this->begin(), this->end());
259 // If this wasn't grown from the inline copy, deallocate the old space.
260 if (!this->isSmall())
263 this->setEnd(NewElts+CurSize);
264 this->BeginX = NewElts;
265 this->CapacityX = this->begin()+NewCapacity;
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> {
274 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
276 // No need to do a destroy loop for POD's.
277 static void destroy_range(T *, T *) {}
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);
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);
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) {
298 uninitialized_copy(I, E, Dest);
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);
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
319 memcpy(Dest, I, (E-I)*sizeof(T));
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));
328 void push_back(const T &Elt) {
329 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
331 memcpy(this->end(), &Elt, sizeof(T));
332 this->setEnd(this->end()+1);
336 this->setEnd(this->end()-1);
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;
347 SmallVectorImpl(const SmallVectorImpl&) = delete;
349 typedef typename SuperClass::iterator iterator;
350 typedef typename SuperClass::size_type size_type;
353 // Default ctor - Initialize to empty.
354 explicit SmallVectorImpl(unsigned N)
355 : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
360 // Destroy the constructed elements in the vector.
361 this->destroy_range(this->begin(), this->end());
363 // If this wasn't grown from the inline copy, deallocate the old space.
364 if (!this->isSmall())
370 this->destroy_range(this->begin(), this->end());
371 this->EndX = this->BeginX;
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)
381 for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
383 this->setEnd(this->begin()+N);
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)
394 std::uninitialized_fill(this->end(), this->begin()+N, NV);
395 this->setEnd(this->begin()+N);
399 void reserve(size_type N) {
400 if (this->capacity() < N)
404 T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
405 T Result = ::std::move(this->back());
410 void swap(SmallVectorImpl &RHS);
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);
420 // Copy the new elements over.
421 this->uninitialized_copy(in_start, in_end, this->end());
422 this->setEnd(this->end() + NumInputs);
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);
431 // Copy the new elements over.
432 std::uninitialized_fill_n(this->end(), NumInputs, Elt);
433 this->setEnd(this->end() + NumInputs);
436 void append(std::initializer_list<T> IL) {
437 append(IL.begin(), IL.end());
440 void assign(size_type NumElts, const T &Elt) {
442 if (this->capacity() < NumElts)
444 this->setEnd(this->begin()+NumElts);
445 std::uninitialized_fill(this->begin(), this->end(), Elt);
448 void assign(std::initializer_list<T> IL) {
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.");
458 // Shift all elts down one.
459 this->move(I+1, this->end(), I);
460 // Drop the last elt.
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.");
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());
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;
485 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
486 assert(I <= this->end() && "Inserting past the end of the vector.");
488 if (this->EndX >= this->CapacityX) {
489 size_t EltNo = I-this->begin();
491 I = this->begin()+EltNo;
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);
499 // If we just moved the element we're inserting, be sure to update
502 if (I <= EltPtr && EltPtr < this->EndX)
505 *I = ::std::move(*EltPtr);
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;
515 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
516 assert(I <= this->end() && "Inserting past the end of the vector.");
518 if (this->EndX >= this->CapacityX) {
519 size_t EltNo = I-this->begin();
521 I = this->begin()+EltNo;
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);
528 // If we just moved the element we're inserting, be sure to update
530 const T *EltPtr = &Elt;
531 if (I <= EltPtr && EltPtr < this->EndX)
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();
542 if (I == this->end()) { // Important special case for empty vector.
543 append(NumToInsert, Elt);
544 return this->begin()+InsertElt;
547 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
548 assert(I <= this->end() && "Inserting past the end of the vector.");
550 // Ensure there is enough space.
551 reserve(this->size() + NumToInsert);
553 // Uninvalidate the iterator.
554 I = this->begin()+InsertElt;
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()));
565 // Copy the existing elements that get replaced.
566 this->move_backward(I, OldEnd-NumToInsert, OldEnd);
568 std::fill_n(I, NumToInsert, Elt);
572 // Otherwise, we're inserting more elements than exist already, and we're
573 // not inserting at the end.
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);
581 // Replace the overwritten part.
582 std::fill_n(I, NumOverwritten, Elt);
584 // Insert the non-overwritten middle part.
585 std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
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();
594 if (I == this->end()) { // Important special case for empty vector.
596 return this->begin()+InsertElt;
599 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
600 assert(I <= this->end() && "Inserting past the end of the vector.");
602 size_t NumToInsert = std::distance(From, To);
604 // Ensure there is enough space.
605 reserve(this->size() + NumToInsert);
607 // Uninvalidate the iterator.
608 I = this->begin()+InsertElt;
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()));
619 // Copy the existing elements that get replaced.
620 this->move_backward(I, OldEnd-NumToInsert, OldEnd);
622 std::copy(From, To, I);
626 // Otherwise, we're inserting more elements than exist already, and we're
627 // not inserting at the end.
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);
635 // Replace the overwritten part.
636 for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
641 // Insert the non-overwritten middle part.
642 this->uninitialized_copy(From, To, OldEnd);
646 void insert(iterator I, std::initializer_list<T> IL) {
647 insert(I, IL.begin(), IL.end());
650 template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
651 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
653 ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
654 this->setEnd(this->end() + 1);
657 SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
659 SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
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());
665 bool operator!=(const SmallVectorImpl &RHS) const {
666 return !(*this == RHS);
669 bool operator<(const SmallVectorImpl &RHS) const {
670 return std::lexicographical_compare(this->begin(), this->end(),
671 RHS.begin(), RHS.end());
674 /// Set the array size to \p N, which the current array must have enough
677 /// This does not construct or destroy any elements in the vector.
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);
690 template <typename T>
691 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
692 if (this == &RHS) return;
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);
701 if (RHS.size() > this->capacity())
702 this->grow(RHS.size());
703 if (this->size() > RHS.capacity())
704 RHS.grow(this->size());
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]);
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);
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;
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.
742 NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
744 NewEnd = this->begin();
746 // Destroy excess elements.
747 this->destroy_range(NewEnd, this->end());
750 this->setEnd(NewEnd);
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());
763 } else if (CurSize) {
764 // Otherwise, use assignment for the already-constructed elements.
765 std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
768 // Copy construct the new elements in place.
769 this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
770 this->begin()+CurSize);
773 this->setEnd(this->begin()+RHSSize);
777 template <typename T>
778 SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
779 // Avoid self-assignment.
780 if (this == &RHS) return *this;
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;
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();
801 NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
803 // Destroy excess elements and trim the bounds.
804 this->destroy_range(NewEnd, this->end());
805 this->setEnd(NewEnd);
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
817 if (this->capacity() < RHSSize) {
818 // Destroy current elements.
819 this->destroy_range(this->begin(), this->end());
820 this->setEnd(this->begin());
823 } else if (CurSize) {
824 // Otherwise, use assignment for the already-constructed elements.
825 this->move(RHS.begin(), RHS.begin()+CurSize, this->begin());
828 // Move-construct the new elements in place.
829 this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
830 this->begin()+CurSize);
833 this->setEnd(this->begin()+RHSSize);
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];
847 template <typename T> struct SmallVectorStorage<T, 1> {};
848 template <typename T> struct SmallVectorStorage<T, 0> {};
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.
856 /// Note that this does not attempt to be exception safe.
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;
863 SmallVector() : SmallVectorImpl<T>(N) {
866 explicit SmallVector(size_t Size, const T &Value = T())
867 : SmallVectorImpl<T>(N) {
868 this->assign(Size, Value);
871 template<typename ItTy>
872 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
876 template <typename RangeTy>
877 explicit SmallVector(const llvm::iterator_range<RangeTy> R)
878 : SmallVectorImpl<T>(N) {
879 this->append(R.begin(), R.end());
882 SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
886 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
888 SmallVectorImpl<T>::operator=(RHS);
891 const SmallVector &operator=(const SmallVector &RHS) {
892 SmallVectorImpl<T>::operator=(RHS);
896 SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
898 SmallVectorImpl<T>::operator=(::std::move(RHS));
901 const SmallVector &operator=(SmallVector &&RHS) {
902 SmallVectorImpl<T>::operator=(::std::move(RHS));
906 SmallVector(SmallVectorImpl<T> &&RHS) : SmallVectorImpl<T>(N) {
908 SmallVectorImpl<T>::operator=(::std::move(RHS));
911 const SmallVector &operator=(SmallVectorImpl<T> &&RHS) {
912 SmallVectorImpl<T>::operator=(::std::move(RHS));
916 const SmallVector &operator=(std::initializer_list<T> IL) {
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();
927 } // End llvm namespace
930 /// Implement std::swap in terms of SmallVector swap.
933 swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
937 /// Implement std::swap in terms of SmallVector swap.
938 template<typename T, unsigned N>
940 swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {