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, typename KInfo = DenseMapInfo<K> >
40
class ScopedHashTable;
42
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
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, typename KInfo = DenseMapInfo<K> >
65
class ScopedHashTableScope {
66
/// HT - The hashtable that we are active for.
67
ScopedHashTable<K, V, KInfo> &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, KInfo> *LastValInScope;
75
void operator=(ScopedHashTableScope&); // DO NOT IMPLEMENT
76
ScopedHashTableScope(ScopedHashTableScope&); // DO NOT IMPLEMENT
78
ScopedHashTableScope(ScopedHashTable<K, V, KInfo> &HT);
79
~ScopedHashTableScope();
82
friend class ScopedHashTable<K, V, KInfo>;
83
ScopedHashTableVal<K, V, KInfo> *getLastValInScope() {
84
return LastValInScope;
86
void setLastValInScope(ScopedHashTableVal<K, V, KInfo> *Val) {
92
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
93
class ScopedHashTableIterator {
94
ScopedHashTableVal<K, V, KInfo> *Node;
96
ScopedHashTableIterator(ScopedHashTableVal<K, V, KInfo> *node) : Node(node) {}
98
V &operator*() const {
99
assert(Node && "Dereference end()");
100
return Node->getValue();
102
V *operator->() const {
103
return &Node->getValue();
106
bool operator==(const ScopedHashTableIterator &RHS) const {
107
return Node == RHS.Node;
109
bool operator!=(const ScopedHashTableIterator &RHS) const {
110
return Node != RHS.Node;
113
inline ScopedHashTableIterator& operator++() { // Preincrement
114
assert(Node && "incrementing past end()");
115
Node = Node->getNextForKey();
118
ScopedHashTableIterator operator++(int) { // Postincrement
119
ScopedHashTableIterator tmp = *this; ++*this; return tmp;
124
template <typename K, typename V, typename KInfo>
125
class ScopedHashTable {
126
DenseMap<K, ScopedHashTableVal<K, V, KInfo>*, KInfo> TopLevelMap;
127
ScopedHashTableScope<K, V, KInfo> *CurScope;
128
ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
129
void operator=(const ScopedHashTable&); // NOT YET IMPLEMENTED
130
friend class ScopedHashTableScope<K, V, KInfo>;
132
ScopedHashTable() : CurScope(0) {}
134
assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
137
bool count(const K &Key) const {
138
return TopLevelMap.count(Key);
141
V lookup(const K &Key) {
142
typename DenseMap<K, ScopedHashTableVal<K, V, KInfo>*, KInfo>::iterator
143
I = TopLevelMap.find(Key);
144
if (I != TopLevelMap.end())
145
return I->second->getValue();
150
void insert(const K &Key, const V &Val) {
151
assert(CurScope && "No scope active!");
153
ScopedHashTableVal<K, V, KInfo> *&KeyEntry = TopLevelMap[Key];
155
KeyEntry= new ScopedHashTableVal<K, V, KInfo>(CurScope->getLastValInScope(),
157
CurScope->setLastValInScope(KeyEntry);
160
typedef ScopedHashTableIterator<K, V, KInfo> iterator;
162
iterator end() { return iterator(0); }
164
iterator begin(const K &Key) {
165
typename DenseMap<K, ScopedHashTableVal<K, V, KInfo>*, KInfo>::iterator I =
166
TopLevelMap.find(Key);
167
if (I == TopLevelMap.end()) return end();
168
return iterator(I->second);
172
/// ScopedHashTableScope ctor - Install this as the current scope for the hash
174
template <typename K, typename V, typename KInfo>
175
ScopedHashTableScope<K, V, KInfo>::
176
ScopedHashTableScope(ScopedHashTable<K, V, KInfo> &ht) : HT(ht) {
177
PrevScope = HT.CurScope;
182
template <typename K, typename V, typename KInfo>
183
ScopedHashTableScope<K, V, KInfo>::~ScopedHashTableScope() {
184
assert(HT.CurScope == this && "Scope imbalance!");
185
HT.CurScope = PrevScope;
187
// Pop and delete all values corresponding to this scope.
188
while (ScopedHashTableVal<K, V, KInfo> *ThisEntry = LastValInScope) {
189
// Pop this value out of the TopLevelMap.
190
if (ThisEntry->getNextForKey() == 0) {
191
assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
193
HT.TopLevelMap.erase(ThisEntry->getKey());
195
ScopedHashTableVal<K, V, KInfo> *&KeyEntry =
196
HT.TopLevelMap[ThisEntry->getKey()];
197
assert(KeyEntry == ThisEntry && "Scope imbalance!");
198
KeyEntry = ThisEntry->getNextForKey();
201
// Pop this value out of the scope.
202
LastValInScope = ThisEntry->getNextInScope();
204
// Delete this entry.
209
} // end namespace llvm