LLVM API Documentation

SmallVector.h
Go to the documentation of this file.
00001 //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the SmallVector class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ADT_SMALLVECTOR_H
00015 #define LLVM_ADT_SMALLVECTOR_H
00016 
00017 #include "llvm/ADT/iterator_range.h"
00018 #include "llvm/Support/AlignOf.h"
00019 #include "llvm/Support/Compiler.h"
00020 #include "llvm/Support/MathExtras.h"
00021 #include "llvm/Support/type_traits.h"
00022 #include <algorithm>
00023 #include <cassert>
00024 #include <cstddef>
00025 #include <cstdlib>
00026 #include <cstring>
00027 #include <initializer_list>
00028 #include <iterator>
00029 #include <memory>
00030 
00031 namespace llvm {
00032 
00033 /// This is all the non-templated stuff common to all SmallVectors.
00034 class SmallVectorBase {
00035 protected:
00036   void *BeginX, *EndX, *CapacityX;
00037 
00038 protected:
00039   SmallVectorBase(void *FirstEl, size_t Size)
00040     : BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl+Size) {}
00041 
00042   /// This is an implementation of the grow() method which only works
00043   /// on POD-like data types and is out of line to reduce code duplication.
00044   void grow_pod(void *FirstEl, size_t MinSizeInBytes, size_t TSize);
00045 
00046 public:
00047   /// This returns size()*sizeof(T).
00048   size_t size_in_bytes() const {
00049     return size_t((char*)EndX - (char*)BeginX);
00050   }
00051 
00052   /// capacity_in_bytes - This returns capacity()*sizeof(T).
00053   size_t capacity_in_bytes() const {
00054     return size_t((char*)CapacityX - (char*)BeginX);
00055   }
00056 
00057   bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return BeginX == EndX; }
00058 };
00059 
00060 template <typename T, unsigned N> struct SmallVectorStorage;
00061 
00062 /// This is the part of SmallVectorTemplateBase which does not depend on whether
00063 /// the type T is a POD. The extra dummy template argument is used by ArrayRef
00064 /// to avoid unnecessarily requiring T to be complete.
00065 template <typename T, typename = void>
00066 class SmallVectorTemplateCommon : public SmallVectorBase {
00067 private:
00068   template <typename, unsigned> friend struct SmallVectorStorage;
00069 
00070   // Allocate raw space for N elements of type T.  If T has a ctor or dtor, we
00071   // don't want it to be automatically run, so we need to represent the space as
00072   // something else.  Use an array of char of sufficient alignment.
00073   typedef llvm::AlignedCharArrayUnion<T> U;
00074   U FirstEl;
00075   // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
00076 
00077 protected:
00078   SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
00079 
00080   void grow_pod(size_t MinSizeInBytes, size_t TSize) {
00081     SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
00082   }
00083 
00084   /// Return true if this is a smallvector which has not had dynamic
00085   /// memory allocated for it.
00086   bool isSmall() const {
00087     return BeginX == static_cast<const void*>(&FirstEl);
00088   }
00089 
00090   /// Put this vector in a state of being small.
00091   void resetToSmall() {
00092     BeginX = EndX = CapacityX = &FirstEl;
00093   }
00094 
00095   void setEnd(T *P) { this->EndX = P; }
00096 public:
00097   typedef size_t size_type;
00098   typedef ptrdiff_t difference_type;
00099   typedef T value_type;
00100   typedef T *iterator;
00101   typedef const T *const_iterator;
00102 
00103   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00104   typedef std::reverse_iterator<iterator> reverse_iterator;
00105 
00106   typedef T &reference;
00107   typedef const T &const_reference;
00108   typedef T *pointer;
00109   typedef const T *const_pointer;
00110 
00111   // forward iterator creation methods.
00112   iterator begin() { return (iterator)this->BeginX; }
00113   const_iterator begin() const { return (const_iterator)this->BeginX; }
00114   iterator end() { return (iterator)this->EndX; }
00115   const_iterator end() const { return (const_iterator)this->EndX; }
00116 protected:
00117   iterator capacity_ptr() { return (iterator)this->CapacityX; }
00118   const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
00119 public:
00120 
00121   // reverse iterator creation methods.
00122   reverse_iterator rbegin()            { return reverse_iterator(end()); }
00123   const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
00124   reverse_iterator rend()              { return reverse_iterator(begin()); }
00125   const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
00126 
00127   size_type size() const { return end()-begin(); }
00128   size_type max_size() const { return size_type(-1) / sizeof(T); }
00129 
00130   /// Return the total number of elements in the currently allocated buffer.
00131   size_t capacity() const { return capacity_ptr() - begin(); }
00132 
00133   /// Return a pointer to the vector's buffer, even if empty().
00134   pointer data() { return pointer(begin()); }
00135   /// Return a pointer to the vector's buffer, even if empty().
00136   const_pointer data() const { return const_pointer(begin()); }
00137 
00138   reference operator[](size_type idx) {
00139     assert(idx < size());
00140     return begin()[idx];
00141   }
00142   const_reference operator[](size_type idx) const {
00143     assert(idx < size());
00144     return begin()[idx];
00145   }
00146 
00147   reference front() {
00148     assert(!empty());
00149     return begin()[0];
00150   }
00151   const_reference front() const {
00152     assert(!empty());
00153     return begin()[0];
00154   }
00155 
00156   reference back() {
00157     assert(!empty());
00158     return end()[-1];
00159   }
00160   const_reference back() const {
00161     assert(!empty());
00162     return end()[-1];
00163   }
00164 };
00165 
00166 /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
00167 /// implementations that are designed to work with non-POD-like T's.
00168 template <typename T, bool isPodLike>
00169 class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
00170 protected:
00171   SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
00172 
00173   static void destroy_range(T *S, T *E) {
00174     while (S != E) {
00175       --E;
00176       E->~T();
00177     }
00178   }
00179 
00180   /// Use move-assignment to move the range [I, E) onto the
00181   /// objects starting with "Dest".  This is just <memory>'s
00182   /// std::move, but not all stdlibs actually provide that.
00183   template<typename It1, typename It2>
00184   static It2 move(It1 I, It1 E, It2 Dest) {
00185     for (; I != E; ++I, ++Dest)
00186       *Dest = ::std::move(*I);
00187     return Dest;
00188   }
00189 
00190   /// Use move-assignment to move the range
00191   /// [I, E) onto the objects ending at "Dest", moving objects
00192   /// in reverse order.  This is just <algorithm>'s
00193   /// std::move_backward, but not all stdlibs actually provide that.
00194   template<typename It1, typename It2>
00195   static It2 move_backward(It1 I, It1 E, It2 Dest) {
00196     while (I != E)
00197       *--Dest = ::std::move(*--E);
00198     return Dest;
00199   }
00200 
00201   /// Move the range [I, E) into the uninitialized memory starting with "Dest",
00202   /// constructing elements as needed.
00203   template<typename It1, typename It2>
00204   static void uninitialized_move(It1 I, It1 E, It2 Dest) {
00205     for (; I != E; ++I, ++Dest)
00206       ::new ((void*) &*Dest) T(::std::move(*I));
00207   }
00208 
00209   /// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
00210   /// constructing elements as needed.
00211   template<typename It1, typename It2>
00212   static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
00213     std::uninitialized_copy(I, E, Dest);
00214   }
00215 
00216   /// Grow the allocated memory (without initializing new elements), doubling
00217   /// the size of the allocated memory. Guarantees space for at least one more
00218   /// element, or MinSize more elements if specified.
00219   void grow(size_t MinSize = 0);
00220 
00221 public:
00222   void push_back(const T &Elt) {
00223     if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
00224       this->grow();
00225     ::new ((void*) this->end()) T(Elt);
00226     this->setEnd(this->end()+1);
00227   }
00228 
00229   void push_back(T &&Elt) {
00230     if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
00231       this->grow();
00232     ::new ((void*) this->end()) T(::std::move(Elt));
00233     this->setEnd(this->end()+1);
00234   }
00235 
00236   void pop_back() {
00237     this->setEnd(this->end()-1);
00238     this->end()->~T();
00239   }
00240 };
00241 
00242 // Define this out-of-line to dissuade the C++ compiler from inlining it.
00243 template <typename T, bool isPodLike>
00244 void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
00245   size_t CurCapacity = this->capacity();
00246   size_t CurSize = this->size();
00247   // Always grow, even from zero.
00248   size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
00249   if (NewCapacity < MinSize)
00250     NewCapacity = MinSize;
00251   T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
00252 
00253   // Move the elements over.
00254   this->uninitialized_move(this->begin(), this->end(), NewElts);
00255 
00256   // Destroy the original elements.
00257   destroy_range(this->begin(), this->end());
00258 
00259   // If this wasn't grown from the inline copy, deallocate the old space.
00260   if (!this->isSmall())
00261     free(this->begin());
00262 
00263   this->setEnd(NewElts+CurSize);
00264   this->BeginX = NewElts;
00265   this->CapacityX = this->begin()+NewCapacity;
00266 }
00267 
00268 
00269 /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
00270 /// implementations that are designed to work with POD-like T's.
00271 template <typename T>
00272 class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
00273 protected:
00274   SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
00275 
00276   // No need to do a destroy loop for POD's.
00277   static void destroy_range(T *, T *) {}
00278 
00279   /// Use move-assignment to move the range [I, E) onto the
00280   /// objects starting with "Dest".  For PODs, this is just memcpy.
00281   template<typename It1, typename It2>
00282   static It2 move(It1 I, It1 E, It2 Dest) {
00283     return ::std::copy(I, E, Dest);
00284   }
00285 
00286   /// Use move-assignment to move the range [I, E) onto the objects ending at
00287   /// "Dest", moving objects in reverse order.
00288   template<typename It1, typename It2>
00289   static It2 move_backward(It1 I, It1 E, It2 Dest) {
00290     return ::std::copy_backward(I, E, Dest);
00291   }
00292 
00293   /// Move the range [I, E) onto the uninitialized memory
00294   /// starting with "Dest", constructing elements into it as needed.
00295   template<typename It1, typename It2>
00296   static void uninitialized_move(It1 I, It1 E, It2 Dest) {
00297     // Just do a copy.
00298     uninitialized_copy(I, E, Dest);
00299   }
00300 
00301   /// Copy the range [I, E) onto the uninitialized memory
00302   /// starting with "Dest", constructing elements into it as needed.
00303   template<typename It1, typename It2>
00304   static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
00305     // Arbitrary iterator types; just use the basic implementation.
00306     std::uninitialized_copy(I, E, Dest);
00307   }
00308 
00309   /// Copy the range [I, E) onto the uninitialized memory
00310   /// starting with "Dest", constructing elements into it as needed.
00311   template <typename T1, typename T2>
00312   static void uninitialized_copy(
00313       T1 *I, T1 *E, T2 *Dest,
00314       typename std::enable_if<std::is_same<typename std::remove_const<T1>::type,
00315                                            T2>::value>::type * = nullptr) {
00316     // Use memcpy for PODs iterated by pointers (which includes SmallVector
00317     // iterators): std::uninitialized_copy optimizes to memmove, but we can
00318     // use memcpy here.
00319     memcpy(Dest, I, (E-I)*sizeof(T));
00320   }
00321 
00322   /// Double the size of the allocated memory, guaranteeing space for at
00323   /// least one more element or MinSize if specified.
00324   void grow(size_t MinSize = 0) {
00325     this->grow_pod(MinSize*sizeof(T), sizeof(T));
00326   }
00327 public:
00328   void push_back(const T &Elt) {
00329     if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
00330       this->grow();
00331     memcpy(this->end(), &Elt, sizeof(T));
00332     this->setEnd(this->end()+1);
00333   }
00334 
00335   void pop_back() {
00336     this->setEnd(this->end()-1);
00337   }
00338 };
00339 
00340 
00341 /// This class consists of common code factored out of the SmallVector class to
00342 /// reduce code duplication based on the SmallVector 'N' template parameter.
00343 template <typename T>
00344 class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
00345   typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
00346 
00347   SmallVectorImpl(const SmallVectorImpl&) = delete;
00348 public:
00349   typedef typename SuperClass::iterator iterator;
00350   typedef typename SuperClass::size_type size_type;
00351 
00352 protected:
00353   // Default ctor - Initialize to empty.
00354   explicit SmallVectorImpl(unsigned N)
00355     : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
00356   }
00357 
00358 public:
00359   ~SmallVectorImpl() {
00360     // Destroy the constructed elements in the vector.
00361     this->destroy_range(this->begin(), this->end());
00362 
00363     // If this wasn't grown from the inline copy, deallocate the old space.
00364     if (!this->isSmall())
00365       free(this->begin());
00366   }
00367 
00368 
00369   void clear() {
00370     this->destroy_range(this->begin(), this->end());
00371     this->EndX = this->BeginX;
00372   }
00373 
00374   void resize(size_type N) {
00375     if (N < this->size()) {
00376       this->destroy_range(this->begin()+N, this->end());
00377       this->setEnd(this->begin()+N);
00378     } else if (N > this->size()) {
00379       if (this->capacity() < N)
00380         this->grow(N);
00381       for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
00382         new (&*I) T();
00383       this->setEnd(this->begin()+N);
00384     }
00385   }
00386 
00387   void resize(size_type N, const T &NV) {
00388     if (N < this->size()) {
00389       this->destroy_range(this->begin()+N, this->end());
00390       this->setEnd(this->begin()+N);
00391     } else if (N > this->size()) {
00392       if (this->capacity() < N)
00393         this->grow(N);
00394       std::uninitialized_fill(this->end(), this->begin()+N, NV);
00395       this->setEnd(this->begin()+N);
00396     }
00397   }
00398 
00399   void reserve(size_type N) {
00400     if (this->capacity() < N)
00401       this->grow(N);
00402   }
00403 
00404   T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
00405     T Result = ::std::move(this->back());
00406     this->pop_back();
00407     return Result;
00408   }
00409 
00410   void swap(SmallVectorImpl &RHS);
00411 
00412   /// Add the specified range to the end of the SmallVector.
00413   template<typename in_iter>
00414   void append(in_iter in_start, in_iter in_end) {
00415     size_type NumInputs = std::distance(in_start, in_end);
00416     // Grow allocated space if needed.
00417     if (NumInputs > size_type(this->capacity_ptr()-this->end()))
00418       this->grow(this->size()+NumInputs);
00419 
00420     // Copy the new elements over.
00421     this->uninitialized_copy(in_start, in_end, this->end());
00422     this->setEnd(this->end() + NumInputs);
00423   }
00424 
00425   /// Add the specified range to the end of the SmallVector.
00426   void append(size_type NumInputs, const T &Elt) {
00427     // Grow allocated space if needed.
00428     if (NumInputs > size_type(this->capacity_ptr()-this->end()))
00429       this->grow(this->size()+NumInputs);
00430 
00431     // Copy the new elements over.
00432     std::uninitialized_fill_n(this->end(), NumInputs, Elt);
00433     this->setEnd(this->end() + NumInputs);
00434   }
00435 
00436   void append(std::initializer_list<T> IL) {
00437     append(IL.begin(), IL.end());
00438   }
00439 
00440   void assign(size_type NumElts, const T &Elt) {
00441     clear();
00442     if (this->capacity() < NumElts)
00443       this->grow(NumElts);
00444     this->setEnd(this->begin()+NumElts);
00445     std::uninitialized_fill(this->begin(), this->end(), Elt);
00446   }
00447 
00448   void assign(std::initializer_list<T> IL) {
00449     clear();
00450     append(IL);
00451   }
00452 
00453   iterator erase(iterator I) {
00454     assert(I >= this->begin() && "Iterator to erase is out of bounds.");
00455     assert(I < this->end() && "Erasing at past-the-end iterator.");
00456 
00457     iterator N = I;
00458     // Shift all elts down one.
00459     this->move(I+1, this->end(), I);
00460     // Drop the last elt.
00461     this->pop_back();
00462     return(N);
00463   }
00464 
00465   iterator erase(iterator S, iterator E) {
00466     assert(S >= this->begin() && "Range to erase is out of bounds.");
00467     assert(S <= E && "Trying to erase invalid range.");
00468     assert(E <= this->end() && "Trying to erase past the end.");
00469 
00470     iterator N = S;
00471     // Shift all elts down.
00472     iterator I = this->move(E, this->end(), S);
00473     // Drop the last elts.
00474     this->destroy_range(I, this->end());
00475     this->setEnd(I);
00476     return(N);
00477   }
00478 
00479   iterator insert(iterator I, T &&Elt) {
00480     if (I == this->end()) {  // Important special case for empty vector.
00481       this->push_back(::std::move(Elt));
00482       return this->end()-1;
00483     }
00484 
00485     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00486     assert(I <= this->end() && "Inserting past the end of the vector.");
00487 
00488     if (this->EndX >= this->CapacityX) {
00489       size_t EltNo = I-this->begin();
00490       this->grow();
00491       I = this->begin()+EltNo;
00492     }
00493 
00494     ::new ((void*) this->end()) T(::std::move(this->back()));
00495     // Push everything else over.
00496     this->move_backward(I, this->end()-1, this->end());
00497     this->setEnd(this->end()+1);
00498 
00499     // If we just moved the element we're inserting, be sure to update
00500     // the reference.
00501     T *EltPtr = &Elt;
00502     if (I <= EltPtr && EltPtr < this->EndX)
00503       ++EltPtr;
00504 
00505     *I = ::std::move(*EltPtr);
00506     return I;
00507   }
00508 
00509   iterator insert(iterator I, const T &Elt) {
00510     if (I == this->end()) {  // Important special case for empty vector.
00511       this->push_back(Elt);
00512       return this->end()-1;
00513     }
00514 
00515     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00516     assert(I <= this->end() && "Inserting past the end of the vector.");
00517 
00518     if (this->EndX >= this->CapacityX) {
00519       size_t EltNo = I-this->begin();
00520       this->grow();
00521       I = this->begin()+EltNo;
00522     }
00523     ::new ((void*) this->end()) T(std::move(this->back()));
00524     // Push everything else over.
00525     this->move_backward(I, this->end()-1, this->end());
00526     this->setEnd(this->end()+1);
00527 
00528     // If we just moved the element we're inserting, be sure to update
00529     // the reference.
00530     const T *EltPtr = &Elt;
00531     if (I <= EltPtr && EltPtr < this->EndX)
00532       ++EltPtr;
00533 
00534     *I = *EltPtr;
00535     return I;
00536   }
00537 
00538   iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
00539     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
00540     size_t InsertElt = I - this->begin();
00541 
00542     if (I == this->end()) {  // Important special case for empty vector.
00543       append(NumToInsert, Elt);
00544       return this->begin()+InsertElt;
00545     }
00546 
00547     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00548     assert(I <= this->end() && "Inserting past the end of the vector.");
00549 
00550     // Ensure there is enough space.
00551     reserve(this->size() + NumToInsert);
00552 
00553     // Uninvalidate the iterator.
00554     I = this->begin()+InsertElt;
00555 
00556     // If there are more elements between the insertion point and the end of the
00557     // range than there are being inserted, we can use a simple approach to
00558     // insertion.  Since we already reserved space, we know that this won't
00559     // reallocate the vector.
00560     if (size_t(this->end()-I) >= NumToInsert) {
00561       T *OldEnd = this->end();
00562       append(std::move_iterator<iterator>(this->end() - NumToInsert),
00563              std::move_iterator<iterator>(this->end()));
00564 
00565       // Copy the existing elements that get replaced.
00566       this->move_backward(I, OldEnd-NumToInsert, OldEnd);
00567 
00568       std::fill_n(I, NumToInsert, Elt);
00569       return I;
00570     }
00571 
00572     // Otherwise, we're inserting more elements than exist already, and we're
00573     // not inserting at the end.
00574 
00575     // Move over the elements that we're about to overwrite.
00576     T *OldEnd = this->end();
00577     this->setEnd(this->end() + NumToInsert);
00578     size_t NumOverwritten = OldEnd-I;
00579     this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
00580 
00581     // Replace the overwritten part.
00582     std::fill_n(I, NumOverwritten, Elt);
00583 
00584     // Insert the non-overwritten middle part.
00585     std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
00586     return I;
00587   }
00588 
00589   template<typename ItTy>
00590   iterator insert(iterator I, ItTy From, ItTy To) {
00591     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
00592     size_t InsertElt = I - this->begin();
00593 
00594     if (I == this->end()) {  // Important special case for empty vector.
00595       append(From, To);
00596       return this->begin()+InsertElt;
00597     }
00598 
00599     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00600     assert(I <= this->end() && "Inserting past the end of the vector.");
00601 
00602     size_t NumToInsert = std::distance(From, To);
00603 
00604     // Ensure there is enough space.
00605     reserve(this->size() + NumToInsert);
00606 
00607     // Uninvalidate the iterator.
00608     I = this->begin()+InsertElt;
00609 
00610     // If there are more elements between the insertion point and the end of the
00611     // range than there are being inserted, we can use a simple approach to
00612     // insertion.  Since we already reserved space, we know that this won't
00613     // reallocate the vector.
00614     if (size_t(this->end()-I) >= NumToInsert) {
00615       T *OldEnd = this->end();
00616       append(std::move_iterator<iterator>(this->end() - NumToInsert),
00617              std::move_iterator<iterator>(this->end()));
00618 
00619       // Copy the existing elements that get replaced.
00620       this->move_backward(I, OldEnd-NumToInsert, OldEnd);
00621 
00622       std::copy(From, To, I);
00623       return I;
00624     }
00625 
00626     // Otherwise, we're inserting more elements than exist already, and we're
00627     // not inserting at the end.
00628 
00629     // Move over the elements that we're about to overwrite.
00630     T *OldEnd = this->end();
00631     this->setEnd(this->end() + NumToInsert);
00632     size_t NumOverwritten = OldEnd-I;
00633     this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
00634 
00635     // Replace the overwritten part.
00636     for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
00637       *J = *From;
00638       ++J; ++From;
00639     }
00640 
00641     // Insert the non-overwritten middle part.
00642     this->uninitialized_copy(From, To, OldEnd);
00643     return I;
00644   }
00645 
00646   void insert(iterator I, std::initializer_list<T> IL) {
00647     insert(I, IL.begin(), IL.end());
00648   }
00649 
00650   template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
00651     if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
00652       this->grow();
00653     ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
00654     this->setEnd(this->end() + 1);
00655   }
00656 
00657   SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
00658 
00659   SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
00660 
00661   bool operator==(const SmallVectorImpl &RHS) const {
00662     if (this->size() != RHS.size()) return false;
00663     return std::equal(this->begin(), this->end(), RHS.begin());
00664   }
00665   bool operator!=(const SmallVectorImpl &RHS) const {
00666     return !(*this == RHS);
00667   }
00668 
00669   bool operator<(const SmallVectorImpl &RHS) const {
00670     return std::lexicographical_compare(this->begin(), this->end(),
00671                                         RHS.begin(), RHS.end());
00672   }
00673 
00674   /// Set the array size to \p N, which the current array must have enough
00675   /// capacity for.
00676   ///
00677   /// This does not construct or destroy any elements in the vector.
00678   ///
00679   /// Clients can use this in conjunction with capacity() to write past the end
00680   /// of the buffer when they know that more elements are available, and only
00681   /// update the size later. This avoids the cost of value initializing elements
00682   /// which will only be overwritten.
00683   void set_size(size_type N) {
00684     assert(N <= this->capacity());
00685     this->setEnd(this->begin() + N);
00686   }
00687 };
00688 
00689 
00690 template <typename T>
00691 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
00692   if (this == &RHS) return;
00693 
00694   // We can only avoid copying elements if neither vector is small.
00695   if (!this->isSmall() && !RHS.isSmall()) {
00696     std::swap(this->BeginX, RHS.BeginX);
00697     std::swap(this->EndX, RHS.EndX);
00698     std::swap(this->CapacityX, RHS.CapacityX);
00699     return;
00700   }
00701   if (RHS.size() > this->capacity())
00702     this->grow(RHS.size());
00703   if (this->size() > RHS.capacity())
00704     RHS.grow(this->size());
00705 
00706   // Swap the shared elements.
00707   size_t NumShared = this->size();
00708   if (NumShared > RHS.size()) NumShared = RHS.size();
00709   for (size_type i = 0; i != NumShared; ++i)
00710     std::swap((*this)[i], RHS[i]);
00711 
00712   // Copy over the extra elts.
00713   if (this->size() > RHS.size()) {
00714     size_t EltDiff = this->size() - RHS.size();
00715     this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
00716     RHS.setEnd(RHS.end()+EltDiff);
00717     this->destroy_range(this->begin()+NumShared, this->end());
00718     this->setEnd(this->begin()+NumShared);
00719   } else if (RHS.size() > this->size()) {
00720     size_t EltDiff = RHS.size() - this->size();
00721     this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
00722     this->setEnd(this->end() + EltDiff);
00723     this->destroy_range(RHS.begin()+NumShared, RHS.end());
00724     RHS.setEnd(RHS.begin()+NumShared);
00725   }
00726 }
00727 
00728 template <typename T>
00729 SmallVectorImpl<T> &SmallVectorImpl<T>::
00730   operator=(const SmallVectorImpl<T> &RHS) {
00731   // Avoid self-assignment.
00732   if (this == &RHS) return *this;
00733 
00734   // If we already have sufficient space, assign the common elements, then
00735   // destroy any excess.
00736   size_t RHSSize = RHS.size();
00737   size_t CurSize = this->size();
00738   if (CurSize >= RHSSize) {
00739     // Assign common elements.
00740     iterator NewEnd;
00741     if (RHSSize)
00742       NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
00743     else
00744       NewEnd = this->begin();
00745 
00746     // Destroy excess elements.
00747     this->destroy_range(NewEnd, this->end());
00748 
00749     // Trim.
00750     this->setEnd(NewEnd);
00751     return *this;
00752   }
00753 
00754   // If we have to grow to have enough elements, destroy the current elements.
00755   // This allows us to avoid copying them during the grow.
00756   // FIXME: don't do this if they're efficiently moveable.
00757   if (this->capacity() < RHSSize) {
00758     // Destroy current elements.
00759     this->destroy_range(this->begin(), this->end());
00760     this->setEnd(this->begin());
00761     CurSize = 0;
00762     this->grow(RHSSize);
00763   } else if (CurSize) {
00764     // Otherwise, use assignment for the already-constructed elements.
00765     std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
00766   }
00767 
00768   // Copy construct the new elements in place.
00769   this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
00770                            this->begin()+CurSize);
00771 
00772   // Set end.
00773   this->setEnd(this->begin()+RHSSize);
00774   return *this;
00775 }
00776 
00777 template <typename T>
00778 SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
00779   // Avoid self-assignment.
00780   if (this == &RHS) return *this;
00781 
00782   // If the RHS isn't small, clear this vector and then steal its buffer.
00783   if (!RHS.isSmall()) {
00784     this->destroy_range(this->begin(), this->end());
00785     if (!this->isSmall()) free(this->begin());
00786     this->BeginX = RHS.BeginX;
00787     this->EndX = RHS.EndX;
00788     this->CapacityX = RHS.CapacityX;
00789     RHS.resetToSmall();
00790     return *this;
00791   }
00792 
00793   // If we already have sufficient space, assign the common elements, then
00794   // destroy any excess.
00795   size_t RHSSize = RHS.size();
00796   size_t CurSize = this->size();
00797   if (CurSize >= RHSSize) {
00798     // Assign common elements.
00799     iterator NewEnd = this->begin();
00800     if (RHSSize)
00801       NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
00802 
00803     // Destroy excess elements and trim the bounds.
00804     this->destroy_range(NewEnd, this->end());
00805     this->setEnd(NewEnd);
00806 
00807     // Clear the RHS.
00808     RHS.clear();
00809 
00810     return *this;
00811   }
00812 
00813   // If we have to grow to have enough elements, destroy the current elements.
00814   // This allows us to avoid copying them during the grow.
00815   // FIXME: this may not actually make any sense if we can efficiently move
00816   // elements.
00817   if (this->capacity() < RHSSize) {
00818     // Destroy current elements.
00819     this->destroy_range(this->begin(), this->end());
00820     this->setEnd(this->begin());
00821     CurSize = 0;
00822     this->grow(RHSSize);
00823   } else if (CurSize) {
00824     // Otherwise, use assignment for the already-constructed elements.
00825     this->move(RHS.begin(), RHS.begin()+CurSize, this->begin());
00826   }
00827 
00828   // Move-construct the new elements in place.
00829   this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
00830                            this->begin()+CurSize);
00831 
00832   // Set end.
00833   this->setEnd(this->begin()+RHSSize);
00834 
00835   RHS.clear();
00836   return *this;
00837 }
00838 
00839 /// Storage for the SmallVector elements which aren't contained in
00840 /// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1'
00841 /// element is in the base class. This is specialized for the N=1 and N=0 cases
00842 /// to avoid allocating unnecessary storage.
00843 template <typename T, unsigned N>
00844 struct SmallVectorStorage {
00845   typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
00846 };
00847 template <typename T> struct SmallVectorStorage<T, 1> {};
00848 template <typename T> struct SmallVectorStorage<T, 0> {};
00849 
00850 /// This is a 'vector' (really, a variable-sized array), optimized
00851 /// for the case when the array is small.  It contains some number of elements
00852 /// in-place, which allows it to avoid heap allocation when the actual number of
00853 /// elements is below that threshold.  This allows normal "small" cases to be
00854 /// fast without losing generality for large inputs.
00855 ///
00856 /// Note that this does not attempt to be exception safe.
00857 ///
00858 template <typename T, unsigned N>
00859 class SmallVector : public SmallVectorImpl<T> {
00860   /// Inline space for elements which aren't stored in the base class.
00861   SmallVectorStorage<T, N> Storage;
00862 public:
00863   SmallVector() : SmallVectorImpl<T>(N) {
00864   }
00865 
00866   explicit SmallVector(size_t Size, const T &Value = T())
00867     : SmallVectorImpl<T>(N) {
00868     this->assign(Size, Value);
00869   }
00870 
00871   template<typename ItTy>
00872   SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
00873     this->append(S, E);
00874   }
00875 
00876   template <typename RangeTy>
00877   explicit SmallVector(const llvm::iterator_range<RangeTy> R)
00878       : SmallVectorImpl<T>(N) {
00879     this->append(R.begin(), R.end());
00880   }
00881 
00882   SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
00883     this->assign(IL);
00884   }
00885 
00886   SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
00887     if (!RHS.empty())
00888       SmallVectorImpl<T>::operator=(RHS);
00889   }
00890 
00891   const SmallVector &operator=(const SmallVector &RHS) {
00892     SmallVectorImpl<T>::operator=(RHS);
00893     return *this;
00894   }
00895 
00896   SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
00897     if (!RHS.empty())
00898       SmallVectorImpl<T>::operator=(::std::move(RHS));
00899   }
00900 
00901   const SmallVector &operator=(SmallVector &&RHS) {
00902     SmallVectorImpl<T>::operator=(::std::move(RHS));
00903     return *this;
00904   }
00905 
00906   SmallVector(SmallVectorImpl<T> &&RHS) : SmallVectorImpl<T>(N) {
00907     if (!RHS.empty())
00908       SmallVectorImpl<T>::operator=(::std::move(RHS));
00909   }
00910 
00911   const SmallVector &operator=(SmallVectorImpl<T> &&RHS) {
00912     SmallVectorImpl<T>::operator=(::std::move(RHS));
00913     return *this;
00914   }
00915 
00916   const SmallVector &operator=(std::initializer_list<T> IL) {
00917     this->assign(IL);
00918     return *this;
00919   }
00920 };
00921 
00922 template<typename T, unsigned N>
00923 static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
00924   return X.capacity_in_bytes();
00925 }
00926 
00927 } // End llvm namespace
00928 
00929 namespace std {
00930   /// Implement std::swap in terms of SmallVector swap.
00931   template<typename T>
00932   inline void
00933   swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
00934     LHS.swap(RHS);
00935   }
00936 
00937   /// Implement std::swap in terms of SmallVector swap.
00938   template<typename T, unsigned N>
00939   inline void
00940   swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {
00941     LHS.swap(RHS);
00942   }
00943 }
00944 
00945 #endif