hashtable_node.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2014
  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. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. #include <boost/intrusive/detail/workaround.hpp>
  21. #include <boost/intrusive/detail/assert.hpp>
  22. #include <boost/intrusive/pointer_traits.hpp>
  23. #include <boost/intrusive/detail/mpl.hpp>
  24. #include <boost/intrusive/trivial_value_traits.hpp>
  25. #include <boost/intrusive/slist.hpp> //make_slist
  26. #include <cstddef>
  27. #include <climits>
  28. #include <boost/move/core.hpp>
  29. namespace boost {
  30. namespace intrusive {
  31. namespace detail {
  32. template <class Slist>
  33. struct bucket_impl : public Slist
  34. {
  35. typedef Slist slist_type;
  36. bucket_impl()
  37. {}
  38. bucket_impl(const bucket_impl &)
  39. {}
  40. ~bucket_impl()
  41. {
  42. //This bucket is still being used!
  43. BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty());
  44. }
  45. bucket_impl &operator=(const bucket_impl&)
  46. {
  47. //This bucket is still in use!
  48. BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty());
  49. //Slist::clear();
  50. return *this;
  51. }
  52. };
  53. template<class Slist>
  54. struct bucket_traits_impl
  55. {
  56. private:
  57. BOOST_COPYABLE_AND_MOVABLE(bucket_traits_impl)
  58. public:
  59. /// @cond
  60. typedef typename pointer_traits
  61. <typename Slist::pointer>::template rebind_pointer
  62. < bucket_impl<Slist> >::type bucket_ptr;
  63. typedef Slist slist;
  64. typedef typename Slist::size_type size_type;
  65. /// @endcond
  66. bucket_traits_impl(bucket_ptr buckets, size_type len)
  67. : buckets_(buckets), buckets_len_(len)
  68. {}
  69. bucket_traits_impl(const bucket_traits_impl &x)
  70. : buckets_(x.buckets_), buckets_len_(x.buckets_len_)
  71. {}
  72. bucket_traits_impl(BOOST_RV_REF(bucket_traits_impl) x)
  73. : buckets_(x.buckets_), buckets_len_(x.buckets_len_)
  74. { x.buckets_ = bucket_ptr(); x.buckets_len_ = 0; }
  75. bucket_traits_impl& operator=(BOOST_RV_REF(bucket_traits_impl) x)
  76. {
  77. buckets_ = x.buckets_; buckets_len_ = x.buckets_len_;
  78. x.buckets_ = bucket_ptr(); x.buckets_len_ = 0; return *this;
  79. }
  80. bucket_traits_impl& operator=(BOOST_COPY_ASSIGN_REF(bucket_traits_impl) x)
  81. {
  82. buckets_ = x.buckets_; buckets_len_ = x.buckets_len_; return *this;
  83. }
  84. BOOST_INTRUSIVE_FORCEINLINE const bucket_ptr &bucket_begin() const
  85. { return buckets_; }
  86. BOOST_INTRUSIVE_FORCEINLINE size_type bucket_count() const
  87. { return buckets_len_; }
  88. private:
  89. bucket_ptr buckets_;
  90. size_type buckets_len_;
  91. };
  92. template <class NodeTraits>
  93. struct hash_reduced_slist_node_traits
  94. {
  95. template <class U> static detail::no_type test(...);
  96. template <class U> static detail::yes_type test(typename U::reduced_slist_node_traits*);
  97. static const bool value = sizeof(test<NodeTraits>(0)) == sizeof(detail::yes_type);
  98. };
  99. template <class NodeTraits>
  100. struct apply_reduced_slist_node_traits
  101. {
  102. typedef typename NodeTraits::reduced_slist_node_traits type;
  103. };
  104. template <class NodeTraits>
  105. struct reduced_slist_node_traits
  106. {
  107. typedef typename detail::eval_if_c
  108. < hash_reduced_slist_node_traits<NodeTraits>::value
  109. , apply_reduced_slist_node_traits<NodeTraits>
  110. , detail::identity<NodeTraits>
  111. >::type type;
  112. };
  113. template<class NodeTraits>
  114. struct get_slist_impl
  115. {
  116. typedef trivial_value_traits<NodeTraits, normal_link> trivial_traits;
  117. //Reducing symbol length
  118. struct type : make_slist
  119. < typename NodeTraits::node
  120. , boost::intrusive::value_traits<trivial_traits>
  121. , boost::intrusive::constant_time_size<false>
  122. , boost::intrusive::size_type<std::size_t>
  123. >::type
  124. {};
  125. };
  126. } //namespace detail {
  127. template<class BucketValueTraits, bool IsConst>
  128. class hashtable_iterator
  129. {
  130. typedef typename BucketValueTraits::value_traits value_traits;
  131. typedef typename BucketValueTraits::bucket_traits bucket_traits;
  132. typedef iiterator< value_traits, IsConst
  133. , std::forward_iterator_tag> types_t;
  134. public:
  135. typedef typename types_t::iterator_type::difference_type difference_type;
  136. typedef typename types_t::iterator_type::value_type value_type;
  137. typedef typename types_t::iterator_type::pointer pointer;
  138. typedef typename types_t::iterator_type::reference reference;
  139. typedef typename types_t::iterator_type::iterator_category iterator_category;
  140. private:
  141. typedef typename value_traits::node_traits node_traits;
  142. typedef typename node_traits::node_ptr node_ptr;
  143. typedef typename detail::get_slist_impl
  144. < typename detail::reduced_slist_node_traits
  145. <node_traits>::type >::type slist_impl;
  146. typedef typename slist_impl::iterator siterator;
  147. typedef typename slist_impl::const_iterator const_siterator;
  148. typedef detail::bucket_impl<slist_impl> bucket_type;
  149. typedef typename pointer_traits
  150. <pointer>::template rebind_pointer
  151. < const BucketValueTraits >::type const_bucketvaltraits_ptr;
  152. typedef typename slist_impl::size_type size_type;
  153. static node_ptr downcast_bucket(typename bucket_type::node_ptr p)
  154. {
  155. return pointer_traits<node_ptr>::
  156. pointer_to(static_cast<typename node_traits::node&>(*p));
  157. }
  158. public:
  159. BOOST_INTRUSIVE_FORCEINLINE hashtable_iterator ()
  160. : slist_it_() //Value initialization to achieve "null iterators" (N3644)
  161. {}
  162. explicit hashtable_iterator(siterator ptr, const BucketValueTraits *cont)
  163. : slist_it_ (ptr)
  164. , traitsptr_ (cont ? pointer_traits<const_bucketvaltraits_ptr>::pointer_to(*cont) : const_bucketvaltraits_ptr() )
  165. {}
  166. hashtable_iterator(const hashtable_iterator<BucketValueTraits, false> &other)
  167. : slist_it_(other.slist_it()), traitsptr_(other.get_bucket_value_traits())
  168. {}
  169. BOOST_INTRUSIVE_FORCEINLINE const siterator &slist_it() const
  170. { return slist_it_; }
  171. hashtable_iterator<BucketValueTraits, false> unconst() const
  172. { return hashtable_iterator<BucketValueTraits, false>(this->slist_it(), this->get_bucket_value_traits()); }
  173. hashtable_iterator& operator++()
  174. { this->increment(); return *this; }
  175. hashtable_iterator operator++(int)
  176. {
  177. hashtable_iterator result (*this);
  178. this->increment();
  179. return result;
  180. }
  181. BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const hashtable_iterator& i, const hashtable_iterator& i2)
  182. { return i.slist_it_ == i2.slist_it_; }
  183. BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const hashtable_iterator& i, const hashtable_iterator& i2)
  184. { return !(i == i2); }
  185. BOOST_INTRUSIVE_FORCEINLINE reference operator*() const
  186. { return *this->operator ->(); }
  187. BOOST_INTRUSIVE_FORCEINLINE pointer operator->() const
  188. {
  189. return this->priv_value_traits().to_value_ptr
  190. (downcast_bucket(slist_it_.pointed_node()));
  191. }
  192. BOOST_INTRUSIVE_FORCEINLINE const const_bucketvaltraits_ptr &get_bucket_value_traits() const
  193. { return traitsptr_; }
  194. BOOST_INTRUSIVE_FORCEINLINE const value_traits &priv_value_traits() const
  195. { return traitsptr_->priv_value_traits(); }
  196. BOOST_INTRUSIVE_FORCEINLINE const bucket_traits &priv_bucket_traits() const
  197. { return traitsptr_->priv_bucket_traits(); }
  198. private:
  199. void increment()
  200. {
  201. const bucket_traits &rbuck_traits = this->priv_bucket_traits();
  202. bucket_type* const buckets = boost::intrusive::detail::to_raw_pointer(rbuck_traits.bucket_begin());
  203. const size_type buckets_len = rbuck_traits.bucket_count();
  204. ++slist_it_;
  205. const typename slist_impl::node_ptr n = slist_it_.pointed_node();
  206. const siterator first_bucket_bbegin = buckets->end();
  207. if(first_bucket_bbegin.pointed_node() <= n && n <= buckets[buckets_len-1].cend().pointed_node()){
  208. //If one-past the node is inside the bucket then look for the next non-empty bucket
  209. //1. get the bucket_impl from the iterator
  210. const bucket_type &b = static_cast<const bucket_type&>
  211. (bucket_type::slist_type::container_from_end_iterator(slist_it_));
  212. //2. Now just calculate the index b has in the bucket array
  213. size_type n_bucket = static_cast<size_type>(&b - buckets);
  214. //3. Iterate until a non-empty bucket is found
  215. do{
  216. if (++n_bucket >= buckets_len){ //bucket overflow, return end() iterator
  217. slist_it_ = buckets->before_begin();
  218. return;
  219. }
  220. }
  221. while (buckets[n_bucket].empty());
  222. slist_it_ = buckets[n_bucket].begin();
  223. }
  224. else{
  225. //++slist_it_ yield to a valid object
  226. }
  227. }
  228. siterator slist_it_;
  229. const_bucketvaltraits_ptr traitsptr_;
  230. };
  231. } //namespace intrusive {
  232. } //namespace boost {
  233. #endif