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 {
28 T *Begin, *End, *Capacity;
30 // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
31 // don't want it to be automatically run, so we need to represent the space as
32 // something else. An array of char would work great, but might not be
33 // aligned sufficiently. Instead, we either use GCC extensions, or some
34 // number of union instances for the space, which guarantee maximal alignment.
42 // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
44 // Default ctor - Initialize to empty.
45 SmallVectorImpl(unsigned N)
46 : Begin((T*)&FirstEl), End((T*)&FirstEl), Capacity((T*)&FirstEl+N) {
50 // Destroy the constructed elements in the vector.
51 destroy_range(Begin, End);
53 // If this wasn't grown from the inline copy, deallocate the old space.
55 delete[] (char*)Begin;
58 typedef size_t size_type;
60 typedef const T* const_iterator;
62 typedef const T& const_reference;
64 bool empty() const { return Begin == End; }
65 size_type size() const { return End-Begin; }
67 iterator begin() { return Begin; }
68 const_iterator begin() const { return Begin; }
70 iterator end() { return End; }
71 const_iterator end() const { return End; }
73 reference operator[](unsigned idx) {
76 const_reference operator[](unsigned idx) const {
83 const_reference front() const {
90 const_reference back() const {
94 void push_back(const_reference Elt) {
111 destroy_range(Begin, End);
115 void resize(unsigned N) {
117 destroy_range(Begin+N, End);
119 } else if (N > size()) {
120 if (Begin+N > Capacity)
122 construct_range(End, Begin+N, T());
127 void resize(unsigned N, const T &NV) {
129 destroy_range(Begin+N, End);
131 } else if (N > size()) {
132 if (Begin+N > Capacity)
134 construct_range(End, Begin+N, NV);
139 void swap(SmallVectorImpl &RHS);
141 /// append - Add the specified range to the end of the SmallVector.
143 template<typename in_iter>
144 void append(in_iter in_start, in_iter in_end) {
145 unsigned NumInputs = std::distance(in_start, in_end);
146 // Grow allocated space if needed.
147 if (End+NumInputs > Capacity)
148 grow(size()+NumInputs);
150 // Copy the new elements over.
151 std::uninitialized_copy(in_start, in_end, End);
155 void assign(unsigned NumElts, const T &Elt) {
157 if (Begin+NumElts > Capacity)
160 construct_range(Begin, End, Elt);
163 void erase(iterator I) {
164 // Shift all elts down one.
165 std::copy(I+1, End, I);
166 // Drop the last elt.
170 void erase(iterator S, iterator E) {
171 // Shift all elts down.
172 iterator I = std::copy(E, End, S);
173 // Drop the last elts.
174 destroy_range(I, End);
178 iterator insert(iterator I, const T &Elt) {
179 if (I == End) { // Important special case for empty vector.
184 if (End < Capacity) {
188 // Push everything else over.
189 std::copy_backward(I, End-1, End);
193 unsigned EltNo = I-Begin;
199 const SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
202 /// isSmall - Return true if this is a smallvector which has not had dynamic
203 /// memory allocated for it.
204 bool isSmall() const {
205 return (void*)Begin == (void*)&FirstEl;
208 /// grow - double the size of the allocated memory, guaranteeing space for at
209 /// least one more element or MinSize if specified.
210 void grow(unsigned MinSize = 0);
212 void construct_range(T *S, T *E, const T &Elt) {
218 void destroy_range(T *S, T *E) {
226 // Define this out-of-line to dissuade the C++ compiler from inlining it.
227 template <typename T>
228 void SmallVectorImpl<T>::grow(unsigned MinSize) {
229 unsigned CurCapacity = Capacity-Begin;
230 unsigned CurSize = size();
231 unsigned NewCapacity = 2*CurCapacity;
232 if (NewCapacity < MinSize)
233 NewCapacity = MinSize;
234 T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
236 // Copy the elements over.
237 std::uninitialized_copy(Begin, End, NewElts);
239 // Destroy the original elements.
240 destroy_range(Begin, End);
242 // If this wasn't grown from the inline copy, deallocate the old space.
244 delete[] (char*)Begin;
247 End = NewElts+CurSize;
248 Capacity = Begin+NewCapacity;
251 template <typename T>
252 void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
253 if (this == &RHS) return;
255 // We can only avoid copying elements if neither vector is small.
256 if (!isSmall() && !RHS.isSmall()) {
257 std::swap(Begin, RHS.Begin);
258 std::swap(End, RHS.End);
259 std::swap(Capacity, RHS.Capacity);
262 if (Begin+RHS.size() > Capacity)
264 if (RHS.begin()+size() > RHS.Capacity)
267 // Swap the shared elements.
268 unsigned NumShared = size();
269 if (NumShared > RHS.size()) NumShared = RHS.size();
270 for (unsigned i = 0; i != NumShared; ++i)
271 std::swap(Begin[i], RHS[i]);
273 // Copy over the extra elts.
274 if (size() > RHS.size()) {
275 unsigned EltDiff = size() - RHS.size();
276 std::uninitialized_copy(Begin+NumShared, End, RHS.End);
278 destroy_range(Begin+NumShared, End);
279 End = Begin+NumShared;
280 } else if (RHS.size() > size()) {
281 unsigned EltDiff = RHS.size() - size();
282 std::uninitialized_copy(RHS.Begin+NumShared, RHS.End, End);
284 destroy_range(RHS.Begin+NumShared, RHS.End);
285 RHS.End = RHS.Begin+NumShared;
289 template <typename T>
290 const SmallVectorImpl<T> &
291 SmallVectorImpl<T>::operator=(const SmallVectorImpl<T> &RHS) {
292 // Avoid self-assignment.
293 if (this == &RHS) return *this;
295 // If we already have sufficient space, assign the common elements, then
296 // destroy any excess.
297 unsigned RHSSize = RHS.size();
298 unsigned CurSize = size();
299 if (CurSize >= RHSSize) {
300 // Assign common elements.
301 iterator NewEnd = std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
303 // Destroy excess elements.
304 destroy_range(NewEnd, End);
311 // If we have to grow to have enough elements, destroy the current elements.
312 // This allows us to avoid copying them during the grow.
313 if (unsigned(Capacity-Begin) < RHSSize) {
314 // Destroy current elements.
315 destroy_range(Begin, End);
319 } else if (CurSize) {
320 // Otherwise, use assignment for the already-constructed elements.
321 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
324 // Copy construct the new elements in place.
325 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
332 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
333 /// for the case when the array is small. It contains some number of elements
334 /// in-place, which allows it to avoid heap allocation when the actual number of
335 /// elements is below that threshold. This allows normal "small" cases to be
336 /// fast without losing generality for large inputs.
338 /// Note that this does not attempt to be exception safe.
340 template <typename T, unsigned N>
341 class SmallVector : public SmallVectorImpl<T> {
342 /// InlineElts - These are 'N-1' elements that are stored inline in the body
343 /// of the vector. The extra '1' element is stored in SmallVectorImpl.
344 typedef typename SmallVectorImpl<T>::U U;
346 // MinUs - The number of U's require to cover N T's.
347 MinUs = (sizeof(T)*N+sizeof(U)-1)/sizeof(U),
349 // NumInlineEltsElts - The number of elements actually in this array. There
350 // is already one in the parent class, and we have to round up to avoid
351 // having a zero-element array.
352 NumInlineEltsElts = (MinUs - 1) > 0 ? (MinUs - 1) : 1,
354 // NumTsAvailable - The number of T's we actually have space for, which may
355 // be more than N due to rounding.
356 NumTsAvailable = (NumInlineEltsElts+1)*sizeof(U) / sizeof(T)
358 U InlineElts[NumInlineEltsElts];
360 SmallVector() : SmallVectorImpl<T>(NumTsAvailable) {
363 template<typename ItTy>
364 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(NumTsAvailable) {
368 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(NumTsAvailable) {
372 const SmallVector &operator=(const SmallVector &RHS) {
373 SmallVectorImpl<T>::operator=(RHS);
378 } // End llvm namespace
381 /// Implement std::swap in terms of SmallVector swap.
384 swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
388 /// Implement std::swap in terms of SmallVector swap.
389 template<typename T, unsigned N>
391 swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {