bucket_array.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Copyright 2003-2020 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_BUCKET_ARRAY_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_BUCKET_ARRAY_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <algorithm>
  15. #include <boost/multi_index/detail/allocator_traits.hpp>
  16. #include <boost/multi_index/detail/auto_space.hpp>
  17. #include <boost/multi_index/detail/hash_index_node.hpp>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/preprocessor/repetition/repeat.hpp>
  20. #include <boost/preprocessor/seq/elem.hpp>
  21. #include <boost/preprocessor/seq/enum.hpp>
  22. #include <boost/preprocessor/seq/size.hpp>
  23. #include <cstddef>
  24. #include <limits.h>
  25. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  26. #include <boost/archive/archive_exception.hpp>
  27. #include <boost/serialization/access.hpp>
  28. #include <boost/throw_exception.hpp>
  29. #endif
  30. namespace boost{
  31. namespace multi_index{
  32. namespace detail{
  33. /* bucket structure for use by hashed indices */
  34. #define BOOST_MULTI_INDEX_BA_SIZES_32BIT \
  35. (53ul)(97ul)(193ul)(389ul)(769ul) \
  36. (1543ul)(3079ul)(6151ul)(12289ul)(24593ul) \
  37. (49157ul)(98317ul)(196613ul)(393241ul)(786433ul) \
  38. (1572869ul)(3145739ul)(6291469ul)(12582917ul)(25165843ul) \
  39. (50331653ul)(100663319ul)(201326611ul)(402653189ul)(805306457ul) \
  40. (1610612741ul)(3221225473ul)
  41. #if ((((ULONG_MAX>>16)>>16)>>16)>>15)==0 /* unsigned long less than 64 bits */
  42. #define BOOST_MULTI_INDEX_BA_SIZES \
  43. BOOST_MULTI_INDEX_BA_SIZES_32BIT \
  44. (4294967291ul)
  45. #else
  46. /* obtained with aid from
  47. * http://javaboutique.internet.com/prime_numb/
  48. * http://www.rsok.com/~jrm/next_ten_primes.html
  49. * and verified with
  50. * http://www.alpertron.com.ar/ECM.HTM
  51. */
  52. #define BOOST_MULTI_INDEX_BA_SIZES \
  53. BOOST_MULTI_INDEX_BA_SIZES_32BIT \
  54. (6442450939ul)(12884901893ul)(25769803751ul)(51539607551ul) \
  55. (103079215111ul)(206158430209ul)(412316860441ul)(824633720831ul) \
  56. (1649267441651ul)(3298534883309ul)(6597069766657ul)(13194139533299ul) \
  57. (26388279066623ul)(52776558133303ul)(105553116266489ul)(211106232532969ul) \
  58. (422212465066001ul)(844424930131963ul)(1688849860263953ul) \
  59. (3377699720527861ul)(6755399441055731ul)(13510798882111483ul) \
  60. (27021597764222939ul)(54043195528445957ul)(108086391056891903ul) \
  61. (216172782113783843ul)(432345564227567621ul)(864691128455135207ul) \
  62. (1729382256910270481ul)(3458764513820540933ul)(6917529027641081903ul) \
  63. (13835058055282163729ul)(18446744073709551557ul)
  64. #endif
  65. template<bool _=true> /* templatized to have in-header static var defs */
  66. class bucket_array_base:private noncopyable
  67. {
  68. protected:
  69. static const std::size_t sizes[
  70. BOOST_PP_SEQ_SIZE(BOOST_MULTI_INDEX_BA_SIZES)];
  71. static std::size_t size_index(std::size_t n)
  72. {
  73. const std::size_t *bound=std::lower_bound(sizes,sizes+sizes_length,n);
  74. if(bound==sizes+sizes_length)--bound;
  75. return bound-sizes;
  76. }
  77. #define BOOST_MULTI_INDEX_BA_POSITION_CASE(z,n,_) \
  78. case n:return hash%BOOST_PP_SEQ_ELEM(n,BOOST_MULTI_INDEX_BA_SIZES);
  79. static std::size_t position(std::size_t hash,std::size_t size_index_)
  80. {
  81. /* Accelerate hash%sizes[size_index_] by replacing with a switch on
  82. * hash%Ci expressions, each Ci a compile-time constant, which the
  83. * compiler can implement without using integer division.
  84. */
  85. switch(size_index_){
  86. default: /* never used */
  87. BOOST_PP_REPEAT(
  88. BOOST_PP_SEQ_SIZE(BOOST_MULTI_INDEX_BA_SIZES),
  89. BOOST_MULTI_INDEX_BA_POSITION_CASE,~)
  90. }
  91. }
  92. private:
  93. static const std::size_t sizes_length;
  94. };
  95. template<bool _>
  96. const std::size_t bucket_array_base<_>::sizes[]={
  97. BOOST_PP_SEQ_ENUM(BOOST_MULTI_INDEX_BA_SIZES)
  98. };
  99. template<bool _>
  100. const std::size_t bucket_array_base<_>::sizes_length=
  101. sizeof(bucket_array_base<_>::sizes)/
  102. sizeof(bucket_array_base<_>::sizes[0]);
  103. #undef BOOST_MULTI_INDEX_BA_POSITION_CASE
  104. #undef BOOST_MULTI_INDEX_BA_SIZES
  105. #undef BOOST_MULTI_INDEX_BA_SIZES_32BIT
  106. template<typename Allocator>
  107. class bucket_array:bucket_array_base<>
  108. {
  109. typedef bucket_array_base<> super;
  110. typedef hashed_index_base_node_impl<
  111. typename rebind_alloc_for<
  112. Allocator,
  113. char
  114. >::type
  115. > base_node_impl_type;
  116. public:
  117. typedef typename base_node_impl_type::base_pointer base_pointer;
  118. typedef typename base_node_impl_type::pointer pointer;
  119. bucket_array(const Allocator& al,pointer end_,std::size_t size_):
  120. size_index_(super::size_index(size_)),
  121. spc(al,static_cast<auto_space_size_type>(super::sizes[size_index_]+1))
  122. {
  123. clear(end_);
  124. }
  125. std::size_t size()const
  126. {
  127. return super::sizes[size_index_];
  128. }
  129. std::size_t position(std::size_t hash)const
  130. {
  131. return super::position(hash,size_index_);
  132. }
  133. base_pointer begin()const{return buckets();}
  134. base_pointer end()const{return buckets()+size();}
  135. base_pointer at(std::size_t n)const{return buckets()+n;}
  136. void clear(pointer end_)
  137. {
  138. for(base_pointer x=begin(),y=end();x!=y;++x)x->prior()=pointer(0);
  139. end()->prior()=end_->prior()=end_;
  140. end_->next()=end();
  141. }
  142. void swap(bucket_array& x)
  143. {
  144. std::swap(size_index_,x.size_index_);
  145. spc.swap(x.spc);
  146. }
  147. template<typename BoolConstant>
  148. void swap(bucket_array& x,BoolConstant swap_allocators)
  149. {
  150. std::swap(size_index_,x.size_index_);
  151. spc.swap(x.spc,swap_allocators);
  152. }
  153. private:
  154. typedef auto_space<base_node_impl_type,Allocator> auto_space_type;
  155. typedef typename auto_space_type::size_type auto_space_size_type;
  156. std::size_t size_index_;
  157. auto_space_type spc;
  158. base_pointer buckets()const
  159. {
  160. return spc.data();
  161. }
  162. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  163. friend class boost::serialization::access;
  164. /* bucket_arrays do not emit any kind of serialization info. They are
  165. * fed to Boost.Serialization as hashed index iterators need to track
  166. * them during serialization.
  167. */
  168. template<class Archive>
  169. void serialize(Archive&,const unsigned int)
  170. {
  171. }
  172. #endif
  173. };
  174. template<typename Allocator>
  175. void swap(bucket_array<Allocator>& x,bucket_array<Allocator>& y)
  176. {
  177. x.swap(y);
  178. }
  179. } /* namespace multi_index::detail */
  180. } /* namespace multi_index */
  181. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  182. /* bucket_arrays never get constructed directly by Boost.Serialization,
  183. * as archives are always fed pointers to previously existent
  184. * arrays. So, if this is called it means we are dealing with a
  185. * somehow invalid archive.
  186. */
  187. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  188. namespace serialization{
  189. #else
  190. namespace multi_index{
  191. namespace detail{
  192. #endif
  193. template<class Archive,typename Allocator>
  194. inline void load_construct_data(
  195. Archive&,boost::multi_index::detail::bucket_array<Allocator>*,
  196. const unsigned int)
  197. {
  198. throw_exception(
  199. archive::archive_exception(archive::archive_exception::other_exception));
  200. }
  201. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  202. } /* namespace serialization */
  203. #else
  204. } /* namespace multi_index::detail */
  205. } /* namespace multi_index */
  206. #endif
  207. #endif
  208. } /* namespace boost */
  209. #endif