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 void insert(const K &Key, const V &Val) {
134 assert(CurScope && "No scope active!");
136 ScopedHashTableVal<K,V> *&KeyEntry = TopLevelMap[Key];
138 KeyEntry = new ScopedHashTableVal<K,V>(CurScope->getLastValInScope(),
140 CurScope->setLastValInScope(KeyEntry);
143 typedef ScopedHashTableIterator<K, V> iterator;
145 iterator end() { return iterator(0); }
147 iterator begin(const K &Key) {
148 typename DenseMap<K, ScopedHashTableVal<K,V>*>::iterator I =
149 TopLevelMap.find(Key);
150 if (I == TopLevelMap.end()) return end();
151 return iterator(I->second);
155 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
157 template <typename K, typename V>
158 ScopedHashTableScope<K, V>::ScopedHashTableScope(ScopedHashTable<K, V> &ht)
160 PrevScope = HT.CurScope;
165 template <typename K, typename V>
166 ScopedHashTableScope<K, V>::~ScopedHashTableScope() {
167 assert(HT.CurScope == this && "Scope imbalance!");
168 HT.CurScope = PrevScope;
170 // Pop and delete all values corresponding to this scope.
171 while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
172 // Pop this value out of the TopLevelMap.
173 if (ThisEntry->getNextForKey() == 0) {
174 assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
176 HT.TopLevelMap.erase(ThisEntry->getKey());
178 ScopedHashTableVal<K,V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
179 assert(KeyEntry == ThisEntry && "Scope imbalance!");
180 KeyEntry = ThisEntry->getNextForKey();
183 // Pop this value out of the scope.
184 LastValInScope = ThisEntry->getNextInScope();
186 // Delete this entry.
191 } // end namespace llvm