hashtable_node.hpp 10.0 KB

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