#ifndef LLVM_ADT_SCOPEDHASHTABLE_H
#define LLVM_ADT_SCOPEDHASHTABLE_H
#include <cassert>
#include "llvm/ADT/DenseMap.h"
namespace llvm {
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
class ScopedHashTable;
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
class ScopedHashTableVal {
ScopedHashTableVal *NextInScope;
ScopedHashTableVal *NextForKey;
K Key;
V Val;
public:
ScopedHashTableVal(ScopedHashTableVal *nextInScope,
ScopedHashTableVal *nextForKey, const K &key, const V &val)
: NextInScope(nextInScope), NextForKey(nextForKey), Key(key), Val(val) {
}
const K &getKey() const { return Key; }
const V &getValue() const { return Val; }
V &getValue() { return Val; }
ScopedHashTableVal *getNextForKey() { return NextForKey; }
const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
public:
ScopedHashTableVal *getNextInScope() { return NextInScope; }
};
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
class ScopedHashTableScope {
ScopedHashTable<K, V, KInfo> &HT;
ScopedHashTableScope *PrevScope;
ScopedHashTableVal<K, V, KInfo> *LastValInScope;
void operator=(ScopedHashTableScope&); ScopedHashTableScope(ScopedHashTableScope&); public:
ScopedHashTableScope(ScopedHashTable<K, V, KInfo> &HT);
~ScopedHashTableScope();
private:
friend class ScopedHashTable<K, V, KInfo>;
ScopedHashTableVal<K, V, KInfo> *getLastValInScope() {
return LastValInScope;
}
void setLastValInScope(ScopedHashTableVal<K, V, KInfo> *Val) {
LastValInScope = Val;
}
};
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
class ScopedHashTableIterator {
ScopedHashTableVal<K, V, KInfo> *Node;
public:
ScopedHashTableIterator(ScopedHashTableVal<K, V, KInfo> *node) : Node(node) {}
V &operator*() const {
assert(Node && "Dereference end()");
return Node->getValue();
}
V *operator->() const {
return &Node->getValue();
}
bool operator==(const ScopedHashTableIterator &RHS) const {
return Node == RHS.Node;
}
bool operator!=(const ScopedHashTableIterator &RHS) const {
return Node != RHS.Node;
}
inline ScopedHashTableIterator& operator++() { assert(Node && "incrementing past end()");
Node = Node->getNextForKey();
return *this;
}
ScopedHashTableIterator operator++(int) { ScopedHashTableIterator tmp = *this; ++*this; return tmp;
}
};
template <typename K, typename V, typename KInfo>
class ScopedHashTable {
DenseMap<K, ScopedHashTableVal<K, V, KInfo>*, KInfo> TopLevelMap;
ScopedHashTableScope<K, V, KInfo> *CurScope;
ScopedHashTable(const ScopedHashTable&); void operator=(const ScopedHashTable&); friend class ScopedHashTableScope<K, V, KInfo>;
public:
ScopedHashTable() : CurScope(0) {}
~ScopedHashTable() {
assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
}
bool count(const K &Key) const {
return TopLevelMap.count(Key);
}
V lookup(const K &Key) {
return TopLevelMap[Key]->getValue();
}
void insert(const K &Key, const V &Val) {
assert(CurScope && "No scope active!");
ScopedHashTableVal<K, V, KInfo> *&KeyEntry = TopLevelMap[Key];
KeyEntry= new ScopedHashTableVal<K, V, KInfo>(CurScope->getLastValInScope(),
KeyEntry, Key, Val);
CurScope->setLastValInScope(KeyEntry);
}
typedef ScopedHashTableIterator<K, V, KInfo> iterator;
iterator end() { return iterator(0); }
iterator begin(const K &Key) {
typename DenseMap<K, ScopedHashTableVal<K, V, KInfo>*, KInfo>::iterator I =
TopLevelMap.find(Key);
if (I == TopLevelMap.end()) return end();
return iterator(I->second);
}
};
template <typename K, typename V, typename KInfo>
ScopedHashTableScope<K, V, KInfo>::
ScopedHashTableScope(ScopedHashTable<K, V, KInfo> &ht) : HT(ht) {
PrevScope = HT.CurScope;
HT.CurScope = this;
LastValInScope = 0;
}
template <typename K, typename V, typename KInfo>
ScopedHashTableScope<K, V, KInfo>::~ScopedHashTableScope() {
assert(HT.CurScope == this && "Scope imbalance!");
HT.CurScope = PrevScope;
while (ScopedHashTableVal<K, V, KInfo> *ThisEntry = LastValInScope) {
if (ThisEntry->getNextForKey() == 0) {
assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
"Scope imbalance!");
HT.TopLevelMap.erase(ThisEntry->getKey());
} else {
ScopedHashTableVal<K, V, KInfo> *&KeyEntry =
HT.TopLevelMap[ThisEntry->getKey()];
assert(KeyEntry == ThisEntry && "Scope imbalance!");
KeyEntry = ThisEntry->getNextForKey();
}
LastValInScope = ThisEntry->getNextInScope();
delete ThisEntry;
}
}
}
#endif