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 /// 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.
29 /// Note that this does not attempt to be exception safe.
31 template <typename T, unsigned N>
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.
45 /// InlineElts - These are the 'N' elements that are stored inline in the body
47 U InlineElts[(sizeof(T)*N+sizeof(U)-1)/sizeof(U)];
48 T *Begin, *End, *Capacity;
50 // Default ctor - Initialize to empty.
51 SmallVector() : Begin((T*)InlineElts), End(Begin), Capacity(Begin+N) {
54 SmallVector(const SmallVector &RHS) {
55 unsigned RHSSize = RHS.size();
56 Begin = (T*)InlineElts;
58 // Doesn't fit in the small case? Allocate space.
60 End = Capacity = Begin;
65 std::uninitialized_copy(RHS.begin(), RHS.end(), Begin);
68 // Destroy the constructed elements in the vector.
69 for (iterator I = Begin, E = End; I != E; ++I)
72 // If this wasn't grown from the inline copy, deallocate the old space.
73 if ((void*)Begin != (void*)InlineElts)
74 delete[] (char*)Begin;
77 typedef size_t size_type;
79 typedef const T* const_iterator;
81 typedef const T& const_reference;
83 bool empty() const { return Begin == End; }
84 size_type size() const { return End-Begin; }
86 iterator begin() { return Begin; }
87 const_iterator begin() const { return Begin; }
89 iterator end() { return End; }
90 const_iterator end() const { return End; }
92 reference operator[](unsigned idx) {
95 const_reference operator[](unsigned idx) const {
102 const_reference back() const {
106 void push_back(const_reference Elt) {
107 if (End < Capacity) {
123 while (End != Begin) {
129 /// append - Add the specified range to the end of the SmallVector.
131 template<typename in_iter>
132 void append(in_iter in_start, in_iter in_end) {
133 unsigned NumInputs = std::distance(in_start, in_end);
134 // Grow allocated space if needed.
135 if (End+NumInputs > Capacity)
136 grow(size()+NumInputs);
138 // Copy the new elements over.
139 std::uninitialized_copy(in_start, in_end, End);
143 const SmallVector &operator=(const SmallVector &RHS) {
144 // Avoid self-assignment.
145 if (this == &RHS) return *this;
147 // If we already have sufficient space, assign the common elements, then
148 // destroy any excess.
149 unsigned RHSSize = RHS.size();
150 unsigned CurSize = size();
151 if (CurSize >= RHSSize) {
152 // Assign common elements.
153 std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
155 // Destroy excess elements.
156 for (unsigned i = RHSSize; i != CurSize; ++i)
160 End = Begin + RHSSize;
164 // If we have to grow to have enough elements, destroy the current elements.
165 // This allows us to avoid copying them during the grow.
166 if (Capacity-Begin < RHSSize) {
167 // Destroy current elements.
168 for (iterator I = Begin, E = End; I != E; ++I)
173 } else if (CurSize) {
174 // Otherwise, use assignment for the already-constructed elements.
175 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
178 // Copy construct the new elements in place.
179 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
186 /// isSmall - Return true if this is a smallvector which has not had dynamic
187 /// memory allocated for it.
188 bool isSmall() const {
189 return (void*)Begin == (void*)InlineElts;
192 /// grow - double the size of the allocated memory, guaranteeing space for at
193 /// least one more element or MinSize if specified.
194 void grow(unsigned MinSize = 0) {
195 unsigned CurCapacity = Capacity-Begin;
196 unsigned CurSize = size();
197 unsigned NewCapacity = 2*CurCapacity;
198 if (NewCapacity < MinSize)
199 NewCapacity = MinSize;
200 T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
202 // Copy the elements over.
203 std::uninitialized_copy(Begin, End, NewElts);
205 // Destroy the original elements.
206 for (iterator I = Begin, E = End; I != E; ++I)
209 // If this wasn't grown from the inline copy, deallocate the old space.
211 delete[] (char*)Begin;
214 End = NewElts+CurSize;
215 Capacity = Begin+NewCapacity;
219 } // End llvm namespace