Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
8.0KB

  1. //
  2. // detail/hash_map.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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 <list>
  17. #include <utility>
  18. #include "asio/detail/assert.hpp"
  19. #include "asio/detail/noncopyable.hpp"
  20. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  21. # include "asio/detail/socket_types.hpp"
  22. #endif // defined(ASIO_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(ASIO_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(ASIO_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_it = buckets_[bucket].last;
  100. ++end_it;
  101. while (it != end_it)
  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_it = buckets_[bucket].last;
  120. ++end_it;
  121. while (it != end_it)
  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_it = buckets_[bucket].last;
  145. ++end_it;
  146. while (it != end_it)
  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_it, 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. ASIO_ASSERT(it != values_.end());
  160. ASIO_ASSERT(num_buckets_ != 0);
  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(ASIO_HASH_MAP_BUCKETS)
  198. ASIO_HASH_MAP_BUCKETS
  199. #else // 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 // 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. ASIO_ASSERT(num_buckets != 0);
  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. num_buckets_ = num_buckets;
  223. for (std::size_t i = 0; i < num_buckets_; ++i)
  224. buckets_[i].first = buckets_[i].last = end_iter;
  225. // Put all values back into the hash.
  226. iterator iter = values_.begin();
  227. while (iter != end_iter)
  228. {
  229. std::size_t bucket = calculate_hash_value(iter->first) % num_buckets_;
  230. if (buckets_[bucket].last == end_iter)
  231. {
  232. buckets_[bucket].first = buckets_[bucket].last = iter++;
  233. }
  234. else if (++buckets_[bucket].last == iter)
  235. {
  236. ++iter;
  237. }
  238. else
  239. {
  240. values_.splice(buckets_[bucket].last, values_, iter++);
  241. --buckets_[bucket].last;
  242. }
  243. }
  244. }
  245. // Insert an element into the values list by splicing from the spares list,
  246. // if a spare is available, and otherwise by inserting a new element.
  247. iterator values_insert(iterator it, const value_type& v)
  248. {
  249. if (spares_.empty())
  250. {
  251. return values_.insert(it, v);
  252. }
  253. else
  254. {
  255. spares_.front() = v;
  256. values_.splice(it, spares_, spares_.begin());
  257. return --it;
  258. }
  259. }
  260. // Erase an element from the values list by splicing it to the spares list.
  261. void values_erase(iterator it)
  262. {
  263. *it = value_type();
  264. spares_.splice(spares_.begin(), values_, it);
  265. }
  266. // The number of elements in the hash.
  267. std::size_t size_;
  268. // The list of all values in the hash map.
  269. std::list<value_type> values_;
  270. // The list of spare nodes waiting to be recycled. Assumes that POD types only
  271. // are stored in the hash map.
  272. std::list<value_type> spares_;
  273. // The type for a bucket in the hash table.
  274. struct bucket_type
  275. {
  276. iterator first;
  277. iterator last;
  278. };
  279. // The buckets in the hash.
  280. bucket_type* buckets_;
  281. // The number of buckets in the hash.
  282. std::size_t num_buckets_;
  283. };
  284. } // namespace detail
  285. } // namespace asio
  286. #include "asio/detail/pop_options.hpp"
  287. #endif // ASIO_DETAIL_HASH_MAP_HPP