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 // If this wasn't grown from the inline copy, deallocate the old space.
70 if ((void*)Begin != (void*)InlineElts)
71 delete[] (char*)Begin;
74 typedef size_t size_type;
76 typedef const T* const_iterator;
78 typedef const T& const_reference;
80 bool empty() const { return Begin == End; }
81 size_type size() const { return End-Begin; }
83 iterator begin() { return Begin; }
84 const_iterator begin() const { return Begin; }
86 iterator end() { return End; }
87 const_iterator end() const { return End; }
89 reference operator[](unsigned idx) {
90 assert(idx < size() && "out of range reference!");
93 const_reference operator[](unsigned idx) const {
94 assert(idx < size() && "out of range reference!");
99 assert(!empty() && "SmallVector is empty!");
102 const_reference back() const {
103 assert(!empty() && "SmallVector is empty!");
107 void push_back(const_reference Elt) {
108 if (End < Capacity) {
118 /// append - Add the specified range to the end of the SmallVector.
120 template<typename in_iter>
121 void append(in_iter in_start, in_iter in_end) {
122 unsigned NumInputs = std::distance(in_start, in_end);
123 // Grow allocated space if needed.
124 if (End+NumInputs > Capacity)
125 grow(size()+NumInputs);
127 // Copy the new elements over.
128 std::uninitialized_copy(in_start, in_end, End);
132 const SmallVector &operator=(const SmallVector &RHS) {
133 // Avoid self-assignment.
134 if (this == &RHS) return *this;
136 // If we already have sufficient space, assign the common elements, then
137 // destroy any excess.
138 unsigned RHSSize = RHS.size();
139 unsigned CurSize = size();
140 if (CurSize >= RHSSize) {
141 // Assign common elements.
142 std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
144 // Destroy excess elements.
145 for (unsigned i = RHSSize; i != CurSize; ++i)
149 End = Begin + RHSSize;
153 // If we have to grow to have enough elements, destroy the current elements.
154 // This allows us to avoid copying them during the grow.
155 if (Capacity-Begin < RHSSize) {
156 // Destroy current elements.
157 for (T *I = Begin, E = End; I != E; ++I)
162 } else if (CurSize) {
163 // Otherwise, use assignment for the already-constructed elements.
164 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
167 // Copy construct the new elements in place.
168 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
175 /// isSmall - Return true if this is a smallvector which has not had dynamic
176 /// memory allocated for it.
177 bool isSmall() const {
178 return (void*)Begin == (void*)InlineElts;
181 /// grow - double the size of the allocated memory, guaranteeing space for at
182 /// least one more element or MinSize if specified.
183 void grow(unsigned MinSize = 0) {
184 unsigned CurCapacity = Capacity-Begin;
185 unsigned CurSize = size();
186 unsigned NewCapacity = 2*CurCapacity;
187 if (NewCapacity < MinSize)
188 NewCapacity = MinSize;
189 T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
191 // Copy the elements over.
192 std::uninitialized_copy(Begin, End, NewElts);
194 // Destroy the original elements.
195 for (T *I = Begin, *E = End; I != E; ++I)
198 // If this wasn't grown from the inline copy, deallocate the old space.
200 delete[] (char*)Begin;
203 End = NewElts+CurSize;
204 Capacity = Begin+NewCapacity*2;
208 } // End llvm namespace