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 LLVM_ATTRIBUTE_ALWAYS_INLINE
113 iterator begin() { return (iterator)this->BeginX; }
114 LLVM_ATTRIBUTE_ALWAYS_INLINE
115 const_iterator begin() const { return (const_iterator)this->BeginX; }
116 LLVM_ATTRIBUTE_ALWAYS_INLINE
117 iterator end() { return (iterator)this->EndX; }
118 LLVM_ATTRIBUTE_ALWAYS_INLINE
119 const_iterator end() const { return (const_iterator)this->EndX; }
121 iterator capacity_ptr() { return (iterator)this->CapacityX; }
122 const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
125 // reverse iterator creation methods.
126 reverse_iterator rbegin() { return reverse_iterator(end()); }
127 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
128 reverse_iterator rend() { return reverse_iterator(begin()); }
129 const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
131 LLVM_ATTRIBUTE_ALWAYS_INLINE
132 size_type size() const { return end()-begin(); }
133 size_type max_size() const { return size_type(-1) / sizeof(T); }
135 /// Return the total number of elements in the currently allocated buffer.
136 size_t capacity() const { return capacity_ptr() - begin(); }
138 /// Return a pointer to the vector's buffer, even if empty().
139 pointer data() { return pointer(begin()); }
140 /// Return a pointer to the vector's buffer, even if empty().
141 const_pointer data() const { return const_pointer(begin()); }
143 LLVM_ATTRIBUTE_ALWAYS_INLINE
144 reference operator[](size_type idx) {
145 assert(idx < size());
148 LLVM_ATTRIBUTE_ALWAYS_INLINE
149 const_reference operator[](size_type idx) const {
150 assert(idx < size());
158 const_reference front() const {
167 const_reference back() const {
173 /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
174 /// implementations that are designed to work with non-POD-like T's.
175 template <typename T, bool isPodLike>
176 class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
178 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
180 static void destroy_range(T *S, T *E) {
187 /// Use move-assignment to move the range [I, E) onto the
188 /// objects starting with "Dest". This is just <memory>'s
189 /// std::move, but not all stdlibs actually provide that.
190 template<typename It1, typename It2>
191 static It2 move(It1 I, It1 E, It2 Dest) {
192 for (; I != E; ++I, ++Dest)
193 *Dest = ::std::move(*I);
197 /// Use move-assignment to move the range
198 /// [I, E) onto the objects ending at "Dest", moving objects
199 /// in reverse order. This is just <algorithm>'s
200 /// std::move_backward, but not all stdlibs actually provide that.
201 template<typename It1, typename It2>
202 static It2 move_backward(It1 I, It1 E, It2 Dest) {
204 *--Dest = ::std::move(*--E);
208 /// Move the range [I, E) into the uninitialized memory starting with "Dest",
209 /// constructing elements as needed.
210 template<typename It1, typename It2>
211 static void uninitialized_move(It1 I, It1 E, It2 Dest) {
212 for (; I != E; ++I, ++Dest)
213 ::new ((void*) &*Dest) T(::std::move(*I));
216 /// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
217 /// constructing elements as needed.
218 template<typename It1, typename It2>
219 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
220 std::uninitialized_copy(I, E, Dest);
223 /// Grow the allocated memory (without initializing new elements), doubling
224 /// the size of the allocated memory. Guarantees space for at least one more
225 /// element, or MinSize more elements if specified.
226 void grow(size_t MinSize = 0);
229 void push_back(const T &Elt) {
230 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
232 ::new ((void*) this->end()) T(Elt);
233 this->setEnd(this->end()+1);
236 void push_back(T &&Elt) {
237 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
239 ::new ((void*) this->end()) T(::std::move(Elt));
240 this->setEnd(this->end()+1);
244 this->setEnd(this->end()-1);
249 // Define this out-of-line to dissuade the C++ compiler from inlining it.
250 template <typename T, bool isPodLike>
251 void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
252 size_t CurCapacity = this->capacity();
253 size_t CurSize = this->size();
254 // Always grow, even from zero.
255 size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
256 if (NewCapacity < MinSize)
257 NewCapacity = MinSize;
258 T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
260 // Move the elements over.
261 this->uninitialized_move(this->begin(), this->end(), NewElts);
263 // Destroy the original elements.
264 destroy_range(this->begin(), this->end());
266 // If this wasn't grown from the inline copy, deallocate the old space.
267 if (!this->isSmall())
270 this->setEnd(NewElts+CurSize);
271 this->BeginX = NewElts;
272 this->CapacityX = this->begin()+NewCapacity;
276 /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
277 /// implementations that are designed to work with POD-like T's.
278 template <typename T>
279 class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
281 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
283 // No need to do a destroy loop for POD's.
284 static void destroy_range(T *, T *) {}
286 /// Use move-assignment to move the range [I, E) onto the
287 /// objects starting with "Dest". For PODs, this is just memcpy.
288 template<typename It1, typename It2>
289 static It2 move(It1 I, It1 E, It2 Dest) {
290 return ::std::copy(I, E, Dest);
293 /// Use move-assignment to move the range [I, E) onto the objects ending at
294 /// "Dest", moving objects in reverse order.
295 template<typename It1, typename It2>
296 static It2 move_backward(It1 I, It1 E, It2 Dest) {
297 return ::std::copy_backward(I, E, Dest);
300 /// Move the range [I, E) onto the uninitialized memory
301 /// starting with "Dest", constructing elements into it as needed.
302 template<typename It1, typename It2>
303 static void uninitialized_move(It1 I, It1 E, It2 Dest) {
305 uninitialized_copy(I, E, Dest);
308 /// Copy the range [I, E) onto the uninitialized memory
309 /// starting with "Dest", constructing elements into it as needed.
310 template<typename It1, typename It2>
311 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
312 // Arbitrary iterator types; just use the basic implementation.
313 std::uninitialized_copy(I, E, Dest);
316 /// Copy the range [I, E) onto the uninitialized memory
317 /// starting with "Dest", constructing elements into it as needed.
318 template <typename T1, typename T2>
319 static void uninitialized_copy(
320 T1 *I, T1 *E, T2 *Dest,
321 typename std::enable_if<std::is_same<typename std::remove_const<T1>::type,
322 T2>::value>::type * = nullptr) {
323 // Use memcpy for PODs iterated by pointers (which includes SmallVector
324 // iterators): std::uninitialized_copy optimizes to memmove, but we can
325 // use memcpy here. Note that I and E are iterators and thus might be
326 // invalid for memcpy if they are equal.
328 memcpy(Dest, I, (E - I) * sizeof(T));
331 /// Double the size of the allocated memory, guaranteeing space for at
332 /// least one more element or MinSize if specified.
333 void grow(size_t MinSize = 0) {
334 this->grow_pod(MinSize*sizeof(T), sizeof(T));
337 void push_back(const T &Elt) {
338 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
340 memcpy(this->end(), &Elt, sizeof(T));
341 this->setEnd(this->end()+1);
345 this->setEnd(this->end()-1);
350 /// This class consists of common code factored out of the SmallVector class to
351 /// reduce code duplication based on the SmallVector 'N' template parameter.
352 template <typename T>
353 class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
354 typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
356 SmallVectorImpl(const SmallVectorImpl&) = delete;
358 typedef typename SuperClass::iterator iterator;
359 typedef typename SuperClass::size_type size_type;
362 // Default ctor - Initialize to empty.
363 explicit SmallVectorImpl(unsigned N)
364 : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
369 // Destroy the constructed elements in the vector.
370 this->destroy_range(this->begin(), this->end());
372 // If this wasn't grown from the inline copy, deallocate the old space.
373 if (!this->isSmall())
379 this->destroy_range(this->begin(), this->end());
380 this->EndX = this->BeginX;
383 void resize(size_type N) {
384 if (N < this->size()) {
385 this->destroy_range(this->begin()+N, this->end());
386 this->setEnd(this->begin()+N);
387 } else if (N > this->size()) {
388 if (this->capacity() < N)
390 for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
392 this->setEnd(this->begin()+N);
396 void resize(size_type N, const T &NV) {
397 if (N < this->size()) {
398 this->destroy_range(this->begin()+N, this->end());
399 this->setEnd(this->begin()+N);
400 } else if (N > this->size()) {
401 if (this->capacity() < N)
403 std::uninitialized_fill(this->end(), this->begin()+N, NV);
404 this->setEnd(this->begin()+N);
408 void reserve(size_type N) {
409 if (this->capacity() < N)
413 T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
414 T Result = ::std::move(this->back());
419 void swap(SmallVectorImpl &RHS);
421 /// Add the specified range to the end of the SmallVector.
422 template<typename in_iter>
423 void append(in_iter in_start, in_iter in_end) {
424 size_type NumInputs = std::distance(in_start, in_end);
425 // Grow allocated space if needed.
426 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
427 this->grow(this->size()+NumInputs);
429 // Copy the new elements over.
430 this->uninitialized_copy(in_start, in_end, this->end());
431 this->setEnd(this->end() + NumInputs);
434 /// Add the specified range to the end of the SmallVector.
435 void append(size_type NumInputs, const T &Elt) {
436 // Grow allocated space if needed.
437 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
438 this->grow(this->size()+NumInputs);
440 // Copy the new elements over.
441 std::uninitialized_fill_n(this->end(), NumInputs, Elt);
442 this->setEnd(this->end() + NumInputs);
445 void append(std::initializer_list<T> IL) {
446 append(IL.begin(), IL.end());
449 void assign(size_type NumElts, const T &Elt) {
451 if (this->capacity() < NumElts)
453 this->setEnd(this->begin()+NumElts);
454 std::uninitialized_fill(this->begin(), this->end(), Elt);
457 void assign(std::initializer_list<T> IL) {
462 iterator erase(iterator I) {
463 assert(I >= this->begin() && "Iterator to erase is out of bounds.");
464 assert(I < this->end() && "Erasing at past-the-end iterator.");
467 // Shift all elts down one.
468 this->move(I+1, this->end(), I);
469 // Drop the last elt.
474 iterator erase(iterator S, iterator E) {
475 assert(S >= this->begin() && "Range to erase is out of bounds.");
476 assert(S <= E && "Trying to erase invalid range.");
477 assert(E <= this->end() && "Trying to erase past the end.");
480 // Shift all elts down.
481 iterator I = this->move(E, this->end(), S);
482 // Drop the last elts.
483 this->destroy_range(I, this->end());
488 iterator insert(iterator I, T &&Elt) {
489 if (I == this->end()) { // Important special case for empty vector.
490 this->push_back(::std::move(Elt));
491 return this->end()-1;
494 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
495 assert(I <= this->end() && "Inserting past the end of the vector.");
497 if (this->EndX >= this->CapacityX) {
498 size_t EltNo = I-this->begin();
500 I = this->begin()+EltNo;
503 ::new ((void*) this->end()) T(::std::move(this->back()));
504 // Push everything else over.
505 this->move_backward(I, this->end()-1, this->end());
506 this->setEnd(this->end()+1);
508 // If we just moved the element we're inserting, be sure to update
511 if (I <= EltPtr && EltPtr < this->EndX)
514 *I = ::std::move(*EltPtr);
518 iterator insert(iterator I, const T &Elt) {
519 if (I == this->end()) { // Important special case for empty vector.
520 this->push_back(Elt);
521 return this->end()-1;
524 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
525 assert(I <= this->end() && "Inserting past the end of the vector.");
527 if (this->EndX >= this->CapacityX) {
528 size_t EltNo = I-this->begin();
530 I = this->begin()+EltNo;
532 ::new ((void*) this->end()) T(std::move(this->back()));
533 // Push everything else over.
534 this->move_backward(I, this->end()-1, this->end());
535 this->setEnd(this->end()+1);
537 // If we just moved the element we're inserting, be sure to update
539 const T *EltPtr = &Elt;
540 if (I <= EltPtr && EltPtr < this->EndX)
547 iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
548 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
549 size_t InsertElt = I - this->begin();
551 if (I == this->end()) { // Important special case for empty vector.
552 append(NumToInsert, Elt);
553 return this->begin()+InsertElt;
556 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
557 assert(I <= this->end() && "Inserting past the end of the vector.");
559 // Ensure there is enough space.
560 reserve(this->size() + NumToInsert);
562 // Uninvalidate the iterator.
563 I = this->begin()+InsertElt;
565 // If there are more elements between the insertion point and the end of the
566 // range than there are being inserted, we can use a simple approach to
567 // insertion. Since we already reserved space, we know that this won't
568 // reallocate the vector.
569 if (size_t(this->end()-I) >= NumToInsert) {
570 T *OldEnd = this->end();
571 append(std::move_iterator<iterator>(this->end() - NumToInsert),
572 std::move_iterator<iterator>(this->end()));
574 // Copy the existing elements that get replaced.
575 this->move_backward(I, OldEnd-NumToInsert, OldEnd);
577 std::fill_n(I, NumToInsert, Elt);
581 // Otherwise, we're inserting more elements than exist already, and we're
582 // not inserting at the end.
584 // Move over the elements that we're about to overwrite.
585 T *OldEnd = this->end();
586 this->setEnd(this->end() + NumToInsert);
587 size_t NumOverwritten = OldEnd-I;
588 this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
590 // Replace the overwritten part.
591 std::fill_n(I, NumOverwritten, Elt);
593 // Insert the non-overwritten middle part.
594 std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
598 template<typename ItTy>
599 iterator insert(iterator I, ItTy From, ItTy To) {
600 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
601 size_t InsertElt = I - this->begin();
603 if (I == this->end()) { // Important special case for empty vector.
605 return this->begin()+InsertElt;
608 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
609 assert(I <= this->end() && "Inserting past the end of the vector.");
611 size_t NumToInsert = std::distance(From, To);
613 // Ensure there is enough space.
614 reserve(this->size() + NumToInsert);
616 // Uninvalidate the iterator.
617 I = this->begin()+InsertElt;
619 // If there are more elements between the insertion point and the end of the
620 // range than there are being inserted, we can use a simple approach to
621 // insertion. Since we already reserved space, we know that this won't
622 // reallocate the vector.
623 if (size_t(this->end()-I) >= NumToInsert) {
624 T *OldEnd = this->end();
625 append(std::move_iterator<iterator>(this->end() - NumToInsert),
626 std::move_iterator<iterator>(this->end()));
628 // Copy the existing elements that get replaced.
629 this->move_backward(I, OldEnd-NumToInsert, OldEnd);
631 std::copy(From, To, I);
635 // Otherwise, we're inserting more elements than exist already, and we're
636 // not inserting at the end.
638 // Move over the elements that we're about to overwrite.
639 T *OldEnd = this->end();
640 this->setEnd(this->end() + NumToInsert);
641 size_t NumOverwritten = OldEnd-I;
642 this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
644 // Replace the overwritten part.
645 for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
650 // Insert the non-overwritten middle part.
651 this->uninitialized_copy(From, To, OldEnd);
655 void insert(iterator I, std::initializer_list<T> IL) {
656 insert(I, IL.begin(), IL.end());
659 template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
660 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
662 ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
663 this->setEnd(this->end() + 1);
666 SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
668 SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
670 bool operator==(const SmallVectorImpl &RHS) const {
671 if (this->size() != RHS.size()) return false;
672 return std::equal(this->begin(), this->end(), RHS.begin());
674 bool operator!=(const SmallVectorImpl &RHS) const {
675 return !(*this == RHS);
678 bool operator<(const SmallVectorImpl &RHS) const {
679 return std::lexicographical_compare(this->begin(), this->end(),
680 RHS.begin(), RHS.end());
683 /// Set the array size to \p N, which the current array must have enough
686 /// This does not construct or destroy any elements in the vector.
688 /// Clients can use this in conjunction with capacity() to write past the end
689 /// of the buffer when they know that more elements are available, and only
690 /// update the size later. This avoids the cost of value initializing elements
691 /// which will only be overwritten.
692 void set_size(size_type N) {
693 assert(N <= this->capacity());
694 this->setEnd(this->begin() + N);
699 template <typename T>
700 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
701 if (this == &RHS) return;
703 // We can only avoid copying elements if neither vector is small.
704 if (!this->isSmall() && !RHS.isSmall()) {
705 std::swap(this->BeginX, RHS.BeginX);
706 std::swap(this->EndX, RHS.EndX);
707 std::swap(this->CapacityX, RHS.CapacityX);
710 if (RHS.size() > this->capacity())
711 this->grow(RHS.size());
712 if (this->size() > RHS.capacity())
713 RHS.grow(this->size());
715 // Swap the shared elements.
716 size_t NumShared = this->size();
717 if (NumShared > RHS.size()) NumShared = RHS.size();
718 for (size_type i = 0; i != NumShared; ++i)
719 std::swap((*this)[i], RHS[i]);
721 // Copy over the extra elts.
722 if (this->size() > RHS.size()) {
723 size_t EltDiff = this->size() - RHS.size();
724 this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
725 RHS.setEnd(RHS.end()+EltDiff);
726 this->destroy_range(this->begin()+NumShared, this->end());
727 this->setEnd(this->begin()+NumShared);
728 } else if (RHS.size() > this->size()) {
729 size_t EltDiff = RHS.size() - this->size();
730 this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
731 this->setEnd(this->end() + EltDiff);
732 this->destroy_range(RHS.begin()+NumShared, RHS.end());
733 RHS.setEnd(RHS.begin()+NumShared);
737 template <typename T>
738 SmallVectorImpl<T> &SmallVectorImpl<T>::
739 operator=(const SmallVectorImpl<T> &RHS) {
740 // Avoid self-assignment.
741 if (this == &RHS) return *this;
743 // If we already have sufficient space, assign the common elements, then
744 // destroy any excess.
745 size_t RHSSize = RHS.size();
746 size_t CurSize = this->size();
747 if (CurSize >= RHSSize) {
748 // Assign common elements.
751 NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
753 NewEnd = this->begin();
755 // Destroy excess elements.
756 this->destroy_range(NewEnd, this->end());
759 this->setEnd(NewEnd);
763 // If we have to grow to have enough elements, destroy the current elements.
764 // This allows us to avoid copying them during the grow.
765 // FIXME: don't do this if they're efficiently moveable.
766 if (this->capacity() < RHSSize) {
767 // Destroy current elements.
768 this->destroy_range(this->begin(), this->end());
769 this->setEnd(this->begin());
772 } else if (CurSize) {
773 // Otherwise, use assignment for the already-constructed elements.
774 std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
777 // Copy construct the new elements in place.
778 this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
779 this->begin()+CurSize);
782 this->setEnd(this->begin()+RHSSize);
786 template <typename T>
787 SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
788 // Avoid self-assignment.
789 if (this == &RHS) return *this;
791 // If the RHS isn't small, clear this vector and then steal its buffer.
792 if (!RHS.isSmall()) {
793 this->destroy_range(this->begin(), this->end());
794 if (!this->isSmall()) free(this->begin());
795 this->BeginX = RHS.BeginX;
796 this->EndX = RHS.EndX;
797 this->CapacityX = RHS.CapacityX;
802 // If we already have sufficient space, assign the common elements, then
803 // destroy any excess.
804 size_t RHSSize = RHS.size();
805 size_t CurSize = this->size();
806 if (CurSize >= RHSSize) {
807 // Assign common elements.
808 iterator NewEnd = this->begin();
810 NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
812 // Destroy excess elements and trim the bounds.
813 this->destroy_range(NewEnd, this->end());
814 this->setEnd(NewEnd);
822 // If we have to grow to have enough elements, destroy the current elements.
823 // This allows us to avoid copying them during the grow.
824 // FIXME: this may not actually make any sense if we can efficiently move
826 if (this->capacity() < RHSSize) {
827 // Destroy current elements.
828 this->destroy_range(this->begin(), this->end());
829 this->setEnd(this->begin());
832 } else if (CurSize) {
833 // Otherwise, use assignment for the already-constructed elements.
834 this->move(RHS.begin(), RHS.begin()+CurSize, this->begin());
837 // Move-construct the new elements in place.
838 this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
839 this->begin()+CurSize);
842 this->setEnd(this->begin()+RHSSize);
848 /// Storage for the SmallVector elements which aren't contained in
849 /// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1'
850 /// element is in the base class. This is specialized for the N=1 and N=0 cases
851 /// to avoid allocating unnecessary storage.
852 template <typename T, unsigned N>
853 struct SmallVectorStorage {
854 typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
856 template <typename T> struct SmallVectorStorage<T, 1> {};
857 template <typename T> struct SmallVectorStorage<T, 0> {};
859 /// This is a 'vector' (really, a variable-sized array), optimized
860 /// for the case when the array is small. It contains some number of elements
861 /// in-place, which allows it to avoid heap allocation when the actual number of
862 /// elements is below that threshold. This allows normal "small" cases to be
863 /// fast without losing generality for large inputs.
865 /// Note that this does not attempt to be exception safe.
867 template <typename T, unsigned N>
868 class SmallVector : public SmallVectorImpl<T> {
869 /// Inline space for elements which aren't stored in the base class.
870 SmallVectorStorage<T, N> Storage;
872 SmallVector() : SmallVectorImpl<T>(N) {
875 explicit SmallVector(size_t Size, const T &Value = T())
876 : SmallVectorImpl<T>(N) {
877 this->assign(Size, Value);
880 template<typename ItTy>
881 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
885 template <typename RangeTy>
886 explicit SmallVector(const llvm::iterator_range<RangeTy> R)
887 : SmallVectorImpl<T>(N) {
888 this->append(R.begin(), R.end());
891 SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
895 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
897 SmallVectorImpl<T>::operator=(RHS);
900 const SmallVector &operator=(const SmallVector &RHS) {
901 SmallVectorImpl<T>::operator=(RHS);
905 SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
907 SmallVectorImpl<T>::operator=(::std::move(RHS));
910 const SmallVector &operator=(SmallVector &&RHS) {
911 SmallVectorImpl<T>::operator=(::std::move(RHS));
915 SmallVector(SmallVectorImpl<T> &&RHS) : SmallVectorImpl<T>(N) {
917 SmallVectorImpl<T>::operator=(::std::move(RHS));
920 const SmallVector &operator=(SmallVectorImpl<T> &&RHS) {
921 SmallVectorImpl<T>::operator=(::std::move(RHS));
925 const SmallVector &operator=(std::initializer_list<T> IL) {
931 template<typename T, unsigned N>
932 static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
933 return X.capacity_in_bytes();
936 } // End llvm namespace
939 /// Implement std::swap in terms of SmallVector swap.
942 swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
946 /// Implement std::swap in terms of SmallVector swap.
947 template<typename T, unsigned N>
949 swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {