//===- ScopedHashTable.h - A simple scoped hash table ---------------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file implements an efficient scoped hash table, which is useful for // things like dominator-based optimizations. This allows clients to do things // like this: // // ScopedHashTable HT; // { // ScopedHashTableScope Scope1(HT); // HT.insert(0, 0); // HT.insert(1, 1); // { // ScopedHashTableScope Scope2(HT); // HT.insert(0, 42); // } // } // // Looking up the value for "0" in the Scope2 block will return 42. Looking // up the value for 0 before 42 is inserted or after Scope2 is popped will // return 0. // //===----------------------------------------------------------------------===// #ifndef LLVM_ADT_SCOPEDHASHTABLE_H #define LLVM_ADT_SCOPEDHASHTABLE_H #include #include "llvm/ADT/DenseMap.h" namespace llvm { template class ScopedHashTable; template 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 class ScopedHashTableScope { /// HT - The hashtable that we are active for. ScopedHashTable &HT; /// PrevScope - This is the scope that we are shadowing in HT. ScopedHashTableScope *PrevScope; /// LastValInScope - This is the last value that was inserted for this scope /// or null if none have been inserted yet. ScopedHashTableVal *LastValInScope; void operator=(ScopedHashTableScope&); // DO NOT IMPLEMENT ScopedHashTableScope(ScopedHashTableScope&); // DO NOT IMPLEMENT public: ScopedHashTableScope(ScopedHashTable &HT); ~ScopedHashTableScope(); private: friend class ScopedHashTable; ScopedHashTableVal *getLastValInScope() { return LastValInScope; } void setLastValInScope(ScopedHashTableVal *Val) { LastValInScope = Val; } }; template class ScopedHashTableIterator { ScopedHashTableVal *Node; public: ScopedHashTableIterator(ScopedHashTableVal *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++() { // Preincrement assert(Node && "incrementing past end()"); Node = Node->getNextForKey(); return *this; } ScopedHashTableIterator operator++(int) { // Postincrement ScopedHashTableIterator tmp = *this; ++*this; return tmp; } }; template class ScopedHashTable { DenseMap*> TopLevelMap; ScopedHashTableScope *CurScope; ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED void operator=(const ScopedHashTable&); // NOT YET IMPLEMENTED friend class ScopedHashTableScope; public: ScopedHashTable() : CurScope(0) {} ~ScopedHashTable() { assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!"); } void insert(const K &Key, const V &Val) { assert(CurScope && "No scope active!"); ScopedHashTableVal *&KeyEntry = TopLevelMap[Key]; KeyEntry = new ScopedHashTableVal(CurScope->getLastValInScope(), KeyEntry, Key, Val); CurScope->setLastValInScope(KeyEntry); } typedef ScopedHashTableIterator iterator; iterator end() { return iterator(0); } iterator begin(const K &Key) { typename DenseMap*>::iterator I = TopLevelMap.find(Key); if (I == TopLevelMap.end()) return end(); return iterator(I->second); } }; /// ScopedHashTableScope ctor - Install this as the current scope for the hash /// table. template ScopedHashTableScope::ScopedHashTableScope(ScopedHashTable &ht) : HT(ht) { PrevScope = HT.CurScope; HT.CurScope = this; LastValInScope = 0; } template ScopedHashTableScope::~ScopedHashTableScope() { assert(HT.CurScope == this && "Scope imbalance!"); HT.CurScope = PrevScope; // Pop and delete all values corresponding to this scope. while (ScopedHashTableVal *ThisEntry = LastValInScope) { // Pop this value out of the TopLevelMap. if (ThisEntry->getNextForKey() == 0) { assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry && "Scope imbalance!"); HT.TopLevelMap.erase(ThisEntry->getKey()); } else { ScopedHashTableVal *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()]; assert(KeyEntry == ThisEntry && "Scope imbalance!"); KeyEntry = ThisEntry->getNextForKey(); } // Pop this value out of the scope. LastValInScope = ThisEntry->getNextInScope(); // Delete this entry. delete ThisEntry; } } } // end namespace llvm #endif