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
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.
30 /// Note that this does not attempt to be exception safe.
32 template <typename T, unsigned N>
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.
46 /// InlineElts - These are the 'N' elements that are stored inline in the body
48 U InlineElts[(sizeof(T)*N+sizeof(U)-1)/sizeof(U)];
49 T *Begin, *End, *Capacity;
51 // Default ctor - Initialize to empty.
52 SmallVector() : Begin((T*)InlineElts), End(Begin), Capacity(Begin+N) {
55 SmallVector(const SmallVector &RHS) {
56 unsigned RHSSize = RHS.size();
57 Begin = (T*)InlineElts;
59 // Doesn't fit in the small case? Allocate space.
61 End = Capacity = Begin;
66 std::uninitialized_copy(RHS.begin(), RHS.end(), Begin);
69 // Destroy the constructed elements in the vector.
70 for (iterator I = Begin, E = End; I != E; ++I)
73 // If this wasn't grown from the inline copy, deallocate the old space.
74 if ((void*)Begin != (void*)InlineElts)
75 delete[] (char*)Begin;
78 typedef size_t size_type;
80 typedef const T* const_iterator;
82 typedef const T& const_reference;
84 bool empty() const { return Begin == End; }
85 size_type size() const { return End-Begin; }
87 iterator begin() { return Begin; }
88 const_iterator begin() const { return Begin; }
90 iterator end() { return End; }
91 const_iterator end() const { return End; }
93 reference operator[](unsigned idx) {
94 assert(idx < size() && "out of range reference!");
97 const_reference operator[](unsigned idx) const {
98 assert(idx < size() && "out of range reference!");
103 assert(!empty() && "SmallVector is empty!");
106 const_reference back() const {
107 assert(!empty() && "SmallVector is empty!");
111 void push_back(const_reference Elt) {
112 if (End < Capacity) {
123 assert(!empty() && "SmallVector is empty!");
129 while (End != Begin) {
135 /// append - Add the specified range to the end of the SmallVector.
137 template<typename in_iter>
138 void append(in_iter in_start, in_iter in_end) {
139 unsigned NumInputs = std::distance(in_start, in_end);
140 // Grow allocated space if needed.
141 if (End+NumInputs > Capacity)
142 grow(size()+NumInputs);
144 // Copy the new elements over.
145 std::uninitialized_copy(in_start, in_end, End);
149 const SmallVector &operator=(const SmallVector &RHS) {
150 // Avoid self-assignment.
151 if (this == &RHS) return *this;
153 // If we already have sufficient space, assign the common elements, then
154 // destroy any excess.
155 unsigned RHSSize = RHS.size();
156 unsigned CurSize = size();
157 if (CurSize >= RHSSize) {
158 // Assign common elements.
159 std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
161 // Destroy excess elements.
162 for (unsigned i = RHSSize; i != CurSize; ++i)
166 End = Begin + RHSSize;
170 // If we have to grow to have enough elements, destroy the current elements.
171 // This allows us to avoid copying them during the grow.
172 if (Capacity-Begin < RHSSize) {
173 // Destroy current elements.
174 for (iterator I = Begin, E = End; I != E; ++I)
179 } else if (CurSize) {
180 // Otherwise, use assignment for the already-constructed elements.
181 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
184 // Copy construct the new elements in place.
185 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
192 /// isSmall - Return true if this is a smallvector which has not had dynamic
193 /// memory allocated for it.
194 bool isSmall() const {
195 return (void*)Begin == (void*)InlineElts;
198 /// grow - double the size of the allocated memory, guaranteeing space for at
199 /// least one more element or MinSize if specified.
200 void grow(unsigned MinSize = 0) {
201 unsigned CurCapacity = Capacity-Begin;
202 unsigned CurSize = size();
203 unsigned NewCapacity = 2*CurCapacity;
204 if (NewCapacity < MinSize)
205 NewCapacity = MinSize;
206 T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
208 // Copy the elements over.
209 std::uninitialized_copy(Begin, End, NewElts);
211 // Destroy the original elements.
212 for (iterator I = Begin, E = End; I != E; ++I)
215 // If this wasn't grown from the inline copy, deallocate the old space.
217 delete[] (char*)Begin;
220 End = NewElts+CurSize;
221 Capacity = Begin+NewCapacity;
225 } // End llvm namespace