Add count() and lookup() to ScopedHashTable. It might be useful to get information...
[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 <cassert>
35 #include "llvm/ADT/DenseMap.h"
36
37 namespace llvm {
38
39 template <typename K, typename V>
40 class ScopedHashTable;
41
42 template <typename K, typename V>
43 class ScopedHashTableVal {
44   ScopedHashTableVal *NextInScope;
45   ScopedHashTableVal *NextForKey;
46   K Key;
47   V Val;
48 public:
49   ScopedHashTableVal(ScopedHashTableVal *nextInScope,
50                      ScopedHashTableVal *nextForKey, const K &key, const V &val)
51     : NextInScope(nextInScope), NextForKey(nextForKey), Key(key), Val(val) {
52   }
53
54   const K &getKey() const { return Key; }
55   const V &getValue() const { return Val; }
56   V &getValue() { return Val; }
57
58   ScopedHashTableVal *getNextForKey() { return NextForKey; }
59   const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
60 public:
61   ScopedHashTableVal *getNextInScope() { return NextInScope; }
62 };
63
64 template <typename K, typename V>
65 class ScopedHashTableScope {
66   /// HT - The hashtable that we are active for.
67   ScopedHashTable<K, V> &HT;
68
69   /// PrevScope - This is the scope that we are shadowing in HT.
70   ScopedHashTableScope *PrevScope;
71
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
77 public:
78   ScopedHashTableScope(ScopedHashTable<K, V> &HT);
79   ~ScopedHashTableScope();
80
81 private:
82   friend class ScopedHashTable<K, V>;
83   ScopedHashTableVal<K, V> *getLastValInScope() { return LastValInScope; }
84   void setLastValInScope(ScopedHashTableVal<K,V> *Val) { LastValInScope = Val; }
85 };
86
87
88 template <typename K, typename V>
89 class ScopedHashTableIterator {
90   ScopedHashTableVal<K,V> *Node;
91 public:
92   ScopedHashTableIterator(ScopedHashTableVal<K,V> *node) : Node(node){}
93
94   V &operator*() const {
95     assert(Node && "Dereference end()");
96     return Node->getValue();
97   }
98   V *operator->() const {
99     return &Node->getValue();
100   }
101
102   bool operator==(const ScopedHashTableIterator &RHS) const {
103     return Node == RHS.Node;
104   }
105   bool operator!=(const ScopedHashTableIterator &RHS) const {
106     return Node != RHS.Node;
107   }
108
109   inline ScopedHashTableIterator& operator++() {          // Preincrement
110     assert(Node && "incrementing past end()");
111     Node = Node->getNextForKey();
112     return *this;
113   }
114   ScopedHashTableIterator operator++(int) {        // Postincrement
115     ScopedHashTableIterator tmp = *this; ++*this; return tmp;
116   }
117 };
118
119
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>;
127 public:
128   ScopedHashTable() : CurScope(0) {}
129   ~ScopedHashTable() {
130     assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
131   }
132
133   bool count(const K &Key) const {
134     return TopLevelMap.count(Key);
135   }
136
137   V lookup(const K &Key) {
138     return TopLevelMap[Key].getValue();
139   }
140
141   void insert(const K &Key, const V &Val) {
142     assert(CurScope && "No scope active!");
143
144     ScopedHashTableVal<K,V> *&KeyEntry = TopLevelMap[Key];
145
146     KeyEntry = new ScopedHashTableVal<K,V>(CurScope->getLastValInScope(),
147                                            KeyEntry, Key, Val);
148     CurScope->setLastValInScope(KeyEntry);
149   }
150
151   typedef ScopedHashTableIterator<K, V> iterator;
152
153   iterator end() { return iterator(0); }
154
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);
160   }
161 };
162
163 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
164 /// table.
165 template <typename K, typename V>
166 ScopedHashTableScope<K, V>::ScopedHashTableScope(ScopedHashTable<K, V> &ht)
167   : HT(ht) {
168   PrevScope = HT.CurScope;
169   HT.CurScope = this;
170   LastValInScope = 0;
171 }
172
173 template <typename K, typename V>
174 ScopedHashTableScope<K, V>::~ScopedHashTableScope() {
175   assert(HT.CurScope == this && "Scope imbalance!");
176   HT.CurScope = PrevScope;
177
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 &&
183              "Scope imbalance!");
184       HT.TopLevelMap.erase(ThisEntry->getKey());
185     } else {
186       ScopedHashTableVal<K,V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
187       assert(KeyEntry == ThisEntry && "Scope imbalance!");
188       KeyEntry = ThisEntry->getNextForKey();
189     }
190
191     // Pop this value out of the scope.
192     LastValInScope = ThisEntry->getNextInScope();
193
194     // Delete this entry.
195     delete ThisEntry;
196   }
197 }
198
199 } // end namespace llvm
200
201 #endif