hash_base.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. };
  58. template<typename Key, typename Value>
  59. unordered_hash_node<Key, Value>::unordered_hash_node(const Key& key, const Value& value)
  60. : first(key)
  61. , second(value)
  62. {
  63. }
  64. template <typename Key>
  65. struct unordered_hash_node<Key, void> {
  66. unordered_hash_node(const Key& key);
  67. const Key first;
  68. unordered_hash_node* next;
  69. unordered_hash_node* prev;
  70. };
  71. template<typename Key>
  72. unordered_hash_node<Key, void>::unordered_hash_node(const Key& key)
  73. : first(key)
  74. {
  75. }
  76. template<typename Key, typename Value>
  77. static void unordered_hash_node_insert(unordered_hash_node<Key, Value>* node, size_t hash, unordered_hash_node<Key, Value>** buckets, size_t nbuckets) {
  78. size_t bucket = hash & (nbuckets - 1);
  79. unordered_hash_node<Key, Value>* it = buckets[bucket + 1];
  80. node->next = it;
  81. if (it) {
  82. node->prev = it->prev;
  83. it->prev = node;
  84. if (node->prev)
  85. node->prev->next = node;
  86. } else {
  87. size_t newbucket = bucket;
  88. while (newbucket && !buckets[newbucket])
  89. --newbucket;
  90. unordered_hash_node<Key, Value>* prev = buckets[newbucket];
  91. while (prev && prev->next)
  92. prev = prev->next;
  93. node->prev = prev;
  94. if (prev)
  95. prev->next = node;
  96. }
  97. // propagate node through buckets
  98. for (; it == buckets[bucket]; --bucket) {
  99. buckets[bucket] = node;
  100. if (!bucket)
  101. break;
  102. }
  103. }
  104. template<typename Key, typename Value>
  105. 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) {
  106. size_t bucket = hash & (nbuckets - 1);
  107. unordered_hash_node<Key, Value>* next = where->next;
  108. for (; buckets[bucket] == where; --bucket) {
  109. buckets[bucket] = next;
  110. if (!bucket)
  111. break;
  112. }
  113. if (where->prev)
  114. where->prev->next = where->next;
  115. if (next)
  116. next->prev = where->prev;
  117. }
  118. template<typename Node>
  119. struct unordered_hash_iterator {
  120. Node* operator->() const;
  121. Node& operator*() const;
  122. Node* node;
  123. };
  124. template<typename Node>
  125. struct unordered_hash_iterator<const Node> {
  126. unordered_hash_iterator() {}
  127. unordered_hash_iterator(unordered_hash_iterator<Node> other)
  128. : node(other.node)
  129. {
  130. }
  131. const Node* operator->() const;
  132. const Node& operator*() const;
  133. const Node* node;
  134. };
  135. template<typename Key>
  136. struct unordered_hash_iterator<const unordered_hash_node<Key, void> > {
  137. const Key* operator->() const;
  138. const Key& operator*() const;
  139. unordered_hash_node<Key, void>* node;
  140. };
  141. template<typename LNode, typename RNode>
  142. static inline bool operator==(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  143. return lhs.node == rhs.node;
  144. }
  145. template<typename LNode, typename RNode>
  146. static inline bool operator!=(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  147. return lhs.node != rhs.node;
  148. }
  149. template<typename Node>
  150. static inline void operator++(unordered_hash_iterator<Node>& lhs) {
  151. lhs.node = lhs.node->next;
  152. }
  153. template<typename Node>
  154. inline Node* unordered_hash_iterator<Node>::operator->() const {
  155. return node;
  156. }
  157. template<typename Node>
  158. inline Node& unordered_hash_iterator<Node>::operator*() const {
  159. return *node;
  160. }
  161. template<typename Node>
  162. inline const Node* unordered_hash_iterator<const Node>::operator->() const {
  163. return node;
  164. }
  165. template<typename Node>
  166. inline const Node& unordered_hash_iterator<const Node>::operator*() const {
  167. return *node;
  168. }
  169. template<typename Key>
  170. inline const Key* unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator->() const {
  171. return &node->first;
  172. }
  173. template<typename Key>
  174. inline const Key& unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator*() const {
  175. return node->first;
  176. }
  177. template<typename Node, typename Key>
  178. static inline Node unordered_hash_find(const Key& key, Node* buckets, size_t nbuckets) {
  179. const size_t bucket = hash(key) & (nbuckets - 2);
  180. for (Node it = buckets[bucket], end = buckets[bucket+1]; it != end; it = it->next)
  181. if (it->first == key)
  182. return it;
  183. return 0;
  184. }
  185. }
  186. #endif