hash_map.hpp 8.1 KB

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