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