capacity is a pointer, not a value
[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 <iterator>
19 #include <memory>
20
21 namespace llvm {
22
23 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
24 /// for the case when the array is small.  It contains some number of elements
25 /// in-place, which allows it to avoid heap allocation when the actual number of
26 /// elements is below that threshold.  This allows normal "small" cases to be
27 /// fast without losing generality for large inputs.
28 ///
29 /// Note that this does not attempt to be exception safe.
30 ///
31 template <typename T, unsigned N>
32 class SmallVector {
33   // Allocate raw space for N elements of type T.  If T has a ctor or dtor, we
34   // don't want it to be automatically run, so we need to represent the space as
35   // something else.  An array of char would work great, but might not be
36   // aligned sufficiently.  Instead, we either use GCC extensions, or some
37   // number of union instances for the space, which guarantee maximal alignment.
38   union U {
39     double D;
40     long double LD;
41     long long L;
42     void *P;
43   };
44   
45   /// InlineElts - These are the 'N' elements that are stored inline in the body
46   /// of the vector
47   U InlineElts[(sizeof(T)*N+sizeof(U)-1)/sizeof(U)];
48   T *Begin, *End, *Capacity;
49 public:
50   // Default ctor - Initialize to empty.
51   SmallVector() : Begin((T*)InlineElts), End(Begin), Capacity(Begin+N) {
52   }
53   
54   template<typename ItTy>
55   SmallVector(ItTy S, ItTy E)
56     : Begin((T*)InlineElts), End(Begin), Capacity(Begin+N) {
57     append(S, E);
58   }
59   
60   SmallVector(const SmallVector &RHS) {
61     unsigned RHSSize = RHS.size();
62     Begin = (T*)InlineElts;
63
64     // Doesn't fit in the small case?  Allocate space.
65     if (RHSSize > N) {
66       End = Capacity = Begin;
67       grow(RHSSize);
68     }
69     End = Begin+RHSSize;
70     Capacity = Begin+N;
71     std::uninitialized_copy(RHS.begin(), RHS.end(), Begin);
72   }
73   ~SmallVector() {
74     // Destroy the constructed elements in the vector.
75     for (iterator I = Begin, E = End; I != E; ++I)
76       I->~T();
77
78     // If this wasn't grown from the inline copy, deallocate the old space.
79     if ((void*)Begin != (void*)InlineElts)
80       delete[] (char*)Begin;
81   }
82   
83   typedef size_t size_type;
84   typedef T* iterator;
85   typedef const T* const_iterator;
86   typedef T& reference;
87   typedef const T& const_reference;
88
89   bool empty() const { return Begin == End; }
90   size_type size() const { return End-Begin; }
91   
92   iterator begin() { return Begin; }
93   const_iterator begin() const { return Begin; }
94
95   iterator end() { return End; }
96   const_iterator end() const { return End; }
97   
98   reference operator[](unsigned idx) {
99     return Begin[idx];
100   }
101   const_reference operator[](unsigned idx) const {
102     return Begin[idx];
103   }
104   
105   reference back() {
106     return end()[-1];
107   }
108   const_reference back() const {
109     return end()[-1];
110   }
111   
112   void push_back(const_reference Elt) {
113     if (End < Capacity) {
114   Retry:
115       new (End) T(Elt);
116       ++End;
117       return;
118     }
119     grow();
120     goto Retry;
121   }
122   
123   void pop_back() {
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   void assign(unsigned NumElts, const T &Elt) {
150     clear();
151     if (Begin+NumElts > Capacity)
152       grow(NumElts);
153     End = Begin+NumElts;
154     for (; NumElts; --NumElts)
155       new (Begin+NumElts-1) T(Elt);
156   }
157   
158   const SmallVector &operator=(const SmallVector &RHS) {
159     // Avoid self-assignment.
160     if (this == &RHS) return *this;
161     
162     // If we already have sufficient space, assign the common elements, then
163     // destroy any excess.
164     unsigned RHSSize = RHS.size();
165     unsigned CurSize = size();
166     if (CurSize >= RHSSize) {
167       // Assign common elements.
168       std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
169       
170       // Destroy excess elements.
171       for (unsigned i = RHSSize; i != CurSize; ++i)
172         Begin[i].~T();
173       
174       // Trim.
175       End = Begin + RHSSize;
176       return *this;
177     }
178     
179     // If we have to grow to have enough elements, destroy the current elements.
180     // This allows us to avoid copying them during the grow.
181     if (Capacity-Begin < RHSSize) {
182       // Destroy current elements.
183       for (iterator I = Begin, E = End; I != E; ++I)
184         I->~T();
185       End = Begin;
186       CurSize = 0;
187       grow(RHSSize);
188     } else if (CurSize) {
189       // Otherwise, use assignment for the already-constructed elements.
190       std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
191     }
192     
193     // Copy construct the new elements in place.
194     std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
195     
196     // Set end.
197     End = Begin+RHSSize;
198   }
199   
200 private:
201   /// isSmall - Return true if this is a smallvector which has not had dynamic
202   /// memory allocated for it.
203   bool isSmall() const {
204     return (void*)Begin == (void*)InlineElts;
205   }
206
207   /// grow - double the size of the allocated memory, guaranteeing space for at
208   /// least one more element or MinSize if specified.
209   void grow(unsigned MinSize = 0) {
210     unsigned CurCapacity = Capacity-Begin;
211     unsigned CurSize = size();
212     unsigned NewCapacity = 2*CurCapacity;
213     if (NewCapacity < MinSize)
214       NewCapacity = MinSize;
215     T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
216
217     // Copy the elements over.
218     std::uninitialized_copy(Begin, End, NewElts);
219     
220     // Destroy the original elements.
221     for (iterator I = Begin, E = End; I != E; ++I)
222       I->~T();
223     
224     // If this wasn't grown from the inline copy, deallocate the old space.
225     if (!isSmall())
226       delete[] (char*)Begin;
227     
228     Begin = NewElts;
229     End = NewElts+CurSize;
230     Capacity = Begin+NewCapacity;
231   }
232 };
233
234 } // End llvm namespace
235
236 #endif