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 #if LLVM_USE_RVALUE_REFERENCES
74 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
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());
103 // implicit conversion operator to ArrayRef.
104 operator ArrayRef<EltTy>() const {
106 return ArrayRef<EltTy>();
107 if (Val.template is<EltTy>())
108 return *Val.getAddrOfPtr1();
109 return *Val.template get<VecTy*>();
113 // This vector can be empty if it contains no element, or if it
114 // contains a pointer to an empty vector.
115 if (Val.isNull()) return true;
116 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
121 unsigned size() const {
124 if (Val.template is<EltTy>())
126 return Val.template get<VecTy*>()->size();
129 typedef const EltTy *const_iterator;
130 typedef EltTy *iterator;
133 if (Val.template is<EltTy>())
134 return Val.getAddrOfPtr1();
136 return Val.template get<VecTy *>()->begin();
140 if (Val.template is<EltTy>())
141 return begin() + (Val.isNull() ? 0 : 1);
143 return Val.template get<VecTy *>()->end();
146 const_iterator begin() const {
147 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
150 const_iterator end() const {
151 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
154 EltTy operator[](unsigned i) const {
155 assert(!Val.isNull() && "can't index into an empty vector");
156 if (EltTy V = Val.template dyn_cast<EltTy>()) {
157 assert(i == 0 && "tinyvector index out of range");
161 assert(i < Val.template get<VecTy*>()->size() &&
162 "tinyvector index out of range");
163 return (*Val.template get<VecTy*>())[i];
166 EltTy front() const {
167 assert(!empty() && "vector empty");
168 if (EltTy V = Val.template dyn_cast<EltTy>())
170 return Val.template get<VecTy*>()->front();
174 assert(!empty() && "vector empty");
175 if (EltTy V = Val.template dyn_cast<EltTy>())
177 return Val.template get<VecTy*>()->back();
180 void push_back(EltTy NewVal) {
181 assert(NewVal != 0 && "Can't add a null value");
183 // If we have nothing, add something.
189 // If we have a single value, convert to a vector.
190 if (EltTy V = Val.template dyn_cast<EltTy>()) {
192 Val.template get<VecTy*>()->push_back(V);
195 // Add the new value, we know we have a vector.
196 Val.template get<VecTy*>()->push_back(NewVal);
200 // If we have a single value, convert to empty.
201 if (Val.template is<EltTy>())
203 else if (VecTy *Vec = Val.template get<VecTy*>())
208 // If we have a single value, convert to empty.
209 if (Val.template is<EltTy>()) {
211 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
212 // If we have a vector form, just clear it.
215 // Otherwise, we're already empty.
218 iterator erase(iterator I) {
219 assert(I >= begin() && "Iterator to erase is out of bounds.");
220 assert(I < end() && "Erasing at past-the-end iterator.");
222 // If we have a single value, convert to empty.
223 if (Val.template is<EltTy>()) {
226 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
227 // multiple items in a vector; just do the erase, there is no
228 // benefit to collapsing back to a pointer
229 return Vec->erase(I);
234 iterator erase(iterator S, iterator E) {
235 assert(S >= begin() && "Range to erase is out of bounds.");
236 assert(S <= E && "Trying to erase invalid range.");
237 assert(E <= end() && "Trying to erase past the end.");
239 if (Val.template is<EltTy>()) {
240 if (S == begin() && S != E)
242 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
243 return Vec->erase(S, E);
248 iterator insert(iterator I, const EltTy &Elt) {
249 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
250 assert(I <= this->end() && "Inserting past the end of the vector.");
253 return llvm::prior(end());
255 assert(!Val.isNull() && "Null value with non-end insert iterator.");
256 if (EltTy V = Val.template dyn_cast<EltTy>()) {
257 assert(I == begin());
263 return Val.template get<VecTy*>()->insert(I, Elt);
266 template<typename ItTy>
267 iterator insert(iterator I, ItTy From, ItTy To) {
268 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
269 assert(I <= this->end() && "Inserting past the end of the vector.");
273 // If we have a single value, convert to a vector.
274 ptrdiff_t Offset = I - begin();
276 if (llvm::next(From) == To) {
282 } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
284 Val.template get<VecTy*>()->push_back(V);
286 return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
289 } // end namespace llvm