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 /// empty() - This vector can be empty if it contains no element, or if it
41 /// contains a pointer to an empty vector.
43 if (Val.isNull()) return true;
44 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
49 unsigned size() const {
52 if (Val. template is<EltTy>())
54 return Val. template get<VecTy*>()->size();
57 typedef const EltTy *iterator;
58 iterator begin() const {
62 if (Val.template is<EltTy>())
63 return Val.template getAddrOf<EltTy>();
65 return Val.template get<VecTy *>()->begin();
68 iterator end() const {
72 if (Val.template is<EltTy>())
75 return Val.template get<VecTy *>()->end();
79 EltTy operator[](unsigned i) const {
80 assert(!Val.isNull() && "can't index into an empty vector");
81 if (EltTy V = Val.template dyn_cast<EltTy>()) {
82 assert(i == 0 && "tinyvector index out of range");
86 assert(i < Val. template get<VecTy*>()->size() &&
87 "tinyvector index out of range");
88 return (*Val. template get<VecTy*>())[i];
92 assert(!empty() && "vector empty");
93 if (EltTy V = Val.template dyn_cast<EltTy>())
95 return Val.template get<VecTy*>()->front();
98 void push_back(EltTy NewVal) {
99 assert(NewVal != 0 && "Can't add a null value");
101 // If we have nothing, add something.
107 // If we have a single value, convert to a vector.
108 if (EltTy V = Val.template dyn_cast<EltTy>()) {
110 Val.template get<VecTy*>()->push_back(V);
113 // Add the new value, we know we have a vector.
114 Val.template get<VecTy*>()->push_back(NewVal);
118 // If we have a single value, convert to empty.
119 if (Val.template is<EltTy>()) {
121 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
122 // If we have a vector form, just clear it.
125 // Otherwise, we're already empty.
129 void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
131 } // end namespace llvm