hash_base.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*-
  2. * Copyright 2012-2018 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 <tinystl/stddef.h>
  29. #include <tinystl/traits.h>
  30. namespace tinystl {
  31. template<typename Key, typename Value>
  32. struct pair {
  33. pair();
  34. pair(const pair& other);
  35. pair(pair&& other);
  36. pair(const Key& key, const Value& value);
  37. pair(Key&& key, Value&& value);
  38. pair& operator=(const pair& other);
  39. pair& operator=(pair&& other);
  40. Key first;
  41. Value second;
  42. using first_type = Key;
  43. using second_type = Value;
  44. };
  45. template<typename Key, typename Value>
  46. inline pair<Key, Value>::pair() {
  47. }
  48. template<typename Key, typename Value>
  49. inline pair<Key, Value>::pair(const pair& other)
  50. : first(other.first)
  51. , second(other.second)
  52. {
  53. }
  54. template<typename Key, typename Value>
  55. inline pair<Key, Value>::pair(pair&& other)
  56. : first(static_cast<Key&&>(other.first))
  57. , second(static_cast<Value&&>(other.second))
  58. {
  59. }
  60. template<typename Key, typename Value>
  61. inline pair<Key, Value>::pair(const Key& key, const Value& value)
  62. : first(key)
  63. , second(value)
  64. {
  65. }
  66. template<typename Key, typename Value>
  67. inline pair<Key, Value>::pair(Key&& key, Value&& value)
  68. : first(static_cast<Key&&>(key))
  69. , second(static_cast<Value&&>(value))
  70. {
  71. }
  72. template<typename Key, typename Value>
  73. inline pair<Key, Value>& pair<Key, Value>::operator=(const pair& other) {
  74. first = other.first;
  75. second = other.second;
  76. return *this;
  77. }
  78. template<typename Key, typename Value>
  79. inline pair<Key, Value>& pair<Key, Value>::operator=(pair&& other) {
  80. first = static_cast<Key&&>(other.first);
  81. second = static_cast<Value&&>(other.second);
  82. return *this;
  83. }
  84. template<typename Key, typename Value>
  85. static inline pair<typename remove_const_reference<Key>::type, typename remove_const_reference<Value>::type>
  86. make_pair(Key&& key, Value&& value) {
  87. return pair<typename remove_const_reference<Key>::type, typename remove_const_reference<Value>::type>(
  88. static_cast<Key&&>(key)
  89. , static_cast<Value&&>(value)
  90. );
  91. }
  92. template<typename Key, typename Value>
  93. struct unordered_hash_node {
  94. unordered_hash_node(const Key& key, const Value& value);
  95. unordered_hash_node(Key&& key, Value&& value);
  96. const Key first;
  97. Value second;
  98. unordered_hash_node* next;
  99. unordered_hash_node* prev;
  100. };
  101. template<typename Key, typename Value>
  102. inline unordered_hash_node<Key, Value>::unordered_hash_node(const Key& key, const Value& value)
  103. : first(key)
  104. , second(value)
  105. {
  106. }
  107. template<typename Key, typename Value>
  108. inline unordered_hash_node<Key, Value>::unordered_hash_node(Key&& key, Value&& value)
  109. : first(static_cast<Key&&>(key))
  110. , second(static_cast<Value&&>(value))
  111. {
  112. }
  113. template <typename Key>
  114. struct unordered_hash_node<Key, void> {
  115. explicit unordered_hash_node(const Key& key);
  116. explicit unordered_hash_node(Key&& key);
  117. const Key first;
  118. unordered_hash_node* next;
  119. unordered_hash_node* prev;
  120. };
  121. template<typename Key>
  122. inline unordered_hash_node<Key, void>::unordered_hash_node(const Key& key)
  123. : first(key)
  124. {
  125. }
  126. template<typename Key>
  127. inline unordered_hash_node<Key, void>::unordered_hash_node(Key&& key)
  128. : first(static_cast<Key&&>(key))
  129. {
  130. }
  131. template<typename Key, typename Value>
  132. static inline void unordered_hash_node_insert(unordered_hash_node<Key, Value>* node, size_t hash, unordered_hash_node<Key, Value>** buckets, size_t nbuckets) {
  133. size_t bucket = hash & (nbuckets - 1);
  134. unordered_hash_node<Key, Value>* it = buckets[bucket + 1];
  135. node->next = it;
  136. if (it) {
  137. node->prev = it->prev;
  138. it->prev = node;
  139. if (node->prev)
  140. node->prev->next = node;
  141. } else {
  142. size_t newbucket = bucket;
  143. while (newbucket && !buckets[newbucket])
  144. --newbucket;
  145. unordered_hash_node<Key, Value>* prev = buckets[newbucket];
  146. while (prev && prev->next)
  147. prev = prev->next;
  148. node->prev = prev;
  149. if (prev)
  150. prev->next = node;
  151. }
  152. // propagate node through buckets
  153. for (; it == buckets[bucket]; --bucket) {
  154. buckets[bucket] = node;
  155. if (!bucket)
  156. break;
  157. }
  158. }
  159. template<typename Key, typename Value>
  160. 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) {
  161. size_t bucket = hash & (nbuckets - 1);
  162. unordered_hash_node<Key, Value>* next = where->next;
  163. for (; buckets[bucket] == where; --bucket) {
  164. buckets[bucket] = next;
  165. if (!bucket)
  166. break;
  167. }
  168. if (where->prev)
  169. where->prev->next = where->next;
  170. if (next)
  171. next->prev = where->prev;
  172. }
  173. template<typename Node>
  174. struct unordered_hash_iterator {
  175. Node* operator->() const;
  176. Node& operator*() const;
  177. Node* node;
  178. };
  179. template<typename Node>
  180. struct unordered_hash_iterator<const Node> {
  181. unordered_hash_iterator() {}
  182. unordered_hash_iterator(unordered_hash_iterator<Node> other)
  183. : node(other.node)
  184. {
  185. }
  186. const Node* operator->() const;
  187. const Node& operator*() const;
  188. const Node* node;
  189. };
  190. template<typename Key>
  191. struct unordered_hash_iterator<const unordered_hash_node<Key, void> > {
  192. const Key* operator->() const;
  193. const Key& operator*() const;
  194. unordered_hash_node<Key, void>* node;
  195. };
  196. template<typename LNode, typename RNode>
  197. static inline bool operator==(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  198. return lhs.node == rhs.node;
  199. }
  200. template<typename LNode, typename RNode>
  201. static inline bool operator!=(const unordered_hash_iterator<LNode>& lhs, const unordered_hash_iterator<RNode>& rhs) {
  202. return lhs.node != rhs.node;
  203. }
  204. template<typename Node>
  205. static inline void operator++(unordered_hash_iterator<Node>& lhs) {
  206. lhs.node = lhs.node->next;
  207. }
  208. template<typename Node>
  209. inline Node* unordered_hash_iterator<Node>::operator->() const {
  210. return node;
  211. }
  212. template<typename Node>
  213. inline Node& unordered_hash_iterator<Node>::operator*() const {
  214. return *node;
  215. }
  216. template<typename Node>
  217. inline const Node* unordered_hash_iterator<const Node>::operator->() const {
  218. return node;
  219. }
  220. template<typename Node>
  221. inline const Node& unordered_hash_iterator<const Node>::operator*() const {
  222. return *node;
  223. }
  224. template<typename Key>
  225. inline const Key* unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator->() const {
  226. return &node->first;
  227. }
  228. template<typename Key>
  229. inline const Key& unordered_hash_iterator<const unordered_hash_node<Key, void> >::operator*() const {
  230. return node->first;
  231. }
  232. template<typename Node, typename Key>
  233. static inline Node unordered_hash_find(const Key& key, Node* buckets, size_t nbuckets) {
  234. if (!buckets) return 0;
  235. const size_t bucket = hash(key) & (nbuckets - 2);
  236. for (Node it = buckets[bucket], end = buckets[bucket+1]; it != end; it = it->next)
  237. if (it->first == key)
  238. return it;
  239. return 0;
  240. }
  241. }
  242. #endif