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 for (iterator I = Begin, E = End; I != E; ++I)
54 // If this wasn't grown from the inline copy, deallocate the old space.
56 delete[] (char*)Begin;
59 typedef size_t size_type;
61 typedef const T* const_iterator;
63 typedef const T& const_reference;
65 bool empty() const { return Begin == End; }
66 size_type size() const { return End-Begin; }
68 iterator begin() { return Begin; }
69 const_iterator begin() const { return Begin; }
71 iterator end() { return End; }
72 const_iterator end() const { return End; }
74 reference operator[](unsigned idx) {
77 const_reference operator[](unsigned idx) const {
84 const_reference back() const {
88 void push_back(const_reference Elt) {
105 while (End != Begin) {
111 /// append - Add the specified range to the end of the SmallVector.
113 template<typename in_iter>
114 void append(in_iter in_start, in_iter in_end) {
115 unsigned NumInputs = std::distance(in_start, in_end);
116 // Grow allocated space if needed.
117 if (End+NumInputs > Capacity)
118 grow(size()+NumInputs);
120 // Copy the new elements over.
121 std::uninitialized_copy(in_start, in_end, End);
125 void assign(unsigned NumElts, const T &Elt) {
127 if (Begin+NumElts > Capacity)
130 for (; NumElts; --NumElts)
131 new (Begin+NumElts-1) T(Elt);
134 const SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
137 /// isSmall - Return true if this is a smallvector which has not had dynamic
138 /// memory allocated for it.
139 bool isSmall() const {
140 return (void*)Begin == (void*)&FirstEl;
143 /// grow - double the size of the allocated memory, guaranteeing space for at
144 /// least one more element or MinSize if specified.
145 void grow(unsigned MinSize = 0);
148 // Define this out-of-line to dissuade the C++ compiler from inlining it.
149 template <typename T>
150 void SmallVectorImpl<T>::grow(unsigned MinSize) {
151 unsigned CurCapacity = Capacity-Begin;
152 unsigned CurSize = size();
153 unsigned NewCapacity = 2*CurCapacity;
154 if (NewCapacity < MinSize)
155 NewCapacity = MinSize;
156 T *NewElts = reinterpret_cast<T*>(new char[NewCapacity*sizeof(T)]);
158 // Copy the elements over.
159 std::uninitialized_copy(Begin, End, NewElts);
161 // Destroy the original elements.
162 for (iterator I = Begin, E = End; I != E; ++I)
165 // If this wasn't grown from the inline copy, deallocate the old space.
167 delete[] (char*)Begin;
170 End = NewElts+CurSize;
171 Capacity = Begin+NewCapacity;
174 template <typename T>
175 const SmallVectorImpl<T> &
176 SmallVectorImpl<T>::operator=(const SmallVectorImpl<T> &RHS) {
177 // Avoid self-assignment.
178 if (this == &RHS) return *this;
180 // If we already have sufficient space, assign the common elements, then
181 // destroy any excess.
182 unsigned RHSSize = RHS.size();
183 unsigned CurSize = size();
184 if (CurSize >= RHSSize) {
185 // Assign common elements.
186 std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
188 // Destroy excess elements.
189 for (unsigned i = RHSSize; i != CurSize; ++i)
193 End = Begin + RHSSize;
197 // If we have to grow to have enough elements, destroy the current elements.
198 // This allows us to avoid copying them during the grow.
199 if (unsigned(Capacity-Begin) < RHSSize) {
200 // Destroy current elements.
201 for (iterator I = Begin, E = End; I != E; ++I)
206 } else if (CurSize) {
207 // Otherwise, use assignment for the already-constructed elements.
208 std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
211 // Copy construct the new elements in place.
212 std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize);
218 /// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
219 /// for the case when the array is small. It contains some number of elements
220 /// in-place, which allows it to avoid heap allocation when the actual number of
221 /// elements is below that threshold. This allows normal "small" cases to be
222 /// fast without losing generality for large inputs.
224 /// Note that this does not attempt to be exception safe.
226 template <typename T, unsigned N>
227 class SmallVector : public SmallVectorImpl<T> {
228 /// InlineElts - These are 'N-1' elements that are stored inline in the body
229 /// of the vector. The extra '1' element is stored in SmallVectorImpl.
230 typedef typename SmallVectorImpl<T>::U U;
231 U InlineElts[(sizeof(T)*N+sizeof(U)-1)/sizeof(U) - 1];
233 SmallVector() : SmallVectorImpl<T>(N) {
236 template<typename ItTy>
237 SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
241 SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
246 } // End llvm namespace