hash_base.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. private:
  73. unordered_hash_node& operator=(const unordered_hash_node&);
  74. };
  75. template<typename Key>
  76. unordered_hash_node<Key, void>::unordered_hash_node(const Key& key)
  77. : first(key)
  78. {
  79. }
  80. template<typename Key, typename Value>
  81. static void unordered_hash_node_insert(unordered_hash_node<Key, Value>* node, size_t hash, unordered_hash_node<Key, Value>** buckets, size_t nbuckets) {
  82. size_t bucket = hash & (nbuckets - 1);
  83. unordered_hash_node<Key, Value>* it = buckets[bucket + 1];
  84. node->next = it;
  85. if (it) {
  86. node->prev = it->prev;
  87. it->prev = node;
  88. if (node->prev)
  89. node->prev->next = node;
  90. } else {
  91. size_t newbucket = bucket;
  92. while (newbucket && !buckets[newbucket])
  93. --newbucket;
  94. unordered_hash_node<Key, Value>* prev = buckets[newbucket];
  95. while (prev && prev->next)
  96. prev = prev->next;
  97. node->prev = prev;
  98. if (prev)
  99. prev->next = node;
  100. }
  101. // propagate node through buckets
  102. for (; it == buckets[bucket]; --bucket) {
  103. buckets[bucket] = node;
  104. if (!bucket)
  105. break;
  106. }
  107. }
  108. template<typename Key, typename Value>
  109. 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) {
  110. size_t bucket = hash & (nbuckets - 1);
  111. unordered_hash_node<Key, Value>* next = where->next;
  112. for (; buckets[bucket] == where; --bucket) {
  113. buckets[bucket] = next;
  114. if (!bucket)
  115. break;
  116. }
  117. if (where->prev)
  118. where->prev->next = where->next;
  119. if (next)
  120. next->prev = where->prev;
  121. }
  122. template<typename Node>
  123. struct unordered_hash_iterator {
  124. Node* operator->() const;
  125. Node& operator*() const;
  126. Node* node;
  127. };
  128. template<typename Node>
  129. struct unordered_hash_iterator<const Node> {
  130. unordered_hash_iterator() {}
  131. unordered_hash_iterator(unordered_hash_iterator<Node> other)
  132. : node(other.node)
  133. {
  134. }
  135. const Node* operator->() const;
  136. const Node& operator*() const;
  137. const Node* node;
  138. };
  139. template<typename Key>
  140. struct unordered_hash_iterator<const unordered_hash_node<Key, void> > {
  141. const Key* operator->() const;
  142. const Key& operator*() const;
  143. unordered_hash_node<Key, void>* 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 LNode, typename RNode>
  150. static inline bool operator!=(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  151. return lhs.node != rhs.node;
  152. }
  153. template<typename Node>
  154. static inline void operator++(unordered_hash_iterator<Node>& lhs) {
  155. lhs.node = lhs.node->next;
  156. }
  157. template<typename Node>
  158. inline Node* unordered_hash_iterator<Node>::operator->() const {
  159. return node;
  160. }
  161. template<typename Node>
  162. inline Node& unordered_hash_iterator<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 Node>
  170. inline const Node& unordered_hash_iterator<const Node>::operator*() const {
  171. return *node;
  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 Key>
  178. inline const Key& unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator*() const {
  179. return node->first;
  180. }
  181. template<typename Node, typename Key>
  182. static inline Node unordered_hash_find(const Key& key, Node* buckets, size_t nbuckets) {
  183. const size_t bucket = hash(key) & (nbuckets - 2);
  184. for (Node it = buckets[bucket], end = buckets[bucket+1]; it != end; it = it->next)
  185. if (it->first == key)
  186. return it;
  187. return 0;
  188. }
  189. }
  190. #endif