bucket_array.hpp 7.2 KB

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