Enhance ScopedHashTable to allow it to take an allocator argument.
[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, typename KInfo = DenseMapInfo<K> >
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 class ScopedHashTableScope {
83   /// HT - The hashtable that we are active for.
84   ScopedHashTable<K, V, KInfo> &HT;
85
86   /// PrevScope - This is the scope that we are shadowing in HT.
87   ScopedHashTableScope *PrevScope;
88
89   /// LastValInScope - This is the last value that was inserted for this scope
90   /// or null if none have been inserted yet.
91   ScopedHashTableVal<K, V, KInfo> *LastValInScope;
92   void operator=(ScopedHashTableScope&);       // DO NOT IMPLEMENT
93   ScopedHashTableScope(ScopedHashTableScope&); // DO NOT IMPLEMENT
94 public:
95   ScopedHashTableScope(ScopedHashTable<K, V, KInfo> &HT);
96   ~ScopedHashTableScope();
97
98 private:
99   friend class ScopedHashTable<K, V, KInfo>;
100   ScopedHashTableVal<K, V, KInfo> *getLastValInScope() {
101     return LastValInScope;
102   }
103   void setLastValInScope(ScopedHashTableVal<K, V, KInfo> *Val) {
104     LastValInScope = Val;
105   }
106 };
107
108
109 template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
110 class ScopedHashTableIterator {
111   ScopedHashTableVal<K, V, KInfo> *Node;
112 public:
113   ScopedHashTableIterator(ScopedHashTableVal<K, V, KInfo> *node) : Node(node) {}
114
115   V &operator*() const {
116     assert(Node && "Dereference end()");
117     return Node->getValue();
118   }
119   V *operator->() const {
120     return &Node->getValue();
121   }
122
123   bool operator==(const ScopedHashTableIterator &RHS) const {
124     return Node == RHS.Node;
125   }
126   bool operator!=(const ScopedHashTableIterator &RHS) const {
127     return Node != RHS.Node;
128   }
129
130   inline ScopedHashTableIterator& operator++() {          // Preincrement
131     assert(Node && "incrementing past end()");
132     Node = Node->getNextForKey();
133     return *this;
134   }
135   ScopedHashTableIterator operator++(int) {        // Postincrement
136     ScopedHashTableIterator tmp = *this; ++*this; return tmp;
137   }
138 };
139
140
141 template <typename K, typename V, typename KInfo, typename AllocatorTy>
142 class ScopedHashTable {
143   typedef ScopedHashTableVal<K, V, KInfo> ValTy;
144   DenseMap<K, ValTy*, KInfo> TopLevelMap;
145   ScopedHashTableScope<K, V, KInfo> *CurScope;
146   
147   AllocatorTy Allocator;
148   
149   ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
150   void operator=(const ScopedHashTable&);  // NOT YET IMPLEMENTED
151   friend class ScopedHashTableScope<K, V, KInfo>;
152 public:
153   ScopedHashTable() : CurScope(0) {}
154   ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
155   ~ScopedHashTable() {
156     assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
157   }
158   
159   typedef typename ReferenceAdder<AllocatorTy>::result AllocatorRefTy;
160   typedef typename ReferenceAdder<const AllocatorTy>::result AllocatorCRefTy;
161   AllocatorRefTy getAllocator() { return Allocator; }
162   AllocatorCRefTy getAllocator() const { return Allocator; }
163
164   bool count(const K &Key) const {
165     return TopLevelMap.count(Key);
166   }
167
168   V lookup(const K &Key) {
169     typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
170     if (I != TopLevelMap.end())
171       return I->second->getValue();
172       
173     return V();
174   }
175
176   void insert(const K &Key, const V &Val) {
177     assert(CurScope && "No scope active!");
178
179     ScopedHashTableVal<K, V, KInfo> *&KeyEntry = TopLevelMap[Key];
180
181     KeyEntry = ValTy::Create(CurScope->getLastValInScope(), KeyEntry, Key, Val,
182                              Allocator);
183     CurScope->setLastValInScope(KeyEntry);
184   }
185
186   typedef ScopedHashTableIterator<K, V, KInfo> iterator;
187
188   iterator end() { return iterator(0); }
189
190   iterator begin(const K &Key) {
191     typename DenseMap<K, ValTy*, KInfo>::iterator I =
192       TopLevelMap.find(Key);
193     if (I == TopLevelMap.end()) return end();
194     return iterator(I->second);
195   }
196 };
197
198 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
199 /// table.
200 template <typename K, typename V, typename KInfo>
201 ScopedHashTableScope<K, V, KInfo>::
202   ScopedHashTableScope(ScopedHashTable<K, V, KInfo> &ht) : HT(ht) {
203   PrevScope = HT.CurScope;
204   HT.CurScope = this;
205   LastValInScope = 0;
206 }
207
208 template <typename K, typename V, typename KInfo>
209 ScopedHashTableScope<K, V, KInfo>::~ScopedHashTableScope() {
210   assert(HT.CurScope == this && "Scope imbalance!");
211   HT.CurScope = PrevScope;
212
213   // Pop and delete all values corresponding to this scope.
214   while (ScopedHashTableVal<K, V, KInfo> *ThisEntry = LastValInScope) {
215     // Pop this value out of the TopLevelMap.
216     if (ThisEntry->getNextForKey() == 0) {
217       assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
218              "Scope imbalance!");
219       HT.TopLevelMap.erase(ThisEntry->getKey());
220     } else {
221       ScopedHashTableVal<K, V, KInfo> *&KeyEntry =
222         HT.TopLevelMap[ThisEntry->getKey()];
223       assert(KeyEntry == ThisEntry && "Scope imbalance!");
224       KeyEntry = ThisEntry->getNextForKey();
225     }
226
227     // Pop this value out of the scope.
228     LastValInScope = ThisEntry->getNextInScope();
229
230     // Delete this entry.
231     ThisEntry->Destroy(HT.getAllocator());
232   }
233 }
234
235 } // end namespace llvm
236
237 #endif