1 //===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_TINYPTRVECTOR_H
11 #define LLVM_ADT_TINYPTRVECTOR_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/PointerUnion.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Compiler.h"
21 /// TinyPtrVector - This class is specialized for cases where there are
22 /// normally 0 or 1 element in a vector, but is general enough to go beyond that
25 /// NOTE: This container doesn't allow you to store a null pointer into it.
27 template <typename EltTy>
30 typedef llvm::SmallVector<EltTy, 4> VecTy;
31 typedef typename VecTy::value_type value_type;
33 llvm::PointerUnion<EltTy, VecTy*> Val;
37 if (VecTy *V = Val.template dyn_cast<VecTy*>())
41 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
42 if (VecTy *V = Val.template dyn_cast<VecTy*>())
45 TinyPtrVector &operator=(const TinyPtrVector &RHS) {
53 // Try to squeeze into the single slot. If it won't fit, allocate a copied
55 if (Val.template is<EltTy>()) {
59 Val = new VecTy(*RHS.Val.template get<VecTy*>());
63 // If we have a full vector allocated, try to re-use it.
64 if (RHS.Val.template is<EltTy>()) {
65 Val.template get<VecTy*>()->clear();
66 Val.template get<VecTy*>()->push_back(RHS.front());
68 *Val.template get<VecTy*>() = *RHS.Val.template get<VecTy*>();
73 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
76 TinyPtrVector &operator=(TinyPtrVector &&RHS) {
84 // If this vector has been allocated on the heap, re-use it if cheap. If it
85 // would require more copying, just delete it and we'll steal the other
87 if (VecTy *V = Val.template dyn_cast<VecTy*>()) {
88 if (RHS.Val.template is<EltTy>()) {
90 V->push_back(RHS.front());
101 // implicit conversion operator to ArrayRef.
102 operator ArrayRef<EltTy>() const {
104 return ArrayRef<EltTy>();
105 if (Val.template is<EltTy>())
106 return *Val.getAddrOfPtr1();
107 return *Val.template get<VecTy*>();
111 // This vector can be empty if it contains no element, or if it
112 // contains a pointer to an empty vector.
113 if (Val.isNull()) return true;
114 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
119 unsigned size() const {
122 if (Val.template is<EltTy>())
124 return Val.template get<VecTy*>()->size();
127 typedef const EltTy *const_iterator;
128 typedef EltTy *iterator;
131 if (Val.template is<EltTy>())
132 return Val.getAddrOfPtr1();
134 return Val.template get<VecTy *>()->begin();
138 if (Val.template is<EltTy>())
139 return begin() + (Val.isNull() ? 0 : 1);
141 return Val.template get<VecTy *>()->end();
144 const_iterator begin() const {
145 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
148 const_iterator end() const {
149 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
152 EltTy operator[](unsigned i) const {
153 assert(!Val.isNull() && "can't index into an empty vector");
154 if (EltTy V = Val.template dyn_cast<EltTy>()) {
155 assert(i == 0 && "tinyvector index out of range");
159 assert(i < Val.template get<VecTy*>()->size() &&
160 "tinyvector index out of range");
161 return (*Val.template get<VecTy*>())[i];
164 EltTy front() const {
165 assert(!empty() && "vector empty");
166 if (EltTy V = Val.template dyn_cast<EltTy>())
168 return Val.template get<VecTy*>()->front();
172 assert(!empty() && "vector empty");
173 if (EltTy V = Val.template dyn_cast<EltTy>())
175 return Val.template get<VecTy*>()->back();
178 void push_back(EltTy NewVal) {
179 assert(NewVal != 0 && "Can't add a null value");
181 // If we have nothing, add something.
187 // If we have a single value, convert to a vector.
188 if (EltTy V = Val.template dyn_cast<EltTy>()) {
190 Val.template get<VecTy*>()->push_back(V);
193 // Add the new value, we know we have a vector.
194 Val.template get<VecTy*>()->push_back(NewVal);
198 // If we have a single value, convert to empty.
199 if (Val.template is<EltTy>())
201 else if (VecTy *Vec = Val.template get<VecTy*>())
206 // If we have a single value, convert to empty.
207 if (Val.template is<EltTy>()) {
209 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
210 // If we have a vector form, just clear it.
213 // Otherwise, we're already empty.
216 iterator erase(iterator I) {
217 assert(I >= begin() && "Iterator to erase is out of bounds.");
218 assert(I < end() && "Erasing at past-the-end iterator.");
220 // If we have a single value, convert to empty.
221 if (Val.template is<EltTy>()) {
224 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
225 // multiple items in a vector; just do the erase, there is no
226 // benefit to collapsing back to a pointer
227 return Vec->erase(I);
232 iterator erase(iterator S, iterator E) {
233 assert(S >= begin() && "Range to erase is out of bounds.");
234 assert(S <= E && "Trying to erase invalid range.");
235 assert(E <= end() && "Trying to erase past the end.");
237 if (Val.template is<EltTy>()) {
238 if (S == begin() && S != E)
240 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
241 return Vec->erase(S, E);
246 iterator insert(iterator I, const EltTy &Elt) {
247 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
248 assert(I <= this->end() && "Inserting past the end of the vector.");
251 return llvm::prior(end());
253 assert(!Val.isNull() && "Null value with non-end insert iterator.");
254 if (EltTy V = Val.template dyn_cast<EltTy>()) {
255 assert(I == begin());
261 return Val.template get<VecTy*>()->insert(I, Elt);
264 template<typename ItTy>
265 iterator insert(iterator I, ItTy From, ItTy To) {
266 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
267 assert(I <= this->end() && "Inserting past the end of the vector.");
271 // If we have a single value, convert to a vector.
272 ptrdiff_t Offset = I - begin();
274 if (llvm::next(From) == To) {
280 } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
282 Val.template get<VecTy*>()->push_back(V);
284 return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
287 } // end namespace llvm