bucket_array.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright 2003-2008 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)&&(_MSC_VER>=1200)
  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/multi_index/detail/prevent_eti.hpp>
  18. #include <boost/noncopyable.hpp>
  19. #include <cstddef>
  20. #include <limits.h>
  21. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  22. #include <boost/archive/archive_exception.hpp>
  23. #include <boost/serialization/access.hpp>
  24. #include <boost/throw_exception.hpp>
  25. #endif
  26. namespace boost{
  27. namespace multi_index{
  28. namespace detail{
  29. /* bucket structure for use by hashed indices */
  30. class bucket_array_base:private noncopyable
  31. {
  32. protected:
  33. inline static std::size_t next_prime(std::size_t n)
  34. {
  35. static const std::size_t prime_list[]={
  36. 53ul, 97ul, 193ul, 389ul, 769ul,
  37. 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
  38. 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
  39. 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
  40. 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
  41. 1610612741ul, 3221225473ul,
  42. #if ((((ULONG_MAX>>16)>>16)>>16)>>15)==0 /* unsigned long less than 64 bits */
  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. 6442450939ul, 12884901893ul, 25769803751ul, 51539607551ul,
  52. 103079215111ul, 206158430209ul, 412316860441ul, 824633720831ul,
  53. 1649267441651ul, 3298534883309ul, 6597069766657ul, 13194139533299ul,
  54. 26388279066623ul, 52776558133303ul, 105553116266489ul, 211106232532969ul,
  55. 422212465066001ul, 844424930131963ul, 1688849860263953ul,
  56. 3377699720527861ul, 6755399441055731ul, 13510798882111483ul,
  57. 27021597764222939ul, 54043195528445957ul, 108086391056891903ul,
  58. 216172782113783843ul, 432345564227567621ul, 864691128455135207ul,
  59. 1729382256910270481ul, 3458764513820540933ul, 6917529027641081903ul,
  60. 13835058055282163729ul, 18446744073709551557ul
  61. #endif
  62. };
  63. static const std::size_t prime_list_size=
  64. sizeof(prime_list)/sizeof(prime_list[0]);
  65. std::size_t const *bound=
  66. std::lower_bound(prime_list,prime_list+prime_list_size,n);
  67. if(bound==prime_list+prime_list_size)bound--;
  68. return *bound;
  69. }
  70. };
  71. template<typename Allocator>
  72. class bucket_array:public bucket_array_base
  73. {
  74. typedef typename prevent_eti<
  75. Allocator,
  76. hashed_index_node_impl<
  77. typename boost::detail::allocator::rebind_to<
  78. Allocator,
  79. char
  80. >::type
  81. >
  82. >::type node_impl_type;
  83. public:
  84. typedef typename node_impl_type::pointer pointer;
  85. bucket_array(const Allocator& al,pointer end_,std::size_t size):
  86. size_(bucket_array_base::next_prime(size)),
  87. spc(al,size_+1)
  88. {
  89. clear();
  90. end()->next()=end_;
  91. end_->next()=end();
  92. }
  93. std::size_t size()const
  94. {
  95. return size_;
  96. }
  97. std::size_t position(std::size_t hash)const
  98. {
  99. return hash%size_;
  100. }
  101. pointer begin()const{return buckets();}
  102. pointer end()const{return buckets()+size_;}
  103. pointer at(std::size_t n)const{return buckets()+n;}
  104. std::size_t first_nonempty(std::size_t n)const
  105. {
  106. for(;;++n){
  107. pointer x=at(n);
  108. if(x->next()!=x)return n;
  109. }
  110. }
  111. void clear()
  112. {
  113. for(pointer x=begin(),y=end();x!=y;++x)x->next()=x;
  114. }
  115. void swap(bucket_array& x)
  116. {
  117. std::swap(size_,x.size_);
  118. spc.swap(x.spc);
  119. }
  120. private:
  121. std::size_t size_;
  122. auto_space<node_impl_type,Allocator> spc;
  123. pointer buckets()const
  124. {
  125. return spc.data();
  126. }
  127. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  128. friend class boost::serialization::access;
  129. /* bucket_arrays do not emit any kind of serialization info. They are
  130. * fed to Boost.Serialization as hashed index iterators need to track
  131. * them during serialization.
  132. */
  133. template<class Archive>
  134. void serialize(Archive&,const unsigned int)
  135. {
  136. }
  137. #endif
  138. };
  139. template<typename Allocator>
  140. void swap(bucket_array<Allocator>& x,bucket_array<Allocator>& y)
  141. {
  142. x.swap(y);
  143. }
  144. } /* namespace multi_index::detail */
  145. } /* namespace multi_index */
  146. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  147. /* bucket_arrays never get constructed directly by Boost.Serialization,
  148. * as archives are always fed pointers to previously existent
  149. * arrays. So, if this is called it means we are dealing with a
  150. * somehow invalid archive.
  151. */
  152. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  153. namespace serialization{
  154. #else
  155. namespace multi_index{
  156. namespace detail{
  157. #endif
  158. template<class Archive,typename Allocator>
  159. inline void load_construct_data(
  160. Archive&,boost::multi_index::detail::bucket_array<Allocator>*,
  161. const unsigned int)
  162. {
  163. throw_exception(
  164. archive::archive_exception(archive::archive_exception::other_exception));
  165. }
  166. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  167. } /* namespace serialization */
  168. #else
  169. } /* namespace multi_index::detail */
  170. } /* namespace multi_index */
  171. #endif
  172. #endif
  173. } /* namespace boost */
  174. #endif