TinyPtrVector.h revision 79976a40728c8baa8cd16de90173fe2c48937e22
1//===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Type class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_ADT_TINYPTRVECTOR_H 15#define LLVM_ADT_TINYPTRVECTOR_H 16 17#include "llvm/ADT/SmallVector.h" 18#include "llvm/ADT/PointerUnion.h" 19 20namespace llvm { 21 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 24/// when required. 25/// 26/// NOTE: This container doesn't allow you to store a null pointer into it. 27/// 28template <typename EltTy> 29class TinyPtrVector { 30public: 31 typedef llvm::SmallVector<EltTy, 4> VecTy; 32 llvm::PointerUnion<EltTy, VecTy*> Val; 33 34 TinyPtrVector() {} 35 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) { 36 if (VecTy *V = Val.template dyn_cast<VecTy*>()) 37 Val = new VecTy(*V); 38 } 39 ~TinyPtrVector() { 40 if (VecTy *V = Val.template dyn_cast<VecTy*>()) 41 delete V; 42 } 43 44 /// empty() - This vector can be empty if it contains no element, or if it 45 /// contains a pointer to an empty vector. 46 bool empty() const { 47 if (Val.isNull()) return true; 48 if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) 49 return Vec->empty(); 50 return false; 51 } 52 53 unsigned size() const { 54 if (empty()) 55 return 0; 56 if (Val. template is<EltTy>()) 57 return 1; 58 return Val. template get<VecTy*>()->size(); 59 } 60 61 typedef const EltTy *iterator; 62 iterator begin() const { 63 if (empty()) 64 return 0; 65 66 if (Val.template is<EltTy>()) 67 return Val.template getAddrOf<EltTy>(); 68 69 return Val.template get<VecTy *>()->begin(); 70 71 } 72 iterator end() const { 73 if (empty()) 74 return 0; 75 76 if (Val.template is<EltTy>()) 77 return begin() + 1; 78 79 return Val.template get<VecTy *>()->end(); 80 } 81 82 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"); 87 return V; 88 } 89 90 assert(i < Val. template get<VecTy*>()->size() && 91 "tinyvector index out of range"); 92 return (*Val. template get<VecTy*>())[i]; 93 } 94 95 EltTy front() const { 96 assert(!empty() && "vector empty"); 97 if (EltTy V = Val.template dyn_cast<EltTy>()) 98 return V; 99 return Val.template get<VecTy*>()->front(); 100 } 101 102 void push_back(EltTy NewVal) { 103 assert(NewVal != 0 && "Can't add a null value"); 104 105 // If we have nothing, add something. 106 if (Val.isNull()) { 107 Val = NewVal; 108 return; 109 } 110 111 // If we have a single value, convert to a vector. 112 if (EltTy V = Val.template dyn_cast<EltTy>()) { 113 Val = new VecTy(); 114 Val.template get<VecTy*>()->push_back(V); 115 } 116 117 // Add the new value, we know we have a vector. 118 Val.template get<VecTy*>()->push_back(NewVal); 119 } 120 121 void clear() { 122 // If we have a single value, convert to empty. 123 if (Val.template is<EltTy>()) { 124 Val = (EltTy)0; 125 } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) { 126 // If we have a vector form, just clear it. 127 Vec->clear(); 128 } 129 // Otherwise, we're already empty. 130 } 131 132private: 133 void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET. 134}; 135} // end namespace llvm 136 137#endif 138