hashtable_node.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_HASHTABLE_NODE_HPP
  13. #define BOOST_INTRUSIVE_HASHTABLE_NODE_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <iterator>
  16. #include <boost/intrusive/detail/assert.hpp>
  17. #include <boost/intrusive/pointer_traits.hpp>
  18. #include <boost/intrusive/circular_list_algorithms.hpp>
  19. #include <boost/intrusive/detail/mpl.hpp>
  20. #include <boost/intrusive/detail/utilities.hpp>
  21. #include <boost/intrusive/slist.hpp> //remove-me
  22. #include <boost/intrusive/pointer_traits.hpp>
  23. #include <boost/intrusive/trivial_value_traits.hpp>
  24. #include <cstddef>
  25. #include <boost/type_traits/make_unsigned.hpp>
  26. #include <boost/pointer_cast.hpp>
  27. #include <boost/move/core.hpp>
  28. namespace boost {
  29. namespace intrusive {
  30. namespace detail {
  31. template<int Dummy = 0>
  32. struct prime_list_holder
  33. {
  34. static const std::size_t prime_list[];
  35. static const std::size_t prime_list_size;
  36. };
  37. template<int Dummy>
  38. const std::size_t prime_list_holder<Dummy>::prime_list[] = {
  39. 3ul, 7ul, 11ul, 17ul, 29ul,
  40. 53ul, 97ul, 193ul, 389ul, 769ul,
  41. 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
  42. 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
  43. 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
  44. 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
  45. 1610612741ul, 3221225473ul, 4294967291ul };
  46. template<int Dummy>
  47. const std::size_t prime_list_holder<Dummy>::prime_list_size
  48. = sizeof(prime_list)/sizeof(std::size_t);
  49. template <class Slist>
  50. struct bucket_impl : public Slist
  51. {
  52. typedef Slist slist_type;
  53. bucket_impl()
  54. {}
  55. bucket_impl(const bucket_impl &)
  56. {}
  57. ~bucket_impl()
  58. {
  59. //This bucket is still being used!
  60. BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty());
  61. }
  62. bucket_impl &operator=(const bucket_impl&)
  63. {
  64. //This bucket is still in use!
  65. BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty());
  66. //Slist::clear();
  67. return *this;
  68. }
  69. };
  70. template<class Slist>
  71. struct bucket_traits_impl
  72. {
  73. private:
  74. BOOST_COPYABLE_AND_MOVABLE(bucket_traits_impl)
  75. public:
  76. /// @cond
  77. typedef typename pointer_traits
  78. <typename Slist::pointer>::template rebind_pointer
  79. < bucket_impl<Slist> >::type bucket_ptr;
  80. typedef Slist slist;
  81. typedef typename Slist::size_type size_type;
  82. /// @endcond
  83. bucket_traits_impl(bucket_ptr buckets, size_type len)
  84. : buckets_(buckets), buckets_len_(len)
  85. {}
  86. bucket_traits_impl(const bucket_traits_impl &x)
  87. : buckets_(x.buckets_), buckets_len_(x.buckets_len_)
  88. {}
  89. bucket_traits_impl(BOOST_RV_REF(bucket_traits_impl) x)
  90. : buckets_(x.buckets_), buckets_len_(x.buckets_len_)
  91. { x.buckets_ = bucket_ptr(); x.buckets_len_ = 0; }
  92. bucket_traits_impl& operator=(BOOST_RV_REF(bucket_traits_impl) x)
  93. {
  94. buckets_ = x.buckets_; buckets_len_ = x.buckets_len_;
  95. x.buckets_ = bucket_ptr(); x.buckets_len_ = 0; return *this;
  96. }
  97. bucket_traits_impl& operator=(BOOST_COPY_ASSIGN_REF(bucket_traits_impl) x)
  98. {
  99. buckets_ = x.buckets_; buckets_len_ = x.buckets_len_; return *this;
  100. }
  101. const bucket_ptr &bucket_begin() const
  102. { return buckets_; }
  103. size_type bucket_count() const
  104. { return buckets_len_; }
  105. private:
  106. bucket_ptr buckets_;
  107. size_type buckets_len_;
  108. };
  109. template <class NodeTraits>
  110. struct hash_reduced_slist_node_traits
  111. {
  112. template <class U> static detail::one test(...);
  113. template <class U> static detail::two test(typename U::reduced_slist_node_traits* = 0);
  114. static const bool value = sizeof(test<NodeTraits>(0)) == sizeof(detail::two);
  115. };
  116. template <class NodeTraits>
  117. struct apply_reduced_slist_node_traits
  118. {
  119. typedef typename NodeTraits::reduced_slist_node_traits type;
  120. };
  121. template <class NodeTraits>
  122. struct reduced_slist_node_traits
  123. {
  124. typedef typename detail::eval_if_c
  125. < hash_reduced_slist_node_traits<NodeTraits>::value
  126. , apply_reduced_slist_node_traits<NodeTraits>
  127. , detail::identity<NodeTraits>
  128. >::type type;
  129. };
  130. template<class NodeTraits>
  131. struct get_slist_impl
  132. {
  133. typedef trivial_value_traits<NodeTraits, normal_link> trivial_traits;
  134. //Reducing symbol length
  135. struct type : make_slist
  136. < typename NodeTraits::node
  137. , boost::intrusive::value_traits<trivial_traits>
  138. , boost::intrusive::constant_time_size<false>
  139. , boost::intrusive::size_type<typename boost::make_unsigned
  140. <typename pointer_traits<typename NodeTraits::node_ptr>::difference_type>::type >
  141. >::type
  142. {};
  143. };
  144. } //namespace detail {
  145. template<class BucketValueTraits, bool IsConst>
  146. class hashtable_iterator
  147. : public std::iterator
  148. < std::forward_iterator_tag
  149. , typename BucketValueTraits::real_value_traits::value_type
  150. , typename pointer_traits<typename BucketValueTraits::real_value_traits::value_type*>::difference_type
  151. , typename detail::add_const_if_c
  152. <typename BucketValueTraits::real_value_traits::value_type, IsConst>::type *
  153. , typename detail::add_const_if_c
  154. <typename BucketValueTraits::real_value_traits::value_type, IsConst>::type &
  155. >
  156. {
  157. typedef typename BucketValueTraits::real_value_traits real_value_traits;
  158. typedef typename BucketValueTraits::real_bucket_traits real_bucket_traits;
  159. typedef typename real_value_traits::node_traits node_traits;
  160. typedef typename detail::get_slist_impl
  161. <typename detail::reduced_slist_node_traits
  162. <typename real_value_traits::node_traits>::type
  163. >::type slist_impl;
  164. typedef typename slist_impl::iterator siterator;
  165. typedef typename slist_impl::const_iterator const_siterator;
  166. typedef detail::bucket_impl<slist_impl> bucket_type;
  167. typedef typename pointer_traits
  168. <typename real_value_traits::pointer>::template rebind_pointer
  169. < const BucketValueTraits >::type const_bucketvaltraits_ptr;
  170. typedef typename slist_impl::size_type size_type;
  171. static typename node_traits::node_ptr downcast_bucket(typename bucket_type::node_ptr p)
  172. {
  173. return pointer_traits<typename node_traits::node_ptr>::
  174. pointer_to(static_cast<typename node_traits::node&>(*p));
  175. }
  176. public:
  177. typedef typename real_value_traits::value_type value_type;
  178. typedef typename detail::add_const_if_c<value_type, IsConst>::type *pointer;
  179. typedef typename detail::add_const_if_c<value_type, IsConst>::type &reference;
  180. hashtable_iterator ()
  181. {}
  182. explicit hashtable_iterator(siterator ptr, const BucketValueTraits *cont)
  183. : slist_it_ (ptr), traitsptr_ (cont ? pointer_traits<const_bucketvaltraits_ptr>::pointer_to(*cont) : const_bucketvaltraits_ptr() )
  184. {}
  185. hashtable_iterator(const hashtable_iterator<BucketValueTraits, false> &other)
  186. : slist_it_(other.slist_it()), traitsptr_(other.get_bucket_value_traits())
  187. {}
  188. const siterator &slist_it() const
  189. { return slist_it_; }
  190. hashtable_iterator<BucketValueTraits, false> unconst() const
  191. { return hashtable_iterator<BucketValueTraits, false>(this->slist_it(), this->get_bucket_value_traits()); }
  192. public:
  193. hashtable_iterator& operator++()
  194. { this->increment(); return *this; }
  195. hashtable_iterator operator++(int)
  196. {
  197. hashtable_iterator result (*this);
  198. this->increment();
  199. return result;
  200. }
  201. friend bool operator== (const hashtable_iterator& i, const hashtable_iterator& i2)
  202. { return i.slist_it_ == i2.slist_it_; }
  203. friend bool operator!= (const hashtable_iterator& i, const hashtable_iterator& i2)
  204. { return !(i == i2); }
  205. reference operator*() const
  206. { return *this->operator ->(); }
  207. pointer operator->() const
  208. {
  209. return boost::intrusive::detail::to_raw_pointer(this->priv_real_value_traits().to_value_ptr
  210. (downcast_bucket(slist_it_.pointed_node())));
  211. }
  212. const const_bucketvaltraits_ptr &get_bucket_value_traits() const
  213. { return traitsptr_; }
  214. const real_value_traits &priv_real_value_traits() const
  215. { return traitsptr_->priv_real_value_traits(); }
  216. const real_bucket_traits &priv_real_bucket_traits() const
  217. { return traitsptr_->priv_real_bucket_traits(); }
  218. private:
  219. void increment()
  220. {
  221. const real_bucket_traits &rbuck_traits = this->priv_real_bucket_traits();
  222. bucket_type* const buckets = boost::intrusive::detail::to_raw_pointer(rbuck_traits.bucket_begin());
  223. const size_type buckets_len = rbuck_traits.bucket_count();
  224. ++slist_it_;
  225. const typename slist_impl::node_ptr n = slist_it_.pointed_node();
  226. const siterator first_bucket_bbegin = buckets->end();
  227. if(first_bucket_bbegin.pointed_node() <= n && n <= buckets[buckets_len-1].cend().pointed_node()){
  228. //If one-past the node is inside the bucket then look for the next non-empty bucket
  229. //1. get the bucket_impl from the iterator
  230. const bucket_type &b = static_cast<const bucket_type&>
  231. (bucket_type::slist_type::container_from_end_iterator(slist_it_));
  232. //2. Now just calculate the index b has in the bucket array
  233. size_type n_bucket = static_cast<size_type>(&b - buckets);
  234. //3. Iterate until a non-empty bucket is found
  235. do{
  236. if (++n_bucket >= buckets_len){ //bucket overflow, return end() iterator
  237. slist_it_ = buckets->before_begin();
  238. return;
  239. }
  240. }
  241. while (buckets[n_bucket].empty());
  242. slist_it_ = buckets[n_bucket].begin();
  243. }
  244. else{
  245. //++slist_it_ yield to a valid object
  246. }
  247. }
  248. siterator slist_it_;
  249. const_bucketvaltraits_ptr traitsptr_;
  250. };
  251. } //namespace intrusive {
  252. } //namespace boost {
  253. #endif