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/SmallVector.h"
15 #include "llvm/ADT/PointerUnion.h"
16 #include "llvm/Support/Compiler.h"
20 /// TinyPtrVector - This class is specialized for cases where there are
21 /// normally 0 or 1 element in a vector, but is general enough to go beyond that
24 /// NOTE: This container doesn't allow you to store a null pointer into it.
26 template <typename EltTy>
29 typedef llvm::SmallVector<EltTy, 4> VecTy;
30 typedef typename VecTy::value_type value_type;
32 llvm::PointerUnion<EltTy, VecTy*> Val;
36 if (VecTy *V = Val.template dyn_cast<VecTy*>())
40 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
41 if (VecTy *V = Val.template dyn_cast<VecTy*>())
44 TinyPtrVector &operator=(const TinyPtrVector &RHS) {
52 // Try to squeeze into the single slot. If it won't fit, allocate a copied
54 if (Val.template is<EltTy>()) {
58 Val = new VecTy(*RHS.Val.template get<VecTy*>());
62 // If we have a full vector allocated, try to re-use it.
63 if (RHS.Val.template is<EltTy>()) {
64 Val.template get<VecTy*>()->clear();
65 Val.template get<VecTy*>()->push_back(RHS.front());
67 *Val.template get<VecTy*>() = *RHS.Val.template get<VecTy*>();
72 #if LLVM_USE_RVALUE_REFERENCES
73 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
76 TinyPtrVector &operator=(TinyPtrVector &&RHS) {
84 // If this vector has been allocated on the heap, re-use it if cheap. If it
85 // would require more copying, just delete it and we'll steal the other
87 if (VecTy *V = Val.template dyn_cast<VecTy*>()) {
88 if (RHS.Val.template is<EltTy>()) {
90 V->push_back(RHS.front());
102 // implicit conversion operator to ArrayRef.
103 operator ArrayRef<EltTy>() const {
105 return ArrayRef<EltTy>();
106 if (Val.template is<EltTy>())
107 return *Val.getAddrOfPtr1();
108 return *Val.template get<VecTy*>();
112 // This vector can be empty if it contains no element, or if it
113 // contains a pointer to an empty vector.
114 if (Val.isNull()) return true;
115 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
120 unsigned size() const {
123 if (Val.template is<EltTy>())
125 return Val.template get<VecTy*>()->size();
128 typedef const EltTy *const_iterator;
129 typedef EltTy *iterator;
132 if (Val.template is<EltTy>())
133 return Val.getAddrOfPtr1();
135 return Val.template get<VecTy *>()->begin();
139 if (Val.template is<EltTy>())
140 return begin() + (Val.isNull() ? 0 : 1);
142 return Val.template get<VecTy *>()->end();
145 const_iterator begin() const {
146 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
149 const_iterator end() const {
150 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
153 EltTy operator[](unsigned i) const {
154 assert(!Val.isNull() && "can't index into an empty vector");
155 if (EltTy V = Val.template dyn_cast<EltTy>()) {
156 assert(i == 0 && "tinyvector index out of range");
160 assert(i < Val.template get<VecTy*>()->size() &&
161 "tinyvector index out of range");
162 return (*Val.template get<VecTy*>())[i];
165 EltTy front() const {
166 assert(!empty() && "vector empty");
167 if (EltTy V = Val.template dyn_cast<EltTy>())
169 return Val.template get<VecTy*>()->front();
173 assert(!empty() && "vector empty");
174 if (EltTy V = Val.template dyn_cast<EltTy>())
176 return Val.template get<VecTy*>()->back();
179 void push_back(EltTy NewVal) {
180 assert(NewVal != 0 && "Can't add a null value");
182 // If we have nothing, add something.
188 // If we have a single value, convert to a vector.
189 if (EltTy V = Val.template dyn_cast<EltTy>()) {
191 Val.template get<VecTy*>()->push_back(V);
194 // Add the new value, we know we have a vector.
195 Val.template get<VecTy*>()->push_back(NewVal);
199 // If we have a single value, convert to empty.
200 if (Val.template is<EltTy>())
202 else if (VecTy *Vec = Val.template get<VecTy*>())
207 // If we have a single value, convert to empty.
208 if (Val.template is<EltTy>()) {
210 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
211 // If we have a vector form, just clear it.
214 // Otherwise, we're already empty.
217 iterator erase(iterator I) {
218 assert(I >= begin() && "Iterator to erase is out of bounds.");
219 assert(I < end() && "Erasing at past-the-end iterator.");
221 // If we have a single value, convert to empty.
222 if (Val.template is<EltTy>()) {
225 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
226 // multiple items in a vector; just do the erase, there is no
227 // benefit to collapsing back to a pointer
228 return Vec->erase(I);
233 } // end namespace llvm