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