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/Support/Compiler.h"
18 #include "llvm/Support/type_traits.h"
29 /// SmallVectorBase - This is all the non-templated stuff common to all
31 class SmallVectorBase {
33 void *BeginX, *EndX, *CapacityX;
35 // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
36 // don't want it to be automatically run, so we need to represent the space as
37 // something else. An array of char would work great, but might not be
38 // aligned sufficiently. Instead we use some number of union instances for
39 // the space, which guarantee maximal alignment.
46 // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
49 SmallVectorBase(size_t Size)
50 : BeginX(&FirstEl), EndX(&FirstEl), CapacityX((char*)&FirstEl+Size) {}
52 /// isSmall - Return true if this is a smallvector which has not had dynamic
53 /// memory allocated for it.
54 bool isSmall() const {
55 return BeginX == static_cast<const void*>(&FirstEl);
58 /// resetToSmall - Put this vector in a state of being small.
60 BeginX = EndX = CapacityX = &FirstEl;
63 /// grow_pod - This is an implementation of the grow() method which only works
64 /// on POD-like data types and is out of line to reduce code duplication.
65 void grow_pod(size_t MinSizeInBytes, size_t TSize);
68 /// size_in_bytes - This returns size()*sizeof(T).
69 size_t size_in_bytes() const {
70 return size_t((char*)EndX - (char*)BeginX);
73 /// capacity_in_bytes - This returns capacity()*sizeof(T).
74 size_t capacity_in_bytes() const {
75 return size_t((char*)CapacityX - (char*)BeginX);
78 bool empty() const { return BeginX == EndX; }
83 class SmallVectorTemplateCommon : public SmallVectorBase {
85 SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(Size) {}
87 void setEnd(T *P) { this->EndX = P; }
89 typedef size_t size_type;
90 typedef ptrdiff_t difference_type;
93 typedef const T *const_iterator;
95 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
96 typedef std::reverse_iterator<iterator> reverse_iterator;
99 typedef const T &const_reference;
101 typedef const T *const_pointer;
103 // forward iterator creation methods.
104 iterator begin() { return (iterator)this->BeginX; }
105 const_iterator begin() const { return (const_iterator)this->BeginX; }
106 iterator end() { return (iterator)this->EndX; }
107 const_iterator end() const { return (const_iterator)this->EndX; }
109 iterator capacity_ptr() { return (iterator)this->CapacityX; }
110 const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
113 // reverse iterator creation methods.
114 reverse_iterator rbegin() { return reverse_iterator(end()); }
115 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
116 reverse_iterator rend() { return reverse_iterator(begin()); }
117 const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
119 size_type size() const { return end()-begin(); }
120 size_type max_size() const { return size_type(-1) / sizeof(T); }
122 /// capacity - Return the total number of elements in the currently allocated
124 size_t capacity() const { return capacity_ptr() - begin(); }
126 /// data - Return a pointer to the vector's buffer, even if empty().
127 pointer data() { return pointer(begin()); }
128 /// data - Return a pointer to the vector's buffer, even if empty().
129 const_pointer data() const { return const_pointer(begin()); }
131 reference operator[](unsigned idx) {
132 assert(begin() + idx < end());
135 const_reference operator[](unsigned idx) const {
136 assert(begin() + idx < end());
143 const_reference front() const {
150 const_reference back() const {
155 /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
156 /// implementations that are designed to work with non-POD-like T's.
157 template <typename T, bool isPodLike>
158 class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
160 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
162 static void destroy_range(T *S, T *E) {
169 /// move - Use move-assignment to move the range [I, E) onto the
170 /// objects starting with "Dest". This is just <memory>'s
171 /// std::move, but not all stdlibs actually provide that.
172 template<typename It1, typename It2>
173 static It2 move(It1 I, It1 E, It2 Dest) {
174 #if LLVM_USE_RVALUE_REFERENCES
175 for (; I != E; ++I, ++Dest)
176 *Dest = ::std::move(*I);
179 return ::std::copy(I, E, Dest);
183 /// move_backward - Use move-assignment to move the range
184 /// [I, E) onto the objects ending at "Dest", moving objects
185 /// in reverse order. This is just <algorithm>'s
186 /// std::move_backward, but not all stdlibs actually provide that.
187 template<typename It1, typename It2>
188 static It2 move_backward(It1 I, It1 E, It2 Dest) {
189 #if LLVM_USE_RVALUE_REFERENCES
191 *--Dest = ::std::move(*--E);
194 return ::std::copy_backward(I, E, Dest);
198 /// uninitialized_move - Move the range [I, E) into the uninitialized
199 /// memory starting with "Dest", constructing elements as needed.
200 template<typename It1, typename It2>
201 static void uninitialized_move(It1 I, It1 E, It2 Dest) {
202 #if LLVM_USE_RVALUE_REFERENCES
203 for (; I != E; ++I, ++Dest)
204 ::new ((void*) &*Dest) T(::std::move(*I));
206 ::std::uninitialized_copy(I, E, Dest);
210 /// uninitialized_copy - Copy the range [I, E) onto the uninitialized
211 /// memory starting with "Dest", constructing elements as needed.
212 template<typename It1, typename It2>
213 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
214 std::uninitialized_copy(I, E, Dest);
217 /// grow - Grow the allocated memory (without initializing new
218 /// elements), doubling the size of the allocated memory.
219 /// Guarantees space for at least one more element, or MinSize more
220 /// elements if specified.
221 void grow(size_t MinSize = 0);
224 void push_back(const T &Elt) {
225 if (this->EndX < this->CapacityX) {
227 ::new ((void*) this->end()) T(Elt);
228 this->setEnd(this->end()+1);
235 #if LLVM_USE_RVALUE_REFERENCES
236 void push_back(T &&Elt) {
237 if (this->EndX < this->CapacityX) {
239 ::new ((void*) this->end()) T(::std::move(Elt));
240 this->setEnd(this->end()+1);
249 this->setEnd(this->end()-1);
254 // Define this out-of-line to dissuade the C++ compiler from inlining it.
255 template <typename T, bool isPodLike>
256 void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
257 size_t CurCapacity = this->capacity();
258 size_t CurSize = this->size();
259 size_t NewCapacity = 2*CurCapacity + 1; // Always grow, even from zero.
260 if (NewCapacity < MinSize)
261 NewCapacity = MinSize;
262 T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
264 // Move the elements over.
265 this->uninitialized_move(this->begin(), this->end(), NewElts);
267 // Destroy the original elements.
268 destroy_range(this->begin(), this->end());
270 // If this wasn't grown from the inline copy, deallocate the old space.
271 if (!this->isSmall())
274 this->setEnd(NewElts+CurSize);
275 this->BeginX = NewElts;
276 this->CapacityX = this->begin()+NewCapacity;
280 /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
281 /// implementations that are designed to work with POD-like T's.
282 template <typename T>
283 class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
285 SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
287 // No need to do a destroy loop for POD's.
288 static void destroy_range(T *, T *) {}
290 /// move - Use move-assignment to move the range [I, E) onto the
291 /// objects starting with "Dest". For PODs, this is just memcpy.
292 template<typename It1, typename It2>
293 static It2 move(It1 I, It1 E, It2 Dest) {
294 return ::std::copy(I, E, Dest);
297 /// move_backward - Use move-assignment to move the range
298 /// [I, E) onto the objects ending at "Dest", moving objects
299 /// in reverse order.
300 template<typename It1, typename It2>
301 static It2 move_backward(It1 I, It1 E, It2 Dest) {
302 return ::std::copy_backward(I, E, Dest);
305 /// uninitialized_move - Move 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_move(It1 I, It1 E, It2 Dest) {
310 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 It1, typename It2>
316 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
317 // Arbitrary iterator types; just use the basic implementation.
318 std::uninitialized_copy(I, E, Dest);
321 /// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
322 /// starting with "Dest", constructing elements into it as needed.
323 template<typename T1, typename T2>
324 static void uninitialized_copy(T1 *I, T1 *E, T2 *Dest) {
325 // Use memcpy for PODs iterated by pointers (which includes SmallVector
326 // iterators): std::uninitialized_copy optimizes to memmove, but we can
328 memcpy(Dest, I, (E-I)*sizeof(T));
331 /// grow - 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 (this->EndX < this->CapacityX) {
340 memcpy(this->end(), &Elt, sizeof(T));
341 this->setEnd(this->end()+1);
349 this->setEnd(this->end()-1);
354 /// SmallVectorImpl - This class consists of common code factored out of the
355 /// SmallVector class to reduce code duplication based on the SmallVector 'N'
356 /// template parameter.
357 template <typename T>
358 class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
359 typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
361 SmallVectorImpl(const SmallVectorImpl&); // DISABLED.
363 typedef typename SuperClass::iterator iterator;
364 typedef typename SuperClass::size_type size_type;
367 // Default ctor - Initialize to empty.
368 explicit SmallVectorImpl(unsigned N)
369 : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
374 // Destroy the constructed elements in the vector.
375 this->destroy_range(this->begin(), this->end());
377 // If this wasn't grown from the inline copy, deallocate the old space.
378 if (!this->isSmall())
384 this->destroy_range(this->begin(), this->end());
385 this->EndX = this->BeginX;
388 void resize(unsigned N) {
389 if (N < this->size()) {
390 this->destroy_range(this->begin()+N, this->end());
391 this->setEnd(this->begin()+N);
392 } else if (N > this->size()) {
393 if (this->capacity() < N)
395 std::uninitialized_fill(this->end(), this->begin()+N, T());
396 this->setEnd(this->begin()+N);
400 void resize(unsigned N, const T &NV) {
401 if (N < this->size()) {
402 this->destroy_range(this->begin()+N, this->end());
403 this->setEnd(this->begin()+N);
404 } else if (N > this->size()) {
405 if (this->capacity() < N)
407 std::uninitialized_fill(this->end(), this->begin()+N, NV);
408 this->setEnd(this->begin()+N);
412 void reserve(unsigned N) {
413 if (this->capacity() < N)
418 #if LLVM_USE_RVALUE_REFERENCES
419 T Result = ::std::move(this->back());
421 T Result = this->back();
427 void swap(SmallVectorImpl &RHS);
429 /// append - Add the specified range to the end of the SmallVector.
431 template<typename in_iter>
432 void append(in_iter in_start, in_iter in_end) {
433 size_type NumInputs = std::distance(in_start, in_end);
434 // Grow allocated space if needed.
435 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
436 this->grow(this->size()+NumInputs);
438 // Copy the new elements over.
439 // TODO: NEED To compile time dispatch on whether in_iter is a random access
440 // iterator to use the fast uninitialized_copy.
441 std::uninitialized_copy(in_start, in_end, this->end());
442 this->setEnd(this->end() + NumInputs);
445 /// append - Add the specified range to the end of the SmallVector.
447 void append(size_type NumInputs, const T &Elt) {
448 // Grow allocated space if needed.
449 if (NumInputs > size_type(this->capacity_ptr()-this->end()))
450 this->grow(this->size()+NumInputs);
452 // Copy the new elements over.
453 std::uninitialized_fill_n(this->end(), NumInputs, Elt);
454 this->setEnd(this->end() + NumInputs);
457 void assign(unsigned NumElts, const T &Elt) {
459 if (this->capacity() < NumElts)
461 this->setEnd(this->begin()+NumElts);
462 std::uninitialized_fill(this->begin(), this->end(), Elt);
465 iterator erase(iterator I) {
467 // Shift all elts down one.
468 std::copy(I+1, this->end(), I);
469 // Drop the last elt.
474 iterator erase(iterator S, iterator E) {
476 // Shift all elts down.
477 iterator I = std::copy(E, this->end(), S);
478 // Drop the last elts.
479 this->destroy_range(I, this->end());
484 #if LLVM_USE_RVALUE_REFERENCES
485 iterator insert(iterator I, T &&Elt) {
486 if (I == this->end()) { // Important special case for empty vector.
487 this->push_back(::std::move(Elt));
488 return this->end()-1;
491 if (this->EndX < this->CapacityX) {
493 ::new ((void*) this->end()) T(::std::move(this->back()));
494 this->setEnd(this->end()+1);
495 // Push everything else over.
496 this->move_backward(I, this->end()-1, this->end());
498 // If we just moved the element we're inserting, be sure to update
501 if (I <= EltPtr && EltPtr < this->EndX)
504 *I = ::std::move(*EltPtr);
507 size_t EltNo = I-this->begin();
509 I = this->begin()+EltNo;
514 iterator insert(iterator I, const T &Elt) {
515 if (I == this->end()) { // Important special case for empty vector.
516 this->push_back(Elt);
517 return this->end()-1;
520 if (this->EndX < this->CapacityX) {
522 ::new ((void*) this->end()) T(this->back());
523 this->setEnd(this->end()+1);
524 // Push everything else over.
525 this->move_backward(I, this->end()-1, this->end());
527 // If we just moved the element we're inserting, be sure to update
529 const T *EltPtr = &Elt;
530 if (I <= EltPtr && EltPtr < this->EndX)
536 size_t EltNo = I-this->begin();
538 I = this->begin()+EltNo;
542 iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
543 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
544 size_t InsertElt = I - this->begin();
546 if (I == this->end()) { // Important special case for empty vector.
547 append(NumToInsert, Elt);
548 return this->begin()+InsertElt;
551 // Ensure there is enough space.
552 reserve(static_cast<unsigned>(this->size() + NumToInsert));
554 // Uninvalidate the iterator.
555 I = this->begin()+InsertElt;
557 // If there are more elements between the insertion point and the end of the
558 // range than there are being inserted, we can use a simple approach to
559 // insertion. Since we already reserved space, we know that this won't
560 // reallocate the vector.
561 if (size_t(this->end()-I) >= NumToInsert) {
562 T *OldEnd = this->end();
563 append(this->end()-NumToInsert, 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 // Copy 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_copy(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 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(this->end()-NumToInsert, this->end());
615 // Copy the existing elements that get replaced.
616 this->move_backward(I, OldEnd-NumToInsert, OldEnd);
618 std::copy(From, To, I);
622 // Otherwise, we're inserting more elements than exist already, and we're
623 // not inserting at the end.
625 // Copy over the elements that we're about to overwrite.
626 T *OldEnd = this->end();
627 this->setEnd(this->end() + NumToInsert);
628 size_t NumOverwritten = OldEnd-I;
629 this->uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
631 // Replace the overwritten part.
632 for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
637 // Insert the non-overwritten middle part.
638 this->uninitialized_copy(From, To, OldEnd);
642 SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
644 #if LLVM_USE_RVALUE_REFERENCES
645 SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
648 bool operator==(const SmallVectorImpl &RHS) const {
649 if (this->size() != RHS.size()) return false;
650 return std::equal(this->begin(), this->end(), RHS.begin());
652 bool operator!=(const SmallVectorImpl &RHS) const {
653 return !(*this == RHS);
656 bool operator<(const SmallVectorImpl &RHS) const {
657 return std::lexicographical_compare(this->begin(), this->end(),
658 RHS.begin(), RHS.end());
661 /// set_size - Set the array size to \arg N, which the current array must have
662 /// enough capacity for.
664 /// This does not construct or destroy any elements in the vector.
666 /// Clients can use this in conjunction with capacity() to write past the end
667 /// of the buffer when they know that more elements are available, and only
668 /// update the size later. This avoids the cost of value initializing elements
669 /// which will only be overwritten.
670 void set_size(unsigned N) {
671 assert(N <= this->capacity());
672 this->setEnd(this->begin() + N);
677 template <typename T>
678 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
679 if (this == &RHS) return;
681 // We can only avoid copying elements if neither vector is small.
682 if (!this->isSmall() && !RHS.isSmall()) {
683 std::swap(this->BeginX, RHS.BeginX);
684 std::swap(this->EndX, RHS.EndX);
685 std::swap(this->CapacityX, RHS.CapacityX);
688 if (RHS.size() > this->capacity())
689 this->grow(RHS.size());
690 if (this->size() > RHS.capacity())
691 RHS.grow(this->size());
693 // Swap the shared elements.
694 size_t NumShared = this->size();
695 if (NumShared > RHS.size()) NumShared = RHS.size();
696 for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
697 std::swap((*this)[i], RHS[i]);
699 // Copy over the extra elts.
700 if (this->size() > RHS.size()) {
701 size_t EltDiff = this->size() - RHS.size();
702 this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
703 RHS.setEnd(RHS.end()+EltDiff);
704 this->destroy_range(this->begin()+NumShared, this->end());
705 this->setEnd(this->begin()+NumShared);
706 } else if (RHS.size() > this->size()) {
707 size_t EltDiff = RHS.size() - this->size();
708 this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
709 this->setEnd(this->end() + EltDiff);
710 this->destroy_range(RHS.begin()+NumShared, RHS.end());
711 RHS.setEnd(RHS.begin()+NumShared);
715 template <typename T>
716 SmallVectorImpl<T> &SmallVectorImpl<T>::
717 operator=(const SmallVectorImpl<T> &RHS) {
718 // Avoid self-assignment.
719 if (this == &RHS) return *this;
721 // If we already have sufficient space, assign the common elements, then
722 // destroy any excess.
723 size_t RHSSize = RHS.size();
724 size_t CurSize = this->size();
725 if (CurSize >= RHSSize) {
726 // Assign common elements.
729 NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
731 NewEnd = this->begin();
733 // Destroy excess elements.
734 this->destroy_range(NewEnd, this->end());
737 this->setEnd(NewEnd);
741 // If we have to grow to have enough elements, destroy the current elements.
742 // This allows us to avoid copying them during the grow.
743 // FIXME: don't do this if they're efficiently moveable.
744 if (this->capacity() < RHSSize) {
745 // Destroy current elements.
746 this->destroy_range(this->begin(), this->end());
747 this->setEnd(this->begin());
750 } else if (CurSize) {
751 // Otherwise, use assignment for the already-constructed elements.
752 std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
755 // Copy construct the new elements in place.
756 this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
757 this->begin()+CurSize);
760 this->setEnd(this->begin()+RHSSize);
764 #if LLVM_USE_RVALUE_REFERENCES
765 template <typename T>
766 SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
767 // Avoid self-assignment.
768 if (this == &RHS) return *this;
770 // If the RHS isn't small, clear this vector and then steal its buffer.
771 if (!RHS.isSmall()) {
772 this->destroy_range(this->begin(), this->end());
773 if (!this->isSmall()) free(this->begin());
774 this->BeginX = RHS.BeginX;
775 this->EndX = RHS.EndX;
776 this->CapacityX = RHS.CapacityX;
781 // If we already have sufficient space, assign the common elements, then
782 // destroy any excess.
783 size_t RHSSize = RHS.size();
784 size_t CurSize = this->size();
785 if (CurSize >= RHSSize) {
786 // Assign common elements.
787 iterator NewEnd = this->begin();
789 NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
791 // Destroy excess elements and trim the bounds.
792 this->destroy_range(NewEnd, this->end());
793 this->setEnd(NewEnd);
801 // If we have to grow to have enough elements, destroy the current elements.
802 // This allows us to avoid copying them during the grow.
803 // FIXME: this may not actually make any sense if we can efficiently move
805 if (this->capacity() < RHSSize) {
806 // Destroy current elements.
807 this->destroy_range(this->begin(), this->end());
808 this->setEnd(this->begin());
811 } else if (CurSize) {
812 // Otherwise, use assignment for the already-constructed elements.
813 this->move(RHS.begin(), RHS.end(), this->begin());
816 // Move-construct the new elements in place.
817 this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
818 this->begin()+CurSize);
821 this->setEnd(this->begin()+RHSSize);
828 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
829 /// for the case when the array is small. It contains some number of elements
830 /// in-place, which allows it to avoid heap allocation when the actual number of
831 /// elements is below that threshold. This allows normal "small" cases to be
832 /// fast without losing generality for large inputs.
834 /// Note that this does not attempt to be exception safe.
836 template <typename T, unsigned N>
837 class SmallVector : public SmallVectorImpl<T> {
838 /// InlineElts - These are 'N-1' elements that are stored inline in the body
839 /// of the vector. The extra '1' element is stored in SmallVectorImpl.
840 typedef typename SmallVectorImpl<T>::U U;
842 // MinUs - The number of U's require to cover N T's.
843 MinUs = (static_cast<unsigned int>(sizeof(T))*N +
844 static_cast<unsigned int>(sizeof(U)) - 1) /
845 static_cast<unsigned int>(sizeof(U)),
847 // NumInlineEltsElts - The number of elements actually in this array. There
848 // is already one in the parent class, and we have to round up to avoid
849 // having a zero-element array.
850 NumInlineEltsElts = MinUs > 1 ? (MinUs - 1) : 1,
852 // NumTsAvailable - The number of T's we actually have space for, which may
853 // be more than N due to rounding.
854 NumTsAvailable = (NumInlineEltsElts+1)*static_cast<unsigned int>(sizeof(U))/
855 static_cast<unsigned int>(sizeof(T))
857 U InlineElts[NumInlineEltsElts];
859 SmallVector() : SmallVectorImpl<T>(NumTsAvailable) {
862 explicit SmallVector(unsigned Size, const T &Value = T())
863 : SmallVectorImpl<T>(NumTsAvailable) {
864 this->assign(Size, Value);
867 template<typename ItTy>
868 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(NumTsAvailable) {
872 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(NumTsAvailable) {
874 SmallVectorImpl<T>::operator=(RHS);
877 const SmallVector &operator=(const SmallVector &RHS) {
878 SmallVectorImpl<T>::operator=(RHS);
882 #if LLVM_USE_RVALUE_REFERENCES
883 SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(NumTsAvailable) {
885 SmallVectorImpl<T>::operator=(::std::move(RHS));
888 const SmallVector &operator=(SmallVector &&RHS) {
889 SmallVectorImpl<T>::operator=(::std::move(RHS));
896 /// Specialize SmallVector at N=0. This specialization guarantees
897 /// that it can be instantiated at an incomplete T if none of its
898 /// members are required.
899 template <typename T>
900 class SmallVector<T,0> : public SmallVectorImpl<T> {
902 SmallVector() : SmallVectorImpl<T>(0) {}
904 explicit SmallVector(unsigned Size, const T &Value = T())
905 : SmallVectorImpl<T>(0) {
906 this->assign(Size, Value);
909 template<typename ItTy>
910 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(0) {
914 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(0) {
915 SmallVectorImpl<T>::operator=(RHS);
918 SmallVector &operator=(const SmallVectorImpl<T> &RHS) {
919 return SmallVectorImpl<T>::operator=(RHS);
924 template<typename T, unsigned N>
925 static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
926 return X.capacity_in_bytes();
929 } // End llvm namespace
932 /// Implement std::swap in terms of SmallVector swap.
935 swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
939 /// Implement std::swap in terms of SmallVector swap.
940 template<typename T, unsigned N>
942 swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {