2
0

hash_base.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*-
  2. * Copyright 2012 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef TINYSTL_HASH_BASE_H
  27. #define TINYSTL_HASH_BASE_H
  28. #include "stddef.h"
  29. namespace tinystl {
  30. template<typename Key, typename Value>
  31. struct pair {
  32. pair();
  33. pair(const Key& key, const Value& value);
  34. Key first;
  35. Value second;
  36. };
  37. template<typename Key, typename Value>
  38. pair<Key, Value>::pair() {
  39. }
  40. template<typename Key, typename Value>
  41. pair<Key, Value>::pair(const Key& key, const Value& value)
  42. : first(key)
  43. , second(value)
  44. {
  45. }
  46. template<typename Key, typename Value>
  47. static inline pair<Key, Value> make_pair(const Key& key, const Value& value) {
  48. return pair<Key, Value>(key, value);
  49. }
  50. template<typename Key, typename Value>
  51. struct unordered_hash_node {
  52. unordered_hash_node(const Key& key, const Value& value);
  53. const Key first;
  54. Value second;
  55. unordered_hash_node* next;
  56. unordered_hash_node* prev;
  57. private:
  58. unordered_hash_node& operator=(const unordered_hash_node&);
  59. };
  60. template<typename Key, typename Value>
  61. unordered_hash_node<Key, Value>::unordered_hash_node(const Key& key, const Value& value)
  62. : first(key)
  63. , second(value)
  64. {
  65. }
  66. template <typename Key>
  67. struct unordered_hash_node<Key, void> {
  68. unordered_hash_node(const Key& key);
  69. const Key first;
  70. unordered_hash_node* next;
  71. unordered_hash_node* prev;
  72. };
  73. template<typename Key>
  74. unordered_hash_node<Key, void>::unordered_hash_node(const Key& key)
  75. : first(key)
  76. {
  77. }
  78. template<typename Key, typename Value>
  79. static void unordered_hash_node_insert(unordered_hash_node<Key, Value>* node, size_t hash, unordered_hash_node<Key, Value>** buckets, size_t nbuckets) {
  80. size_t bucket = hash & (nbuckets - 1);
  81. unordered_hash_node<Key, Value>* it = buckets[bucket + 1];
  82. node->next = it;
  83. if (it) {
  84. node->prev = it->prev;
  85. it->prev = node;
  86. if (node->prev)
  87. node->prev->next = node;
  88. } else {
  89. size_t newbucket = bucket;
  90. while (newbucket && !buckets[newbucket])
  91. --newbucket;
  92. unordered_hash_node<Key, Value>* prev = buckets[newbucket];
  93. while (prev && prev->next)
  94. prev = prev->next;
  95. node->prev = prev;
  96. if (prev)
  97. prev->next = node;
  98. }
  99. // propagate node through buckets
  100. for (; it == buckets[bucket]; --bucket) {
  101. buckets[bucket] = node;
  102. if (!bucket)
  103. break;
  104. }
  105. }
  106. template<typename Key, typename Value>
  107. static inline void unordered_hash_node_erase(const unordered_hash_node<Key, Value>* where, size_t hash, unordered_hash_node<Key, Value>** buckets, size_t nbuckets) {
  108. size_t bucket = hash & (nbuckets - 1);
  109. unordered_hash_node<Key, Value>* next = where->next;
  110. for (; buckets[bucket] == where; --bucket) {
  111. buckets[bucket] = next;
  112. if (!bucket)
  113. break;
  114. }
  115. if (where->prev)
  116. where->prev->next = where->next;
  117. if (next)
  118. next->prev = where->prev;
  119. }
  120. template<typename Node>
  121. struct unordered_hash_iterator {
  122. Node* operator->() const;
  123. Node& operator*() const;
  124. Node* node;
  125. };
  126. template<typename Node>
  127. struct unordered_hash_iterator<const Node> {
  128. unordered_hash_iterator() {}
  129. unordered_hash_iterator(unordered_hash_iterator<Node> other)
  130. : node(other.node)
  131. {
  132. }
  133. const Node* operator->() const;
  134. const Node& operator*() const;
  135. const Node* node;
  136. };
  137. template<typename Key>
  138. struct unordered_hash_iterator<const unordered_hash_node<Key, void> > {
  139. const Key* operator->() const;
  140. const Key& operator*() const;
  141. unordered_hash_node<Key, void>* node;
  142. };
  143. template<typename LNode, typename RNode>
  144. static inline bool operator==(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  145. return lhs.node == rhs.node;
  146. }
  147. template<typename LNode, typename RNode>
  148. static inline bool operator!=(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  149. return lhs.node != rhs.node;
  150. }
  151. template<typename Node>
  152. static inline void operator++(unordered_hash_iterator<Node>& lhs) {
  153. lhs.node = lhs.node->next;
  154. }
  155. template<typename Node>
  156. inline Node* unordered_hash_iterator<Node>::operator->() const {
  157. return node;
  158. }
  159. template<typename Node>
  160. inline Node& unordered_hash_iterator<Node>::operator*() const {
  161. return *node;
  162. }
  163. template<typename Node>
  164. inline const Node* unordered_hash_iterator<const Node>::operator->() const {
  165. return node;
  166. }
  167. template<typename Node>
  168. inline const Node& unordered_hash_iterator<const Node>::operator*() const {
  169. return *node;
  170. }
  171. template<typename Key>
  172. inline const Key* unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator->() const {
  173. return &node->first;
  174. }
  175. template<typename Key>
  176. inline const Key& unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator*() const {
  177. return node->first;
  178. }
  179. template<typename Node, typename Key>
  180. static inline Node unordered_hash_find(const Key& key, Node* buckets, size_t nbuckets) {
  181. const size_t bucket = hash(key) & (nbuckets - 2);
  182. for (Node it = buckets[bucket], end = buckets[bucket+1]; it != end; it = it->next)
  183. if (it->first == key)
  184. return it;
  185. return 0;
  186. }
  187. }
  188. #endif