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.template getAddrOf<EltTy>();
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 *iterator;
67 iterator begin() const {
71 if (Val.template is<EltTy>())
72 return Val.template getAddrOf<EltTy>();
74 return Val.template get<VecTy *>()->begin();
77 iterator end() const {
81 if (Val.template is<EltTy>())
84 return Val.template get<VecTy *>()->end();
88 EltTy operator[](unsigned i) const {
89 assert(!Val.isNull() && "can't index into an empty vector");
90 if (EltTy V = Val.template dyn_cast<EltTy>()) {
91 assert(i == 0 && "tinyvector index out of range");
95 assert(i < Val.template get<VecTy*>()->size() &&
96 "tinyvector index out of range");
97 return (*Val.template get<VecTy*>())[i];
100 EltTy front() const {
101 assert(!empty() && "vector empty");
102 if (EltTy V = Val.template dyn_cast<EltTy>())
104 return Val.template get<VecTy*>()->front();
107 void push_back(EltTy NewVal) {
108 assert(NewVal != 0 && "Can't add a null value");
110 // If we have nothing, add something.
116 // If we have a single value, convert to a vector.
117 if (EltTy V = Val.template dyn_cast<EltTy>()) {
119 Val.template get<VecTy*>()->push_back(V);
122 // Add the new value, we know we have a vector.
123 Val.template get<VecTy*>()->push_back(NewVal);
127 // If we have a single value, convert to empty.
128 if (Val.template is<EltTy>()) {
130 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
131 // If we have a vector form, just clear it.
134 // Otherwise, we're already empty.
138 void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
140 } // end namespace llvm