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 // This file defines the Type class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_TINYPTRVECTOR_H
15 #define LLVM_ADT_TINYPTRVECTOR_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/PointerUnion.h"
22 /// TinyPtrVector - This class is specialized for cases where there are
23 /// normally 0 or 1 element in a vector, but is general enough to go beyond that
26 /// NOTE: This container doesn't allow you to store a null pointer into it.
28 template <typename EltTy>
31 typedef llvm::SmallVector<EltTy, 4> VecTy;
32 llvm::PointerUnion<EltTy, VecTy*> Val;
35 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
36 if (VecTy *V = Val.template dyn_cast<VecTy*>())
40 if (VecTy *V = Val.template dyn_cast<VecTy*>())
44 /// empty() - This vector can be empty if it contains no element, or if it
45 /// contains a pointer to an empty vector.
47 if (Val.isNull()) return true;
48 if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
53 unsigned size() const {
56 if (Val. template is<EltTy>())
58 return Val. template get<VecTy*>()->size();
61 typedef const EltTy *iterator;
62 iterator begin() const {
66 if (Val.template is<EltTy>())
67 return Val.template getAddrOf<EltTy>();
69 return Val.template get<VecTy *>()->begin();
72 iterator end() const {
76 if (Val.template is<EltTy>())
79 return Val.template get<VecTy *>()->end();
83 EltTy operator[](unsigned i) const {
84 assert(!Val.isNull() && "can't index into an empty vector");
85 if (EltTy V = Val.template dyn_cast<EltTy>()) {
86 assert(i == 0 && "tinyvector index out of range");
90 assert(i < Val. template get<VecTy*>()->size() &&
91 "tinyvector index out of range");
92 return (*Val. template get<VecTy*>())[i];
96 assert(!empty() && "vector empty");
97 if (EltTy V = Val.template dyn_cast<EltTy>())
99 return Val.template get<VecTy*>()->front();
102 void push_back(EltTy NewVal) {
103 assert(NewVal != 0 && "Can't add a null value");
105 // If we have nothing, add something.
111 // If we have a single value, convert to a vector.
112 if (EltTy V = Val.template dyn_cast<EltTy>()) {
114 Val.template get<VecTy*>()->push_back(V);
117 // Add the new value, we know we have a vector.
118 Val.template get<VecTy*>()->push_back(NewVal);
122 // If we have a single value, convert to empty.
123 if (Val.template is<EltTy>()) {
125 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
126 // If we have a vector form, just clear it.
129 // Otherwise, we're already empty.
133 void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
135 } // end namespace llvm