#ifndef LLVM_ADT_SCOPEDHASHTABLE_H
#define LLVM_ADT_SCOPEDHASHTABLE_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/Allocator.h"
namespace llvm {
template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
typename AllocatorTy = MallocAllocator>
class ScopedHashTable;
template <typename K, typename V>
class ScopedHashTableVal {
ScopedHashTableVal *NextInScope;
ScopedHashTableVal *NextForKey;
K Key;
V Val;
ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
public:
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; }
ScopedHashTableVal *getNextInScope() { return NextInScope; }
template <typename AllocatorTy>
static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
ScopedHashTableVal *nextForKey,
const K &key, const V &val,
AllocatorTy &Allocator) {
ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
new (New) ScopedHashTableVal(key, val);
New->NextInScope = nextInScope;
New->NextForKey = nextForKey;
return New;
}
template <typename AllocatorTy>
void Destroy(AllocatorTy &Allocator) {
this->~ScopedHashTableVal();
Allocator.Deallocate(this);
}
};
template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
typename AllocatorTy = MallocAllocator>
class ScopedHashTableScope {
ScopedHashTable<K, V, KInfo, AllocatorTy> &HT;
ScopedHashTableScope *PrevScope;
ScopedHashTableVal<K, V> *LastValInScope;
void operator=(ScopedHashTableScope&); ScopedHashTableScope(ScopedHashTableScope&); public:
ScopedHashTableScope(ScopedHashTable<K, V, KInfo, AllocatorTy> &HT);
~ScopedHashTableScope();
ScopedHashTableScope *getParentScope() { return PrevScope; }
const ScopedHashTableScope *getParentScope() const { return PrevScope; }
private:
friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
ScopedHashTableVal<K, V> *getLastValInScope() {
return LastValInScope;
}
void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
LastValInScope = Val;
}
};
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
class ScopedHashTableIterator {
ScopedHashTableVal<K, V> *Node;
public:
ScopedHashTableIterator(ScopedHashTableVal<K, V> *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, typename AllocatorTy>
class ScopedHashTable {
public:
typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
private:
typedef ScopedHashTableVal<K, V> ValTy;
DenseMap<K, ValTy*, KInfo> TopLevelMap;
ScopeTy *CurScope;
AllocatorTy Allocator;
ScopedHashTable(const ScopedHashTable&); void operator=(const ScopedHashTable&); friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
public:
ScopedHashTable() : CurScope(0) {}
ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
~ScopedHashTable() {
assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
}
typedef typename ReferenceAdder<AllocatorTy>::result AllocatorRefTy;
typedef typename ReferenceAdder<const AllocatorTy>::result AllocatorCRefTy;
AllocatorRefTy getAllocator() { return Allocator; }
AllocatorCRefTy getAllocator() const { return Allocator; }
bool count(const K &Key) const {
return TopLevelMap.count(Key);
}
V lookup(const K &Key) {
typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
if (I != TopLevelMap.end())
return I->second->getValue();
return V();
}
void insert(const K &Key, const V &Val) {
insertIntoScope(CurScope, Key, Val);
}
typedef ScopedHashTableIterator<K, V, KInfo> iterator;
iterator end() { return iterator(0); }
iterator begin(const K &Key) {
typename DenseMap<K, ValTy*, KInfo>::iterator I =
TopLevelMap.find(Key);
if (I == TopLevelMap.end()) return end();
return iterator(I->second);
}
ScopeTy *getCurScope() { return CurScope; }
const ScopeTy *getCurScope() const { return CurScope; }
void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
assert(S && "No scope active!");
ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
Allocator);
S->setLastValInScope(KeyEntry);
}
};
template <typename K, typename V, typename KInfo, typename Allocator>
ScopedHashTableScope<K, V, KInfo, Allocator>::
ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
PrevScope = HT.CurScope;
HT.CurScope = this;
LastValInScope = 0;
}
template <typename K, typename V, typename KInfo, typename Allocator>
ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
assert(HT.CurScope == this && "Scope imbalance!");
HT.CurScope = PrevScope;
while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
if (ThisEntry->getNextForKey() == 0) {
assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
"Scope imbalance!");
HT.TopLevelMap.erase(ThisEntry->getKey());
} else {
ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
assert(KeyEntry == ThisEntry && "Scope imbalance!");
KeyEntry = ThisEntry->getNextForKey();
}
LastValInScope = ThisEntry->getNextInScope();
ThisEntry->Destroy(HT.getAllocator());
}
}
}
#endif