hash_map.hpp 7.9 KB

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