HashMap.inl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. namespace anki
  6. {
  7. namespace detail
  8. {
  9. //==============================================================================
  10. // HashMapBase =
  11. //==============================================================================
  12. //==============================================================================
  13. template<typename TKey,
  14. typename TValue,
  15. typename THasher,
  16. typename TCompare,
  17. typename TNode>
  18. void HashMapBase<TKey, TValue, THasher, TCompare, TNode>::insertNode(
  19. TNode* node)
  20. {
  21. if(ANKI_UNLIKELY(!m_root))
  22. {
  23. m_root = node;
  24. return;
  25. }
  26. const U64 hash = node->m_hash;
  27. TNode* it = m_root;
  28. Bool done = false;
  29. do
  30. {
  31. const U64 nhash = it->m_hash;
  32. if(hash > nhash)
  33. {
  34. // Go to right
  35. TNode* right = it->m_right;
  36. if(right)
  37. {
  38. it = right;
  39. }
  40. else
  41. {
  42. node->m_parent = it;
  43. it->m_right = node;
  44. done = true;
  45. }
  46. }
  47. else
  48. {
  49. ANKI_ASSERT(hash != nhash && "Not supported");
  50. // Go to left
  51. TNode* left = it->m_left;
  52. if(left)
  53. {
  54. it = left;
  55. }
  56. else
  57. {
  58. node->m_parent = it;
  59. it->m_left = node;
  60. done = true;
  61. }
  62. }
  63. } while(!done);
  64. }
  65. //==============================================================================
  66. template<typename TKey,
  67. typename TValue,
  68. typename THasher,
  69. typename TCompare,
  70. typename TNode>
  71. typename HashMapBase<TKey, TValue, THasher, TCompare, TNode>::Iterator
  72. HashMapBase<TKey, TValue, THasher, TCompare, TNode>::find(const Key& key)
  73. {
  74. const U64 hash = THasher()(key);
  75. TNode* node = m_root;
  76. while(node)
  77. {
  78. const U64 bhash = node->m_hash;
  79. if(hash < bhash)
  80. {
  81. node = node->m_left;
  82. }
  83. else if(hash > bhash)
  84. {
  85. node = node->m_right;
  86. }
  87. else
  88. {
  89. // Found
  90. break;
  91. }
  92. }
  93. return Iterator(node);
  94. }
  95. //==============================================================================
  96. template<typename TKey,
  97. typename TValue,
  98. typename THasher,
  99. typename TCompare,
  100. typename TNode>
  101. void HashMapBase<TKey, TValue, THasher, TCompare, TNode>::removeNode(TNode* del)
  102. {
  103. ANKI_ASSERT(del);
  104. TNode* parent = del->m_parent;
  105. TNode* left = del->m_left;
  106. TNode* right = del->m_right;
  107. if(parent)
  108. {
  109. // If it has a parent then remove the connection to the parent and
  110. // insert left and right like regular nodes
  111. if(parent->m_left == del)
  112. {
  113. parent->m_left = nullptr;
  114. }
  115. else
  116. {
  117. ANKI_ASSERT(parent->m_right == del);
  118. parent->m_right = nullptr;
  119. }
  120. if(left)
  121. {
  122. ANKI_ASSERT(left->m_parent == del);
  123. left->m_parent = nullptr;
  124. insertNode(left);
  125. }
  126. if(right)
  127. {
  128. ANKI_ASSERT(right->m_parent == del);
  129. right->m_parent = nullptr;
  130. insertNode(right);
  131. }
  132. }
  133. else
  134. {
  135. // It's the root node. Make arbitrarily the left root and add the right
  136. ANKI_ASSERT(del == m_root && "It must be the root");
  137. if(left)
  138. {
  139. left->m_parent = nullptr;
  140. m_root = left;
  141. if(right)
  142. {
  143. right->m_parent = nullptr;
  144. insertNode(right);
  145. }
  146. }
  147. else
  148. {
  149. if(right)
  150. {
  151. right->m_parent = nullptr;
  152. }
  153. m_root = right;
  154. }
  155. }
  156. }
  157. } // end namespace detail
  158. //==============================================================================
  159. // HashMap =
  160. //==============================================================================
  161. //==============================================================================
  162. template<typename TKey, typename TValue, typename THasher, typename TCompare>
  163. template<typename TAllocator>
  164. void HashMap<TKey, TValue, THasher, TCompare>::destroy(TAllocator alloc)
  165. {
  166. if(Base::m_root)
  167. {
  168. destroyInternal(alloc, Base::m_root);
  169. Base::m_root = nullptr;
  170. }
  171. }
  172. //==============================================================================
  173. template<typename TKey, typename TValue, typename THasher, typename TCompare>
  174. template<typename TAllocator>
  175. void HashMap<TKey, TValue, THasher, TCompare>::destroyInternal(
  176. TAllocator alloc, Node* node)
  177. {
  178. ANKI_ASSERT(node);
  179. if(node->m_right)
  180. {
  181. destroyInternal(alloc, node->m_right);
  182. }
  183. if(node->m_left)
  184. {
  185. destroyInternal(alloc, node->m_left);
  186. }
  187. alloc.deleteInstance(node);
  188. }
  189. } // end namespace anki