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