/Src/Dependencies/Boost/boost/asio/detail/hash_map.hpp

http://hadesmem.googlecode.com/ · C++ Header · 331 lines · 245 code · 44 blank · 42 comment · 49 complexity · 1f7a87e8ce0eaccdd31a95622f7ede17 MD5 · raw file

  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 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 <boost/assert.hpp>
  17. #include <list>
  18. #include <utility>
  19. #include <boost/asio/detail/noncopyable.hpp>
  20. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  21. # include <boost/asio/detail/socket_types.hpp>
  22. #endif // defined(BOOST_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_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_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_ASSERT(it != values_.end());
  161. size_t bucket = calculate_hash_value(it->first) % num_buckets_;
  162. bool is_first = (it == buckets_[bucket].first);
  163. bool is_last = (it == buckets_[bucket].last);
  164. if (is_first && is_last)
  165. buckets_[bucket].first = buckets_[bucket].last = values_.end();
  166. else if (is_first)
  167. ++buckets_[bucket].first;
  168. else if (is_last)
  169. --buckets_[bucket].last;
  170. values_erase(it);
  171. --size_;
  172. }
  173. // Erase a key from the map.
  174. void erase(const K& k)
  175. {
  176. iterator it = find(k);
  177. if (it != values_.end())
  178. erase(it);
  179. }
  180. // Remove all entries from the map.
  181. void clear()
  182. {
  183. // Clear the values.
  184. values_.clear();
  185. size_ = 0;
  186. // Initialise all buckets to empty.
  187. iterator end_it = values_.end();
  188. for (size_t i = 0; i < num_buckets_; ++i)
  189. buckets_[i].first = buckets_[i].last = end_it;
  190. }
  191. private:
  192. // Calculate the hash size for the specified number of elements.
  193. static std::size_t hash_size(std::size_t num_elems)
  194. {
  195. static std::size_t sizes[] =
  196. {
  197. #if defined(BOOST_ASIO_HASH_MAP_BUCKETS)
  198. BOOST_ASIO_HASH_MAP_BUCKETS
  199. #else // BOOST_ASIO_HASH_MAP_BUCKETS
  200. 3, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
  201. 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
  202. 12582917, 25165843
  203. #endif // BOOST_ASIO_HASH_MAP_BUCKETS
  204. };
  205. const std::size_t nth_size = sizeof(sizes) / sizeof(std::size_t) - 1;
  206. for (std::size_t i = 0; i < nth_size; ++i)
  207. if (num_elems < sizes[i])
  208. return sizes[i];
  209. return sizes[nth_size];
  210. }
  211. // Re-initialise the hash from the values already contained in the list.
  212. void rehash(std::size_t num_buckets)
  213. {
  214. if (num_buckets == num_buckets_)
  215. return;
  216. num_buckets_ = num_buckets;
  217. iterator end_iter = values_.end();
  218. // Update number of buckets and initialise all buckets to empty.
  219. bucket_type* tmp = new bucket_type[num_buckets_];
  220. delete[] buckets_;
  221. buckets_ = tmp;
  222. for (std::size_t i = 0; i < num_buckets_; ++i)
  223. buckets_[i].first = buckets_[i].last = end_iter;
  224. // Put all values back into the hash.
  225. iterator iter = values_.begin();
  226. while (iter != end_iter)
  227. {
  228. std::size_t bucket = calculate_hash_value(iter->first) % num_buckets_;
  229. if (buckets_[bucket].last == end_iter)
  230. {
  231. buckets_[bucket].first = buckets_[bucket].last = iter++;
  232. }
  233. else if (++buckets_[bucket].last == iter)
  234. {
  235. ++iter;
  236. }
  237. else
  238. {
  239. values_.splice(buckets_[bucket].last, values_, iter++);
  240. --buckets_[bucket].last;
  241. }
  242. }
  243. }
  244. // Insert an element into the values list by splicing from the spares list,
  245. // if a spare is available, and otherwise by inserting a new element.
  246. iterator values_insert(iterator it, const value_type& v)
  247. {
  248. if (spares_.empty())
  249. {
  250. return values_.insert(it, v);
  251. }
  252. else
  253. {
  254. spares_.front() = v;
  255. values_.splice(it, spares_, spares_.begin());
  256. return --it;
  257. }
  258. }
  259. // Erase an element from the values list by splicing it to the spares list.
  260. void values_erase(iterator it)
  261. {
  262. *it = value_type();
  263. spares_.splice(spares_.begin(), values_, it);
  264. }
  265. // The number of elements in the hash.
  266. std::size_t size_;
  267. // The list of all values in the hash map.
  268. std::list<value_type> values_;
  269. // The list of spare nodes waiting to be recycled. Assumes that POD types only
  270. // are stored in the hash map.
  271. std::list<value_type> spares_;
  272. // The type for a bucket in the hash table.
  273. struct bucket_type
  274. {
  275. iterator first;
  276. iterator last;
  277. };
  278. // The buckets in the hash.
  279. bucket_type* buckets_;
  280. // The number of buckets in the hash.
  281. std::size_t num_buckets_;
  282. };
  283. } // namespace detail
  284. } // namespace asio
  285. } // namespace boost
  286. #include <boost/asio/detail/pop_options.hpp>
  287. #endif // BOOST_ASIO_DETAIL_HASH_MAP_HPP