1 //===- ScopedHashTable.h - A simple scoped hash table ---------------------===//
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 implements an efficient scoped hash table, which is useful for
11 // things like dominator-based optimizations. This allows clients to do things
14 // ScopedHashTable<int, int> HT;
16 // ScopedHashTableScope<int, int> Scope1(HT);
20 // ScopedHashTableScope<int, int> Scope2(HT);
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
29 //===----------------------------------------------------------------------===//
31 #ifndef LLVM_ADT_SCOPEDHASHTABLE_H
32 #define LLVM_ADT_SCOPEDHASHTABLE_H
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/Support/Allocator.h"
39 template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
40 typename AllocatorTy = MallocAllocator>
41 class ScopedHashTable;
43 template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
44 class ScopedHashTableVal {
45 ScopedHashTableVal *NextInScope;
46 ScopedHashTableVal *NextForKey;
49 ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
52 const K &getKey() const { return Key; }
53 const V &getValue() const { return Val; }
54 V &getValue() { return Val; }
56 ScopedHashTableVal *getNextForKey() { return NextForKey; }
57 const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
58 ScopedHashTableVal *getNextInScope() { return NextInScope; }
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>();
67 new (New) ScopedHashTableVal(key, val);
68 New->NextInScope = nextInScope;
69 New->NextForKey = nextForKey;
73 template <typename AllocatorTy>
74 void Destroy(AllocatorTy &Allocator) {
75 // Free memory referenced by the item.
76 this->~ScopedHashTableVal();
77 Allocator.Deallocate(this);
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;
86 /// PrevScope - This is the scope that we are shadowing in HT.
87 ScopedHashTableScope *PrevScope;
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
95 ScopedHashTableScope(ScopedHashTable<K, V, KInfo> &HT);
96 ~ScopedHashTableScope();
99 friend class ScopedHashTable<K, V, KInfo>;
100 ScopedHashTableVal<K, V, KInfo> *getLastValInScope() {
101 return LastValInScope;
103 void setLastValInScope(ScopedHashTableVal<K, V, KInfo> *Val) {
104 LastValInScope = Val;
109 template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
110 class ScopedHashTableIterator {
111 ScopedHashTableVal<K, V, KInfo> *Node;
113 ScopedHashTableIterator(ScopedHashTableVal<K, V, KInfo> *node) : Node(node) {}
115 V &operator*() const {
116 assert(Node && "Dereference end()");
117 return Node->getValue();
119 V *operator->() const {
120 return &Node->getValue();
123 bool operator==(const ScopedHashTableIterator &RHS) const {
124 return Node == RHS.Node;
126 bool operator!=(const ScopedHashTableIterator &RHS) const {
127 return Node != RHS.Node;
130 inline ScopedHashTableIterator& operator++() { // Preincrement
131 assert(Node && "incrementing past end()");
132 Node = Node->getNextForKey();
135 ScopedHashTableIterator operator++(int) { // Postincrement
136 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
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;
147 AllocatorTy Allocator;
149 ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
150 void operator=(const ScopedHashTable&); // NOT YET IMPLEMENTED
151 friend class ScopedHashTableScope<K, V, KInfo>;
153 ScopedHashTable() : CurScope(0) {}
154 ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
156 assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
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; }
164 bool count(const K &Key) const {
165 return TopLevelMap.count(Key);
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();
176 void insert(const K &Key, const V &Val) {
177 assert(CurScope && "No scope active!");
179 ScopedHashTableVal<K, V, KInfo> *&KeyEntry = TopLevelMap[Key];
181 KeyEntry = ValTy::Create(CurScope->getLastValInScope(), KeyEntry, Key, Val,
183 CurScope->setLastValInScope(KeyEntry);
186 typedef ScopedHashTableIterator<K, V, KInfo> iterator;
188 iterator end() { return iterator(0); }
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);
198 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
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;
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;
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 &&
219 HT.TopLevelMap.erase(ThisEntry->getKey());
221 ScopedHashTableVal<K, V, KInfo> *&KeyEntry =
222 HT.TopLevelMap[ThisEntry->getKey()];
223 assert(KeyEntry == ThisEntry && "Scope imbalance!");
224 KeyEntry = ThisEntry->getNextForKey();
227 // Pop this value out of the scope.
228 LastValInScope = ThisEntry->getNextInScope();
230 // Delete this entry.
231 ThisEntry->Destroy(HT.getAllocator());
235 } // end namespace llvm