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 template<typename ItTy>
55 SmallVector(ItTy S, ItTy E)
56 : Begin((T*)InlineElts), End(Begin), Capacity(Begin+N) {
60 SmallVector(const SmallVector &RHS) {
61 unsigned RHSSize = RHS.size();
62 Begin = (T*)InlineElts;
64 // Doesn't fit in the small case? Allocate space.
66 End = Capacity = Begin;
71 std::uninitialized_copy(RHS.begin(), RHS.end(), Begin);
74 // Destroy the constructed elements in the vector.
75 for (iterator I = Begin, E = End; I != E; ++I)
78 // If this wasn't grown from the inline copy, deallocate the old space.
79 if ((void*)Begin != (void*)InlineElts)
80 delete[] (char*)Begin;
83 typedef size_t size_type;
85 typedef const T* const_iterator;
87 typedef const T& const_reference;
89 bool empty() const { return Begin == End; }
90 size_type size() const { return End-Begin; }
92 iterator begin() { return Begin; }
93 const_iterator begin() const { return Begin; }
95 iterator end() { return End; }
96 const_iterator end() const { return End; }
98 reference operator[](unsigned idx) {
101 const_reference operator[](unsigned idx) const {
108 const_reference back() const {
112 void push_back(const_reference Elt) {
113 if (End < Capacity) {
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 void assign(unsigned NumElts, const T &Elt) {
151 if (Begin+NumElts > Capacity)
154 for (; NumElts; --NumElts)
155 new (Begin+NumElts-1) T(Elt);
158 const SmallVector &operator=(const SmallVector &RHS) {
159 // Avoid self-assignment.
160 if (this == &RHS) return *this;
162 // If we already have sufficient space, assign the common elements, then
163 // destroy any excess.
164 unsigned RHSSize = RHS.size();
165 unsigned CurSize = size();
166 if (CurSize >= RHSSize) {
167 // Assign common elements.
168 std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
170 // Destroy excess elements.
171 for (unsigned i = RHSSize; i != CurSize; ++i)
175 End = Begin + RHSSize;
179 // If we have to grow to have enough elements, destroy the current elements.
180 // This allows us to avoid copying them during the grow.
181 if (Capacity-Begin < RHSSize) {
182 // Destroy current elements.
183 for (iterator I = Begin, E = End; I != E; ++I)
188 } else if (CurSize) {
189 // Otherwise, use assignment for the already-constructed elements.
190 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
193 // Copy construct the new elements in place.
194 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
201 /// isSmall - Return true if this is a smallvector which has not had dynamic
202 /// memory allocated for it.
203 bool isSmall() const {
204 return (void*)Begin == (void*)InlineElts;
207 /// grow - double the size of the allocated memory, guaranteeing space for at
208 /// least one more element or MinSize if specified.
209 void grow(unsigned MinSize = 0) {
210 unsigned CurCapacity = Capacity-Begin;
211 unsigned CurSize = size();
212 unsigned NewCapacity = 2*CurCapacity;
213 if (NewCapacity < MinSize)
214 NewCapacity = MinSize;
215 T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
217 // Copy the elements over.
218 std::uninitialized_copy(Begin, End, NewElts);
220 // Destroy the original elements.
221 for (iterator I = Begin, E = End; I != E; ++I)
224 // If this wasn't grown from the inline copy, deallocate the old space.
226 delete[] (char*)Begin;
229 End = NewElts+CurSize;
230 Capacity = Begin+NewCapacity;
234 } // End llvm namespace