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;
30 typedef llvm::PointerUnion<EltTy, VecTy *> PtrUnion;
38 if (VecTy *V = Val.template dyn_cast<VecTy*>())
42 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
43 if (VecTy *V = Val.template dyn_cast<VecTy*>())
46 TinyPtrVector &operator=(const TinyPtrVector &RHS) {
54 // Try to squeeze into the single slot. If it won't fit, allocate a copied
56 if (Val.template is<EltTy>()) {
60 Val = new VecTy(*RHS.Val.template get<VecTy*>());
64 // If we have a full vector allocated, try to re-use it.
65 if (RHS.Val.template is<EltTy>()) {
66 Val.template get<VecTy*>()->clear();
67 Val.template get<VecTy*>()->push_back(RHS.front());
69 *Val.template get<VecTy*>() = *RHS.Val.template get<VecTy*>();
74 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
75 RHS.Val = (EltTy)nullptr;
77 TinyPtrVector &operator=(TinyPtrVector &&RHS) {
85 // If this vector has been allocated on the heap, re-use it if cheap. If it
86 // would require more copying, just delete it and we'll steal the other
88 if (VecTy *V = Val.template dyn_cast<VecTy*>()) {
89 if (RHS.Val.template is<EltTy>()) {
91 V->push_back(RHS.front());
98 RHS.Val = (EltTy)nullptr;
102 /// Constructor from an ArrayRef.
104 /// This also is a constructor for individual array elements due to the single
105 /// element constructor for ArrayRef.
106 explicit TinyPtrVector(ArrayRef<EltTy> Elts)
107 : Val(Elts.size() == 1 ? PtrUnion(Elts[0])
108 : PtrUnion(new VecTy(Elts.begin(), Elts.end()))) {}
110 // implicit conversion operator to ArrayRef.
111 operator ArrayRef<EltTy>() const {
114 if (Val.template is<EltTy>())
115 return *Val.getAddrOfPtr1();
116 return *Val.template get<VecTy*>();
120 // This vector can be empty if it contains no element, or if it
121 // contains a pointer to an empty vector.
122 if (Val.isNull()) return true;
123 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
128 unsigned size() const {
131 if (Val.template is<EltTy>())
133 return Val.template get<VecTy*>()->size();
136 typedef const EltTy *const_iterator;
137 typedef EltTy *iterator;
140 if (Val.template is<EltTy>())
141 return Val.getAddrOfPtr1();
143 return Val.template get<VecTy *>()->begin();
147 if (Val.template is<EltTy>())
148 return begin() + (Val.isNull() ? 0 : 1);
150 return Val.template get<VecTy *>()->end();
153 const_iterator begin() const {
154 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
157 const_iterator end() const {
158 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
161 EltTy operator[](unsigned i) const {
162 assert(!Val.isNull() && "can't index into an empty vector");
163 if (EltTy V = Val.template dyn_cast<EltTy>()) {
164 assert(i == 0 && "tinyvector index out of range");
168 assert(i < Val.template get<VecTy*>()->size() &&
169 "tinyvector index out of range");
170 return (*Val.template get<VecTy*>())[i];
173 EltTy front() const {
174 assert(!empty() && "vector empty");
175 if (EltTy V = Val.template dyn_cast<EltTy>())
177 return Val.template get<VecTy*>()->front();
181 assert(!empty() && "vector empty");
182 if (EltTy V = Val.template dyn_cast<EltTy>())
184 return Val.template get<VecTy*>()->back();
187 void push_back(EltTy NewVal) {
188 assert(NewVal && "Can't add a null value");
190 // If we have nothing, add something.
196 // If we have a single value, convert to a vector.
197 if (EltTy V = Val.template dyn_cast<EltTy>()) {
199 Val.template get<VecTy*>()->push_back(V);
202 // Add the new value, we know we have a vector.
203 Val.template get<VecTy*>()->push_back(NewVal);
207 // If we have a single value, convert to empty.
208 if (Val.template is<EltTy>())
209 Val = (EltTy)nullptr;
210 else if (VecTy *Vec = Val.template get<VecTy*>())
215 // If we have a single value, convert to empty.
216 if (Val.template is<EltTy>()) {
217 Val = (EltTy)nullptr;
218 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
219 // If we have a vector form, just clear it.
222 // Otherwise, we're already empty.
225 iterator erase(iterator I) {
226 assert(I >= begin() && "Iterator to erase is out of bounds.");
227 assert(I < end() && "Erasing at past-the-end iterator.");
229 // If we have a single value, convert to empty.
230 if (Val.template is<EltTy>()) {
232 Val = (EltTy)nullptr;
233 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
234 // multiple items in a vector; just do the erase, there is no
235 // benefit to collapsing back to a pointer
236 return Vec->erase(I);
241 iterator erase(iterator S, iterator E) {
242 assert(S >= begin() && "Range to erase is out of bounds.");
243 assert(S <= E && "Trying to erase invalid range.");
244 assert(E <= end() && "Trying to erase past the end.");
246 if (Val.template is<EltTy>()) {
247 if (S == begin() && S != E)
248 Val = (EltTy)nullptr;
249 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
250 return Vec->erase(S, E);
255 iterator insert(iterator I, const EltTy &Elt) {
256 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
257 assert(I <= this->end() && "Inserting past the end of the vector.");
260 return std::prev(end());
262 assert(!Val.isNull() && "Null value with non-end insert iterator.");
263 if (EltTy V = Val.template dyn_cast<EltTy>()) {
264 assert(I == begin());
270 return Val.template get<VecTy*>()->insert(I, Elt);
273 template<typename ItTy>
274 iterator insert(iterator I, ItTy From, ItTy To) {
275 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
276 assert(I <= this->end() && "Inserting past the end of the vector.");
280 // If we have a single value, convert to a vector.
281 ptrdiff_t Offset = I - begin();
283 if (std::next(From) == To) {
289 } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
291 Val.template get<VecTy*>()->push_back(V);
293 return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
296 } // end namespace llvm