5b5b1b547fe817b3a5fb82b1c5b1e41f2a859629
[oota-llvm.git] / include / llvm / ADT / TinyPtrVector.h
1 //===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ADT_TINYPTRVECTOR_H
11 #define LLVM_ADT_TINYPTRVECTOR_H
12
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"
18
19 namespace llvm {
20   
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
23 /// when required.
24 ///
25 /// NOTE: This container doesn't allow you to store a null pointer into it.
26 ///
27 template <typename EltTy>
28 class TinyPtrVector {
29 public:
30   typedef llvm::SmallVector<EltTy, 4> VecTy;
31   typedef typename VecTy::value_type value_type;
32
33   llvm::PointerUnion<EltTy, VecTy*> Val;
34
35   TinyPtrVector() {}
36   ~TinyPtrVector() {
37     if (VecTy *V = Val.template dyn_cast<VecTy*>())
38       delete V;
39   }
40
41   TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
42     if (VecTy *V = Val.template dyn_cast<VecTy*>())
43       Val = new VecTy(*V);
44   }
45   TinyPtrVector &operator=(const TinyPtrVector &RHS) {
46     if (this == &RHS)
47       return *this;
48     if (RHS.empty()) {
49       this->clear();
50       return *this;
51     }
52
53     // Try to squeeze into the single slot. If it won't fit, allocate a copied
54     // vector.
55     if (Val.template is<EltTy>()) {
56       if (RHS.size() == 1)
57         Val = RHS.front();
58       else
59         Val = new VecTy(*RHS.Val.template get<VecTy*>());
60       return *this;
61     }
62
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());
67     } else {
68       *Val.template get<VecTy*>() = *RHS.Val.template get<VecTy*>();
69     }
70     return *this;
71   }
72
73   TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
74     RHS.Val = (EltTy)0;
75   }
76   TinyPtrVector &operator=(TinyPtrVector &&RHS) {
77     if (this == &RHS)
78       return *this;
79     if (RHS.empty()) {
80       this->clear();
81       return *this;
82     }
83
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
86     // side.
87     if (VecTy *V = Val.template dyn_cast<VecTy*>()) {
88       if (RHS.Val.template is<EltTy>()) {
89         V->clear();
90         V->push_back(RHS.front());
91         return *this;
92       }
93       delete V;
94     }
95
96     Val = RHS.Val;
97     RHS.Val = (EltTy)0;
98     return *this;
99   }
100
101   // implicit conversion operator to ArrayRef.
102   operator ArrayRef<EltTy>() const {
103     if (Val.isNull())
104       return ArrayRef<EltTy>();
105     if (Val.template is<EltTy>())
106       return *Val.getAddrOfPtr1();
107     return *Val.template get<VecTy*>();
108   }
109
110   bool empty() const {
111     // This vector can be empty if it contains no element, or if it
112     // contains a pointer to an empty vector.
113     if (Val.isNull()) return true;
114     if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
115       return Vec->empty();
116     return false;
117   }
118
119   unsigned size() const {
120     if (empty())
121       return 0;
122     if (Val.template is<EltTy>())
123       return 1;
124     return Val.template get<VecTy*>()->size();
125   }
126
127   typedef const EltTy *const_iterator;
128   typedef EltTy *iterator;
129
130   iterator begin() {
131     if (Val.template is<EltTy>())
132       return Val.getAddrOfPtr1();
133
134     return Val.template get<VecTy *>()->begin();
135
136   }
137   iterator end() {
138     if (Val.template is<EltTy>())
139       return begin() + (Val.isNull() ? 0 : 1);
140
141     return Val.template get<VecTy *>()->end();
142   }
143
144   const_iterator begin() const {
145     return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
146   }
147
148   const_iterator end() const {
149     return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
150   }
151
152   EltTy operator[](unsigned i) const {
153     assert(!Val.isNull() && "can't index into an empty vector");
154     if (EltTy V = Val.template dyn_cast<EltTy>()) {
155       assert(i == 0 && "tinyvector index out of range");
156       return V;
157     }
158
159     assert(i < Val.template get<VecTy*>()->size() &&
160            "tinyvector index out of range");
161     return (*Val.template get<VecTy*>())[i];
162   }
163
164   EltTy front() const {
165     assert(!empty() && "vector empty");
166     if (EltTy V = Val.template dyn_cast<EltTy>())
167       return V;
168     return Val.template get<VecTy*>()->front();
169   }
170
171   EltTy back() const {
172     assert(!empty() && "vector empty");
173     if (EltTy V = Val.template dyn_cast<EltTy>())
174       return V;
175     return Val.template get<VecTy*>()->back();
176   }
177
178   void push_back(EltTy NewVal) {
179     assert(NewVal != 0 && "Can't add a null value");
180
181     // If we have nothing, add something.
182     if (Val.isNull()) {
183       Val = NewVal;
184       return;
185     }
186
187     // If we have a single value, convert to a vector.
188     if (EltTy V = Val.template dyn_cast<EltTy>()) {
189       Val = new VecTy();
190       Val.template get<VecTy*>()->push_back(V);
191     }
192
193     // Add the new value, we know we have a vector.
194     Val.template get<VecTy*>()->push_back(NewVal);
195   }
196
197   void pop_back() {
198     // If we have a single value, convert to empty.
199     if (Val.template is<EltTy>())
200       Val = (EltTy)0;
201     else if (VecTy *Vec = Val.template get<VecTy*>())
202       Vec->pop_back();
203   }
204
205   void clear() {
206     // If we have a single value, convert to empty.
207     if (Val.template is<EltTy>()) {
208       Val = (EltTy)0;
209     } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
210       // If we have a vector form, just clear it.
211       Vec->clear();
212     }
213     // Otherwise, we're already empty.
214   }
215
216   iterator erase(iterator I) {
217     assert(I >= begin() && "Iterator to erase is out of bounds.");
218     assert(I < end() && "Erasing at past-the-end iterator.");
219
220     // If we have a single value, convert to empty.
221     if (Val.template is<EltTy>()) {
222       if (I == begin())
223         Val = (EltTy)0;
224     } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
225       // multiple items in a vector; just do the erase, there is no
226       // benefit to collapsing back to a pointer
227       return Vec->erase(I);
228     }
229     return end();
230   }
231
232   iterator erase(iterator S, iterator E) {
233     assert(S >= begin() && "Range to erase is out of bounds.");
234     assert(S <= E && "Trying to erase invalid range.");
235     assert(E <= end() && "Trying to erase past the end.");
236
237     if (Val.template is<EltTy>()) {
238       if (S == begin() && S != E)
239         Val = (EltTy)0;
240     } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
241       return Vec->erase(S, E);
242     }
243     return end();
244   }
245
246   iterator insert(iterator I, const EltTy &Elt) {
247     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
248     assert(I <= this->end() && "Inserting past the end of the vector.");
249     if (I == end()) {
250       push_back(Elt);
251       return llvm::prior(end());
252     }
253     assert(!Val.isNull() && "Null value with non-end insert iterator.");
254     if (EltTy V = Val.template dyn_cast<EltTy>()) {
255       assert(I == begin());
256       Val = Elt;
257       push_back(V);
258       return begin();
259     }
260
261     return Val.template get<VecTy*>()->insert(I, Elt);
262   }
263
264   template<typename ItTy>
265   iterator insert(iterator I, ItTy From, ItTy To) {
266     assert(I >= this->begin() && "Insertion iterator is out of bounds.");
267     assert(I <= this->end() && "Inserting past the end of the vector.");
268     if (From == To)
269       return I;
270
271     // If we have a single value, convert to a vector.
272     ptrdiff_t Offset = I - begin();
273     if (Val.isNull()) {
274       if (llvm::next(From) == To) {
275         Val = *From;
276         return begin();
277       }
278
279       Val = new VecTy();
280     } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
281       Val = new VecTy();
282       Val.template get<VecTy*>()->push_back(V);
283     }
284     return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
285   }
286 };
287 } // end namespace llvm
288
289 #endif