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 llvm::PointerUnion<EltTy, VecTy*> Val;
33 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
34 if (VecTy *V = Val.template dyn_cast<VecTy*>())
37 #if LLVM_USE_RVALUE_REFERENCES
38 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
43 if (VecTy *V = Val.template dyn_cast<VecTy*>())
47 // implicit conversion operator to ArrayRef.
48 operator ArrayRef<EltTy>() const {
50 return ArrayRef<EltTy>();
51 if (Val.template is<EltTy>())
52 return *Val.getAddrOfPtr1();
53 return *Val.template get<VecTy*>();
57 // This vector can be empty if it contains no element, or if it
58 // contains a pointer to an empty vector.
59 if (Val.isNull()) return true;
60 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
65 unsigned size() const {
68 if (Val.template is<EltTy>())
70 return Val.template get<VecTy*>()->size();
73 typedef const EltTy *const_iterator;
74 typedef EltTy *iterator;
80 if (Val.template is<EltTy>())
81 return Val.getAddrOfPtr1();
83 return Val.template get<VecTy *>()->begin();
90 if (Val.template is<EltTy>())
93 return Val.template get<VecTy *>()->end();
96 const_iterator begin() const {
97 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
100 const_iterator end() const {
101 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
104 EltTy operator[](unsigned i) const {
105 assert(!Val.isNull() && "can't index into an empty vector");
106 if (EltTy V = Val.template dyn_cast<EltTy>()) {
107 assert(i == 0 && "tinyvector index out of range");
111 assert(i < Val.template get<VecTy*>()->size() &&
112 "tinyvector index out of range");
113 return (*Val.template get<VecTy*>())[i];
116 EltTy front() const {
117 assert(!empty() && "vector empty");
118 if (EltTy V = Val.template dyn_cast<EltTy>())
120 return Val.template get<VecTy*>()->front();
124 assert(!empty() && "vector empty");
125 if (EltTy V = Val.template dyn_cast<EltTy>())
127 return Val.template get<VecTy*>()->back();
131 void push_back(EltTy NewVal) {
132 assert(NewVal != 0 && "Can't add a null value");
134 // If we have nothing, add something.
140 // If we have a single value, convert to a vector.
141 if (EltTy V = Val.template dyn_cast<EltTy>()) {
143 Val.template get<VecTy*>()->push_back(V);
146 // Add the new value, we know we have a vector.
147 Val.template get<VecTy*>()->push_back(NewVal);
151 // If we have a single value, convert to empty.
152 if (Val.template is<EltTy>())
154 else if (VecTy *Vec = Val.template get<VecTy*>())
160 // If we have a single value, convert to empty.
161 if (Val.template is<EltTy>()) {
163 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
164 // If we have a vector form, just clear it.
167 // Otherwise, we're already empty.
170 iterator erase(iterator I) {
171 // If we have a single value, convert to empty.
172 if (Val.template is<EltTy>()) {
175 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
176 // multiple items in a vector; just do the erase, there is no
177 // benefit to collapsing back to a pointer
178 return Vec->erase(I);
185 void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
186 #if LLVM_USE_RVALUE_REFERENCES
187 void operator=(TinyPtrVector&&); // NOT IMPLEMENTED YET.
190 } // end namespace llvm