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
35 #include "llvm/ADT/DenseMap.h"
39 template <typename K, typename V>
40 class ScopedHashTable;
42 template <typename K, typename V>
43 class ScopedHashTableVal {
44 ScopedHashTableVal *NextInScope;
45 ScopedHashTableVal *NextForKey;
49 ScopedHashTableVal(ScopedHashTableVal *nextInScope,
50 ScopedHashTableVal *nextForKey, const K &key, const V &val)
51 : NextInScope(nextInScope), NextForKey(nextForKey), Key(key), Val(val) {
54 const K &getKey() const { return Key; }
55 const V &getValue() const { return Val; }
56 V &getValue() { return Val; }
58 ScopedHashTableVal *getNextForKey() { return NextForKey; }
59 const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
61 ScopedHashTableVal *getNextInScope() { return NextInScope; }
64 template <typename K, typename V>
65 class ScopedHashTableScope {
66 /// HT - The hashtable that we are active for.
67 ScopedHashTable<K, V> &HT;
69 /// PrevScope - This is the scope that we are shadowing in HT.
70 ScopedHashTableScope *PrevScope;
72 /// LastValInScope - This is the last value that was inserted for this scope
73 /// or null if none have been inserted yet.
74 ScopedHashTableVal<K,V> *LastValInScope;
75 void operator=(ScopedHashTableScope&); // DO NOT IMPLEMENT
76 ScopedHashTableScope(ScopedHashTableScope&); // DO NOT IMPLEMENT
78 ScopedHashTableScope(ScopedHashTable<K, V> &HT);
79 ~ScopedHashTableScope();
82 friend class ScopedHashTable<K, V>;
83 ScopedHashTableVal<K, V> *getLastValInScope() { return LastValInScope; }
84 void setLastValInScope(ScopedHashTableVal<K,V> *Val) { LastValInScope = Val; }
88 template <typename K, typename V>
89 class ScopedHashTableIterator {
90 ScopedHashTableVal<K,V> *Node;
92 ScopedHashTableIterator(ScopedHashTableVal<K,V> *node) : Node(node){}
94 V &operator*() const {
95 assert(Node && "Dereference end()");
96 return Node->getValue();
98 V *operator->() const {
99 return &Node->getValue();
102 bool operator==(const ScopedHashTableIterator &RHS) const {
103 return Node == RHS.Node;
105 bool operator!=(const ScopedHashTableIterator &RHS) const {
106 return Node != RHS.Node;
109 inline ScopedHashTableIterator& operator++() { // Preincrement
110 assert(Node && "incrementing past end()");
111 Node = Node->getNextForKey();
114 ScopedHashTableIterator operator++(int) { // Postincrement
115 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
120 template <typename K, typename V>
121 class ScopedHashTable {
122 DenseMap<K, ScopedHashTableVal<K,V>*> TopLevelMap;
123 ScopedHashTableScope<K, V> *CurScope;
124 ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
125 void operator=(const ScopedHashTable&); // NOT YET IMPLEMENTED
126 friend class ScopedHashTableScope<K, V>;
128 ScopedHashTable() : CurScope(0) {}
130 assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
133 bool count(const K &Key) const {
134 return TopLevelMap.count(Key);
137 V lookup(const K &Key) {
138 return TopLevelMap[Key].getValue();
141 void insert(const K &Key, const V &Val) {
142 assert(CurScope && "No scope active!");
144 ScopedHashTableVal<K,V> *&KeyEntry = TopLevelMap[Key];
146 KeyEntry = new ScopedHashTableVal<K,V>(CurScope->getLastValInScope(),
148 CurScope->setLastValInScope(KeyEntry);
151 typedef ScopedHashTableIterator<K, V> iterator;
153 iterator end() { return iterator(0); }
155 iterator begin(const K &Key) {
156 typename DenseMap<K, ScopedHashTableVal<K,V>*>::iterator I =
157 TopLevelMap.find(Key);
158 if (I == TopLevelMap.end()) return end();
159 return iterator(I->second);
163 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
165 template <typename K, typename V>
166 ScopedHashTableScope<K, V>::ScopedHashTableScope(ScopedHashTable<K, V> &ht)
168 PrevScope = HT.CurScope;
173 template <typename K, typename V>
174 ScopedHashTableScope<K, V>::~ScopedHashTableScope() {
175 assert(HT.CurScope == this && "Scope imbalance!");
176 HT.CurScope = PrevScope;
178 // Pop and delete all values corresponding to this scope.
179 while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
180 // Pop this value out of the TopLevelMap.
181 if (ThisEntry->getNextForKey() == 0) {
182 assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
184 HT.TopLevelMap.erase(ThisEntry->getKey());
186 ScopedHashTableVal<K,V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
187 assert(KeyEntry == ThisEntry && "Scope imbalance!");
188 KeyEntry = ThisEntry->getNextForKey();
191 // Pop this value out of the scope.
192 LastValInScope = ThisEntry->getNextInScope();
194 // Delete this entry.
199 } // end namespace llvm