Counter Strike : Global Offensive Source Code

256 lines
8.3 KiB

  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. #ifndef LLVM_ADT_SCOPEDHASHTABLE_H
  31. #define LLVM_ADT_SCOPEDHASHTABLE_H
  32. #include "llvm/ADT/DenseMap.h"
  33. #include "llvm/Support/Allocator.h"
  34. namespace llvm {
  35. template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
  36. typename AllocatorTy = MallocAllocator>
  37. class ScopedHashTable;
  38. template <typename K, typename V>
  39. class ScopedHashTableVal {
  40. ScopedHashTableVal *NextInScope;
  41. ScopedHashTableVal *NextForKey;
  42. K Key;
  43. V Val;
  44. ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
  45. public:
  46. const K &getKey() const { return Key; }
  47. const V &getValue() const { return Val; }
  48. V &getValue() { return Val; }
  49. ScopedHashTableVal *getNextForKey() { return NextForKey; }
  50. const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
  51. ScopedHashTableVal *getNextInScope() { return NextInScope; }
  52. template <typename AllocatorTy>
  53. static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
  54. ScopedHashTableVal *nextForKey,
  55. const K &key, const V &val,
  56. AllocatorTy &Allocator) {
  57. ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
  58. // Set up the value.
  59. new (New) ScopedHashTableVal(key, val);
  60. New->NextInScope = nextInScope;
  61. New->NextForKey = nextForKey;
  62. return New;
  63. }
  64. template <typename AllocatorTy>
  65. void Destroy(AllocatorTy &Allocator) {
  66. // Free memory referenced by the item.
  67. this->~ScopedHashTableVal();
  68. Allocator.Deallocate(this);
  69. }
  70. };
  71. template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
  72. typename AllocatorTy = MallocAllocator>
  73. class ScopedHashTableScope {
  74. /// HT - The hashtable that we are active for.
  75. ScopedHashTable<K, V, KInfo, AllocatorTy> &HT;
  76. /// PrevScope - This is the scope that we are shadowing in HT.
  77. ScopedHashTableScope *PrevScope;
  78. /// LastValInScope - This is the last value that was inserted for this scope
  79. /// or null if none have been inserted yet.
  80. ScopedHashTableVal<K, V> *LastValInScope;
  81. void operator=(ScopedHashTableScope&) LLVM_DELETED_FUNCTION;
  82. ScopedHashTableScope(ScopedHashTableScope&) LLVM_DELETED_FUNCTION;
  83. public:
  84. ScopedHashTableScope(ScopedHashTable<K, V, KInfo, AllocatorTy> &HT);
  85. ~ScopedHashTableScope();
  86. ScopedHashTableScope *getParentScope() { return PrevScope; }
  87. const ScopedHashTableScope *getParentScope() const { return PrevScope; }
  88. private:
  89. friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
  90. ScopedHashTableVal<K, V> *getLastValInScope() {
  91. return LastValInScope;
  92. }
  93. void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
  94. LastValInScope = Val;
  95. }
  96. };
  97. template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
  98. class ScopedHashTableIterator {
  99. ScopedHashTableVal<K, V> *Node;
  100. public:
  101. ScopedHashTableIterator(ScopedHashTableVal<K, V> *node) : Node(node) {}
  102. V &operator*() const {
  103. assert(Node && "Dereference end()");
  104. return Node->getValue();
  105. }
  106. V *operator->() const {
  107. return &Node->getValue();
  108. }
  109. bool operator==(const ScopedHashTableIterator &RHS) const {
  110. return Node == RHS.Node;
  111. }
  112. bool operator!=(const ScopedHashTableIterator &RHS) const {
  113. return Node != RHS.Node;
  114. }
  115. inline ScopedHashTableIterator& operator++() { // Preincrement
  116. assert(Node && "incrementing past end()");
  117. Node = Node->getNextForKey();
  118. return *this;
  119. }
  120. ScopedHashTableIterator operator++(int) { // Postincrement
  121. ScopedHashTableIterator tmp = *this; ++*this; return tmp;
  122. }
  123. };
  124. template <typename K, typename V, typename KInfo, typename AllocatorTy>
  125. class ScopedHashTable {
  126. public:
  127. /// ScopeTy - This is a helpful typedef that allows clients to get easy access
  128. /// to the name of the scope for this hash table.
  129. typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
  130. private:
  131. typedef ScopedHashTableVal<K, V> ValTy;
  132. DenseMap<K, ValTy*, KInfo> TopLevelMap;
  133. ScopeTy *CurScope;
  134. AllocatorTy Allocator;
  135. ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
  136. void operator=(const ScopedHashTable&); // NOT YET IMPLEMENTED
  137. friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
  138. public:
  139. ScopedHashTable() : CurScope(0) {}
  140. ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
  141. ~ScopedHashTable() {
  142. assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
  143. }
  144. /// Access to the allocator.
  145. typedef typename ReferenceAdder<AllocatorTy>::result AllocatorRefTy;
  146. typedef typename ReferenceAdder<const AllocatorTy>::result AllocatorCRefTy;
  147. AllocatorRefTy getAllocator() { return Allocator; }
  148. AllocatorCRefTy getAllocator() const { return Allocator; }
  149. bool count(const K &Key) const {
  150. return TopLevelMap.count(Key);
  151. }
  152. V lookup(const K &Key) {
  153. typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
  154. if (I != TopLevelMap.end())
  155. return I->second->getValue();
  156. return V();
  157. }
  158. void insert(const K &Key, const V &Val) {
  159. insertIntoScope(CurScope, Key, Val);
  160. }
  161. typedef ScopedHashTableIterator<K, V, KInfo> iterator;
  162. iterator end() { return iterator(0); }
  163. iterator begin(const K &Key) {
  164. typename DenseMap<K, ValTy*, KInfo>::iterator I =
  165. TopLevelMap.find(Key);
  166. if (I == TopLevelMap.end()) return end();
  167. return iterator(I->second);
  168. }
  169. ScopeTy *getCurScope() { return CurScope; }
  170. const ScopeTy *getCurScope() const { return CurScope; }
  171. /// insertIntoScope - This inserts the specified key/value at the specified
  172. /// (possibly not the current) scope. While it is ok to insert into a scope
  173. /// that isn't the current one, it isn't ok to insert *underneath* an existing
  174. /// value of the specified key.
  175. void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
  176. assert(S && "No scope active!");
  177. ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
  178. KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
  179. Allocator);
  180. S->setLastValInScope(KeyEntry);
  181. }
  182. };
  183. /// ScopedHashTableScope ctor - Install this as the current scope for the hash
  184. /// table.
  185. template <typename K, typename V, typename KInfo, typename Allocator>
  186. ScopedHashTableScope<K, V, KInfo, Allocator>::
  187. ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
  188. PrevScope = HT.CurScope;
  189. HT.CurScope = this;
  190. LastValInScope = 0;
  191. }
  192. template <typename K, typename V, typename KInfo, typename Allocator>
  193. ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
  194. assert(HT.CurScope == this && "Scope imbalance!");
  195. HT.CurScope = PrevScope;
  196. // Pop and delete all values corresponding to this scope.
  197. while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
  198. // Pop this value out of the TopLevelMap.
  199. if (ThisEntry->getNextForKey() == 0) {
  200. assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
  201. "Scope imbalance!");
  202. HT.TopLevelMap.erase(ThisEntry->getKey());
  203. } else {
  204. ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
  205. assert(KeyEntry == ThisEntry && "Scope imbalance!");
  206. KeyEntry = ThisEntry->getNextForKey();
  207. }
  208. // Pop this value out of the scope.
  209. LastValInScope = ThisEntry->getNextInScope();
  210. // Delete this entry.
  211. ThisEntry->Destroy(HT.getAllocator());
  212. }
  213. }
  214. } // end namespace llvm
  215. #endif