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 (LLVM_UNLIKELY(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 (LLVM_UNLIKELY(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 (LLVM_UNLIKELY(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       for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
00384         new (&*I) T();
00385       this->setEnd(this->begin()+N);
00386     }
00387   }
00388 
00389   void resize(unsigned N, const T &NV) {
00390     if (N < this->size()) {
00391       this->destroy_range(this->begin()+N, this->end());
00392       this->setEnd(this->begin()+N);
00393     } else if (N > this->size()) {
00394       if (this->capacity() < N)
00395         this->grow(N);
00396       std::uninitialized_fill(this->end(), this->begin()+N, NV);
00397       this->setEnd(this->begin()+N);
00398     }
00399   }
00400 
00401   void reserve(unsigned N) {
00402     if (this->capacity() < N)
00403       this->grow(N);
00404   }
00405 
00406   T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
00407     T Result = ::std::move(this->back());
00408     this->pop_back();
00409     return Result;
00410   }
00411 
00412   void swap(SmallVectorImpl &RHS);
00413 
00414   /// append - Add the specified range to the end of the SmallVector.
00415   ///
00416   template<typename in_iter>
00417   void append(in_iter in_start, in_iter in_end) {
00418     size_type NumInputs = std::distance(in_start, in_end);
00419     // Grow allocated space if needed.
00420     if (NumInputs > size_type(this->capacity_ptr()-this->end()))
00421       this->grow(this->size()+NumInputs);
00422 
00423     // Copy the new elements over.
00424     // TODO: NEED To compile time dispatch on whether in_iter is a random access
00425     // iterator to use the fast uninitialized_copy.
00426     std::uninitialized_copy(in_start, in_end, this->end());
00427     this->setEnd(this->end() + NumInputs);
00428   }
00429 
00430   /// append - Add the specified range to the end of the SmallVector.
00431   ///
00432   void append(size_type NumInputs, const T &Elt) {
00433     // Grow allocated space if needed.
00434     if (NumInputs > size_type(this->capacity_ptr()-this->end()))
00435       this->grow(this->size()+NumInputs);
00436 
00437     // Copy the new elements over.
00438     std::uninitialized_fill_n(this->end(), NumInputs, Elt);
00439     this->setEnd(this->end() + NumInputs);
00440   }
00441 
00442   void assign(unsigned NumElts, const T &Elt) {
00443     clear();
00444     if (this->capacity() < NumElts)
00445       this->grow(NumElts);
00446     this->setEnd(this->begin()+NumElts);
00447     std::uninitialized_fill(this->begin(), this->end(), Elt);
00448   }
00449 
00450   iterator erase(iterator I) {
00451     assert(I >= this->begin() && "Iterator to erase is out of bounds.");
00452     assert(I < this->end() && "Erasing at past-the-end iterator.");
00453 
00454     iterator N = I;
00455     // Shift all elts down one.
00456     this->move(I+1, this->end(), I);
00457     // Drop the last elt.
00458     this->pop_back();
00459     return(N);
00460   }
00461 
00462   iterator erase(iterator S, iterator E) {
00463     assert(S >= this->begin() && "Range to erase is out of bounds.");
00464     assert(S <= E && "Trying to erase invalid range.");
00465     assert(E <= this->end() && "Trying to erase past the end.");
00466 
00467     iterator N = S;
00468     // Shift all elts down.
00469     iterator I = this->move(E, this->end(), S);
00470     // Drop the last elts.
00471     this->destroy_range(I, this->end());
00472     this->setEnd(I);
00473     return(N);
00474   }
00475 
00476   iterator insert(iterator I, T &&Elt) {
00477     if (I == this->end()) {  // Important special case for empty vector.
00478       this->push_back(::std::move(Elt));
00479       return this->end()-1;
00480     }
00481 
00482     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00483     assert(I <= this->end() && "Inserting past the end of the vector.");
00484 
00485     if (this->EndX >= this->CapacityX) {
00486       size_t EltNo = I-this->begin();
00487       this->grow();
00488       I = this->begin()+EltNo;
00489     }
00490 
00491     ::new ((void*) this->end()) T(::std::move(this->back()));
00492     // Push everything else over.
00493     this->move_backward(I, this->end()-1, this->end());
00494     this->setEnd(this->end()+1);
00495 
00496     // If we just moved the element we're inserting, be sure to update
00497     // the reference.
00498     T *EltPtr = &Elt;
00499     if (I <= EltPtr && EltPtr < this->EndX)
00500       ++EltPtr;
00501 
00502     *I = ::std::move(*EltPtr);
00503     return I;
00504   }
00505 
00506   iterator insert(iterator I, const T &Elt) {
00507     if (I == this->end()) {  // Important special case for empty vector.
00508       this->push_back(Elt);
00509       return this->end()-1;
00510     }
00511 
00512     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00513     assert(I <= this->end() && "Inserting past the end of the vector.");
00514 
00515     if (this->EndX >= this->CapacityX) {
00516       size_t EltNo = I-this->begin();
00517       this->grow();
00518       I = this->begin()+EltNo;
00519     }
00520     ::new ((void*) this->end()) T(std::move(this->back()));
00521     // Push everything else over.
00522     this->move_backward(I, this->end()-1, this->end());
00523     this->setEnd(this->end()+1);
00524 
00525     // If we just moved the element we're inserting, be sure to update
00526     // the reference.
00527     const T *EltPtr = &Elt;
00528     if (I <= EltPtr && EltPtr < this->EndX)
00529       ++EltPtr;
00530 
00531     *I = *EltPtr;
00532     return I;
00533   }
00534 
00535   iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
00536     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
00537     size_t InsertElt = I - this->begin();
00538 
00539     if (I == this->end()) {  // Important special case for empty vector.
00540       append(NumToInsert, Elt);
00541       return this->begin()+InsertElt;
00542     }
00543 
00544     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00545     assert(I <= this->end() && "Inserting past the end of the vector.");
00546 
00547     // Ensure there is enough space.
00548     reserve(static_cast<unsigned>(this->size() + NumToInsert));
00549 
00550     // Uninvalidate the iterator.
00551     I = this->begin()+InsertElt;
00552 
00553     // If there are more elements between the insertion point and the end of the
00554     // range than there are being inserted, we can use a simple approach to
00555     // insertion.  Since we already reserved space, we know that this won't
00556     // reallocate the vector.
00557     if (size_t(this->end()-I) >= NumToInsert) {
00558       T *OldEnd = this->end();
00559       append(std::move_iterator<iterator>(this->end() - NumToInsert),
00560              std::move_iterator<iterator>(this->end()));
00561 
00562       // Copy the existing elements that get replaced.
00563       this->move_backward(I, OldEnd-NumToInsert, OldEnd);
00564 
00565       std::fill_n(I, NumToInsert, Elt);
00566       return I;
00567     }
00568 
00569     // Otherwise, we're inserting more elements than exist already, and we're
00570     // not inserting at the end.
00571 
00572     // Move over the elements that we're about to overwrite.
00573     T *OldEnd = this->end();
00574     this->setEnd(this->end() + NumToInsert);
00575     size_t NumOverwritten = OldEnd-I;
00576     this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
00577 
00578     // Replace the overwritten part.
00579     std::fill_n(I, NumOverwritten, Elt);
00580 
00581     // Insert the non-overwritten middle part.
00582     std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
00583     return I;
00584   }
00585 
00586   template<typename ItTy>
00587   iterator insert(iterator I, ItTy From, ItTy To) {
00588     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
00589     size_t InsertElt = I - this->begin();
00590 
00591     if (I == this->end()) {  // Important special case for empty vector.
00592       append(From, To);
00593       return this->begin()+InsertElt;
00594     }
00595 
00596     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
00597     assert(I <= this->end() && "Inserting past the end of the vector.");
00598 
00599     size_t NumToInsert = std::distance(From, To);
00600 
00601     // Ensure there is enough space.
00602     reserve(static_cast<unsigned>(this->size() + NumToInsert));
00603 
00604     // Uninvalidate the iterator.
00605     I = this->begin()+InsertElt;
00606 
00607     // If there are more elements between the insertion point and the end of the
00608     // range than there are being inserted, we can use a simple approach to
00609     // insertion.  Since we already reserved space, we know that this won't
00610     // reallocate the vector.
00611     if (size_t(this->end()-I) >= NumToInsert) {
00612       T *OldEnd = this->end();
00613       append(std::move_iterator<iterator>(this->end() - NumToInsert),
00614              std::move_iterator<iterator>(this->end()));
00615 
00616       // Copy the existing elements that get replaced.
00617       this->move_backward(I, OldEnd-NumToInsert, OldEnd);
00618 
00619       std::copy(From, To, I);
00620       return I;
00621     }
00622 
00623     // Otherwise, we're inserting more elements than exist already, and we're
00624     // not inserting at the end.
00625 
00626     // Move over the elements that we're about to overwrite.
00627     T *OldEnd = this->end();
00628     this->setEnd(this->end() + NumToInsert);
00629     size_t NumOverwritten = OldEnd-I;
00630     this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
00631 
00632     // Replace the overwritten part.
00633     for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
00634       *J = *From;
00635       ++J; ++From;
00636     }
00637 
00638     // Insert the non-overwritten middle part.
00639     this->uninitialized_copy(From, To, OldEnd);
00640     return I;
00641   }
00642 
00643   SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
00644 
00645   SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
00646 
00647   bool operator==(const SmallVectorImpl &RHS) const {
00648     if (this->size() != RHS.size()) return false;
00649     return std::equal(this->begin(), this->end(), RHS.begin());
00650   }
00651   bool operator!=(const SmallVectorImpl &RHS) const {
00652     return !(*this == RHS);
00653   }
00654 
00655   bool operator<(const SmallVectorImpl &RHS) const {
00656     return std::lexicographical_compare(this->begin(), this->end(),
00657                                         RHS.begin(), RHS.end());
00658   }
00659 
00660   /// Set the array size to \p N, which the current array must have enough
00661   /// capacity for.
00662   ///
00663   /// This does not construct or destroy any elements in the vector.
00664   ///
00665   /// Clients can use this in conjunction with capacity() to write past the end
00666   /// of the buffer when they know that more elements are available, and only
00667   /// update the size later. This avoids the cost of value initializing elements
00668   /// which will only be overwritten.
00669   void set_size(unsigned N) {
00670     assert(N <= this->capacity());
00671     this->setEnd(this->begin() + N);
00672   }
00673 };
00674 
00675 
00676 template <typename T>
00677 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
00678   if (this == &RHS) return;
00679 
00680   // We can only avoid copying elements if neither vector is small.
00681   if (!this->isSmall() && !RHS.isSmall()) {
00682     std::swap(this->BeginX, RHS.BeginX);
00683     std::swap(this->EndX, RHS.EndX);
00684     std::swap(this->CapacityX, RHS.CapacityX);
00685     return;
00686   }
00687   if (RHS.size() > this->capacity())
00688     this->grow(RHS.size());
00689   if (this->size() > RHS.capacity())
00690     RHS.grow(this->size());
00691 
00692   // Swap the shared elements.
00693   size_t NumShared = this->size();
00694   if (NumShared > RHS.size()) NumShared = RHS.size();
00695   for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
00696     std::swap((*this)[i], RHS[i]);
00697 
00698   // Copy over the extra elts.
00699   if (this->size() > RHS.size()) {
00700     size_t EltDiff = this->size() - RHS.size();
00701     this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
00702     RHS.setEnd(RHS.end()+EltDiff);
00703     this->destroy_range(this->begin()+NumShared, this->end());
00704     this->setEnd(this->begin()+NumShared);
00705   } else if (RHS.size() > this->size()) {
00706     size_t EltDiff = RHS.size() - this->size();
00707     this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
00708     this->setEnd(this->end() + EltDiff);
00709     this->destroy_range(RHS.begin()+NumShared, RHS.end());
00710     RHS.setEnd(RHS.begin()+NumShared);
00711   }
00712 }
00713 
00714 template <typename T>
00715 SmallVectorImpl<T> &SmallVectorImpl<T>::
00716   operator=(const SmallVectorImpl<T> &RHS) {
00717   // Avoid self-assignment.
00718   if (this == &RHS) return *this;
00719 
00720   // If we already have sufficient space, assign the common elements, then
00721   // destroy any excess.
00722   size_t RHSSize = RHS.size();
00723   size_t CurSize = this->size();
00724   if (CurSize >= RHSSize) {
00725     // Assign common elements.
00726     iterator NewEnd;
00727     if (RHSSize)
00728       NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
00729     else
00730       NewEnd = this->begin();
00731 
00732     // Destroy excess elements.
00733     this->destroy_range(NewEnd, this->end());
00734 
00735     // Trim.
00736     this->setEnd(NewEnd);
00737     return *this;
00738   }
00739 
00740   // If we have to grow to have enough elements, destroy the current elements.
00741   // This allows us to avoid copying them during the grow.
00742   // FIXME: don't do this if they're efficiently moveable.
00743   if (this->capacity() < RHSSize) {
00744     // Destroy current elements.
00745     this->destroy_range(this->begin(), this->end());
00746     this->setEnd(this->begin());
00747     CurSize = 0;
00748     this->grow(RHSSize);
00749   } else if (CurSize) {
00750     // Otherwise, use assignment for the already-constructed elements.
00751     std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
00752   }
00753 
00754   // Copy construct the new elements in place.
00755   this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
00756                            this->begin()+CurSize);
00757 
00758   // Set end.
00759   this->setEnd(this->begin()+RHSSize);
00760   return *this;
00761 }
00762 
00763 template <typename T>
00764 SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
00765   // Avoid self-assignment.
00766   if (this == &RHS) return *this;
00767 
00768   // If the RHS isn't small, clear this vector and then steal its buffer.
00769   if (!RHS.isSmall()) {
00770     this->destroy_range(this->begin(), this->end());
00771     if (!this->isSmall()) free(this->begin());
00772     this->BeginX = RHS.BeginX;
00773     this->EndX = RHS.EndX;
00774     this->CapacityX = RHS.CapacityX;
00775     RHS.resetToSmall();
00776     return *this;
00777   }
00778 
00779   // If we already have sufficient space, assign the common elements, then
00780   // destroy any excess.
00781   size_t RHSSize = RHS.size();
00782   size_t CurSize = this->size();
00783   if (CurSize >= RHSSize) {
00784     // Assign common elements.
00785     iterator NewEnd = this->begin();
00786     if (RHSSize)
00787       NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
00788 
00789     // Destroy excess elements and trim the bounds.
00790     this->destroy_range(NewEnd, this->end());
00791     this->setEnd(NewEnd);
00792 
00793     // Clear the RHS.
00794     RHS.clear();
00795 
00796     return *this;
00797   }
00798 
00799   // If we have to grow to have enough elements, destroy the current elements.
00800   // This allows us to avoid copying them during the grow.
00801   // FIXME: this may not actually make any sense if we can efficiently move
00802   // elements.
00803   if (this->capacity() < RHSSize) {
00804     // Destroy current elements.
00805     this->destroy_range(this->begin(), this->end());
00806     this->setEnd(this->begin());
00807     CurSize = 0;
00808     this->grow(RHSSize);
00809   } else if (CurSize) {
00810     // Otherwise, use assignment for the already-constructed elements.
00811     this->move(RHS.begin(), RHS.begin()+CurSize, this->begin());
00812   }
00813 
00814   // Move-construct the new elements in place.
00815   this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
00816                            this->begin()+CurSize);
00817 
00818   // Set end.
00819   this->setEnd(this->begin()+RHSSize);
00820 
00821   RHS.clear();
00822   return *this;
00823 }
00824 
00825 /// Storage for the SmallVector elements which aren't contained in
00826 /// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1'
00827 /// element is in the base class. This is specialized for the N=1 and N=0 cases
00828 /// to avoid allocating unnecessary storage.
00829 template <typename T, unsigned N>
00830 struct SmallVectorStorage {
00831   typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
00832 };
00833 template <typename T> struct SmallVectorStorage<T, 1> {};
00834 template <typename T> struct SmallVectorStorage<T, 0> {};
00835 
00836 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
00837 /// for the case when the array is small.  It contains some number of elements
00838 /// in-place, which allows it to avoid heap allocation when the actual number of
00839 /// elements is below that threshold.  This allows normal "small" cases to be
00840 /// fast without losing generality for large inputs.
00841 ///
00842 /// Note that this does not attempt to be exception safe.
00843 ///
00844 template <typename T, unsigned N>
00845 class SmallVector : public SmallVectorImpl<T> {
00846   /// Storage - Inline space for elements which aren't stored in the base class.
00847   SmallVectorStorage<T, N> Storage;
00848 public:
00849   SmallVector() : SmallVectorImpl<T>(N) {
00850   }
00851 
00852   explicit SmallVector(unsigned Size, const T &Value = T())
00853     : SmallVectorImpl<T>(N) {
00854     this->assign(Size, Value);
00855   }
00856 
00857   template<typename ItTy>
00858   SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
00859     this->append(S, E);
00860   }
00861 
00862   template <typename RangeTy>
00863   explicit SmallVector(const llvm::iterator_range<RangeTy> R)
00864       : SmallVectorImpl<T>(N) {
00865     this->append(R.begin(), R.end());
00866   }
00867 
00868   SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
00869     if (!RHS.empty())
00870       SmallVectorImpl<T>::operator=(RHS);
00871   }
00872 
00873   const SmallVector &operator=(const SmallVector &RHS) {
00874     SmallVectorImpl<T>::operator=(RHS);
00875     return *this;
00876   }
00877 
00878   SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
00879     if (!RHS.empty())
00880       SmallVectorImpl<T>::operator=(::std::move(RHS));
00881   }
00882 
00883   const SmallVector &operator=(SmallVector &&RHS) {
00884     SmallVectorImpl<T>::operator=(::std::move(RHS));
00885     return *this;
00886   }
00887 };
00888 
00889 template<typename T, unsigned N>
00890 static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
00891   return X.capacity_in_bytes();
00892 }
00893 
00894 } // End llvm namespace
00895 
00896 namespace std {
00897   /// Implement std::swap in terms of SmallVector swap.
00898   template<typename T>
00899   inline void
00900   swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
00901     LHS.swap(RHS);
00902   }
00903 
00904   /// Implement std::swap in terms of SmallVector swap.
00905   template<typename T, unsigned N>
00906   inline void
00907   swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {
00908     LHS.swap(RHS);
00909   }
00910 }
00911 
00912 #endif