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 // implicit conversion operator to ArrayRef.
100 operator ArrayRef<EltTy>() const {
102 return ArrayRef<EltTy>();
103 if (Val.template is<EltTy>())
104 return *Val.getAddrOfPtr1();
105 return *Val.template get<VecTy*>();
109 // This vector can be empty if it contains no element, or if it
110 // contains a pointer to an empty vector.
111 if (Val.isNull()) return true;
112 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
117 unsigned size() const {
120 if (Val.template is<EltTy>())
122 return Val.template get<VecTy*>()->size();
125 typedef const EltTy *const_iterator;
126 typedef EltTy *iterator;
129 if (Val.template is<EltTy>())
130 return Val.getAddrOfPtr1();
132 return Val.template get<VecTy *>()->begin();
136 if (Val.template is<EltTy>())
137 return begin() + (Val.isNull() ? 0 : 1);
139 return Val.template get<VecTy *>()->end();
142 const_iterator begin() const {
143 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
146 const_iterator end() const {
147 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
150 EltTy operator[](unsigned i) const {
151 assert(!Val.isNull() && "can't index into an empty vector");
152 if (EltTy V = Val.template dyn_cast<EltTy>()) {
153 assert(i == 0 && "tinyvector index out of range");
157 assert(i < Val.template get<VecTy*>()->size() &&
158 "tinyvector index out of range");
159 return (*Val.template get<VecTy*>())[i];
162 EltTy front() const {
163 assert(!empty() && "vector empty");
164 if (EltTy V = Val.template dyn_cast<EltTy>())
166 return Val.template get<VecTy*>()->front();
170 assert(!empty() && "vector empty");
171 if (EltTy V = Val.template dyn_cast<EltTy>())
173 return Val.template get<VecTy*>()->back();
176 void push_back(EltTy NewVal) {
177 assert(NewVal && "Can't add a null value");
179 // If we have nothing, add something.
185 // If we have a single value, convert to a vector.
186 if (EltTy V = Val.template dyn_cast<EltTy>()) {
188 Val.template get<VecTy*>()->push_back(V);
191 // Add the new value, we know we have a vector.
192 Val.template get<VecTy*>()->push_back(NewVal);
196 // If we have a single value, convert to empty.
197 if (Val.template is<EltTy>())
198 Val = (EltTy)nullptr;
199 else if (VecTy *Vec = Val.template get<VecTy*>())
204 // If we have a single value, convert to empty.
205 if (Val.template is<EltTy>()) {
206 Val = (EltTy)nullptr;
207 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
208 // If we have a vector form, just clear it.
211 // Otherwise, we're already empty.
214 iterator erase(iterator I) {
215 assert(I >= begin() && "Iterator to erase is out of bounds.");
216 assert(I < end() && "Erasing at past-the-end iterator.");
218 // If we have a single value, convert to empty.
219 if (Val.template is<EltTy>()) {
221 Val = (EltTy)nullptr;
222 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
223 // multiple items in a vector; just do the erase, there is no
224 // benefit to collapsing back to a pointer
225 return Vec->erase(I);
230 iterator erase(iterator S, iterator E) {
231 assert(S >= begin() && "Range to erase is out of bounds.");
232 assert(S <= E && "Trying to erase invalid range.");
233 assert(E <= end() && "Trying to erase past the end.");
235 if (Val.template is<EltTy>()) {
236 if (S == begin() && S != E)
237 Val = (EltTy)nullptr;
238 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
239 return Vec->erase(S, E);
244 iterator insert(iterator I, const EltTy &Elt) {
245 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
246 assert(I <= this->end() && "Inserting past the end of the vector.");
249 return std::prev(end());
251 assert(!Val.isNull() && "Null value with non-end insert iterator.");
252 if (EltTy V = Val.template dyn_cast<EltTy>()) {
253 assert(I == begin());
259 return Val.template get<VecTy*>()->insert(I, Elt);
262 template<typename ItTy>
263 iterator insert(iterator I, ItTy From, ItTy To) {
264 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
265 assert(I <= this->end() && "Inserting past the end of the vector.");
269 // If we have a single value, convert to a vector.
270 ptrdiff_t Offset = I - begin();
272 if (std::next(From) == To) {
278 } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
280 Val.template get<VecTy*>()->push_back(V);
282 return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
285 } // end namespace llvm