Removed trailing whitespace.
[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   void insert(const K &Key, const V &Val) {
134     assert(CurScope && "No scope active!");
135
136     ScopedHashTableVal<K,V> *&KeyEntry = TopLevelMap[Key];
137
138     KeyEntry = new ScopedHashTableVal<K,V>(CurScope->getLastValInScope(),
139                                            KeyEntry, Key, Val);
140     CurScope->setLastValInScope(KeyEntry);
141   }
142
143   typedef ScopedHashTableIterator<K, V> iterator;
144
145   iterator end() { return iterator(0); }
146
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);
152   }
153 };
154
155 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
156 /// table.
157 template <typename K, typename V>
158 ScopedHashTableScope<K, V>::ScopedHashTableScope(ScopedHashTable<K, V> &ht)
159   : HT(ht) {
160   PrevScope = HT.CurScope;
161   HT.CurScope = this;
162   LastValInScope = 0;
163 }
164
165 template <typename K, typename V>
166 ScopedHashTableScope<K, V>::~ScopedHashTableScope() {
167   assert(HT.CurScope == this && "Scope imbalance!");
168   HT.CurScope = PrevScope;
169
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 &&
175              "Scope imbalance!");
176       HT.TopLevelMap.erase(ThisEntry->getKey());
177     } else {
178       ScopedHashTableVal<K,V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
179       assert(KeyEntry == ThisEntry && "Scope imbalance!");
180       KeyEntry = ThisEntry->getNextForKey();
181     }
182
183     // Pop this value out of the scope.
184     LastValInScope = ThisEntry->getNextInScope();
185
186     // Delete this entry.
187     delete ThisEntry;
188   }
189 }
190
191 } // end namespace llvm
192
193 #endif