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/None.h"
15 #include "llvm/ADT/PointerUnion.h"
16 #include "llvm/ADT/SmallVector.h"
20 #include <type_traits>
24 /// TinyPtrVector - This class is specialized for cases where there are
25 /// normally 0 or 1 element in a vector, but is general enough to go beyond that
28 /// NOTE: This container doesn't allow you to store a null pointer into it.
30 template <typename EltTy>
33 using VecTy = SmallVector<EltTy, 4>;
34 using value_type = typename VecTy::value_type;
35 using PtrUnion = PointerUnion<EltTy, VecTy *>;
41 TinyPtrVector() = default;
44 if (VecTy *V = Val.template dyn_cast<VecTy*>())
48 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
49 if (VecTy *V = Val.template dyn_cast<VecTy*>())
53 TinyPtrVector &operator=(const TinyPtrVector &RHS) {
61 // Try to squeeze into the single slot. If it won't fit, allocate a copied
63 if (Val.template is<EltTy>()) {
67 Val = new VecTy(*RHS.Val.template get<VecTy*>());
71 // If we have a full vector allocated, try to re-use it.
72 if (RHS.Val.template is<EltTy>()) {
73 Val.template get<VecTy*>()->clear();
74 Val.template get<VecTy*>()->push_back(RHS.front());
76 *Val.template get<VecTy*>() = *RHS.Val.template get<VecTy*>();
81 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
82 RHS.Val = (EltTy)nullptr;
85 TinyPtrVector &operator=(TinyPtrVector &&RHS) {
93 // If this vector has been allocated on the heap, re-use it if cheap. If it
94 // would require more copying, just delete it and we'll steal the other
96 if (VecTy *V = Val.template dyn_cast<VecTy*>()) {
97 if (RHS.Val.template is<EltTy>()) {
99 V->push_back(RHS.front());
106 RHS.Val = (EltTy)nullptr;
110 /// Constructor from an ArrayRef.
112 /// This also is a constructor for individual array elements due to the single
113 /// element constructor for ArrayRef.
114 explicit TinyPtrVector(ArrayRef<EltTy> Elts)
119 : PtrUnion(new VecTy(Elts.begin(), Elts.end()))) {}
121 TinyPtrVector(size_t Count, EltTy Value)
122 : Val(Count == 0 ? PtrUnion()
123 : Count == 1 ? PtrUnion(Value)
124 : PtrUnion(new VecTy(Count, Value))) {}
126 // implicit conversion operator to ArrayRef.
127 operator ArrayRef<EltTy>() const {
130 if (Val.template is<EltTy>())
131 return *Val.getAddrOfPtr1();
132 return *Val.template get<VecTy*>();
135 // implicit conversion operator to MutableArrayRef.
136 operator MutableArrayRef<EltTy>() {
139 if (Val.template is<EltTy>())
140 return *Val.getAddrOfPtr1();
141 return *Val.template get<VecTy*>();
144 // Implicit conversion to ArrayRef<U> if EltTy* implicitly converts to U*.
146 typename std::enable_if<
147 std::is_convertible<ArrayRef<EltTy>, ArrayRef<U>>::value,
149 operator ArrayRef<U>() const {
150 return operator ArrayRef<EltTy>();
154 // This vector can be empty if it contains no element, or if it
155 // contains a pointer to an empty vector.
156 if (Val.isNull()) return true;
157 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
162 unsigned size() const {
165 if (Val.template is<EltTy>())
167 return Val.template get<VecTy*>()->size();
170 using iterator = EltTy *;
171 using const_iterator = const EltTy *;
172 using reverse_iterator = std::reverse_iterator<iterator>;
173 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
176 if (Val.template is<EltTy>())
177 return Val.getAddrOfPtr1();
179 return Val.template get<VecTy *>()->begin();
183 if (Val.template is<EltTy>())
184 return begin() + (Val.isNull() ? 0 : 1);
186 return Val.template get<VecTy *>()->end();
189 const_iterator begin() const {
190 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
193 const_iterator end() const {
194 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
197 reverse_iterator rbegin() { return reverse_iterator(end()); }
198 reverse_iterator rend() { return reverse_iterator(begin()); }
200 const_reverse_iterator rbegin() const {
201 return const_reverse_iterator(end());
204 const_reverse_iterator rend() const {
205 return const_reverse_iterator(begin());
208 EltTy operator[](unsigned i) const {
209 assert(!Val.isNull() && "can't index into an empty vector");
210 if (EltTy V = Val.template dyn_cast<EltTy>()) {
211 assert(i == 0 && "tinyvector index out of range");
215 assert(i < Val.template get<VecTy*>()->size() &&
216 "tinyvector index out of range");
217 return (*Val.template get<VecTy*>())[i];
220 EltTy front() const {
221 assert(!empty() && "vector empty");
222 if (EltTy V = Val.template dyn_cast<EltTy>())
224 return Val.template get<VecTy*>()->front();
228 assert(!empty() && "vector empty");
229 if (EltTy V = Val.template dyn_cast<EltTy>())
231 return Val.template get<VecTy*>()->back();
234 void push_back(EltTy NewVal) {
235 assert(NewVal && "Can't add a null value");
237 // If we have nothing, add something.
243 // If we have a single value, convert to a vector.
244 if (EltTy V = Val.template dyn_cast<EltTy>()) {
246 Val.template get<VecTy*>()->push_back(V);
249 // Add the new value, we know we have a vector.
250 Val.template get<VecTy*>()->push_back(NewVal);
254 // If we have a single value, convert to empty.
255 if (Val.template is<EltTy>())
256 Val = (EltTy)nullptr;
257 else if (VecTy *Vec = Val.template get<VecTy*>())
262 // If we have a single value, convert to empty.
263 if (Val.template is<EltTy>()) {
264 Val = (EltTy)nullptr;
265 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
266 // If we have a vector form, just clear it.
269 // Otherwise, we're already empty.
272 iterator erase(iterator I) {
273 assert(I >= begin() && "Iterator to erase is out of bounds.");
274 assert(I < end() && "Erasing at past-the-end iterator.");
276 // If we have a single value, convert to empty.
277 if (Val.template is<EltTy>()) {
279 Val = (EltTy)nullptr;
280 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
281 // multiple items in a vector; just do the erase, there is no
282 // benefit to collapsing back to a pointer
283 return Vec->erase(I);
288 iterator erase(iterator S, iterator E) {
289 assert(S >= begin() && "Range to erase is out of bounds.");
290 assert(S <= E && "Trying to erase invalid range.");
291 assert(E <= end() && "Trying to erase past the end.");
293 if (Val.template is<EltTy>()) {
294 if (S == begin() && S != E)
295 Val = (EltTy)nullptr;
296 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
297 return Vec->erase(S, E);
302 iterator insert(iterator I, const EltTy &Elt) {
303 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
304 assert(I <= this->end() && "Inserting past the end of the vector.");
307 return std::prev(end());
309 assert(!Val.isNull() && "Null value with non-end insert iterator.");
310 if (EltTy V = Val.template dyn_cast<EltTy>()) {
311 assert(I == begin());
317 return Val.template get<VecTy*>()->insert(I, Elt);
320 template<typename ItTy>
321 iterator insert(iterator I, ItTy From, ItTy To) {
322 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
323 assert(I <= this->end() && "Inserting past the end of the vector.");
327 // If we have a single value, convert to a vector.
328 ptrdiff_t Offset = I - begin();
330 if (std::next(From) == To) {
336 } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
338 Val.template get<VecTy*>()->push_back(V);
340 return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
344 } // end namespace llvm
346 #endif // LLVM_ADT_TINYPTRVECTOR_H