bucket_array.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright 2003-2018 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. private:
  148. typedef auto_space<base_node_impl_type,Allocator> auto_space_type;
  149. typedef typename auto_space_type::size_type auto_space_size_type;
  150. std::size_t size_index_;
  151. auto_space_type spc;
  152. base_pointer buckets()const
  153. {
  154. return spc.data();
  155. }
  156. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  157. friend class boost::serialization::access;
  158. /* bucket_arrays do not emit any kind of serialization info. They are
  159. * fed to Boost.Serialization as hashed index iterators need to track
  160. * them during serialization.
  161. */
  162. template<class Archive>
  163. void serialize(Archive&,const unsigned int)
  164. {
  165. }
  166. #endif
  167. };
  168. template<typename Allocator>
  169. void swap(bucket_array<Allocator>& x,bucket_array<Allocator>& y)
  170. {
  171. x.swap(y);
  172. }
  173. } /* namespace multi_index::detail */
  174. } /* namespace multi_index */
  175. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  176. /* bucket_arrays never get constructed directly by Boost.Serialization,
  177. * as archives are always fed pointers to previously existent
  178. * arrays. So, if this is called it means we are dealing with a
  179. * somehow invalid archive.
  180. */
  181. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  182. namespace serialization{
  183. #else
  184. namespace multi_index{
  185. namespace detail{
  186. #endif
  187. template<class Archive,typename Allocator>
  188. inline void load_construct_data(
  189. Archive&,boost::multi_index::detail::bucket_array<Allocator>*,
  190. const unsigned int)
  191. {
  192. throw_exception(
  193. archive::archive_exception(archive::archive_exception::other_exception));
  194. }
  195. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  196. } /* namespace serialization */
  197. #else
  198. } /* namespace multi_index::detail */
  199. } /* namespace multi_index */
  200. #endif
  201. #endif
  202. } /* namespace boost */
  203. #endif