1 //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SmallVector class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SMALLVECTOR_H
15 #define LLVM_ADT_SMALLVECTOR_H
23 /// SmallVectorImpl - This class consists of common code factored out of the
24 /// SmallVector class to reduce code duplication based on the SmallVector 'N'
25 /// template parameter.
27 class SmallVectorImpl {
29 T *Begin, *End, *Capacity;
31 // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
32 // don't want it to be automatically run, so we need to represent the space as
33 // something else. An array of char would work great, but might not be
34 // aligned sufficiently. Instead, we either use GCC extensions, or some
35 // number of union instances for the space, which guarantee maximal alignment.
39 U FirstEl __attribute__((aligned));
48 // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
50 // Default ctor - Initialize to empty.
51 SmallVectorImpl(unsigned N)
52 : Begin((T*)&FirstEl), End((T*)&FirstEl), Capacity((T*)&FirstEl+N) {
56 // Destroy the constructed elements in the vector.
57 destroy_range(Begin, End);
59 // If this wasn't grown from the inline copy, deallocate the old space.
61 delete[] (char*)Begin;
64 typedef size_t size_type;
66 typedef const T* const_iterator;
68 typedef const T& const_reference;
70 bool empty() const { return Begin == End; }
71 size_type size() const { return End-Begin; }
73 iterator begin() { return Begin; }
74 const_iterator begin() const { return Begin; }
76 iterator end() { return End; }
77 const_iterator end() const { return End; }
79 reference operator[](unsigned idx) {
82 const_reference operator[](unsigned idx) const {
89 const_reference front() const {
96 const_reference back() const {
100 void push_back(const_reference Elt) {
101 if (End < Capacity) {
117 destroy_range(Begin, End);
121 void resize(unsigned N) {
123 destroy_range(Begin+N, End);
125 } else if (N > size()) {
126 if (Begin+N > Capacity)
128 construct_range(End, Begin+N, T());
133 void resize(unsigned N, const T &NV) {
135 destroy_range(Begin+N, End);
137 } else if (N > size()) {
138 if (Begin+N > Capacity)
140 construct_range(End, Begin+N, NV);
145 void reserve(unsigned N) {
146 if (unsigned(Capacity-Begin) < N)
150 void swap(SmallVectorImpl &RHS);
152 /// append - Add the specified range to the end of the SmallVector.
154 template<typename in_iter>
155 void append(in_iter in_start, in_iter in_end) {
156 unsigned NumInputs = std::distance(in_start, in_end);
157 // Grow allocated space if needed.
158 if (End+NumInputs > Capacity)
159 grow(size()+NumInputs);
161 // Copy the new elements over.
162 std::uninitialized_copy(in_start, in_end, End);
166 void assign(unsigned NumElts, const T &Elt) {
168 if (Begin+NumElts > Capacity)
171 construct_range(Begin, End, Elt);
174 void erase(iterator I) {
175 // Shift all elts down one.
176 std::copy(I+1, End, I);
177 // Drop the last elt.
181 void erase(iterator S, iterator E) {
182 // Shift all elts down.
183 iterator I = std::copy(E, End, S);
184 // Drop the last elts.
185 destroy_range(I, End);
189 iterator insert(iterator I, const T &Elt) {
190 if (I == End) { // Important special case for empty vector.
195 if (End < Capacity) {
199 // Push everything else over.
200 std::copy_backward(I, End-1, End);
204 unsigned EltNo = I-Begin;
210 const SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
213 /// isSmall - Return true if this is a smallvector which has not had dynamic
214 /// memory allocated for it.
215 bool isSmall() const {
216 return (void*)Begin == (void*)&FirstEl;
219 /// grow - double the size of the allocated memory, guaranteeing space for at
220 /// least one more element or MinSize if specified.
221 void grow(unsigned MinSize = 0);
223 void construct_range(T *S, T *E, const T &Elt) {
229 void destroy_range(T *S, T *E) {
237 // Define this out-of-line to dissuade the C++ compiler from inlining it.
238 template <typename T>
239 void SmallVectorImpl<T>::grow(unsigned MinSize) {
240 unsigned CurCapacity = Capacity-Begin;
241 unsigned CurSize = size();
242 unsigned NewCapacity = 2*CurCapacity;
243 if (NewCapacity < MinSize)
244 NewCapacity = MinSize;
245 T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
247 // Copy the elements over.
248 std::uninitialized_copy(Begin, End, NewElts);
250 // Destroy the original elements.
251 destroy_range(Begin, End);
253 // If this wasn't grown from the inline copy, deallocate the old space.
255 delete[] (char*)Begin;
258 End = NewElts+CurSize;
259 Capacity = Begin+NewCapacity;
262 template <typename T>
263 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
264 if (this == &RHS) return;
266 // We can only avoid copying elements if neither vector is small.
267 if (!isSmall() && !RHS.isSmall()) {
268 std::swap(Begin, RHS.Begin);
269 std::swap(End, RHS.End);
270 std::swap(Capacity, RHS.Capacity);
273 if (Begin+RHS.size() > Capacity)
275 if (RHS.begin()+size() > RHS.Capacity)
278 // Swap the shared elements.
279 unsigned NumShared = size();
280 if (NumShared > RHS.size()) NumShared = RHS.size();
281 for (unsigned i = 0; i != NumShared; ++i)
282 std::swap(Begin[i], RHS[i]);
284 // Copy over the extra elts.
285 if (size() > RHS.size()) {
286 unsigned EltDiff = size() - RHS.size();
287 std::uninitialized_copy(Begin+NumShared, End, RHS.End);
289 destroy_range(Begin+NumShared, End);
290 End = Begin+NumShared;
291 } else if (RHS.size() > size()) {
292 unsigned EltDiff = RHS.size() - size();
293 std::uninitialized_copy(RHS.Begin+NumShared, RHS.End, End);
295 destroy_range(RHS.Begin+NumShared, RHS.End);
296 RHS.End = RHS.Begin+NumShared;
300 template <typename T>
301 const SmallVectorImpl<T> &
302 SmallVectorImpl<T>::operator=(const SmallVectorImpl<T> &RHS) {
303 // Avoid self-assignment.
304 if (this == &RHS) return *this;
306 // If we already have sufficient space, assign the common elements, then
307 // destroy any excess.
308 unsigned RHSSize = RHS.size();
309 unsigned CurSize = size();
310 if (CurSize >= RHSSize) {
311 // Assign common elements.
312 iterator NewEnd = std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
314 // Destroy excess elements.
315 destroy_range(NewEnd, End);
322 // If we have to grow to have enough elements, destroy the current elements.
323 // This allows us to avoid copying them during the grow.
324 if (unsigned(Capacity-Begin) < RHSSize) {
325 // Destroy current elements.
326 destroy_range(Begin, End);
330 } else if (CurSize) {
331 // Otherwise, use assignment for the already-constructed elements.
332 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
335 // Copy construct the new elements in place.
336 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
343 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
344 /// for the case when the array is small. It contains some number of elements
345 /// in-place, which allows it to avoid heap allocation when the actual number of
346 /// elements is below that threshold. This allows normal "small" cases to be
347 /// fast without losing generality for large inputs.
349 /// Note that this does not attempt to be exception safe.
351 template <typename T, unsigned N>
352 class SmallVector : public SmallVectorImpl<T> {
353 /// InlineElts - These are 'N-1' elements that are stored inline in the body
354 /// of the vector. The extra '1' element is stored in SmallVectorImpl.
355 typedef typename SmallVectorImpl<T>::U U;
357 // MinUs - The number of U's require to cover N T's.
358 MinUs = (sizeof(T)*N+sizeof(U)-1)/sizeof(U),
360 // NumInlineEltsElts - The number of elements actually in this array. There
361 // is already one in the parent class, and we have to round up to avoid
362 // having a zero-element array.
363 NumInlineEltsElts = (MinUs - 1) > 0 ? (MinUs - 1) : 1,
365 // NumTsAvailable - The number of T's we actually have space for, which may
366 // be more than N due to rounding.
367 NumTsAvailable = (NumInlineEltsElts+1)*sizeof(U) / sizeof(T)
369 U InlineElts[NumInlineEltsElts];
371 SmallVector() : SmallVectorImpl<T>(NumTsAvailable) {
374 template<typename ItTy>
375 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(NumTsAvailable) {
379 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(NumTsAvailable) {
383 const SmallVector &operator=(const SmallVector &RHS) {
384 SmallVectorImpl<T>::operator=(RHS);
389 } // End llvm namespace
392 /// Implement std::swap in terms of SmallVector swap.
395 swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
399 /// Implement std::swap in terms of SmallVector swap.
400 template<typename T, unsigned N>
402 swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {