1 //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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
17 #include "llvm/ADT/iterator.h"
18 #include "llvm/Support/type_traits.h"
26 // Work around flawed VC++ implementation of std::uninitialized_copy. Define
27 // additional overloads so that elements with pointer types are recognized as
28 // scalars and not objects, causing bizarre type conversion errors.
29 template<class T1, class T2>
30 inline _Scalar_ptr_iterator_tag _Ptr_cat(T1 **, T2 **) {
31 _Scalar_ptr_iterator_tag _Cat;
35 template<class T1, class T2>
36 inline _Scalar_ptr_iterator_tag _Ptr_cat(T1* const *, T2 **) {
37 _Scalar_ptr_iterator_tag _Cat;
41 // FIXME: It is not clear if the problem is fixed in VS 2005. What is clear
42 // is that the above hack won't work if it wasn't fixed.
49 /// SmallVectorImpl - This class consists of common code factored out of the
50 /// SmallVector class to reduce code duplication based on the SmallVector 'N'
51 /// template parameter.
53 class SmallVectorImpl {
55 T *Begin, *End, *Capacity;
57 // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
58 // don't want it to be automatically run, so we need to represent the space as
59 // something else. An array of char would work great, but might not be
60 // aligned sufficiently. Instead, we either use GCC extensions, or some
61 // number of union instances for the space, which guarantee maximal alignment.
65 U FirstEl __attribute__((aligned));
74 // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
76 // Default ctor - Initialize to empty.
77 SmallVectorImpl(unsigned N)
78 : Begin(reinterpret_cast<T*>(&FirstEl)),
79 End(reinterpret_cast<T*>(&FirstEl)),
80 Capacity(reinterpret_cast<T*>(&FirstEl)+N) {
84 // Destroy the constructed elements in the vector.
85 destroy_range(Begin, End);
87 // If this wasn't grown from the inline copy, deallocate the old space.
89 operator delete(Begin);
92 typedef size_t size_type;
95 typedef const T* const_iterator;
97 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
98 typedef std::reverse_iterator<iterator> reverse_iterator;
100 typedef T& reference;
101 typedef const T& const_reference;
103 bool empty() const { return Begin == End; }
104 size_type size() const { return End-Begin; }
106 // forward iterator creation methods.
107 iterator begin() { return Begin; }
108 const_iterator begin() const { return Begin; }
109 iterator end() { return End; }
110 const_iterator end() const { return End; }
112 // reverse iterator creation methods.
113 reverse_iterator rbegin() { return reverse_iterator(end()); }
114 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
115 reverse_iterator rend() { return reverse_iterator(begin()); }
116 const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
119 reference operator[](unsigned idx) {
122 const_reference operator[](unsigned idx) const {
129 const_reference front() const {
136 const_reference back() const {
140 void push_back(const_reference Elt) {
141 if (End < Capacity) {
157 destroy_range(Begin, End);
161 void resize(unsigned N) {
163 destroy_range(Begin+N, End);
165 } else if (N > size()) {
166 if (unsigned(Capacity-Begin) < N)
168 construct_range(End, Begin+N, T());
173 void resize(unsigned N, const T &NV) {
175 destroy_range(Begin+N, End);
177 } else if (N > size()) {
178 if (unsigned(Capacity-Begin) < N)
180 construct_range(End, Begin+N, NV);
185 void reserve(unsigned N) {
186 if (unsigned(Capacity-Begin) < N)
190 void swap(SmallVectorImpl &RHS);
192 /// append - Add the specified range to the end of the SmallVector.
194 template<typename in_iter>
195 void append(in_iter in_start, in_iter in_end) {
196 size_type NumInputs = std::distance(in_start, in_end);
197 // Grow allocated space if needed.
198 if (End+NumInputs > Capacity)
199 grow(size()+NumInputs);
201 // Copy the new elements over.
202 std::uninitialized_copy(in_start, in_end, End);
206 void assign(unsigned NumElts, const T &Elt) {
208 if (unsigned(Capacity-Begin) < NumElts)
211 construct_range(Begin, End, Elt);
214 iterator erase(iterator I) {
216 // Shift all elts down one.
217 std::copy(I+1, End, I);
218 // Drop the last elt.
223 iterator erase(iterator S, iterator E) {
225 // Shift all elts down.
226 iterator I = std::copy(E, End, S);
227 // Drop the last elts.
228 destroy_range(I, End);
233 iterator insert(iterator I, const T &Elt) {
234 if (I == End) { // Important special case for empty vector.
239 if (End < Capacity) {
243 // Push everything else over.
244 std::copy_backward(I, End-1, End);
248 size_t EltNo = I-Begin;
254 template<typename ItTy>
255 iterator insert(iterator I, ItTy From, ItTy To) {
256 if (I == End) { // Important special case for empty vector.
261 size_t NumToInsert = std::distance(From, To);
262 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
263 size_t InsertElt = I-begin();
265 // Ensure there is enough space.
266 reserve(static_cast<unsigned>(size() + NumToInsert));
268 // Uninvalidate the iterator.
269 I = begin()+InsertElt;
271 // If we already have this many elements in the collection, append the
272 // dest elements at the end, then copy over the appropriate elements. Since
273 // we already reserved space, we know that this won't reallocate the vector.
274 if (size() >= NumToInsert) {
276 append(End-NumToInsert, End);
278 // Copy the existing elements that get replaced.
279 std::copy(I, OldEnd-NumToInsert, I+NumToInsert);
281 std::copy(From, To, I);
285 // Otherwise, we're inserting more elements than exist already, and we're
286 // not inserting at the end.
288 // Copy over the elements that we're about to overwrite.
291 size_t NumOverwritten = OldEnd-I;
292 std::uninitialized_copy(I, OldEnd, End-NumOverwritten);
294 // Replace the overwritten part.
295 std::copy(From, From+NumOverwritten, I);
297 // Insert the non-overwritten middle part.
298 std::uninitialized_copy(From+NumOverwritten, To, OldEnd);
302 const SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
304 bool operator==(const SmallVectorImpl &RHS) const {
305 if (size() != RHS.size()) return false;
306 for (T *This = Begin, *That = RHS.Begin, *E = Begin+size();
307 This != E; ++This, ++That)
312 bool operator!=(const SmallVectorImpl &RHS) const { return !(*this == RHS); }
314 bool operator<(const SmallVectorImpl &RHS) const {
315 return std::lexicographical_compare(begin(), end(),
316 RHS.begin(), RHS.end());
320 /// isSmall - Return true if this is a smallvector which has not had dynamic
321 /// memory allocated for it.
322 bool isSmall() const {
323 return static_cast<const void*>(Begin) ==
324 static_cast<const void*>(&FirstEl);
327 /// grow - double the size of the allocated memory, guaranteeing space for at
328 /// least one more element or MinSize if specified.
329 void grow(size_type MinSize = 0);
331 void construct_range(T *S, T *E, const T &Elt) {
336 void destroy_range(T *S, T *E) {
344 // Define this out-of-line to dissuade the C++ compiler from inlining it.
345 template <typename T>
346 void SmallVectorImpl<T>::grow(size_t MinSize) {
347 size_t CurCapacity = Capacity-Begin;
348 size_t CurSize = size();
349 size_t NewCapacity = 2*CurCapacity;
350 if (NewCapacity < MinSize)
351 NewCapacity = MinSize;
352 T *NewElts = static_cast<T*>(operator new(NewCapacity*sizeof(T)));
354 // Copy the elements over.
355 if (is_class<T>::value)
356 std::uninitialized_copy(Begin, End, NewElts);
358 // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
359 memcpy(NewElts, Begin, CurSize * sizeof(T));
361 // Destroy the original elements.
362 destroy_range(Begin, End);
364 // If this wasn't grown from the inline copy, deallocate the old space.
366 operator delete(Begin);
369 End = NewElts+CurSize;
370 Capacity = Begin+NewCapacity;
373 template <typename T>
374 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
375 if (this == &RHS) return;
377 // We can only avoid copying elements if neither vector is small.
378 if (!isSmall() && !RHS.isSmall()) {
379 std::swap(Begin, RHS.Begin);
380 std::swap(End, RHS.End);
381 std::swap(Capacity, RHS.Capacity);
384 if (Begin+RHS.size() > Capacity)
386 if (RHS.begin()+size() > RHS.Capacity)
389 // Swap the shared elements.
390 size_t NumShared = size();
391 if (NumShared > RHS.size()) NumShared = RHS.size();
392 for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
393 std::swap(Begin[i], RHS[i]);
395 // Copy over the extra elts.
396 if (size() > RHS.size()) {
397 size_t EltDiff = size() - RHS.size();
398 std::uninitialized_copy(Begin+NumShared, End, RHS.End);
400 destroy_range(Begin+NumShared, End);
401 End = Begin+NumShared;
402 } else if (RHS.size() > size()) {
403 size_t EltDiff = RHS.size() - size();
404 std::uninitialized_copy(RHS.Begin+NumShared, RHS.End, End);
406 destroy_range(RHS.Begin+NumShared, RHS.End);
407 RHS.End = RHS.Begin+NumShared;
411 template <typename T>
412 const SmallVectorImpl<T> &
413 SmallVectorImpl<T>::operator=(const SmallVectorImpl<T> &RHS) {
414 // Avoid self-assignment.
415 if (this == &RHS) return *this;
417 // If we already have sufficient space, assign the common elements, then
418 // destroy any excess.
419 unsigned RHSSize = unsigned(RHS.size());
420 unsigned CurSize = unsigned(size());
421 if (CurSize >= RHSSize) {
422 // Assign common elements.
425 NewEnd = std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
429 // Destroy excess elements.
430 destroy_range(NewEnd, End);
437 // If we have to grow to have enough elements, destroy the current elements.
438 // This allows us to avoid copying them during the grow.
439 if (unsigned(Capacity-Begin) < RHSSize) {
440 // Destroy current elements.
441 destroy_range(Begin, End);
445 } else if (CurSize) {
446 // Otherwise, use assignment for the already-constructed elements.
447 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
450 // Copy construct the new elements in place.
451 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
458 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
459 /// for the case when the array is small. It contains some number of elements
460 /// in-place, which allows it to avoid heap allocation when the actual number of
461 /// elements is below that threshold. This allows normal "small" cases to be
462 /// fast without losing generality for large inputs.
464 /// Note that this does not attempt to be exception safe.
466 template <typename T, unsigned N>
467 class SmallVector : public SmallVectorImpl<T> {
468 /// InlineElts - These are 'N-1' elements that are stored inline in the body
469 /// of the vector. The extra '1' element is stored in SmallVectorImpl.
470 typedef typename SmallVectorImpl<T>::U U;
472 // MinUs - The number of U's require to cover N T's.
473 MinUs = (static_cast<unsigned int>(sizeof(T))*N +
474 static_cast<unsigned int>(sizeof(U)) - 1) /
475 static_cast<unsigned int>(sizeof(U)),
477 // NumInlineEltsElts - The number of elements actually in this array. There
478 // is already one in the parent class, and we have to round up to avoid
479 // having a zero-element array.
480 NumInlineEltsElts = (MinUs - 1) > 0 ? (MinUs - 1) : 1,
482 // NumTsAvailable - The number of T's we actually have space for, which may
483 // be more than N due to rounding.
484 NumTsAvailable = (NumInlineEltsElts+1)*static_cast<unsigned int>(sizeof(U))/
485 static_cast<unsigned int>(sizeof(T))
487 U InlineElts[NumInlineEltsElts];
489 SmallVector() : SmallVectorImpl<T>(NumTsAvailable) {
492 explicit SmallVector(unsigned Size, const T &Value = T())
493 : SmallVectorImpl<T>(NumTsAvailable) {
499 template<typename ItTy>
500 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(NumTsAvailable) {
504 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(NumTsAvailable) {
509 const SmallVector &operator=(const SmallVector &RHS) {
510 SmallVectorImpl<T>::operator=(RHS);
516 } // End llvm namespace
519 /// Implement std::swap in terms of SmallVector swap.
522 swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
526 /// Implement std::swap in terms of SmallVector swap.
527 template<typename T, unsigned N>
529 swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {