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/SmallVector.h"
19 /// TinyPtrVector - This class is specialized for cases where there are
20 /// normally 0 or 1 element in a vector, but is general enough to go beyond that
23 /// NOTE: This container doesn't allow you to store a null pointer into it.
25 template <typename EltTy>
28 typedef llvm::SmallVector<EltTy, 4> VecTy;
29 typedef typename VecTy::value_type value_type;
31 llvm::PointerUnion<EltTy, VecTy*> Val;
35 if (VecTy *V = Val.template dyn_cast<VecTy*>())
39 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
40 if (VecTy *V = Val.template dyn_cast<VecTy*>())
43 TinyPtrVector &operator=(const TinyPtrVector &RHS) {
51 // Try to squeeze into the single slot. If it won't fit, allocate a copied
53 if (Val.template is<EltTy>()) {
57 Val = new VecTy(*RHS.Val.template get<VecTy*>());
61 // If we have a full vector allocated, try to re-use it.
62 if (RHS.Val.template is<EltTy>()) {
63 Val.template get<VecTy*>()->clear();
64 Val.template get<VecTy*>()->push_back(RHS.front());
66 *Val.template get<VecTy*>() = *RHS.Val.template get<VecTy*>();
71 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
72 RHS.Val = (EltTy)nullptr;
74 TinyPtrVector &operator=(TinyPtrVector &&RHS) {
82 // If this vector has been allocated on the heap, re-use it if cheap. If it
83 // would require more copying, just delete it and we'll steal the other
85 if (VecTy *V = Val.template dyn_cast<VecTy*>()) {
86 if (RHS.Val.template is<EltTy>()) {
88 V->push_back(RHS.front());
95 RHS.Val = (EltTy)nullptr;
99 /// Constructor from a single element.
100 explicit TinyPtrVector(EltTy Elt) : Val(Elt) {}
102 /// Constructor from an ArrayRef.
103 explicit TinyPtrVector(ArrayRef<EltTy> Elts)
104 : Val(new VecTy(Elts.begin(), Elts.end())) {}
106 // implicit conversion operator to ArrayRef.
107 operator ArrayRef<EltTy>() const {
110 if (Val.template is<EltTy>())
111 return *Val.getAddrOfPtr1();
112 return *Val.template get<VecTy*>();
116 // This vector can be empty if it contains no element, or if it
117 // contains a pointer to an empty vector.
118 if (Val.isNull()) return true;
119 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
124 unsigned size() const {
127 if (Val.template is<EltTy>())
129 return Val.template get<VecTy*>()->size();
132 typedef const EltTy *const_iterator;
133 typedef EltTy *iterator;
136 if (Val.template is<EltTy>())
137 return Val.getAddrOfPtr1();
139 return Val.template get<VecTy *>()->begin();
143 if (Val.template is<EltTy>())
144 return begin() + (Val.isNull() ? 0 : 1);
146 return Val.template get<VecTy *>()->end();
149 const_iterator begin() const {
150 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
153 const_iterator end() const {
154 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
157 EltTy operator[](unsigned i) const {
158 assert(!Val.isNull() && "can't index into an empty vector");
159 if (EltTy V = Val.template dyn_cast<EltTy>()) {
160 assert(i == 0 && "tinyvector index out of range");
164 assert(i < Val.template get<VecTy*>()->size() &&
165 "tinyvector index out of range");
166 return (*Val.template get<VecTy*>())[i];
169 EltTy front() const {
170 assert(!empty() && "vector empty");
171 if (EltTy V = Val.template dyn_cast<EltTy>())
173 return Val.template get<VecTy*>()->front();
177 assert(!empty() && "vector empty");
178 if (EltTy V = Val.template dyn_cast<EltTy>())
180 return Val.template get<VecTy*>()->back();
183 void push_back(EltTy NewVal) {
184 assert(NewVal && "Can't add a null value");
186 // If we have nothing, add something.
192 // If we have a single value, convert to a vector.
193 if (EltTy V = Val.template dyn_cast<EltTy>()) {
195 Val.template get<VecTy*>()->push_back(V);
198 // Add the new value, we know we have a vector.
199 Val.template get<VecTy*>()->push_back(NewVal);
203 // If we have a single value, convert to empty.
204 if (Val.template is<EltTy>())
205 Val = (EltTy)nullptr;
206 else if (VecTy *Vec = Val.template get<VecTy*>())
211 // If we have a single value, convert to empty.
212 if (Val.template is<EltTy>()) {
213 Val = (EltTy)nullptr;
214 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
215 // If we have a vector form, just clear it.
218 // Otherwise, we're already empty.
221 iterator erase(iterator I) {
222 assert(I >= begin() && "Iterator to erase is out of bounds.");
223 assert(I < end() && "Erasing at past-the-end iterator.");
225 // If we have a single value, convert to empty.
226 if (Val.template is<EltTy>()) {
228 Val = (EltTy)nullptr;
229 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
230 // multiple items in a vector; just do the erase, there is no
231 // benefit to collapsing back to a pointer
232 return Vec->erase(I);
237 iterator erase(iterator S, iterator E) {
238 assert(S >= begin() && "Range to erase is out of bounds.");
239 assert(S <= E && "Trying to erase invalid range.");
240 assert(E <= end() && "Trying to erase past the end.");
242 if (Val.template is<EltTy>()) {
243 if (S == begin() && S != E)
244 Val = (EltTy)nullptr;
245 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
246 return Vec->erase(S, E);
251 iterator insert(iterator I, const EltTy &Elt) {
252 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
253 assert(I <= this->end() && "Inserting past the end of the vector.");
256 return std::prev(end());
258 assert(!Val.isNull() && "Null value with non-end insert iterator.");
259 if (EltTy V = Val.template dyn_cast<EltTy>()) {
260 assert(I == begin());
266 return Val.template get<VecTy*>()->insert(I, Elt);
269 template<typename ItTy>
270 iterator insert(iterator I, ItTy From, ItTy To) {
271 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
272 assert(I <= this->end() && "Inserting past the end of the vector.");
276 // If we have a single value, convert to a vector.
277 ptrdiff_t Offset = I - begin();
279 if (std::next(From) == To) {
285 } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
287 Val.template get<VecTy*>()->push_back(V);
289 return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
292 } // end namespace llvm