hash_base.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. typedef Key first_type;
  33. typedef Value second_type;
  34. pair();
  35. pair(const Key& key, const Value& value);
  36. Key first;
  37. Value second;
  38. };
  39. template<typename Key, typename Value>
  40. pair<Key, Value>::pair() {
  41. }
  42. template<typename Key, typename Value>
  43. pair<Key, Value>::pair(const Key& key, const Value& value)
  44. : first(key)
  45. , second(value)
  46. {
  47. }
  48. template<typename Key, typename Value>
  49. static inline pair<Key, Value> make_pair(const Key& key, const Value& value) {
  50. return pair<Key, Value>(key, value);
  51. }
  52. template<typename Key, typename Value>
  53. struct unordered_hash_node {
  54. unordered_hash_node(const Key& key, const Value& value);
  55. const Key first;
  56. Value second;
  57. unordered_hash_node* next;
  58. unordered_hash_node* prev;
  59. private:
  60. unordered_hash_node& operator=(const unordered_hash_node&);
  61. };
  62. template<typename Key, typename Value>
  63. unordered_hash_node<Key, Value>::unordered_hash_node(const Key& key, const Value& value)
  64. : first(key)
  65. , second(value)
  66. {
  67. }
  68. template <typename Key>
  69. struct unordered_hash_node<Key, void> {
  70. unordered_hash_node(const Key& key);
  71. const Key first;
  72. unordered_hash_node* next;
  73. unordered_hash_node* prev;
  74. private:
  75. unordered_hash_node& operator=(const unordered_hash_node&);
  76. };
  77. template<typename Key>
  78. unordered_hash_node<Key, void>::unordered_hash_node(const Key& key)
  79. : first(key)
  80. {
  81. }
  82. template<typename Key, typename Value>
  83. static void unordered_hash_node_insert(unordered_hash_node<Key, Value>* node, size_t hash, unordered_hash_node<Key, Value>** buckets, size_t nbuckets) {
  84. size_t bucket = hash & (nbuckets - 1);
  85. unordered_hash_node<Key, Value>* it = buckets[bucket + 1];
  86. node->next = it;
  87. if (it) {
  88. node->prev = it->prev;
  89. it->prev = node;
  90. if (node->prev)
  91. node->prev->next = node;
  92. } else {
  93. size_t newbucket = bucket;
  94. while (newbucket && !buckets[newbucket])
  95. --newbucket;
  96. unordered_hash_node<Key, Value>* prev = buckets[newbucket];
  97. while (prev && prev->next)
  98. prev = prev->next;
  99. node->prev = prev;
  100. if (prev)
  101. prev->next = node;
  102. }
  103. // propagate node through buckets
  104. for (; it == buckets[bucket]; --bucket) {
  105. buckets[bucket] = node;
  106. if (!bucket)
  107. break;
  108. }
  109. }
  110. template<typename Key, typename Value>
  111. 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) {
  112. size_t bucket = hash & (nbuckets - 1);
  113. unordered_hash_node<Key, Value>* next = where->next;
  114. for (; buckets[bucket] == where; --bucket) {
  115. buckets[bucket] = next;
  116. if (!bucket)
  117. break;
  118. }
  119. if (where->prev)
  120. where->prev->next = where->next;
  121. if (next)
  122. next->prev = where->prev;
  123. }
  124. template<typename Node>
  125. struct unordered_hash_iterator {
  126. Node* operator->() const;
  127. Node& operator*() const;
  128. Node* node;
  129. };
  130. template<typename Node>
  131. struct unordered_hash_iterator<const Node> {
  132. unordered_hash_iterator() {}
  133. unordered_hash_iterator(unordered_hash_iterator<Node> other)
  134. : node(other.node)
  135. {
  136. }
  137. const Node* operator->() const;
  138. const Node& operator*() const;
  139. const Node* node;
  140. };
  141. template<typename Key>
  142. struct unordered_hash_iterator<const unordered_hash_node<Key, void> > {
  143. const Key* operator->() const;
  144. const Key& operator*() const;
  145. unordered_hash_node<Key, void>* 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 LNode, typename RNode>
  152. static inline bool operator!=(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  153. return lhs.node != rhs.node;
  154. }
  155. template<typename Node>
  156. static inline void operator++(unordered_hash_iterator<Node>& lhs) {
  157. lhs.node = lhs.node->next;
  158. }
  159. template<typename Node>
  160. inline Node* unordered_hash_iterator<Node>::operator->() const {
  161. return node;
  162. }
  163. template<typename Node>
  164. inline Node& unordered_hash_iterator<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 Node>
  172. inline const Node& unordered_hash_iterator<const Node>::operator*() const {
  173. return *node;
  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 Key>
  180. inline const Key& unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator*() const {
  181. return node->first;
  182. }
  183. template<typename Node, typename Key>
  184. static inline Node unordered_hash_find(const Key& key, Node* buckets, size_t nbuckets) {
  185. const size_t bucket = hash(key) & (nbuckets - 2);
  186. for (Node it = buckets[bucket], end = buckets[bucket+1]; it != end; it = it->next)
  187. if (it->first == key)
  188. return it;
  189. return 0;
  190. }
  191. }
  192. #endif