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