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