factor out the grow() method for all pod implementations into one
[oota-llvm.git] / include / llvm / ADT / SmallVector.h
1 //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the SmallVector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_SMALLVECTOR_H
15 #define LLVM_ADT_SMALLVECTOR_H
16
17 #include "llvm/Support/type_traits.h"
18 #include <algorithm>
19 #include <cassert>
20 #include <cstring>
21 #include <memory>
22
23 #ifdef _MSC_VER
24 namespace std {
25 #if _MSC_VER <= 1310
26   // Work around flawed VC++ implementation of std::uninitialized_copy.  Define
27   // additional overloads so that elements with pointer types are recognized as
28   // scalars and not objects, causing bizarre type conversion errors.
29   template<class T1, class T2>
30   inline _Scalar_ptr_iterator_tag _Ptr_cat(T1 **, T2 **) {
31     _Scalar_ptr_iterator_tag _Cat;
32     return _Cat;
33   }
34
35   template<class T1, class T2>
36   inline _Scalar_ptr_iterator_tag _Ptr_cat(T1* const *, T2 **) {
37     _Scalar_ptr_iterator_tag _Cat;
38     return _Cat;
39   }
40 #else
41 // FIXME: It is not clear if the problem is fixed in VS 2005.  What is clear
42 // is that the above hack won't work if it wasn't fixed.
43 #endif
44 }
45 #endif
46
47 namespace llvm {
48
49 /// SmallVectorBase - This is all the non-templated stuff common to all
50 /// SmallVectors.
51 class SmallVectorBase {
52 protected:
53   void *BeginX, *EndX, *CapacityX;
54
55   // Allocate raw space for N elements of type T.  If T has a ctor or dtor, we
56   // don't want it to be automatically run, so we need to represent the space as
57   // something else.  An array of char would work great, but might not be
58   // aligned sufficiently.  Instead, we either use GCC extensions, or some
59   // number of union instances for the space, which guarantee maximal alignment.
60 #ifdef __GNUC__
61   typedef char U;
62   U FirstEl __attribute__((aligned));
63 #else
64   union U {
65     double D;
66     long double LD;
67     long long L;
68     void *P;
69   } FirstEl;
70 #endif
71   // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
72   
73 protected:
74   SmallVectorBase(size_t Size)
75     : BeginX(&FirstEl), EndX(&FirstEl), CapacityX((char*)&FirstEl+Size) {}
76   
77   /// isSmall - Return true if this is a smallvector which has not had dynamic
78   /// memory allocated for it.
79   bool isSmall() const {
80     return BeginX == static_cast<const void*>(&FirstEl);
81   }
82   
83   /// size_in_bytes - This returns size()*sizeof(T).
84   size_t size_in_bytes() const {
85     return size_t((char*)EndX - (char*)BeginX);
86   }
87   
88   /// capacity_in_bytes - This returns capacity()*sizeof(T).
89   size_t capacity_in_bytes() const {
90     return size_t((char*)CapacityX - (char*)BeginX);
91   }
92   
93   inline void grow_pod(size_t MinSizeInBytes, size_t TSize);
94   
95 public:
96   bool empty() const { return BeginX == EndX; }
97 };
98   
99 inline void SmallVectorBase::grow_pod(size_t MinSizeInBytes, size_t TSize) {
100   size_t CurSizeBytes = size_in_bytes();
101   size_t NewCapacityInBytes = 2 * capacity_in_bytes();
102   if (NewCapacityInBytes < MinSizeInBytes)
103     NewCapacityInBytes = MinSizeInBytes;
104   void *NewElts = operator new(NewCapacityInBytes);
105   
106   // Copy the elements over.
107   memcpy(NewElts, this->BeginX, CurSizeBytes);
108   
109   // If this wasn't grown from the inline copy, deallocate the old space.
110   if (!this->isSmall())
111     operator delete(this->BeginX);
112   
113   this->EndX = (char*)NewElts+CurSizeBytes;
114   this->BeginX = NewElts;
115   this->CapacityX = (char*)this->BeginX + NewCapacityInBytes;
116 }
117
118   
119
120 template <typename T>
121 class SmallVectorTemplateCommon : public SmallVectorBase {
122 protected:
123   void setEnd(T *P) { this->EndX = P; }
124 public:
125   SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(Size) {}
126   
127   typedef size_t size_type;
128   typedef ptrdiff_t difference_type;
129   typedef T value_type;
130   typedef T *iterator;
131   typedef const T *const_iterator;
132   
133   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
134   typedef std::reverse_iterator<iterator> reverse_iterator;
135   
136   typedef T &reference;
137   typedef const T &const_reference;
138   typedef T *pointer;
139   typedef const T *const_pointer;
140   
141   // forward iterator creation methods.
142   iterator begin() { return (iterator)this->BeginX; }
143   const_iterator begin() const { return (const_iterator)this->BeginX; }
144   iterator end() { return (iterator)this->EndX; }
145   const_iterator end() const { return (const_iterator)this->EndX; }
146 protected:
147   iterator capacity_ptr() { return (iterator)this->CapacityX; }
148   const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
149 public:
150   
151   // reverse iterator creation methods.
152   reverse_iterator rbegin()            { return reverse_iterator(end()); }
153   const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
154   reverse_iterator rend()              { return reverse_iterator(begin()); }
155   const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
156
157   size_type size() const { return end()-begin(); }
158   size_type max_size() const { return size_type(-1) / sizeof(T); }
159   
160   /// capacity - Return the total number of elements in the currently allocated
161   /// buffer.
162   size_t capacity() const { return capacity_ptr() - begin(); }
163   
164   /// data - Return a pointer to the vector's buffer, even if empty().
165   pointer data() { return pointer(begin()); }
166   /// data - Return a pointer to the vector's buffer, even if empty().
167   const_pointer data() const { return const_pointer(begin()); }
168   
169   reference operator[](unsigned idx) {
170     assert(begin() + idx < end());
171     return begin()[idx];
172   }
173   const_reference operator[](unsigned idx) const {
174     assert(begin() + idx < end());
175     return begin()[idx];
176   }
177
178   reference front() {
179     return begin()[0];
180   }
181   const_reference front() const {
182     return begin()[0];
183   }
184
185   reference back() {
186     return end()[-1];
187   }
188   const_reference back() const {
189     return end()[-1];
190   }
191 };
192   
193 /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
194 /// implementations that are designed to work with non-POD-like T's.
195 template <typename T, bool isPodLike>
196 class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
197 public:
198   SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
199
200   static void destroy_range(T *S, T *E) {
201     while (S != E) {
202       --E;
203       E->~T();
204     }
205   }
206   
207   /// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
208   /// starting with "Dest", constructing elements into it as needed.
209   template<typename It1, typename It2>
210   static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
211     std::uninitialized_copy(I, E, Dest);
212   }
213   
214   /// grow - double the size of the allocated memory, guaranteeing space for at
215   /// least one more element or MinSize if specified.
216   void grow(size_t MinSize = 0);
217 };
218
219 // Define this out-of-line to dissuade the C++ compiler from inlining it.
220 template <typename T, bool isPodLike>
221 void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
222   size_t CurCapacity = this->capacity();
223   size_t CurSize = this->size();
224   size_t NewCapacity = 2*CurCapacity;
225   if (NewCapacity < MinSize)
226     NewCapacity = MinSize;
227   T *NewElts = static_cast<T*>(operator new(NewCapacity*sizeof(T)));
228   
229   // Copy the elements over.
230   uninitialized_copy(this->begin(), this->end(), NewElts);
231   
232   // Destroy the original elements.
233   destroy_range(this->begin(), this->end());
234   
235   // If this wasn't grown from the inline copy, deallocate the old space.
236   if (!this->isSmall())
237     operator delete(this->begin());
238   
239   setEnd(NewElts+CurSize);
240   this->BeginX = NewElts;
241   this->CapacityX = this->begin()+NewCapacity;
242 }
243   
244   
245 /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
246 /// implementations that are designed to work with POD-like T's.
247 template <typename T>
248 class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
249 public:
250   SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
251   
252   // No need to do a destroy loop for POD's.
253   static void destroy_range(T *S, T *E) {}
254   
255   /// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
256   /// starting with "Dest", constructing elements into it as needed.
257   template<typename It1, typename It2>
258   static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
259     // Use memcpy for PODs: std::uninitialized_copy optimizes to memmove, memcpy
260     // is better.
261     memcpy(&*Dest, &*I, (E-I)*sizeof(T));
262   }
263   
264   /// grow - double the size of the allocated memory, guaranteeing space for at
265   /// least one more element or MinSize if specified.
266   void grow(size_t MinSize = 0) {
267     this->grow_pod(MinSize*sizeof(T), sizeof(T));
268   }
269 };
270   
271   
272 /// SmallVectorImpl - This class consists of common code factored out of the
273 /// SmallVector class to reduce code duplication based on the SmallVector 'N'
274 /// template parameter.
275 template <typename T>
276 class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
277   typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
278 public:
279   typedef typename SuperClass::iterator iterator;
280   typedef typename SuperClass::size_type size_type;
281   
282   // Default ctor - Initialize to empty.
283   explicit SmallVectorImpl(unsigned N)
284     : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
285   }
286   
287   ~SmallVectorImpl() {
288     // Destroy the constructed elements in the vector.
289     destroy_range(this->begin(), this->end());
290     
291     // If this wasn't grown from the inline copy, deallocate the old space.
292     if (!this->isSmall())
293       operator delete(this->begin());
294   }
295   
296   
297   void clear() {
298     destroy_range(this->begin(), this->end());
299     this->EndX = this->BeginX;
300   }
301
302   void resize(unsigned N) {
303     if (N < this->size()) {
304       this->destroy_range(this->begin()+N, this->end());
305       this->setEnd(this->begin()+N);
306     } else if (N > this->size()) {
307       if (this->capacity() < N)
308         this->grow(N);
309       this->construct_range(this->end(), this->begin()+N, T());
310       this->setEnd(this->begin()+N);
311     }
312   }
313
314   void resize(unsigned N, const T &NV) {
315     if (N < this->size()) {
316       destroy_range(this->begin()+N, this->end());
317       setEnd(this->begin()+N);
318     } else if (N > this->size()) {
319       if (this->capacity() < N)
320         this->grow(N);
321       construct_range(this->end(), this->begin()+N, NV);
322       setEnd(this->begin()+N);
323     }
324   }
325
326   void reserve(unsigned N) {
327     if (this->capacity() < N)
328       this->grow(N);
329   }
330   
331   void push_back(const T &Elt) {
332     if (this->EndX < this->CapacityX) {
333     Retry:
334       new (this->end()) T(Elt);
335       setEnd(this->end()+1);
336       return;
337     }
338     this->grow();
339     goto Retry;
340   }
341   
342   void pop_back() {
343     setEnd(this->end()-1);
344     this->end()->~T();
345   }
346   
347   T pop_back_val() {
348     T Result = this->back();
349     pop_back();
350     return Result;
351   }
352   
353   
354   void swap(SmallVectorImpl &RHS);
355   
356   /// append - Add the specified range to the end of the SmallVector.
357   ///
358   template<typename in_iter>
359   void append(in_iter in_start, in_iter in_end) {
360     size_type NumInputs = std::distance(in_start, in_end);
361     // Grow allocated space if needed.
362     if (NumInputs > size_type(this->capacity_ptr()-this->end()))
363       this->grow(this->size()+NumInputs);
364     
365     // Copy the new elements over.
366     // TODO: NEED To compile time dispatch on whether in_iter is a random access
367     // iterator to use the fast uninitialized_copy.
368     std::uninitialized_copy(in_start, in_end, this->end());
369     setEnd(this->end() + NumInputs);
370   }
371   
372   /// append - Add the specified range to the end of the SmallVector.
373   ///
374   void append(size_type NumInputs, const T &Elt) {
375     // Grow allocated space if needed.
376     if (NumInputs > size_type(this->capacity_ptr()-this->end()))
377       this->grow(this->size()+NumInputs);
378     
379     // Copy the new elements over.
380     std::uninitialized_fill_n(this->end(), NumInputs, Elt);
381     setEnd(this->end() + NumInputs);
382   }
383   
384   void assign(unsigned NumElts, const T &Elt) {
385     clear();
386     if (this->capacity() < NumElts)
387       this->grow(NumElts);
388     setEnd(this->begin()+NumElts);
389     construct_range(this->begin(), this->end(), Elt);
390   }
391   
392   iterator erase(iterator I) {
393     iterator N = I;
394     // Shift all elts down one.
395     std::copy(I+1, this->end(), I);
396     // Drop the last elt.
397     pop_back();
398     return(N);
399   }
400   
401   iterator erase(iterator S, iterator E) {
402     iterator N = S;
403     // Shift all elts down.
404     iterator I = std::copy(E, this->end(), S);
405     // Drop the last elts.
406     destroy_range(I, this->end());
407     setEnd(I);
408     return(N);
409   }
410   
411   iterator insert(iterator I, const T &Elt) {
412     if (I == this->end()) {  // Important special case for empty vector.
413       push_back(Elt);
414       return this->end()-1;
415     }
416     
417     if (this->EndX < this->CapacityX) {
418     Retry:
419       new (this->end()) T(this->back());
420       this->setEnd(this->end()+1);
421       // Push everything else over.
422       std::copy_backward(I, this->end()-1, this->end());
423       *I = Elt;
424       return I;
425     }
426     size_t EltNo = I-this->begin();
427     this->grow();
428     I = this->begin()+EltNo;
429     goto Retry;
430   }
431   
432   iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
433     if (I == this->end()) {  // Important special case for empty vector.
434       append(NumToInsert, Elt);
435       return this->end()-1;
436     }
437     
438     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
439     size_t InsertElt = I - this->begin();
440     
441     // Ensure there is enough space.
442     reserve(static_cast<unsigned>(this->size() + NumToInsert));
443     
444     // Uninvalidate the iterator.
445     I = this->begin()+InsertElt;
446     
447     // If there are more elements between the insertion point and the end of the
448     // range than there are being inserted, we can use a simple approach to
449     // insertion.  Since we already reserved space, we know that this won't
450     // reallocate the vector.
451     if (size_t(this->end()-I) >= NumToInsert) {
452       T *OldEnd = this->end();
453       append(this->end()-NumToInsert, this->end());
454       
455       // Copy the existing elements that get replaced.
456       std::copy_backward(I, OldEnd-NumToInsert, OldEnd);
457       
458       std::fill_n(I, NumToInsert, Elt);
459       return I;
460     }
461     
462     // Otherwise, we're inserting more elements than exist already, and we're
463     // not inserting at the end.
464     
465     // Copy over the elements that we're about to overwrite.
466     T *OldEnd = this->end();
467     setEnd(this->end() + NumToInsert);
468     size_t NumOverwritten = OldEnd-I;
469     uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
470     
471     // Replace the overwritten part.
472     std::fill_n(I, NumOverwritten, Elt);
473     
474     // Insert the non-overwritten middle part.
475     std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
476     return I;
477   }
478   
479   template<typename ItTy>
480   iterator insert(iterator I, ItTy From, ItTy To) {
481     if (I == this->end()) {  // Important special case for empty vector.
482       append(From, To);
483       return this->end()-1;
484     }
485     
486     size_t NumToInsert = std::distance(From, To);
487     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
488     size_t InsertElt = I - this->begin();
489     
490     // Ensure there is enough space.
491     reserve(static_cast<unsigned>(this->size() + NumToInsert));
492     
493     // Uninvalidate the iterator.
494     I = this->begin()+InsertElt;
495     
496     // If there are more elements between the insertion point and the end of the
497     // range than there are being inserted, we can use a simple approach to
498     // insertion.  Since we already reserved space, we know that this won't
499     // reallocate the vector.
500     if (size_t(this->end()-I) >= NumToInsert) {
501       T *OldEnd = this->end();
502       append(this->end()-NumToInsert, this->end());
503       
504       // Copy the existing elements that get replaced.
505       std::copy_backward(I, OldEnd-NumToInsert, OldEnd);
506       
507       std::copy(From, To, I);
508       return I;
509     }
510     
511     // Otherwise, we're inserting more elements than exist already, and we're
512     // not inserting at the end.
513     
514     // Copy over the elements that we're about to overwrite.
515     T *OldEnd = this->end();
516     setEnd(this->end() + NumToInsert);
517     size_t NumOverwritten = OldEnd-I;
518     uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
519     
520     // Replace the overwritten part.
521     std::copy(From, From+NumOverwritten, I);
522     
523     // Insert the non-overwritten middle part.
524     uninitialized_copy(From+NumOverwritten, To, OldEnd);
525     return I;
526   }
527   
528   const SmallVectorImpl
529   &operator=(const SmallVectorImpl &RHS);
530   
531   bool operator==(const SmallVectorImpl &RHS) const {
532     if (this->size() != RHS.size()) return false;
533     return std::equal(this->begin(), this->end(), RHS.begin());
534   }
535   bool operator!=(const SmallVectorImpl &RHS) const {
536     return !(*this == RHS);
537   }
538   
539   bool operator<(const SmallVectorImpl &RHS) const {
540     return std::lexicographical_compare(this->begin(), this->end(),
541                                         RHS.begin(), RHS.end());
542   }
543   
544   /// set_size - Set the array size to \arg N, which the current array must have
545   /// enough capacity for.
546   ///
547   /// This does not construct or destroy any elements in the vector.
548   ///
549   /// Clients can use this in conjunction with capacity() to write past the end
550   /// of the buffer when they know that more elements are available, and only
551   /// update the size later. This avoids the cost of value initializing elements
552   /// which will only be overwritten.
553   void set_size(unsigned N) {
554     assert(N <= this->capacity());
555     setEnd(this->begin() + N);
556   }
557   
558 private:
559   static void construct_range(T *S, T *E, const T &Elt) {
560     for (; S != E; ++S)
561       new (S) T(Elt);
562   }
563 };
564   
565
566 template <typename T>
567 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
568   if (this == &RHS) return;
569
570   // We can only avoid copying elements if neither vector is small.
571   if (!this->isSmall() && !RHS.isSmall()) {
572     std::swap(this->BeginX, RHS.BeginX);
573     std::swap(this->EndX, RHS.EndX);
574     std::swap(this->CapacityX, RHS.CapacityX);
575     return;
576   }
577   if (RHS.size() > this->capacity())
578     this->grow(RHS.size());
579   if (this->size() > RHS.capacity())
580     RHS.grow(this->size());
581
582   // Swap the shared elements.
583   size_t NumShared = this->size();
584   if (NumShared > RHS.size()) NumShared = RHS.size();
585   for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
586     std::swap((*this)[i], RHS[i]);
587
588   // Copy over the extra elts.
589   if (this->size() > RHS.size()) {
590     size_t EltDiff = this->size() - RHS.size();
591     uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
592     RHS.setEnd(RHS.end()+EltDiff);
593     destroy_range(this->begin()+NumShared, this->end());
594     setEnd(this->begin()+NumShared);
595   } else if (RHS.size() > this->size()) {
596     size_t EltDiff = RHS.size() - this->size();
597     uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
598     setEnd(this->end() + EltDiff);
599     destroy_range(RHS.begin()+NumShared, RHS.end());
600     RHS.setEnd(RHS.begin()+NumShared);
601   }
602 }
603
604 template <typename T>
605 const SmallVectorImpl<T> &SmallVectorImpl<T>::
606   operator=(const SmallVectorImpl<T> &RHS) {
607   // Avoid self-assignment.
608   if (this == &RHS) return *this;
609
610   // If we already have sufficient space, assign the common elements, then
611   // destroy any excess.
612   size_t RHSSize = RHS.size();
613   size_t CurSize = this->size();
614   if (CurSize >= RHSSize) {
615     // Assign common elements.
616     iterator NewEnd;
617     if (RHSSize)
618       NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
619     else
620       NewEnd = this->begin();
621
622     // Destroy excess elements.
623     destroy_range(NewEnd, this->end());
624
625     // Trim.
626     setEnd(NewEnd);
627     return *this;
628   }
629
630   // If we have to grow to have enough elements, destroy the current elements.
631   // This allows us to avoid copying them during the grow.
632   if (this->capacity() < RHSSize) {
633     // Destroy current elements.
634     destroy_range(this->begin(), this->end());
635     setEnd(this->begin());
636     CurSize = 0;
637     this->grow(RHSSize);
638   } else if (CurSize) {
639     // Otherwise, use assignment for the already-constructed elements.
640     std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
641   }
642
643   // Copy construct the new elements in place.
644   uninitialized_copy(RHS.begin()+CurSize, RHS.end(), this->begin()+CurSize);
645
646   // Set end.
647   setEnd(this->begin()+RHSSize);
648   return *this;
649 }
650
651
652 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
653 /// for the case when the array is small.  It contains some number of elements
654 /// in-place, which allows it to avoid heap allocation when the actual number of
655 /// elements is below that threshold.  This allows normal "small" cases to be
656 /// fast without losing generality for large inputs.
657 ///
658 /// Note that this does not attempt to be exception safe.
659 ///
660 template <typename T, unsigned N>
661 class SmallVector : public SmallVectorImpl<T> {
662   /// InlineElts - These are 'N-1' elements that are stored inline in the body
663   /// of the vector.  The extra '1' element is stored in SmallVectorImpl.
664   typedef typename SmallVectorImpl<T>::U U;
665   enum {
666     // MinUs - The number of U's require to cover N T's.
667     MinUs = (static_cast<unsigned int>(sizeof(T))*N +
668              static_cast<unsigned int>(sizeof(U)) - 1) /
669             static_cast<unsigned int>(sizeof(U)),
670
671     // NumInlineEltsElts - The number of elements actually in this array.  There
672     // is already one in the parent class, and we have to round up to avoid
673     // having a zero-element array.
674     NumInlineEltsElts = MinUs > 1 ? (MinUs - 1) : 1,
675
676     // NumTsAvailable - The number of T's we actually have space for, which may
677     // be more than N due to rounding.
678     NumTsAvailable = (NumInlineEltsElts+1)*static_cast<unsigned int>(sizeof(U))/
679                      static_cast<unsigned int>(sizeof(T))
680   };
681   U InlineElts[NumInlineEltsElts];
682 public:
683   SmallVector() : SmallVectorImpl<T>(NumTsAvailable) {
684   }
685
686   explicit SmallVector(unsigned Size, const T &Value = T())
687     : SmallVectorImpl<T>(NumTsAvailable) {
688     this->reserve(Size);
689     while (Size--)
690       this->push_back(Value);
691   }
692
693   template<typename ItTy>
694   SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(NumTsAvailable) {
695     this->append(S, E);
696   }
697
698   SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(NumTsAvailable) {
699     if (!RHS.empty())
700       SmallVectorImpl<T>::operator=(RHS);
701   }
702
703   const SmallVector &operator=(const SmallVector &RHS) {
704     SmallVectorImpl<T>::operator=(RHS);
705     return *this;
706   }
707
708 };
709
710 } // End llvm namespace
711
712 namespace std {
713   /// Implement std::swap in terms of SmallVector swap.
714   template<typename T>
715   inline void
716   swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
717     LHS.swap(RHS);
718   }
719
720   /// Implement std::swap in terms of SmallVector swap.
721   template<typename T, unsigned N>
722   inline void
723   swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {
724     LHS.swap(RHS);
725   }
726 }
727
728 #endif