8fbd6f61e27353f6d1a99324fe0d4b90e916c694
[oota-llvm.git] / include / llvm / ADT / TinyPtrVector.h
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
20 namespace 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 ///
28 template <typename EltTy>
29 class TinyPtrVector {
30 public:
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   EltTy operator[](unsigned i) const {
62     assert(!Val.isNull() && "can't index into an empty vector");
63     if (EltTy V = Val.template dyn_cast<EltTy>()) {
64       assert(i == 0 && "tinyvector index out of range");
65       return V;
66     }
67     
68     assert(i < Val. template get<VecTy*>()->size() && 
69            "tinyvector index out of range");
70     return (*Val. template get<VecTy*>())[i];
71   }
72   
73   EltTy front() const {
74     assert(!empty() && "vector empty");
75     if (EltTy V = Val.template dyn_cast<EltTy>())
76       return V;
77     return Val.template get<VecTy*>()->front();
78   }
79   
80   void push_back(EltTy NewVal) {
81     assert(NewVal != 0 && "Can't add a null value");
82     
83     // If we have nothing, add something.
84     if (Val.isNull()) {
85       Val = NewVal;
86       return;
87     }
88     
89     // If we have a single value, convert to a vector.
90     if (EltTy V = Val.template  dyn_cast<EltTy>()) {
91       Val = new VecTy();
92       Val.template get<VecTy*>()->push_back(V);
93     }
94     
95     // Add the new value, we know we have a vector.
96     Val.template get<VecTy*>()->push_back(NewVal);
97   }
98   
99   void clear() {
100     // If we have a single value, convert to empty.
101     if (Val.template is<EltTy>()) {
102       Val = (EltTy)0;
103     } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
104       // If we have a vector form, just clear it.
105       Vec->clear();
106     }
107     // Otherwise, we're already empty.
108   }
109   
110 private:
111   void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
112 };
113 } // end namespace llvm
114
115 #endif