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*>();
119 // implicit conversion operator to MutableArrayRef.
120 operator MutableArrayRef<EltTy>() {
123 if (Val.template is<EltTy>())
124 return *Val.getAddrOfPtr1();
125 return *Val.template get<VecTy*>();
129 // This vector can be empty if it contains no element, or if it
130 // contains a pointer to an empty vector.
131 if (Val.isNull()) return true;
132 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
137 unsigned size() const {
140 if (Val.template is<EltTy>())
142 return Val.template get<VecTy*>()->size();
145 typedef const EltTy *const_iterator;
146 typedef EltTy *iterator;
149 if (Val.template is<EltTy>())
150 return Val.getAddrOfPtr1();
152 return Val.template get<VecTy *>()->begin();
156 if (Val.template is<EltTy>())
157 return begin() + (Val.isNull() ? 0 : 1);
159 return Val.template get<VecTy *>()->end();
162 const_iterator begin() const {
163 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
166 const_iterator end() const {
167 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
170 EltTy operator[](unsigned i) const {
171 assert(!Val.isNull() && "can't index into an empty vector");
172 if (EltTy V = Val.template dyn_cast<EltTy>()) {
173 assert(i == 0 && "tinyvector index out of range");
177 assert(i < Val.template get<VecTy*>()->size() &&
178 "tinyvector index out of range");
179 return (*Val.template get<VecTy*>())[i];
182 EltTy front() const {
183 assert(!empty() && "vector empty");
184 if (EltTy V = Val.template dyn_cast<EltTy>())
186 return Val.template get<VecTy*>()->front();
190 assert(!empty() && "vector empty");
191 if (EltTy V = Val.template dyn_cast<EltTy>())
193 return Val.template get<VecTy*>()->back();
196 void push_back(EltTy NewVal) {
197 assert(NewVal && "Can't add a null value");
199 // If we have nothing, add something.
205 // If we have a single value, convert to a vector.
206 if (EltTy V = Val.template dyn_cast<EltTy>()) {
208 Val.template get<VecTy*>()->push_back(V);
211 // Add the new value, we know we have a vector.
212 Val.template get<VecTy*>()->push_back(NewVal);
216 // If we have a single value, convert to empty.
217 if (Val.template is<EltTy>())
218 Val = (EltTy)nullptr;
219 else if (VecTy *Vec = Val.template get<VecTy*>())
224 // If we have a single value, convert to empty.
225 if (Val.template is<EltTy>()) {
226 Val = (EltTy)nullptr;
227 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
228 // If we have a vector form, just clear it.
231 // Otherwise, we're already empty.
234 iterator erase(iterator I) {
235 assert(I >= begin() && "Iterator to erase is out of bounds.");
236 assert(I < end() && "Erasing at past-the-end iterator.");
238 // If we have a single value, convert to empty.
239 if (Val.template is<EltTy>()) {
241 Val = (EltTy)nullptr;
242 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
243 // multiple items in a vector; just do the erase, there is no
244 // benefit to collapsing back to a pointer
245 return Vec->erase(I);
250 iterator erase(iterator S, iterator E) {
251 assert(S >= begin() && "Range to erase is out of bounds.");
252 assert(S <= E && "Trying to erase invalid range.");
253 assert(E <= end() && "Trying to erase past the end.");
255 if (Val.template is<EltTy>()) {
256 if (S == begin() && S != E)
257 Val = (EltTy)nullptr;
258 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
259 return Vec->erase(S, E);
264 iterator insert(iterator I, const EltTy &Elt) {
265 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
266 assert(I <= this->end() && "Inserting past the end of the vector.");
269 return std::prev(end());
271 assert(!Val.isNull() && "Null value with non-end insert iterator.");
272 if (EltTy V = Val.template dyn_cast<EltTy>()) {
273 assert(I == begin());
279 return Val.template get<VecTy*>()->insert(I, Elt);
282 template<typename ItTy>
283 iterator insert(iterator I, ItTy From, ItTy To) {
284 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
285 assert(I <= this->end() && "Inserting past the end of the vector.");
289 // If we have a single value, convert to a vector.
290 ptrdiff_t Offset = I - begin();
292 if (std::next(From) == To) {
298 } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
300 Val.template get<VecTy*>()->push_back(V);
302 return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
305 } // end namespace llvm