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/SmallVector.h"
14 #include "llvm/ADT/PointerUnion.h"
18 /// TinyPtrVector - This class is specialized for cases where there are
19 /// normally 0 or 1 element in a vector, but is general enough to go beyond that
22 /// NOTE: This container doesn't allow you to store a null pointer into it.
24 template <typename EltTy>
27 typedef llvm::SmallVector<EltTy, 4> VecTy;
28 llvm::PointerUnion<EltTy, VecTy*> Val;
31 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
32 if (VecTy *V = Val.template dyn_cast<VecTy*>())
36 if (VecTy *V = Val.template dyn_cast<VecTy*>())
40 // implicit conversion operator to ArrayRef.
41 operator ArrayRef<EltTy>() const {
43 return ArrayRef<EltTy>();
44 if (Val.template is<EltTy>())
45 return *Val.getAddrOfPtr1();
46 return *Val.template get<VecTy*>();
50 // This vector can be empty if it contains no element, or if it
51 // contains a pointer to an empty vector.
52 if (Val.isNull()) return true;
53 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
58 unsigned size() const {
61 if (Val.template is<EltTy>())
63 return Val.template get<VecTy*>()->size();
66 typedef const EltTy *const_iterator;
67 typedef EltTy *iterator;
73 if (Val.template is<EltTy>())
74 return Val.getAddrOfPtr1();
76 return Val.template get<VecTy *>()->begin();
83 if (Val.template is<EltTy>())
86 return Val.template get<VecTy *>()->end();
89 const_iterator begin() const {
90 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
93 const_iterator end() const {
94 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
97 EltTy operator[](unsigned i) const {
98 assert(!Val.isNull() && "can't index into an empty vector");
99 if (EltTy V = Val.template dyn_cast<EltTy>()) {
100 assert(i == 0 && "tinyvector index out of range");
104 assert(i < Val.template get<VecTy*>()->size() &&
105 "tinyvector index out of range");
106 return (*Val.template get<VecTy*>())[i];
109 EltTy front() const {
110 assert(!empty() && "vector empty");
111 if (EltTy V = Val.template dyn_cast<EltTy>())
113 return Val.template get<VecTy*>()->front();
116 void push_back(EltTy NewVal) {
117 assert(NewVal != 0 && "Can't add a null value");
119 // If we have nothing, add something.
125 // If we have a single value, convert to a vector.
126 if (EltTy V = Val.template dyn_cast<EltTy>()) {
128 Val.template get<VecTy*>()->push_back(V);
131 // Add the new value, we know we have a vector.
132 Val.template get<VecTy*>()->push_back(NewVal);
136 // If we have a single value, convert to empty.
137 if (Val.template is<EltTy>()) {
139 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
140 // If we have a vector form, just clear it.
143 // Otherwise, we're already empty.
146 iterator erase(iterator I) {
147 // If we have a single value, convert to empty.
148 if (Val.template is<EltTy>()) {
151 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
152 // multiple items in a vector; just do the erase, there is no
153 // benefit to collapsing back to a pointer
154 return Vec->erase(I);
161 void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
163 } // end namespace llvm