The smallvector dtor should destroy the elements.
[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   /// append - Add the specified range to the end of the SmallVector.
129   ///
130   template<typename in_iter>
131   void append(in_iter in_start, in_iter in_end) {
132     unsigned NumInputs = std::distance(in_start, in_end);
133     // Grow allocated space if needed.
134     if (End+NumInputs > Capacity)
135       grow(size()+NumInputs);
136
137     // Copy the new elements over.
138     std::uninitialized_copy(in_start, in_end, End);
139     End += NumInputs;
140   }
141   
142   const SmallVector &operator=(const SmallVector &RHS) {
143     // Avoid self-assignment.
144     if (this == &RHS) return *this;
145     
146     // If we already have sufficient space, assign the common elements, then
147     // destroy any excess.
148     unsigned RHSSize = RHS.size();
149     unsigned CurSize = size();
150     if (CurSize >= RHSSize) {
151       // Assign common elements.
152       std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
153       
154       // Destroy excess elements.
155       for (unsigned i = RHSSize; i != CurSize; ++i)
156         Begin[i].~T();
157       
158       // Trim.
159       End = Begin + RHSSize;
160       return *this;
161     }
162     
163     // If we have to grow to have enough elements, destroy the current elements.
164     // This allows us to avoid copying them during the grow.
165     if (Capacity-Begin < RHSSize) {
166       // Destroy current elements.
167       for (iterator I = Begin, E = End; I != E; ++I)
168         I->~T();
169       End = Begin;
170       CurSize = 0;
171       grow(RHSSize);
172     } else if (CurSize) {
173       // Otherwise, use assignment for the already-constructed elements.
174       std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
175     }
176     
177     // Copy construct the new elements in place.
178     std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
179     
180     // Set end.
181     End = Begin+RHSSize;
182   }
183   
184 private:
185   /// isSmall - Return true if this is a smallvector which has not had dynamic
186   /// memory allocated for it.
187   bool isSmall() const {
188     return (void*)Begin == (void*)InlineElts;
189   }
190
191   /// grow - double the size of the allocated memory, guaranteeing space for at
192   /// least one more element or MinSize if specified.
193   void grow(unsigned MinSize = 0) {
194     unsigned CurCapacity = Capacity-Begin;
195     unsigned CurSize = size();
196     unsigned NewCapacity = 2*CurCapacity;
197     if (NewCapacity < MinSize)
198       NewCapacity = MinSize;
199     T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
200
201     // Copy the elements over.
202     std::uninitialized_copy(Begin, End, NewElts);
203     
204     // Destroy the original elements.
205     for (iterator I = Begin, E = End; I != E; ++I)
206       I->~T();
207     
208     // If this wasn't grown from the inline copy, deallocate the old space.
209     if (!isSmall())
210       delete[] (char*)Begin;
211     
212     Begin = NewElts;
213     End = NewElts+CurSize;
214     Capacity = Begin+NewCapacity*2;
215   }
216 };
217
218 } // End llvm namespace
219
220 #endif