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