hash_map.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // hash_map.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_HASH_MAP_HPP
  11. #define ASIO_DETAIL_HASH_MAP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cassert>
  18. #include <list>
  19. #include <utility>
  20. #include <boost/functional/hash.hpp>
  21. #include "asio/detail/pop_options.hpp"
  22. #include "asio/detail/noncopyable.hpp"
  23. #include "asio/detail/socket_types.hpp"
  24. namespace asio {
  25. namespace detail {
  26. template <typename T>
  27. inline std::size_t calculate_hash_value(const T& t)
  28. {
  29. return boost::hash_value(t);
  30. }
  31. #if defined(_WIN64)
  32. inline std::size_t calculate_hash_value(SOCKET s)
  33. {
  34. return static_cast<std::size_t>(s);
  35. }
  36. #endif // defined(_WIN64)
  37. // Note: assumes K and V are POD types.
  38. template <typename K, typename V>
  39. class hash_map
  40. : private noncopyable
  41. {
  42. public:
  43. // The type of a value in the map.
  44. typedef std::pair<K, V> value_type;
  45. // The type of a non-const iterator over the hash map.
  46. typedef typename std::list<value_type>::iterator iterator;
  47. // The type of a const iterator over the hash map.
  48. typedef typename std::list<value_type>::const_iterator const_iterator;
  49. // Constructor.
  50. hash_map()
  51. : size_(0),
  52. buckets_(0),
  53. num_buckets_(0)
  54. {
  55. }
  56. // Destructor.
  57. ~hash_map()
  58. {
  59. delete[] buckets_;
  60. }
  61. // Get an iterator for the beginning of the map.
  62. iterator begin()
  63. {
  64. return values_.begin();
  65. }
  66. // Get an iterator for the beginning of the map.
  67. const_iterator begin() const
  68. {
  69. return values_.begin();
  70. }
  71. // Get an iterator for the end of the map.
  72. iterator end()
  73. {
  74. return values_.end();
  75. }
  76. // Get an iterator for the end of the map.
  77. const_iterator end() const
  78. {
  79. return values_.end();
  80. }
  81. // Check whether the map is empty.
  82. bool empty() const
  83. {
  84. return values_.empty();
  85. }
  86. // Find an entry in the map.
  87. iterator find(const K& k)
  88. {
  89. if (num_buckets_)
  90. {
  91. size_t bucket = calculate_hash_value(k) % num_buckets_;
  92. iterator it = buckets_[bucket].first;
  93. if (it == values_.end())
  94. return values_.end();
  95. iterator end = buckets_[bucket].last;
  96. ++end;
  97. while (it != end)
  98. {
  99. if (it->first == k)
  100. return it;
  101. ++it;
  102. }
  103. }
  104. return values_.end();
  105. }
  106. // Find an entry in the map.
  107. const_iterator find(const K& k) const
  108. {
  109. if (num_buckets_)
  110. {
  111. size_t bucket = calculate_hash_value(k) % num_buckets_;
  112. const_iterator it = buckets_[bucket].first;
  113. if (it == values_.end())
  114. return it;
  115. const_iterator end = buckets_[bucket].last;
  116. ++end;
  117. while (it != end)
  118. {
  119. if (it->first == k)
  120. return it;
  121. ++it;
  122. }
  123. }
  124. return values_.end();
  125. }
  126. // Insert a new entry into the map.
  127. std::pair<iterator, bool> insert(const value_type& v)
  128. {
  129. if (size_ + 1 >= num_buckets_)
  130. rehash(hash_size(size_ + 1));
  131. size_t bucket = calculate_hash_value(v.first) % num_buckets_;
  132. iterator it = buckets_[bucket].first;
  133. if (it == values_.end())
  134. {
  135. buckets_[bucket].first = buckets_[bucket].last =
  136. values_insert(values_.end(), v);
  137. ++size_;
  138. return std::pair<iterator, bool>(buckets_[bucket].last, true);
  139. }
  140. iterator end = buckets_[bucket].last;
  141. ++end;
  142. while (it != end)
  143. {
  144. if (it->first == v.first)
  145. return std::pair<iterator, bool>(it, false);
  146. ++it;
  147. }
  148. buckets_[bucket].last = values_insert(end, v);
  149. ++size_;
  150. return std::pair<iterator, bool>(buckets_[bucket].last, true);
  151. }
  152. // Erase an entry from the map.
  153. void erase(iterator it)
  154. {
  155. assert(it != values_.end());
  156. size_t bucket = calculate_hash_value(it->first) % num_buckets_;
  157. bool is_first = (it == buckets_[bucket].first);
  158. bool is_last = (it == buckets_[bucket].last);
  159. if (is_first && is_last)
  160. buckets_[bucket].first = buckets_[bucket].last = values_.end();
  161. else if (is_first)
  162. ++buckets_[bucket].first;
  163. else if (is_last)
  164. --buckets_[bucket].last;
  165. values_erase(it);
  166. --size_;
  167. }
  168. // Erase a key from the map.
  169. void erase(const K& k)
  170. {
  171. iterator it = find(k);
  172. if (it != values_.end())
  173. erase(it);
  174. }
  175. // Remove all entries from the map.
  176. void clear()
  177. {
  178. // Clear the values.
  179. values_.clear();
  180. size_ = 0;
  181. // Initialise all buckets to empty.
  182. iterator end = values_.end();
  183. for (size_t i = 0; i < num_buckets_; ++i)
  184. buckets_[i].first = buckets_[i].last = end;
  185. }
  186. private:
  187. // Calculate the hash size for the specified number of elements.
  188. static std::size_t hash_size(std::size_t num_elems)
  189. {
  190. static std::size_t sizes[] =
  191. {
  192. #if defined(ASIO_HASH_MAP_BUCKETS)
  193. ASIO_HASH_MAP_BUCKETS
  194. #else // ASIO_HASH_MAP_BUCKETS
  195. 3, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
  196. 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
  197. 12582917, 25165843
  198. #endif // ASIO_HASH_MAP_BUCKETS
  199. };
  200. const std::size_t nth_size = sizeof(sizes) / sizeof(std::size_t) - 1;
  201. for (std::size_t i = 0; i < nth_size; ++i)
  202. if (num_elems < sizes[i])
  203. return sizes[i];
  204. return sizes[nth_size];
  205. }
  206. // Re-initialise the hash from the values already contained in the list.
  207. void rehash(std::size_t num_buckets)
  208. {
  209. if (num_buckets == num_buckets_)
  210. return;
  211. num_buckets_ = num_buckets;
  212. iterator end = values_.end();
  213. // Update number of buckets and initialise all buckets to empty.
  214. bucket_type* tmp = new bucket_type[num_buckets_];
  215. delete[] buckets_;
  216. buckets_ = tmp;
  217. for (std::size_t i = 0; i < num_buckets_; ++i)
  218. buckets_[i].first = buckets_[i].last = end;
  219. // Put all values back into the hash.
  220. iterator iter = values_.begin();
  221. while (iter != end)
  222. {
  223. std::size_t bucket = calculate_hash_value(iter->first) % num_buckets_;
  224. if (buckets_[bucket].last == end)
  225. {
  226. buckets_[bucket].first = buckets_[bucket].last = iter++;
  227. }
  228. else if (++buckets_[bucket].last == iter)
  229. {
  230. ++iter;
  231. }
  232. else
  233. {
  234. values_.splice(buckets_[bucket].last, values_, iter++);
  235. --buckets_[bucket].last;
  236. }
  237. }
  238. }
  239. // Insert an element into the values list by splicing from the spares list,
  240. // if a spare is available, and otherwise by inserting a new element.
  241. iterator values_insert(iterator it, const value_type& v)
  242. {
  243. if (spares_.empty())
  244. {
  245. return values_.insert(it, v);
  246. }
  247. else
  248. {
  249. spares_.front() = v;
  250. values_.splice(it, spares_, spares_.begin());
  251. return --it;
  252. }
  253. }
  254. // Erase an element from the values list by splicing it to the spares list.
  255. void values_erase(iterator it)
  256. {
  257. *it = value_type();
  258. spares_.splice(spares_.begin(), values_, it);
  259. }
  260. // The number of elements in the hash.
  261. std::size_t size_;
  262. // The list of all values in the hash map.
  263. std::list<value_type> values_;
  264. // The list of spare nodes waiting to be recycled. Assumes that POD types only
  265. // are stored in the hash map.
  266. std::list<value_type> spares_;
  267. // The type for a bucket in the hash table.
  268. struct bucket_type
  269. {
  270. iterator first;
  271. iterator last;
  272. };
  273. // The buckets in the hash.
  274. bucket_type* buckets_;
  275. // The number of buckets in the hash.
  276. std::size_t num_buckets_;
  277. };
  278. } // namespace detail
  279. } // namespace asio
  280. #include "asio/detail/pop_options.hpp"
  281. #endif // ASIO_DETAIL_HASH_MAP_HPP