Add a clear method to SmallVector
[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 was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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 <algorithm>
18 #include <cassert>
19 #include <iterator>
20 #include <memory>
21
22 namespace llvm {
23
24 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
25 /// for the case when the array is small.  It contains some number of elements
26 /// in-place, which allows it to avoid heap allocation when the actual number of
27 /// elements is below that threshold.  This allows normal "small" cases to be
28 /// fast without losing generality for large inputs.
29 ///
30 /// Note that this does not attempt to be exception safe.
31 ///
32 template <typename T, unsigned N>
33 class SmallVector {
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 either use GCC extensions, or some
38   // number of union instances for the space, which guarantee maximal alignment.
39   union U {
40     double D;
41     long double LD;
42     long long L;
43     void *P;
44   };
45   
46   /// InlineElts - These are the 'N' elements that are stored inline in the body
47   /// of the vector
48   U InlineElts[(sizeof(T)*N+sizeof(U)-1)/sizeof(U)];
49   T *Begin, *End, *Capacity;
50 public:
51   // Default ctor - Initialize to empty.
52   SmallVector() : Begin((T*)InlineElts), End(Begin), Capacity(Begin+N) {
53   }
54   
55   SmallVector(const SmallVector &RHS) {
56     unsigned RHSSize = RHS.size();
57     Begin = (T*)InlineElts;
58
59     // Doesn't fit in the small case?  Allocate space.
60     if (RHSSize > N) {
61       End = Capacity = Begin;
62       grow(RHSSize);
63     }
64     End = Begin+RHSSize;
65     Capacity = Begin+N;
66     std::uninitialized_copy(RHS.begin(), RHS.end(), Begin);
67   }
68   ~SmallVector() {
69     // Destroy the constructed elements in the vector.
70     for (iterator I = Begin, E = End; I != E; ++I)
71       I->~T();
72
73     // If this wasn't grown from the inline copy, deallocate the old space.
74     if ((void*)Begin != (void*)InlineElts)
75       delete[] (char*)Begin;
76   }
77   
78   typedef size_t size_type;
79   typedef T* iterator;
80   typedef const T* const_iterator;
81   typedef T& reference;
82   typedef const T& const_reference;
83
84   bool empty() const { return Begin == End; }
85   size_type size() const { return End-Begin; }
86   
87   iterator begin() { return Begin; }
88   const_iterator begin() const { return Begin; }
89
90   iterator end() { return End; }
91   const_iterator end() const { return End; }
92   
93   reference operator[](unsigned idx) {
94     assert(idx < size() && "out of range reference!");
95     return Begin[idx];
96   }
97   const_reference operator[](unsigned idx) const {
98     assert(idx < size() && "out of range reference!");
99     return Begin[idx];
100   }
101   
102   reference back() {
103     assert(!empty() && "SmallVector is empty!");
104     return end()[-1];
105   }
106   const_reference back() const {
107     assert(!empty() && "SmallVector is empty!");
108     return end()[-1];
109   }
110   
111   void push_back(const_reference Elt) {
112     if (End < Capacity) {
113   Retry:
114       new (End) T(Elt);
115       ++End;
116       return;
117     }
118     grow();
119     goto Retry;
120   }
121   
122   void pop_back() {
123     assert(!empty() && "SmallVector is empty!");
124     --End;
125     End->~T();
126   }
127   
128   void clear() {
129     while (End != Begin) {
130       End->~T();
131       --End;
132     }
133   }
134   
135   /// append - Add the specified range to the end of the SmallVector.
136   ///
137   template<typename in_iter>
138   void append(in_iter in_start, in_iter in_end) {
139     unsigned NumInputs = std::distance(in_start, in_end);
140     // Grow allocated space if needed.
141     if (End+NumInputs > Capacity)
142       grow(size()+NumInputs);
143
144     // Copy the new elements over.
145     std::uninitialized_copy(in_start, in_end, End);
146     End += NumInputs;
147   }
148   
149   const SmallVector &operator=(const SmallVector &RHS) {
150     // Avoid self-assignment.
151     if (this == &RHS) return *this;
152     
153     // If we already have sufficient space, assign the common elements, then
154     // destroy any excess.
155     unsigned RHSSize = RHS.size();
156     unsigned CurSize = size();
157     if (CurSize >= RHSSize) {
158       // Assign common elements.
159       std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
160       
161       // Destroy excess elements.
162       for (unsigned i = RHSSize; i != CurSize; ++i)
163         Begin[i].~T();
164       
165       // Trim.
166       End = Begin + RHSSize;
167       return *this;
168     }
169     
170     // If we have to grow to have enough elements, destroy the current elements.
171     // This allows us to avoid copying them during the grow.
172     if (Capacity-Begin < RHSSize) {
173       // Destroy current elements.
174       for (iterator I = Begin, E = End; I != E; ++I)
175         I->~T();
176       End = Begin;
177       CurSize = 0;
178       grow(RHSSize);
179     } else if (CurSize) {
180       // Otherwise, use assignment for the already-constructed elements.
181       std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
182     }
183     
184     // Copy construct the new elements in place.
185     std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
186     
187     // Set end.
188     End = Begin+RHSSize;
189   }
190   
191 private:
192   /// isSmall - Return true if this is a smallvector which has not had dynamic
193   /// memory allocated for it.
194   bool isSmall() const {
195     return (void*)Begin == (void*)InlineElts;
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(unsigned MinSize = 0) {
201     unsigned CurCapacity = Capacity-Begin;
202     unsigned CurSize = size();
203     unsigned NewCapacity = 2*CurCapacity;
204     if (NewCapacity < MinSize)
205       NewCapacity = MinSize;
206     T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
207
208     // Copy the elements over.
209     std::uninitialized_copy(Begin, End, NewElts);
210     
211     // Destroy the original elements.
212     for (iterator I = Begin, E = End; I != E; ++I)
213       I->~T();
214     
215     // If this wasn't grown from the inline copy, deallocate the old space.
216     if (!isSmall())
217       delete[] (char*)Begin;
218     
219     Begin = NewElts;
220     End = NewElts+CurSize;
221     Capacity = Begin+NewCapacity;
222   }
223 };
224
225 } // End llvm namespace
226
227 #endif