add a handy typedef.
[oota-llvm.git] / include / llvm / ADT / ScopedHashTable.h
1 //===- ScopedHashTable.h - A simple scoped hash table ---------------------===//
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 implements an efficient scoped hash table, which is useful for
11 // things like dominator-based optimizations.  This allows clients to do things
12 // like this:
13 //
14 //  ScopedHashTable<int, int> HT;
15 //  {
16 //    ScopedHashTableScope<int, int> Scope1(HT);
17 //    HT.insert(0, 0);
18 //    HT.insert(1, 1);
19 //    {
20 //      ScopedHashTableScope<int, int> Scope2(HT);
21 //      HT.insert(0, 42);
22 //    }
23 //  }
24 //
25 // Looking up the value for "0" in the Scope2 block will return 42.  Looking
26 // up the value for 0 before 42 is inserted or after Scope2 is popped will
27 // return 0.
28 //
29 //===----------------------------------------------------------------------===//
30
31 #ifndef LLVM_ADT_SCOPEDHASHTABLE_H
32 #define LLVM_ADT_SCOPEDHASHTABLE_H
33
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/Support/Allocator.h"
36
37 namespace llvm {
38
39 template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
40           typename AllocatorTy = MallocAllocator>
41 class ScopedHashTable;
42
43 template <typename K, typename V>
44 class ScopedHashTableVal {
45   ScopedHashTableVal *NextInScope;
46   ScopedHashTableVal *NextForKey;
47   K Key;
48   V Val;
49   ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
50 public:
51
52   const K &getKey() const { return Key; }
53   const V &getValue() const { return Val; }
54   V &getValue() { return Val; }
55
56   ScopedHashTableVal *getNextForKey() { return NextForKey; }
57   const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
58   ScopedHashTableVal *getNextInScope() { return NextInScope; }
59   
60   template <typename AllocatorTy>
61   static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
62                                     ScopedHashTableVal *nextForKey,
63                                     const K &key, const V &val,
64                                     AllocatorTy &Allocator) {
65     ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
66     // Set up the value.
67     new (New) ScopedHashTableVal(key, val);
68     New->NextInScope = nextInScope;
69     New->NextForKey = nextForKey; 
70     return New;
71   }
72   
73   template <typename AllocatorTy>
74   void Destroy(AllocatorTy &Allocator) {
75     // Free memory referenced by the item.
76     this->~ScopedHashTableVal();
77     Allocator.Deallocate(this);
78   }
79 };
80
81 template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
82           typename AllocatorTy = MallocAllocator>
83 class ScopedHashTableScope {
84   /// HT - The hashtable that we are active for.
85   ScopedHashTable<K, V, KInfo, AllocatorTy> &HT;
86
87   /// PrevScope - This is the scope that we are shadowing in HT.
88   ScopedHashTableScope *PrevScope;
89
90   /// LastValInScope - This is the last value that was inserted for this scope
91   /// or null if none have been inserted yet.
92   ScopedHashTableVal<K, V> *LastValInScope;
93   void operator=(ScopedHashTableScope&);       // DO NOT IMPLEMENT
94   ScopedHashTableScope(ScopedHashTableScope&); // DO NOT IMPLEMENT
95 public:
96   ScopedHashTableScope(ScopedHashTable<K, V, KInfo, AllocatorTy> &HT);
97   ~ScopedHashTableScope();
98
99 private:
100   friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
101   ScopedHashTableVal<K, V> *getLastValInScope() {
102     return LastValInScope;
103   }
104   void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
105     LastValInScope = Val;
106   }
107 };
108
109
110 template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
111 class ScopedHashTableIterator {
112   ScopedHashTableVal<K, V> *Node;
113 public:
114   ScopedHashTableIterator(ScopedHashTableVal<K, V> *node) : Node(node) {}
115
116   V &operator*() const {
117     assert(Node && "Dereference end()");
118     return Node->getValue();
119   }
120   V *operator->() const {
121     return &Node->getValue();
122   }
123
124   bool operator==(const ScopedHashTableIterator &RHS) const {
125     return Node == RHS.Node;
126   }
127   bool operator!=(const ScopedHashTableIterator &RHS) const {
128     return Node != RHS.Node;
129   }
130
131   inline ScopedHashTableIterator& operator++() {          // Preincrement
132     assert(Node && "incrementing past end()");
133     Node = Node->getNextForKey();
134     return *this;
135   }
136   ScopedHashTableIterator operator++(int) {        // Postincrement
137     ScopedHashTableIterator tmp = *this; ++*this; return tmp;
138   }
139 };
140
141
142 template <typename K, typename V, typename KInfo, typename AllocatorTy>
143 class ScopedHashTable {
144   typedef ScopedHashTableVal<K, V> ValTy;
145   DenseMap<K, ValTy*, KInfo> TopLevelMap;
146   ScopedHashTableScope<K, V, KInfo, AllocatorTy> *CurScope;
147   
148   AllocatorTy Allocator;
149   
150   ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
151   void operator=(const ScopedHashTable&);  // NOT YET IMPLEMENTED
152   friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
153 public:
154   ScopedHashTable() : CurScope(0) {}
155   ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
156   ~ScopedHashTable() {
157     assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
158   }
159   
160   /// ScopeTy - This is a helpful typedef that allows clients to get easy access
161   /// to the name of the scope for this hash table.
162   typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
163
164   /// Access to the allocator.
165   typedef typename ReferenceAdder<AllocatorTy>::result AllocatorRefTy;
166   typedef typename ReferenceAdder<const AllocatorTy>::result AllocatorCRefTy;
167   AllocatorRefTy getAllocator() { return Allocator; }
168   AllocatorCRefTy getAllocator() const { return Allocator; }
169
170   bool count(const K &Key) const {
171     return TopLevelMap.count(Key);
172   }
173
174   V lookup(const K &Key) {
175     typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
176     if (I != TopLevelMap.end())
177       return I->second->getValue();
178       
179     return V();
180   }
181
182   void insert(const K &Key, const V &Val) {
183     assert(CurScope && "No scope active!");
184
185     ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
186
187     KeyEntry = ValTy::Create(CurScope->getLastValInScope(), KeyEntry, Key, Val,
188                              Allocator);
189     CurScope->setLastValInScope(KeyEntry);
190   }
191
192   typedef ScopedHashTableIterator<K, V, KInfo> iterator;
193
194   iterator end() { return iterator(0); }
195
196   iterator begin(const K &Key) {
197     typename DenseMap<K, ValTy*, KInfo>::iterator I =
198       TopLevelMap.find(Key);
199     if (I == TopLevelMap.end()) return end();
200     return iterator(I->second);
201   }
202 };
203
204 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
205 /// table.
206 template <typename K, typename V, typename KInfo, typename Allocator>
207 ScopedHashTableScope<K, V, KInfo, Allocator>::
208   ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
209   PrevScope = HT.CurScope;
210   HT.CurScope = this;
211   LastValInScope = 0;
212 }
213
214 template <typename K, typename V, typename KInfo, typename Allocator>
215 ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
216   assert(HT.CurScope == this && "Scope imbalance!");
217   HT.CurScope = PrevScope;
218
219   // Pop and delete all values corresponding to this scope.
220   while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
221     // Pop this value out of the TopLevelMap.
222     if (ThisEntry->getNextForKey() == 0) {
223       assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
224              "Scope imbalance!");
225       HT.TopLevelMap.erase(ThisEntry->getKey());
226     } else {
227       ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
228       assert(KeyEntry == ThisEntry && "Scope imbalance!");
229       KeyEntry = ThisEntry->getNextForKey();
230     }
231
232     // Pop this value out of the scope.
233     LastValInScope = ThisEntry->getNextInScope();
234
235     // Delete this entry.
236     ThisEntry->Destroy(HT.getAllocator());
237   }
238 }
239
240 } // end namespace llvm
241
242 #endif