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"
32 /// SmallVectorBase - This is all the non-templated stuff common to all
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 /// grow_pod - 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 /// size_in_bytes - 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 /// SmallVectorTemplateCommon - This is the part of SmallVectorTemplateBase
63 /// which does not depend on whether the type T is a POD. The extra dummy
64 /// template argument is used by ArrayRef to avoid unnecessarily requiring T
66 template <typename T, typename = void>
67 class SmallVectorTemplateCommon : public SmallVectorBase {
69 template <typename, unsigned> friend struct SmallVectorStorage;
71 // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
72 // don't want it to be automatically run, so we need to represent the space as
73 // something else. Use an array of char of sufficient alignment.
74 typedef llvm::AlignedCharArrayUnion<T> U;
76 // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
79 SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
81 void grow_pod(size_t MinSizeInBytes, size_t TSize) {
82 SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
85 /// isSmall - Return true if this is a smallvector which has not had dynamic
86 /// memory allocated for it.
87 bool isSmall() const {
88 return BeginX == static_cast<const void*>(&FirstEl);
91 /// resetToSmall - Put this vector in a state of being small.
93 BeginX = EndX = CapacityX = &FirstEl;
96 void setEnd(T *P) { this->EndX = P; }
98 typedef size_t size_type;
99 typedef ptrdiff_t difference_type;
100 typedef T value_type;
102 typedef const T *const_iterator;
104 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
105 typedef std::reverse_iterator<iterator> reverse_iterator;
107 typedef T &reference;
108 typedef const T &const_reference;
110 typedef const T *const_pointer;
112 // forward iterator creation methods.
113 iterator begin() { return (iterator)this->BeginX; }
114 const_iterator begin() const { return (const_iterator)this->BeginX; }
115 iterator end() { return (iterator)this->EndX; }
116 const_iterator end() const { return (const_iterator)this->EndX; }
118 iterator capacity_ptr() { return (iterator)this->CapacityX; }
119 const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
122 // reverse iterator creation methods.
123 reverse_iterator rbegin() { return reverse_iterator(end()); }
124 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
125 reverse_iterator rend() { return reverse_iterator(begin()); }
126 const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
128 size_type size() const { return end()-begin(); }
129 size_type max_size() const { return size_type(-1) / sizeof(T); }
131 /// capacity - Return the total number of elements in the currently allocated
133 size_t capacity() const { return capacity_ptr() - begin(); }
135 /// data - Return a pointer to the vector's buffer, even if empty().
136 pointer data() { return pointer(begin()); }
137 /// data - Return a pointer to the vector's buffer, even if empty().
138 const_pointer data() const { return const_pointer(begin()); }
140 reference operator[](unsigned idx) {
141 assert(begin() + idx < end());
144 const_reference operator[](unsigned idx) const {
145 assert(begin() + idx < end());
153 const_reference front() const {
162 const_reference back() const {
168 /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
169 /// implementations that are designed to work with non-POD-like T's.
170 template <typename T, bool isPodLike>
171 class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
173 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
175 static void destroy_range(T *S, T *E) {
182 /// move - Use move-assignment to move the range [I, E) onto the
183 /// objects starting with "Dest". This is just <memory>'s
184 /// std::move, but not all stdlibs actually provide that.
185 template<typename It1, typename It2>
186 static It2 move(It1 I, It1 E, It2 Dest) {
187 for (; I != E; ++I, ++Dest)
188 *Dest = ::std::move(*I);
192 /// move_backward - Use move-assignment to move the range
193 /// [I, E) onto the objects ending at "Dest", moving objects
194 /// in reverse order. This is just <algorithm>'s
195 /// std::move_backward, but not all stdlibs actually provide that.
196 template<typename It1, typename It2>
197 static It2 move_backward(It1 I, It1 E, It2 Dest) {
199 *--Dest = ::std::move(*--E);
203 /// uninitialized_move - Move the range [I, E) into the uninitialized
204 /// memory starting with "Dest", constructing elements as needed.
205 template<typename It1, typename It2>
206 static void uninitialized_move(It1 I, It1 E, It2 Dest) {
207 for (; I != E; ++I, ++Dest)
208 ::new ((void*) &*Dest) T(::std::move(*I));
211 /// uninitialized_copy - Copy the range [I, E) onto the uninitialized
212 /// memory starting with "Dest", constructing elements as needed.
213 template<typename It1, typename It2>
214 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
215 std::uninitialized_copy(I, E, Dest);
218 /// grow - Grow the allocated memory (without initializing new
219 /// elements), doubling the size of the allocated memory.
220 /// Guarantees space for at least one more element, or MinSize more
221 /// elements if specified.
222 void grow(size_t MinSize = 0);
225 void push_back(const T &Elt) {
226 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
228 ::new ((void*) this->end()) T(Elt);
229 this->setEnd(this->end()+1);
232 void push_back(T &&Elt) {
233 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
235 ::new ((void*) this->end()) T(::std::move(Elt));
236 this->setEnd(this->end()+1);
240 this->setEnd(this->end()-1);
245 // Define this out-of-line to dissuade the C++ compiler from inlining it.
246 template <typename T, bool isPodLike>
247 void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
248 size_t CurCapacity = this->capacity();
249 size_t CurSize = this->size();
250 // Always grow, even from zero.
251 size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
252 if (NewCapacity < MinSize)
253 NewCapacity = MinSize;
254 T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
256 // Move the elements over.
257 this->uninitialized_move(this->begin(), this->end(), NewElts);
259 // Destroy the original elements.
260 destroy_range(this->begin(), this->end());
262 // If this wasn't grown from the inline copy, deallocate the old space.
263 if (!this->isSmall())
266 this->setEnd(NewElts+CurSize);
267 this->BeginX = NewElts;
268 this->CapacityX = this->begin()+NewCapacity;
272 /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
273 /// implementations that are designed to work with POD-like T's.
274 template <typename T>
275 class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
277 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
279 // No need to do a destroy loop for POD's.
280 static void destroy_range(T *, T *) {}
282 /// move - Use move-assignment to move the range [I, E) onto the
283 /// objects starting with "Dest". For PODs, this is just memcpy.
284 template<typename It1, typename It2>
285 static It2 move(It1 I, It1 E, It2 Dest) {
286 return ::std::copy(I, E, Dest);
289 /// move_backward - Use move-assignment to move the range
290 /// [I, E) onto the objects ending at "Dest", moving objects
291 /// in reverse order.
292 template<typename It1, typename It2>
293 static It2 move_backward(It1 I, It1 E, It2 Dest) {
294 return ::std::copy_backward(I, E, Dest);
297 /// uninitialized_move - Move the range [I, E) onto the uninitialized memory
298 /// starting with "Dest", constructing elements into it as needed.
299 template<typename It1, typename It2>
300 static void uninitialized_move(It1 I, It1 E, It2 Dest) {
302 uninitialized_copy(I, E, Dest);
305 /// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
306 /// starting with "Dest", constructing elements into it as needed.
307 template<typename It1, typename It2>
308 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
309 // Arbitrary iterator types; just use the basic implementation.
310 std::uninitialized_copy(I, E, Dest);
313 /// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
314 /// starting with "Dest", constructing elements into it as needed.
315 template<typename T1, typename T2>
316 static void uninitialized_copy(T1 *I, T1 *E, T2 *Dest) {
317 // Use memcpy for PODs iterated by pointers (which includes SmallVector
318 // iterators): std::uninitialized_copy optimizes to memmove, but we can
320 memcpy(Dest, I, (E-I)*sizeof(T));
323 /// grow - double the size of the allocated memory, guaranteeing space for at
324 /// least one more element or MinSize if specified.
325 void grow(size_t MinSize = 0) {
326 this->grow_pod(MinSize*sizeof(T), sizeof(T));
329 void push_back(const T &Elt) {
330 if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
332 memcpy(this->end(), &Elt, sizeof(T));
333 this->setEnd(this->end()+1);
337 this->setEnd(this->end()-1);
342 /// SmallVectorImpl - This class consists of common code factored out of the
343 /// SmallVector class to reduce code duplication based on the SmallVector 'N'
344 /// template parameter.
345 template <typename T>
346 class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
347 typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
349 SmallVectorImpl(const SmallVectorImpl&) LLVM_DELETED_FUNCTION;
351 typedef typename SuperClass::iterator iterator;
352 typedef typename SuperClass::size_type size_type;
355 // Default ctor - Initialize to empty.
356 explicit SmallVectorImpl(unsigned N)
357 : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
362 // Destroy the constructed elements in the vector.
363 this->destroy_range(this->begin(), this->end());
365 // If this wasn't grown from the inline copy, deallocate the old space.
366 if (!this->isSmall())
372 this->destroy_range(this->begin(), this->end());
373 this->EndX = this->BeginX;
376 void resize(unsigned N) {
377 if (N < this->size()) {
378 this->destroy_range(this->begin()+N, this->end());
379 this->setEnd(this->begin()+N);
380 } else if (N > this->size()) {
381 if (this->capacity() < N)
383 for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
385 this->setEnd(this->begin()+N);
389 void resize(unsigned N, const T &NV) {
390 if (N < this->size()) {
391 this->destroy_range(this->begin()+N, this->end());
392 this->setEnd(this->begin()+N);
393 } else if (N > this->size()) {
394 if (this->capacity() < N)
396 std::uninitialized_fill(this->end(), this->begin()+N, NV);
397 this->setEnd(this->begin()+N);
401 void reserve(unsigned N) {
402 if (this->capacity() < N)
406 T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
407 T Result = ::std::move(this->back());
412 void swap(SmallVectorImpl &RHS);
414 /// append - Add the specified range to the end of the SmallVector.
416 template<typename in_iter>
417 void append(in_iter in_start, in_iter in_end) {
418 size_type NumInputs = std::distance(in_start, in_end);
419 // Grow allocated space if needed.
420 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
421 this->grow(this->size()+NumInputs);
423 // Copy the new elements over.
424 // TODO: NEED To compile time dispatch on whether in_iter is a random access
425 // iterator to use the fast uninitialized_copy.
426 std::uninitialized_copy(in_start, in_end, this->end());
427 this->setEnd(this->end() + NumInputs);
430 /// append - Add the specified range to the end of the SmallVector.
432 void append(size_type NumInputs, const T &Elt) {
433 // Grow allocated space if needed.
434 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
435 this->grow(this->size()+NumInputs);
437 // Copy the new elements over.
438 std::uninitialized_fill_n(this->end(), NumInputs, Elt);
439 this->setEnd(this->end() + NumInputs);
442 void assign(unsigned NumElts, const T &Elt) {
444 if (this->capacity() < NumElts)
446 this->setEnd(this->begin()+NumElts);
447 std::uninitialized_fill(this->begin(), this->end(), Elt);
450 iterator erase(iterator I) {
451 assert(I >= this->begin() && "Iterator to erase is out of bounds.");
452 assert(I < this->end() && "Erasing at past-the-end iterator.");
455 // Shift all elts down one.
456 this->move(I+1, this->end(), I);
457 // Drop the last elt.
462 iterator erase(iterator S, iterator E) {
463 assert(S >= this->begin() && "Range to erase is out of bounds.");
464 assert(S <= E && "Trying to erase invalid range.");
465 assert(E <= this->end() && "Trying to erase past the end.");
468 // Shift all elts down.
469 iterator I = this->move(E, this->end(), S);
470 // Drop the last elts.
471 this->destroy_range(I, this->end());
476 iterator insert(iterator I, T &&Elt) {
477 if (I == this->end()) { // Important special case for empty vector.
478 this->push_back(::std::move(Elt));
479 return this->end()-1;
482 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
483 assert(I <= this->end() && "Inserting past the end of the vector.");
485 if (this->EndX >= this->CapacityX) {
486 size_t EltNo = I-this->begin();
488 I = this->begin()+EltNo;
491 ::new ((void*) this->end()) T(::std::move(this->back()));
492 // Push everything else over.
493 this->move_backward(I, this->end()-1, this->end());
494 this->setEnd(this->end()+1);
496 // If we just moved the element we're inserting, be sure to update
499 if (I <= EltPtr && EltPtr < this->EndX)
502 *I = ::std::move(*EltPtr);
506 iterator insert(iterator I, const T &Elt) {
507 if (I == this->end()) { // Important special case for empty vector.
508 this->push_back(Elt);
509 return this->end()-1;
512 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
513 assert(I <= this->end() && "Inserting past the end of the vector.");
515 if (this->EndX >= this->CapacityX) {
516 size_t EltNo = I-this->begin();
518 I = this->begin()+EltNo;
520 ::new ((void*) this->end()) T(std::move(this->back()));
521 // Push everything else over.
522 this->move_backward(I, this->end()-1, this->end());
523 this->setEnd(this->end()+1);
525 // If we just moved the element we're inserting, be sure to update
527 const T *EltPtr = &Elt;
528 if (I <= EltPtr && EltPtr < this->EndX)
535 iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
536 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
537 size_t InsertElt = I - this->begin();
539 if (I == this->end()) { // Important special case for empty vector.
540 append(NumToInsert, Elt);
541 return this->begin()+InsertElt;
544 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
545 assert(I <= this->end() && "Inserting past the end of the vector.");
547 // Ensure there is enough space.
548 reserve(static_cast<unsigned>(this->size() + NumToInsert));
550 // Uninvalidate the iterator.
551 I = this->begin()+InsertElt;
553 // If there are more elements between the insertion point and the end of the
554 // range than there are being inserted, we can use a simple approach to
555 // insertion. Since we already reserved space, we know that this won't
556 // reallocate the vector.
557 if (size_t(this->end()-I) >= NumToInsert) {
558 T *OldEnd = this->end();
559 append(std::move_iterator<iterator>(this->end() - NumToInsert),
560 std::move_iterator<iterator>(this->end()));
562 // Copy the existing elements that get replaced.
563 this->move_backward(I, OldEnd-NumToInsert, OldEnd);
565 std::fill_n(I, NumToInsert, Elt);
569 // Otherwise, we're inserting more elements than exist already, and we're
570 // not inserting at the end.
572 // Move over the elements that we're about to overwrite.
573 T *OldEnd = this->end();
574 this->setEnd(this->end() + NumToInsert);
575 size_t NumOverwritten = OldEnd-I;
576 this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
578 // Replace the overwritten part.
579 std::fill_n(I, NumOverwritten, Elt);
581 // Insert the non-overwritten middle part.
582 std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
586 template<typename ItTy>
587 iterator insert(iterator I, ItTy From, ItTy To) {
588 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
589 size_t InsertElt = I - this->begin();
591 if (I == this->end()) { // Important special case for empty vector.
593 return this->begin()+InsertElt;
596 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
597 assert(I <= this->end() && "Inserting past the end of the vector.");
599 size_t NumToInsert = std::distance(From, To);
601 // Ensure there is enough space.
602 reserve(static_cast<unsigned>(this->size() + NumToInsert));
604 // Uninvalidate the iterator.
605 I = this->begin()+InsertElt;
607 // If there are more elements between the insertion point and the end of the
608 // range than there are being inserted, we can use a simple approach to
609 // insertion. Since we already reserved space, we know that this won't
610 // reallocate the vector.
611 if (size_t(this->end()-I) >= NumToInsert) {
612 T *OldEnd = this->end();
613 append(std::move_iterator<iterator>(this->end() - NumToInsert),
614 std::move_iterator<iterator>(this->end()));
616 // Copy the existing elements that get replaced.
617 this->move_backward(I, OldEnd-NumToInsert, OldEnd);
619 std::copy(From, To, I);
623 // Otherwise, we're inserting more elements than exist already, and we're
624 // not inserting at the end.
626 // Move over the elements that we're about to overwrite.
627 T *OldEnd = this->end();
628 this->setEnd(this->end() + NumToInsert);
629 size_t NumOverwritten = OldEnd-I;
630 this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
632 // Replace the overwritten part.
633 for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
638 // Insert the non-overwritten middle part.
639 this->uninitialized_copy(From, To, OldEnd);
643 SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
645 SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
647 bool operator==(const SmallVectorImpl &RHS) const {
648 if (this->size() != RHS.size()) return false;
649 return std::equal(this->begin(), this->end(), RHS.begin());
651 bool operator!=(const SmallVectorImpl &RHS) const {
652 return !(*this == RHS);
655 bool operator<(const SmallVectorImpl &RHS) const {
656 return std::lexicographical_compare(this->begin(), this->end(),
657 RHS.begin(), RHS.end());
660 /// Set the array size to \p N, which the current array must have enough
663 /// This does not construct or destroy any elements in the vector.
665 /// Clients can use this in conjunction with capacity() to write past the end
666 /// of the buffer when they know that more elements are available, and only
667 /// update the size later. This avoids the cost of value initializing elements
668 /// which will only be overwritten.
669 void set_size(unsigned N) {
670 assert(N <= this->capacity());
671 this->setEnd(this->begin() + N);
676 template <typename T>
677 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
678 if (this == &RHS) return;
680 // We can only avoid copying elements if neither vector is small.
681 if (!this->isSmall() && !RHS.isSmall()) {
682 std::swap(this->BeginX, RHS.BeginX);
683 std::swap(this->EndX, RHS.EndX);
684 std::swap(this->CapacityX, RHS.CapacityX);
687 if (RHS.size() > this->capacity())
688 this->grow(RHS.size());
689 if (this->size() > RHS.capacity())
690 RHS.grow(this->size());
692 // Swap the shared elements.
693 size_t NumShared = this->size();
694 if (NumShared > RHS.size()) NumShared = RHS.size();
695 for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
696 std::swap((*this)[i], RHS[i]);
698 // Copy over the extra elts.
699 if (this->size() > RHS.size()) {
700 size_t EltDiff = this->size() - RHS.size();
701 this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
702 RHS.setEnd(RHS.end()+EltDiff);
703 this->destroy_range(this->begin()+NumShared, this->end());
704 this->setEnd(this->begin()+NumShared);
705 } else if (RHS.size() > this->size()) {
706 size_t EltDiff = RHS.size() - this->size();
707 this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
708 this->setEnd(this->end() + EltDiff);
709 this->destroy_range(RHS.begin()+NumShared, RHS.end());
710 RHS.setEnd(RHS.begin()+NumShared);
714 template <typename T>
715 SmallVectorImpl<T> &SmallVectorImpl<T>::
716 operator=(const SmallVectorImpl<T> &RHS) {
717 // Avoid self-assignment.
718 if (this == &RHS) return *this;
720 // If we already have sufficient space, assign the common elements, then
721 // destroy any excess.
722 size_t RHSSize = RHS.size();
723 size_t CurSize = this->size();
724 if (CurSize >= RHSSize) {
725 // Assign common elements.
728 NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
730 NewEnd = this->begin();
732 // Destroy excess elements.
733 this->destroy_range(NewEnd, this->end());
736 this->setEnd(NewEnd);
740 // If we have to grow to have enough elements, destroy the current elements.
741 // This allows us to avoid copying them during the grow.
742 // FIXME: don't do this if they're efficiently moveable.
743 if (this->capacity() < RHSSize) {
744 // Destroy current elements.
745 this->destroy_range(this->begin(), this->end());
746 this->setEnd(this->begin());
749 } else if (CurSize) {
750 // Otherwise, use assignment for the already-constructed elements.
751 std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
754 // Copy construct the new elements in place.
755 this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
756 this->begin()+CurSize);
759 this->setEnd(this->begin()+RHSSize);
763 template <typename T>
764 SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
765 // Avoid self-assignment.
766 if (this == &RHS) return *this;
768 // If the RHS isn't small, clear this vector and then steal its buffer.
769 if (!RHS.isSmall()) {
770 this->destroy_range(this->begin(), this->end());
771 if (!this->isSmall()) free(this->begin());
772 this->BeginX = RHS.BeginX;
773 this->EndX = RHS.EndX;
774 this->CapacityX = RHS.CapacityX;
779 // If we already have sufficient space, assign the common elements, then
780 // destroy any excess.
781 size_t RHSSize = RHS.size();
782 size_t CurSize = this->size();
783 if (CurSize >= RHSSize) {
784 // Assign common elements.
785 iterator NewEnd = this->begin();
787 NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
789 // Destroy excess elements and trim the bounds.
790 this->destroy_range(NewEnd, this->end());
791 this->setEnd(NewEnd);
799 // If we have to grow to have enough elements, destroy the current elements.
800 // This allows us to avoid copying them during the grow.
801 // FIXME: this may not actually make any sense if we can efficiently move
803 if (this->capacity() < RHSSize) {
804 // Destroy current elements.
805 this->destroy_range(this->begin(), this->end());
806 this->setEnd(this->begin());
809 } else if (CurSize) {
810 // Otherwise, use assignment for the already-constructed elements.
811 this->move(RHS.begin(), RHS.begin()+CurSize, this->begin());
814 // Move-construct the new elements in place.
815 this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
816 this->begin()+CurSize);
819 this->setEnd(this->begin()+RHSSize);
825 /// Storage for the SmallVector elements which aren't contained in
826 /// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1'
827 /// element is in the base class. This is specialized for the N=1 and N=0 cases
828 /// to avoid allocating unnecessary storage.
829 template <typename T, unsigned N>
830 struct SmallVectorStorage {
831 typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
833 template <typename T> struct SmallVectorStorage<T, 1> {};
834 template <typename T> struct SmallVectorStorage<T, 0> {};
836 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
837 /// for the case when the array is small. It contains some number of elements
838 /// in-place, which allows it to avoid heap allocation when the actual number of
839 /// elements is below that threshold. This allows normal "small" cases to be
840 /// fast without losing generality for large inputs.
842 /// Note that this does not attempt to be exception safe.
844 template <typename T, unsigned N>
845 class SmallVector : public SmallVectorImpl<T> {
846 /// Storage - Inline space for elements which aren't stored in the base class.
847 SmallVectorStorage<T, N> Storage;
849 SmallVector() : SmallVectorImpl<T>(N) {
852 explicit SmallVector(unsigned Size, const T &Value = T())
853 : SmallVectorImpl<T>(N) {
854 this->assign(Size, Value);
857 template<typename ItTy>
858 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
862 template <typename RangeTy>
863 explicit SmallVector(const llvm::iterator_range<RangeTy> R)
864 : SmallVectorImpl<T>(N) {
865 this->append(R.begin(), R.end());
868 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
870 SmallVectorImpl<T>::operator=(RHS);
873 const SmallVector &operator=(const SmallVector &RHS) {
874 SmallVectorImpl<T>::operator=(RHS);
878 SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
880 SmallVectorImpl<T>::operator=(::std::move(RHS));
883 const SmallVector &operator=(SmallVector &&RHS) {
884 SmallVectorImpl<T>::operator=(::std::move(RHS));
889 template<typename T, unsigned N>
890 static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
891 return X.capacity_in_bytes();
894 } // End llvm namespace
897 /// Implement std::swap in terms of SmallVector swap.
900 swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
904 /// Implement std::swap in terms of SmallVector swap.
905 template<typename T, unsigned N>
907 swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {