]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/multi_index/hashed_index.hpp
fae4ae9ab63edca953d90ac2e79bda8822f10868
[ceph.git] / ceph / src / boost / boost / multi_index / hashed_index.hpp
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
9 #ifndef BOOST_MULTI_INDEX_HASHED_INDEX_HPP
10 #define BOOST_MULTI_INDEX_HASHED_INDEX_HPP
11
12 #if defined(_MSC_VER)
13 #pragma once
14 #endif
15
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <algorithm>
18 #include <boost/call_traits.hpp>
19 #include <boost/core/addressof.hpp>
20 #include <boost/detail/no_exceptions_support.hpp>
21 #include <boost/detail/workaround.hpp>
22 #include <boost/foreach_fwd.hpp>
23 #include <boost/limits.hpp>
24 #include <boost/move/core.hpp>
25 #include <boost/mpl/bool.hpp>
26 #include <boost/mpl/if.hpp>
27 #include <boost/mpl/push_front.hpp>
28 #include <boost/multi_index/detail/access_specifier.hpp>
29 #include <boost/multi_index/detail/adl_swap.hpp>
30 #include <boost/multi_index/detail/allocator_traits.hpp>
31 #include <boost/multi_index/detail/auto_space.hpp>
32 #include <boost/multi_index/detail/bucket_array.hpp>
33 #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
34 #include <boost/multi_index/detail/hash_index_iterator.hpp>
35 #include <boost/multi_index/detail/index_node_base.hpp>
36 #include <boost/multi_index/detail/modify_key_adaptor.hpp>
37 #include <boost/multi_index/detail/promotes_arg.hpp>
38 #include <boost/multi_index/detail/safe_mode.hpp>
39 #include <boost/multi_index/detail/scope_guard.hpp>
40 #include <boost/multi_index/detail/vartempl_support.hpp>
41 #include <boost/multi_index/hashed_index_fwd.hpp>
42 #include <boost/tuple/tuple.hpp>
43 #include <boost/type_traits/is_same.hpp>
44 #include <cmath>
45 #include <cstddef>
46 #include <functional>
47 #include <iterator>
48 #include <utility>
49
50 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
51 #include <initializer_list>
52 #endif
53
54 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
55 #include <boost/serialization/nvp.hpp>
56 #endif
57
58 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
59 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x) \
60 detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
61 detail::make_obj_guard(x,&hashed_index::check_invariant_); \
62 BOOST_JOIN(check_invariant_,__LINE__).touch();
63 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT \
64 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(*this)
65 #else
66 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x)
67 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT
68 #endif
69
70 namespace boost{
71
72 namespace multi_index{
73
74 namespace detail{
75
76 /* hashed_index adds a layer of hashed indexing to a given Super */
77
78 /* Most of the implementation of unique and non-unique indices is
79 * shared. We tell from one another on instantiation time by using
80 * Category tags defined in hash_index_node.hpp.
81 */
82
83 template<
84 typename KeyFromValue,typename Hash,typename Pred,
85 typename SuperMeta,typename TagList,typename Category
86 >
87 class hashed_index:
88 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
89
90 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
91 ,public safe_mode::safe_container<
92 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category> >
93 #endif
94
95 {
96 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
97 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
98 /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
99 * lifetime of const references bound to temporaries --precisely what
100 * scopeguards are.
101 */
102
103 #pragma parse_mfunc_templ off
104 #endif
105
106 typedef typename SuperMeta::type super;
107
108 protected:
109 typedef hashed_index_node<
110 typename super::node_type,Category> node_type;
111
112 private:
113 typedef typename node_type::node_alg node_alg;
114 typedef typename node_type::impl_type node_impl_type;
115 typedef typename node_impl_type::pointer node_impl_pointer;
116 typedef typename node_impl_type::base_pointer node_impl_base_pointer;
117 typedef bucket_array<
118 typename super::final_allocator_type> bucket_array_type;
119
120 public:
121 /* types */
122
123 typedef typename KeyFromValue::result_type key_type;
124 typedef typename node_type::value_type value_type;
125 typedef KeyFromValue key_from_value;
126 typedef Hash hasher;
127 typedef Pred key_equal;
128 typedef typename super::final_allocator_type allocator_type;
129
130 private:
131 typedef allocator_traits<allocator_type> alloc_traits;
132
133 public:
134 typedef typename alloc_traits::pointer pointer;
135 typedef typename alloc_traits::const_pointer const_pointer;
136 typedef value_type& reference;
137 typedef const value_type& const_reference;
138 typedef typename alloc_traits::size_type size_type;
139 typedef typename alloc_traits::difference_type difference_type;
140 typedef tuple<size_type,
141 key_from_value,hasher,key_equal> ctor_args;
142
143 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
144 typedef safe_mode::safe_iterator<
145 hashed_index_iterator<
146 node_type,bucket_array_type,
147 hashed_index_global_iterator_tag>,
148 hashed_index> iterator;
149 #else
150 typedef hashed_index_iterator<
151 node_type,bucket_array_type,
152 hashed_index_global_iterator_tag> iterator;
153 #endif
154
155 typedef iterator const_iterator;
156
157 typedef hashed_index_iterator<
158 node_type,bucket_array_type,
159 hashed_index_local_iterator_tag> local_iterator;
160 typedef local_iterator const_local_iterator;
161
162 typedef TagList tag_list;
163
164 protected:
165 typedef typename super::final_node_type final_node_type;
166 typedef tuples::cons<
167 ctor_args,
168 typename super::ctor_args_list> ctor_args_list;
169 typedef typename mpl::push_front<
170 typename super::index_type_list,
171 hashed_index>::type index_type_list;
172 typedef typename mpl::push_front<
173 typename super::iterator_type_list,
174 iterator>::type iterator_type_list;
175 typedef typename mpl::push_front<
176 typename super::const_iterator_type_list,
177 const_iterator>::type const_iterator_type_list;
178 typedef typename super::copy_map_type copy_map_type;
179
180 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
181 typedef typename super::index_saver_type index_saver_type;
182 typedef typename super::index_loader_type index_loader_type;
183 #endif
184
185 private:
186 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
187 typedef safe_mode::safe_container<
188 hashed_index> safe_super;
189 #endif
190
191 typedef typename call_traits<value_type>::param_type value_param_type;
192 typedef typename call_traits<
193 key_type>::param_type key_param_type;
194
195 /* Needed to avoid commas in BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL
196 * expansion.
197 */
198
199 typedef std::pair<iterator,bool> emplace_return_type;
200
201 public:
202
203 /* construct/destroy/copy
204 * Default and copy ctors are in the protected section as indices are
205 * not supposed to be created on their own. No range ctor either.
206 */
207
208 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& operator=(
209 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
210 {
211 this->final()=x.final();
212 return *this;
213 }
214
215 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
216 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& operator=(
217 std::initializer_list<value_type> list)
218 {
219 this->final()=list;
220 return *this;
221 }
222 #endif
223
224 allocator_type get_allocator()const BOOST_NOEXCEPT
225 {
226 return this->final().get_allocator();
227 }
228
229 /* size and capacity */
230
231 bool empty()const BOOST_NOEXCEPT{return this->final_empty_();}
232 size_type size()const BOOST_NOEXCEPT{return this->final_size_();}
233 size_type max_size()const BOOST_NOEXCEPT{return this->final_max_size_();}
234
235 /* iterators */
236
237 iterator begin()BOOST_NOEXCEPT
238 {return make_iterator(node_type::from_impl(header()->next()->prior()));}
239 const_iterator begin()const BOOST_NOEXCEPT
240 {return make_iterator(node_type::from_impl(header()->next()->prior()));}
241 iterator end()BOOST_NOEXCEPT{return make_iterator(header());}
242 const_iterator end()const BOOST_NOEXCEPT{return make_iterator(header());}
243 const_iterator cbegin()const BOOST_NOEXCEPT{return begin();}
244 const_iterator cend()const BOOST_NOEXCEPT{return end();}
245
246 iterator iterator_to(const value_type& x)
247 {
248 return make_iterator(node_from_value<node_type>(boost::addressof(x)));
249 }
250
251 const_iterator iterator_to(const value_type& x)const
252 {
253 return make_iterator(node_from_value<node_type>(boost::addressof(x)));
254 }
255
256 /* modifiers */
257
258 BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
259 emplace_return_type,emplace,emplace_impl)
260
261 BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG(
262 iterator,emplace_hint,emplace_hint_impl,iterator,position)
263
264 std::pair<iterator,bool> insert(const value_type& x)
265 {
266 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
267 std::pair<final_node_type*,bool> p=this->final_insert_(x);
268 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
269 }
270
271 std::pair<iterator,bool> insert(BOOST_RV_REF(value_type) x)
272 {
273 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
274 std::pair<final_node_type*,bool> p=this->final_insert_rv_(x);
275 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
276 }
277
278 iterator insert(iterator position,const value_type& x)
279 {
280 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
281 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
282 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
283 std::pair<final_node_type*,bool> p=this->final_insert_(
284 x,static_cast<final_node_type*>(position.get_node()));
285 return make_iterator(p.first);
286 }
287
288 iterator insert(iterator position,BOOST_RV_REF(value_type) x)
289 {
290 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
291 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
292 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
293 std::pair<final_node_type*,bool> p=this->final_insert_rv_(
294 x,static_cast<final_node_type*>(position.get_node()));
295 return make_iterator(p.first);
296 }
297
298 template<typename InputIterator>
299 void insert(InputIterator first,InputIterator last)
300 {
301 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
302 for(;first!=last;++first)this->final_insert_ref_(*first);
303 }
304
305 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
306 void insert(std::initializer_list<value_type> list)
307 {
308 insert(list.begin(),list.end());
309 }
310 #endif
311
312 iterator erase(iterator position)
313 {
314 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
315 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
316 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
317 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
318 this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
319 return position;
320 }
321
322 size_type erase(key_param_type k)
323 {
324 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
325
326 std::size_t buc=buckets.position(hash_(k));
327 for(node_impl_pointer x=buckets.at(buc)->prior();
328 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
329 if(eq_(k,key(node_type::from_impl(x)->value()))){
330 node_impl_pointer y=end_of_range(x);
331 size_type s=0;
332 do{
333 node_impl_pointer z=node_alg::after(x);
334 this->final_erase_(
335 static_cast<final_node_type*>(node_type::from_impl(x)));
336 x=z;
337 ++s;
338 }while(x!=y);
339 return s;
340 }
341 }
342 return 0;
343 }
344
345 iterator erase(iterator first,iterator last)
346 {
347 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
348 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
349 BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
350 BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
351 BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
352 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
353 while(first!=last){
354 first=erase(first);
355 }
356 return first;
357 }
358
359 bool replace(iterator position,const value_type& x)
360 {
361 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
362 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
363 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
364 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
365 return this->final_replace_(
366 x,static_cast<final_node_type*>(position.get_node()));
367 }
368
369 bool replace(iterator position,BOOST_RV_REF(value_type) x)
370 {
371 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
372 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
373 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
374 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
375 return this->final_replace_rv_(
376 x,static_cast<final_node_type*>(position.get_node()));
377 }
378
379 template<typename Modifier>
380 bool modify(iterator position,Modifier mod)
381 {
382 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
383 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
384 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
385 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
386
387 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
388 /* MSVC++ 6.0 optimizer on safe mode code chokes if this
389 * this is not added. Left it for all compilers as it does no
390 * harm.
391 */
392
393 position.detach();
394 #endif
395
396 return this->final_modify_(
397 mod,static_cast<final_node_type*>(position.get_node()));
398 }
399
400 template<typename Modifier,typename Rollback>
401 bool modify(iterator position,Modifier mod,Rollback back_)
402 {
403 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
404 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
405 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
406 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
407
408 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
409 /* MSVC++ 6.0 optimizer on safe mode code chokes if this
410 * this is not added. Left it for all compilers as it does no
411 * harm.
412 */
413
414 position.detach();
415 #endif
416
417 return this->final_modify_(
418 mod,back_,static_cast<final_node_type*>(position.get_node()));
419 }
420
421 template<typename Modifier>
422 bool modify_key(iterator position,Modifier mod)
423 {
424 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
425 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
426 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
427 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
428 return modify(
429 position,modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key));
430 }
431
432 template<typename Modifier,typename Rollback>
433 bool modify_key(iterator position,Modifier mod,Rollback back_)
434 {
435 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
436 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
437 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
438 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
439 return modify(
440 position,
441 modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key),
442 modify_key_adaptor<Rollback,value_type,KeyFromValue>(back_,key));
443 }
444
445 void clear()BOOST_NOEXCEPT
446 {
447 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
448 this->final_clear_();
449 }
450
451 void swap(hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
452 {
453 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
454 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x);
455 this->final_swap_(x.final());
456 }
457
458 /* observers */
459
460 key_from_value key_extractor()const{return key;}
461 hasher hash_function()const{return hash_;}
462 key_equal key_eq()const{return eq_;}
463
464 /* lookup */
465
466 /* Internally, these ops rely on const_iterator being the same
467 * type as iterator.
468 */
469
470 /* Implementation note: When CompatibleKey is consistently promoted to
471 * KeyFromValue::result_type for equality comparison, the promotion is made
472 * once in advance to increase efficiency.
473 */
474
475 template<typename CompatibleKey>
476 iterator find(const CompatibleKey& k)const
477 {
478 return find(k,hash_,eq_);
479 }
480
481 template<
482 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
483 >
484 iterator find(
485 const CompatibleKey& k,
486 const CompatibleHash& hash,const CompatiblePred& eq)const
487 {
488 return find(
489 k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
490 }
491
492 template<typename CompatibleKey>
493 size_type count(const CompatibleKey& k)const
494 {
495 return count(k,hash_,eq_);
496 }
497
498 template<
499 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
500 >
501 size_type count(
502 const CompatibleKey& k,
503 const CompatibleHash& hash,const CompatiblePred& eq)const
504 {
505 return count(
506 k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
507 }
508
509 template<typename CompatibleKey>
510 std::pair<iterator,iterator> equal_range(const CompatibleKey& k)const
511 {
512 return equal_range(k,hash_,eq_);
513 }
514
515 template<
516 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
517 >
518 std::pair<iterator,iterator> equal_range(
519 const CompatibleKey& k,
520 const CompatibleHash& hash,const CompatiblePred& eq)const
521 {
522 return equal_range(
523 k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
524 }
525
526 /* bucket interface */
527
528 size_type bucket_count()const BOOST_NOEXCEPT
529 {
530 return static_cast<size_type>(buckets.size());
531 }
532
533 size_type max_bucket_count()const BOOST_NOEXCEPT{return static_cast<size_type>(-1);}
534
535 size_type bucket_size(size_type n)const
536 {
537 size_type res=0;
538 for(node_impl_pointer x=buckets.at(n)->prior();
539 x!=node_impl_pointer(0);x=node_alg::after_local(x)){
540 ++res;
541 }
542 return res;
543 }
544
545 size_type bucket(key_param_type k)const
546 {
547 return static_cast<size_type>(buckets.position(hash_(k)));
548 }
549
550 local_iterator begin(size_type n)
551 {
552 return const_cast<const hashed_index*>(this)->begin(n);
553 }
554
555 const_local_iterator begin(size_type n)const
556 {
557 node_impl_pointer x=buckets.at(n)->prior();
558 if(x==node_impl_pointer(0))return end(n);
559 return make_local_iterator(node_type::from_impl(x));
560 }
561
562 local_iterator end(size_type n)
563 {
564 return const_cast<const hashed_index*>(this)->end(n);
565 }
566
567 const_local_iterator end(size_type)const
568 {
569 return make_local_iterator(0);
570 }
571
572 const_local_iterator cbegin(size_type n)const{return begin(n);}
573 const_local_iterator cend(size_type n)const{return end(n);}
574
575 local_iterator local_iterator_to(const value_type& x)
576 {
577 return make_local_iterator(
578 node_from_value<node_type>(boost::addressof(x)));
579 }
580
581 const_local_iterator local_iterator_to(const value_type& x)const
582 {
583 return make_local_iterator(
584 node_from_value<node_type>(boost::addressof(x)));
585 }
586
587 /* hash policy */
588
589 float load_factor()const BOOST_NOEXCEPT
590 {return static_cast<float>(size())/bucket_count();}
591 float max_load_factor()const BOOST_NOEXCEPT{return mlf;}
592 void max_load_factor(float z){mlf=z;calculate_max_load();}
593
594 void rehash(size_type n)
595 {
596 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
597 if(size()<=max_load&&n<=bucket_count())return;
598
599 size_type bc =(std::numeric_limits<size_type>::max)();
600 float fbc=1.0f+static_cast<float>(size())/mlf;
601 if(bc>fbc){
602 bc=static_cast<size_type>(fbc);
603 if(bc<n)bc=n;
604 }
605 unchecked_rehash(bc);
606 }
607
608 void reserve(size_type n)
609 {
610 rehash(static_cast<size_type>(std::ceil(static_cast<float>(n)/mlf)));
611 }
612
613 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
614 hashed_index(const ctor_args_list& args_list,const allocator_type& al):
615 super(args_list.get_tail(),al),
616 key(tuples::get<1>(args_list.get_head())),
617 hash_(tuples::get<2>(args_list.get_head())),
618 eq_(tuples::get<3>(args_list.get_head())),
619 buckets(al,header()->impl(),tuples::get<0>(args_list.get_head())),
620 mlf(1.0f)
621 {
622 calculate_max_load();
623 }
624
625 hashed_index(
626 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x):
627 super(x),
628
629 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
630 safe_super(),
631 #endif
632
633 key(x.key),
634 hash_(x.hash_),
635 eq_(x.eq_),
636 buckets(x.get_allocator(),header()->impl(),x.buckets.size()),
637 mlf(x.mlf),
638 max_load(x.max_load)
639 {
640 /* Copy ctor just takes the internal configuration objects from x. The rest
641 * is done in subsequent call to copy_().
642 */
643 }
644
645 hashed_index(
646 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
647 do_not_copy_elements_tag):
648 super(x,do_not_copy_elements_tag()),
649
650 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
651 safe_super(),
652 #endif
653
654 key(x.key),
655 hash_(x.hash_),
656 eq_(x.eq_),
657 buckets(x.get_allocator(),header()->impl(),0),
658 mlf(1.0f)
659 {
660 calculate_max_load();
661 }
662
663 ~hashed_index()
664 {
665 /* the container is guaranteed to be empty by now */
666 }
667
668 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
669 iterator make_iterator(node_type* node)
670 {
671 return iterator(node,this);
672 }
673
674 const_iterator make_iterator(node_type* node)const
675 {
676 return const_iterator(node,const_cast<hashed_index*>(this));
677 }
678 #else
679 iterator make_iterator(node_type* node)
680 {
681 return iterator(node);
682 }
683
684 const_iterator make_iterator(node_type* node)const
685 {
686 return const_iterator(node);
687 }
688 #endif
689
690 local_iterator make_local_iterator(node_type* node)
691 {
692 return local_iterator(node);
693 }
694
695 const_local_iterator make_local_iterator(node_type* node)const
696 {
697 return const_local_iterator(node);
698 }
699
700 void copy_(
701 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
702 const copy_map_type& map)
703 {
704 copy_(x,map,Category());
705 }
706
707 void copy_(
708 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
709 const copy_map_type& map,hashed_unique_tag)
710 {
711 if(x.size()!=0){
712 node_impl_pointer end_org=x.header()->impl(),
713 org=end_org,
714 cpy=header()->impl();
715 do{
716 node_impl_pointer prev_org=org->prior(),
717 prev_cpy=
718 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
719 node_type::from_impl(prev_org))))->impl();
720 cpy->prior()=prev_cpy;
721 if(node_alg::is_first_of_bucket(org)){
722 node_impl_base_pointer buc_org=prev_org->next(),
723 buc_cpy=
724 buckets.begin()+(buc_org-x.buckets.begin());
725 prev_cpy->next()=buc_cpy;
726 buc_cpy->prior()=cpy;
727 }
728 else{
729 prev_cpy->next()=node_impl_type::base_pointer_from(cpy);
730 }
731 org=prev_org;
732 cpy=prev_cpy;
733 }while(org!=end_org);
734 }
735
736 super::copy_(x,map);
737 }
738
739 void copy_(
740 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
741 const copy_map_type& map,hashed_non_unique_tag)
742 {
743 if(x.size()!=0){
744 node_impl_pointer end_org=x.header()->impl(),
745 org=end_org,
746 cpy=header()->impl();
747 do{
748 node_impl_pointer next_org=node_alg::after(org),
749 next_cpy=
750 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
751 node_type::from_impl(next_org))))->impl();
752 if(node_alg::is_first_of_bucket(next_org)){
753 node_impl_base_pointer buc_org=org->next(),
754 buc_cpy=
755 buckets.begin()+(buc_org-x.buckets.begin());
756 cpy->next()=buc_cpy;
757 buc_cpy->prior()=next_cpy;
758 next_cpy->prior()=cpy;
759 }
760 else{
761 if(org->next()==node_impl_type::base_pointer_from(next_org)){
762 cpy->next()=node_impl_type::base_pointer_from(next_cpy);
763 }
764 else{
765 cpy->next()=
766 node_impl_type::base_pointer_from(
767 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
768 node_type::from_impl(
769 node_impl_type::pointer_from(org->next())))))->impl());
770 }
771
772 if(next_org->prior()!=org){
773 next_cpy->prior()=
774 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
775 node_type::from_impl(next_org->prior()))))->impl();
776 }
777 else{
778 next_cpy->prior()=cpy;
779 }
780 }
781 org=next_org;
782 cpy=next_cpy;
783 }while(org!=end_org);
784 }
785
786 super::copy_(x,map);
787 }
788
789 template<typename Variant>
790 final_node_type* insert_(
791 value_param_type v,final_node_type*& x,Variant variant)
792 {
793 reserve_for_insert(size()+1);
794
795 std::size_t buc=find_bucket(v);
796 link_info pos(buckets.at(buc));
797 if(!link_point(v,pos)){
798 return static_cast<final_node_type*>(
799 node_type::from_impl(node_impl_type::pointer_from(pos)));
800 }
801
802 final_node_type* res=super::insert_(v,x,variant);
803 if(res==x)link(static_cast<node_type*>(x),pos);
804 return res;
805 }
806
807 template<typename Variant>
808 final_node_type* insert_(
809 value_param_type v,node_type* position,final_node_type*& x,Variant variant)
810 {
811 reserve_for_insert(size()+1);
812
813 std::size_t buc=find_bucket(v);
814 link_info pos(buckets.at(buc));
815 if(!link_point(v,pos)){
816 return static_cast<final_node_type*>(
817 node_type::from_impl(node_impl_type::pointer_from(pos)));
818 }
819
820 final_node_type* res=super::insert_(v,position,x,variant);
821 if(res==x)link(static_cast<node_type*>(x),pos);
822 return res;
823 }
824
825 void erase_(node_type* x)
826 {
827 unlink(x);
828 super::erase_(x);
829
830 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
831 detach_iterators(x);
832 #endif
833 }
834
835 void delete_all_nodes_()
836 {
837 delete_all_nodes_(Category());
838 }
839
840 void delete_all_nodes_(hashed_unique_tag)
841 {
842 for(node_impl_pointer x_end=header()->impl(),x=x_end->prior();x!=x_end;){
843 node_impl_pointer y=x->prior();
844 this->final_delete_node_(
845 static_cast<final_node_type*>(node_type::from_impl(x)));
846 x=y;
847 }
848 }
849
850 void delete_all_nodes_(hashed_non_unique_tag)
851 {
852 for(node_impl_pointer x_end=header()->impl(),x=x_end->prior();x!=x_end;){
853 node_impl_pointer y=x->prior();
854 if(y->next()!=node_impl_type::base_pointer_from(x)&&
855 y->next()->prior()!=x){ /* n-1 of group */
856 /* Make the second node prior() pointer back-linked so that it won't
857 * refer to a deleted node when the time for its own destruction comes.
858 */
859
860 node_impl_pointer first=node_impl_type::pointer_from(y->next());
861 first->next()->prior()=first;
862 }
863 this->final_delete_node_(
864 static_cast<final_node_type*>(node_type::from_impl(x)));
865 x=y;
866 }
867 }
868
869 void clear_()
870 {
871 super::clear_();
872 buckets.clear(header()->impl());
873
874 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
875 safe_super::detach_dereferenceable_iterators();
876 #endif
877 }
878
879 template<typename BoolConstant>
880 void swap_(
881 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
882 BoolConstant swap_allocators)
883 {
884 adl_swap(key,x.key);
885 adl_swap(hash_,x.hash_);
886 adl_swap(eq_,x.eq_);
887 buckets.swap(x.buckets,swap_allocators);
888 std::swap(mlf,x.mlf);
889 std::swap(max_load,x.max_load);
890
891 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
892 safe_super::swap(x);
893 #endif
894
895 super::swap_(x,swap_allocators);
896 }
897
898 void swap_elements_(
899 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
900 {
901 buckets.swap(x.buckets);
902 std::swap(mlf,x.mlf);
903 std::swap(max_load,x.max_load);
904
905 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
906 safe_super::swap(x);
907 #endif
908
909 super::swap_elements_(x);
910 }
911
912 template<typename Variant>
913 bool replace_(value_param_type v,node_type* x,Variant variant)
914 {
915 if(eq_(key(v),key(x->value()))){
916 return super::replace_(v,x,variant);
917 }
918
919 unlink_undo undo;
920 unlink(x,undo);
921
922 BOOST_TRY{
923 std::size_t buc=find_bucket(v);
924 link_info pos(buckets.at(buc));
925 if(link_point(v,pos)&&super::replace_(v,x,variant)){
926 link(x,pos);
927 return true;
928 }
929 undo();
930 return false;
931 }
932 BOOST_CATCH(...){
933 undo();
934 BOOST_RETHROW;
935 }
936 BOOST_CATCH_END
937 }
938
939 bool modify_(node_type* x)
940 {
941 std::size_t buc;
942 bool b;
943 BOOST_TRY{
944 buc=find_bucket(x->value());
945 b=in_place(x->impl(),key(x->value()),buc);
946 }
947 BOOST_CATCH(...){
948 erase_(x);
949 BOOST_RETHROW;
950 }
951 BOOST_CATCH_END
952 if(!b){
953 unlink(x);
954 BOOST_TRY{
955 link_info pos(buckets.at(buc));
956 if(!link_point(x->value(),pos)){
957 super::erase_(x);
958
959 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
960 detach_iterators(x);
961 #endif
962 return false;
963 }
964 link(x,pos);
965 }
966 BOOST_CATCH(...){
967 super::erase_(x);
968
969 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
970 detach_iterators(x);
971 #endif
972
973 BOOST_RETHROW;
974 }
975 BOOST_CATCH_END
976 }
977
978 BOOST_TRY{
979 if(!super::modify_(x)){
980 unlink(x);
981
982 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
983 detach_iterators(x);
984 #endif
985 return false;
986 }
987 else return true;
988 }
989 BOOST_CATCH(...){
990 unlink(x);
991
992 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
993 detach_iterators(x);
994 #endif
995
996 BOOST_RETHROW;
997 }
998 BOOST_CATCH_END
999 }
1000
1001 bool modify_rollback_(node_type* x)
1002 {
1003 std::size_t buc=find_bucket(x->value());
1004 if(in_place(x->impl(),key(x->value()),buc)){
1005 return super::modify_rollback_(x);
1006 }
1007
1008 unlink_undo undo;
1009 unlink(x,undo);
1010
1011 BOOST_TRY{
1012 link_info pos(buckets.at(buc));
1013 if(link_point(x->value(),pos)&&super::modify_rollback_(x)){
1014 link(x,pos);
1015 return true;
1016 }
1017 undo();
1018 return false;
1019 }
1020 BOOST_CATCH(...){
1021 undo();
1022 BOOST_RETHROW;
1023 }
1024 BOOST_CATCH_END
1025 }
1026
1027 bool check_rollback_(node_type* x)const
1028 {
1029 std::size_t buc=find_bucket(x->value());
1030 return in_place(x->impl(),key(x->value()),buc)&&super::check_rollback_(x);
1031 }
1032
1033 /* comparison */
1034
1035 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
1036 /* defect macro refers to class, not function, templates, but anyway */
1037
1038 template<typename K,typename H,typename P,typename S,typename T,typename C>
1039 friend bool operator==(
1040 const hashed_index<K,H,P,S,T,C>&,const hashed_index<K,H,P,S,T,C>& y);
1041 #endif
1042
1043 bool equals(const hashed_index& x)const{return equals(x,Category());}
1044
1045 bool equals(const hashed_index& x,hashed_unique_tag)const
1046 {
1047 if(size()!=x.size())return false;
1048 for(const_iterator it=begin(),it_end=end(),it2_end=x.end();
1049 it!=it_end;++it){
1050 const_iterator it2=x.find(key(*it));
1051 if(it2==it2_end||!(*it==*it2))return false;
1052 }
1053 return true;
1054 }
1055
1056 bool equals(const hashed_index& x,hashed_non_unique_tag)const
1057 {
1058 if(size()!=x.size())return false;
1059 for(const_iterator it=begin(),it_end=end();it!=it_end;){
1060 const_iterator it2,it2_last;
1061 boost::tie(it2,it2_last)=x.equal_range(key(*it));
1062 if(it2==it2_last)return false;
1063
1064 const_iterator it_last=make_iterator(
1065 node_type::from_impl(end_of_range(it.get_node()->impl())));
1066 if(std::distance(it,it_last)!=std::distance(it2,it2_last))return false;
1067
1068 /* From is_permutation code in
1069 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3068.pdf
1070 */
1071
1072 for(;it!=it_last;++it,++it2){
1073 if(!(*it==*it2))break;
1074 }
1075 if(it!=it_last){
1076 for(const_iterator scan=it;scan!=it_last;++scan){
1077 if(std::find(it,scan,*scan)!=scan)continue;
1078 difference_type matches=std::count(it2,it2_last,*scan);
1079 if(matches==0||matches!=std::count(scan,it_last,*scan))return false;
1080 }
1081 it=it_last;
1082 }
1083 }
1084 return true;
1085 }
1086
1087 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
1088 /* serialization */
1089
1090 template<typename Archive>
1091 void save_(
1092 Archive& ar,const unsigned int version,const index_saver_type& sm)const
1093 {
1094 ar<<serialization::make_nvp("position",buckets);
1095 super::save_(ar,version,sm);
1096 }
1097
1098 template<typename Archive>
1099 void load_(Archive& ar,const unsigned int version,const index_loader_type& lm)
1100 {
1101 ar>>serialization::make_nvp("position",buckets);
1102 super::load_(ar,version,lm);
1103 }
1104 #endif
1105
1106 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
1107 /* invariant stuff */
1108
1109 bool invariant_()const
1110 {
1111 if(size()==0||begin()==end()){
1112 if(size()!=0||begin()!=end())return false;
1113 }
1114 else{
1115 size_type s0=0;
1116 for(const_iterator it=begin(),it_end=end();it!=it_end;++it,++s0){}
1117 if(s0!=size())return false;
1118
1119 size_type s1=0;
1120 for(size_type buc=0;buc<bucket_count();++buc){
1121 size_type ss1=0;
1122 for(const_local_iterator it=begin(buc),it_end=end(buc);
1123 it!=it_end;++it,++ss1){
1124 if(find_bucket(*it)!=buc)return false;
1125 }
1126 if(ss1!=bucket_size(buc))return false;
1127 s1+=ss1;
1128 }
1129 if(s1!=size())return false;
1130 }
1131
1132 return super::invariant_();
1133 }
1134
1135 /* This forwarding function eases things for the boost::mem_fn construct
1136 * in BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT. Actually,
1137 * final_check_invariant is already an inherited member function of index.
1138 */
1139 void check_invariant_()const{this->final_check_invariant_();}
1140 #endif
1141
1142 private:
1143 node_type* header()const{return this->final_header();}
1144
1145 std::size_t find_bucket(value_param_type v)const
1146 {
1147 return bucket(key(v));
1148 }
1149
1150 struct link_info_non_unique
1151 {
1152 link_info_non_unique(node_impl_base_pointer pos):
1153 first(pos),last(node_impl_base_pointer(0)){}
1154
1155 operator const node_impl_base_pointer&()const{return this->first;}
1156
1157 node_impl_base_pointer first,last;
1158 };
1159
1160 typedef typename mpl::if_<
1161 is_same<Category,hashed_unique_tag>,
1162 node_impl_base_pointer,
1163 link_info_non_unique
1164 >::type link_info;
1165
1166 bool link_point(value_param_type v,link_info& pos)
1167 {
1168 return link_point(v,pos,Category());
1169 }
1170
1171 bool link_point(
1172 value_param_type v,node_impl_base_pointer& pos,hashed_unique_tag)
1173 {
1174 for(node_impl_pointer x=pos->prior();x!=node_impl_pointer(0);
1175 x=node_alg::after_local(x)){
1176 if(eq_(key(v),key(node_type::from_impl(x)->value()))){
1177 pos=node_impl_type::base_pointer_from(x);
1178 return false;
1179 }
1180 }
1181 return true;
1182 }
1183
1184 bool link_point(
1185 value_param_type v,link_info_non_unique& pos,hashed_non_unique_tag)
1186 {
1187 for(node_impl_pointer x=pos.first->prior();x!=node_impl_pointer(0);
1188 x=node_alg::next_to_inspect(x)){
1189 if(eq_(key(v),key(node_type::from_impl(x)->value()))){
1190 pos.first=node_impl_type::base_pointer_from(x);
1191 pos.last=node_impl_type::base_pointer_from(last_of_range(x));
1192 return true;
1193 }
1194 }
1195 return true;
1196 }
1197
1198 node_impl_pointer last_of_range(node_impl_pointer x)const
1199 {
1200 return last_of_range(x,Category());
1201 }
1202
1203 node_impl_pointer last_of_range(node_impl_pointer x,hashed_unique_tag)const
1204 {
1205 return x;
1206 }
1207
1208 node_impl_pointer last_of_range(
1209 node_impl_pointer x,hashed_non_unique_tag)const
1210 {
1211 node_impl_base_pointer y=x->next();
1212 node_impl_pointer z=y->prior();
1213 if(z==x){ /* range of size 1 or 2 */
1214 node_impl_pointer yy=node_impl_type::pointer_from(y);
1215 return
1216 eq_(
1217 key(node_type::from_impl(x)->value()),
1218 key(node_type::from_impl(yy)->value()))?yy:x;
1219 }
1220 else if(z->prior()==x) /* last of bucket */
1221 return x;
1222 else /* group of size>2 */
1223 return z;
1224 }
1225
1226 node_impl_pointer end_of_range(node_impl_pointer x)const
1227 {
1228 return end_of_range(x,Category());
1229 }
1230
1231 node_impl_pointer end_of_range(node_impl_pointer x,hashed_unique_tag)const
1232 {
1233 return node_alg::after(last_of_range(x));
1234 }
1235
1236 node_impl_pointer end_of_range(
1237 node_impl_pointer x,hashed_non_unique_tag)const
1238 {
1239 node_impl_base_pointer y=x->next();
1240 node_impl_pointer z=y->prior();
1241 if(z==x){ /* range of size 1 or 2 */
1242 node_impl_pointer yy=node_impl_type::pointer_from(y);
1243 if(!eq_(
1244 key(node_type::from_impl(x)->value()),
1245 key(node_type::from_impl(yy)->value())))yy=x;
1246 return yy->next()->prior()==yy?
1247 node_impl_type::pointer_from(yy->next()):
1248 yy->next()->prior();
1249 }
1250 else if(z->prior()==x) /* last of bucket */
1251 return z;
1252 else /* group of size>2 */
1253 return z->next()->prior()==z?
1254 node_impl_type::pointer_from(z->next()):
1255 z->next()->prior();
1256 }
1257
1258 void link(node_type* x,const link_info& pos)
1259 {
1260 link(x,pos,Category());
1261 }
1262
1263 void link(node_type* x,node_impl_base_pointer pos,hashed_unique_tag)
1264 {
1265 node_alg::link(x->impl(),pos,header()->impl());
1266 }
1267
1268 void link(node_type* x,const link_info_non_unique& pos,hashed_non_unique_tag)
1269 {
1270 if(pos.last==node_impl_base_pointer(0)){
1271 node_alg::link(x->impl(),pos.first,header()->impl());
1272 }
1273 else{
1274 node_alg::link(
1275 x->impl(),
1276 node_impl_type::pointer_from(pos.first),
1277 node_impl_type::pointer_from(pos.last));
1278 }
1279 }
1280
1281 void unlink(node_type* x)
1282 {
1283 node_alg::unlink(x->impl());
1284 }
1285
1286 typedef typename node_alg::unlink_undo unlink_undo;
1287
1288 void unlink(node_type* x,unlink_undo& undo)
1289 {
1290 node_alg::unlink(x->impl(),undo);
1291 }
1292
1293 void calculate_max_load()
1294 {
1295 float fml=mlf*static_cast<float>(bucket_count());
1296 max_load=(std::numeric_limits<size_type>::max)();
1297 if(max_load>fml)max_load=static_cast<size_type>(fml);
1298 }
1299
1300 void reserve_for_insert(size_type n)
1301 {
1302 if(n>max_load){
1303 size_type bc =(std::numeric_limits<size_type>::max)();
1304 float fbc=1.0f+static_cast<float>(n)/mlf;
1305 if(bc>fbc)bc =static_cast<size_type>(fbc);
1306 unchecked_rehash(bc);
1307 }
1308 }
1309
1310 void unchecked_rehash(size_type n){unchecked_rehash(n,Category());}
1311
1312 void unchecked_rehash(size_type n,hashed_unique_tag)
1313 {
1314 node_impl_type cpy_end_node;
1315 node_impl_pointer cpy_end=node_impl_pointer(&cpy_end_node),
1316 end_=header()->impl();
1317 bucket_array_type buckets_cpy(get_allocator(),cpy_end,n);
1318
1319 if(size()!=0){
1320 auto_space<
1321 std::size_t,allocator_type> hashes(get_allocator(),size());
1322 auto_space<
1323 node_impl_pointer,allocator_type> node_ptrs(get_allocator(),size());
1324 std::size_t i=0,size_=size();
1325 bool within_bucket=false;
1326 BOOST_TRY{
1327 for(;i!=size_;++i){
1328 node_impl_pointer x=end_->prior();
1329
1330 /* only this can possibly throw */
1331 std::size_t h=hash_(key(node_type::from_impl(x)->value()));
1332
1333 hashes.data()[i]=h;
1334 node_ptrs.data()[i]=x;
1335 within_bucket=!node_alg::unlink_last(end_);
1336 node_alg::link(x,buckets_cpy.at(buckets_cpy.position(h)),cpy_end);
1337 }
1338 }
1339 BOOST_CATCH(...){
1340 if(i!=0){
1341 std::size_t prev_buc=buckets.position(hashes.data()[i-1]);
1342 if(!within_bucket)prev_buc=~prev_buc;
1343
1344 for(std::size_t j=i;j--;){
1345 std::size_t buc=buckets.position(hashes.data()[j]);
1346 node_impl_pointer x=node_ptrs.data()[j];
1347 if(buc==prev_buc)node_alg::append(x,end_);
1348 else node_alg::link(x,buckets.at(buc),end_);
1349 prev_buc=buc;
1350 }
1351 }
1352 BOOST_RETHROW;
1353 }
1354 BOOST_CATCH_END
1355 }
1356
1357 end_->prior()=cpy_end->prior()!=cpy_end?cpy_end->prior():end_;
1358 end_->next()=cpy_end->next();
1359 end_->prior()->next()->prior()=end_->next()->prior()->prior()=end_;
1360 buckets.swap(buckets_cpy);
1361 calculate_max_load();
1362 }
1363
1364 void unchecked_rehash(size_type n,hashed_non_unique_tag)
1365 {
1366 node_impl_type cpy_end_node;
1367 node_impl_pointer cpy_end=node_impl_pointer(&cpy_end_node),
1368 end_=header()->impl();
1369 bucket_array_type buckets_cpy(get_allocator(),cpy_end,n);
1370
1371 if(size()!=0){
1372 auto_space<
1373 std::size_t,allocator_type> hashes(get_allocator(),size());
1374 auto_space<
1375 node_impl_pointer,allocator_type> node_ptrs(get_allocator(),size());
1376 std::size_t i=0;
1377 bool within_bucket=false;
1378 BOOST_TRY{
1379 for(;;++i){
1380 node_impl_pointer x=end_->prior();
1381 if(x==end_)break;
1382
1383 /* only this can possibly throw */
1384 std::size_t h=hash_(key(node_type::from_impl(x)->value()));
1385
1386 hashes.data()[i]=h;
1387 node_ptrs.data()[i]=x;
1388 std::pair<node_impl_pointer,bool> p=
1389 node_alg::unlink_last_group(end_);
1390 node_alg::link_range(
1391 p.first,x,buckets_cpy.at(buckets_cpy.position(h)),cpy_end);
1392 within_bucket=!(p.second);
1393 }
1394 }
1395 BOOST_CATCH(...){
1396 if(i!=0){
1397 std::size_t prev_buc=buckets.position(hashes.data()[i-1]);
1398 if(!within_bucket)prev_buc=~prev_buc;
1399
1400 for(std::size_t j=i;j--;){
1401 std::size_t buc=buckets.position(hashes.data()[j]);
1402 node_impl_pointer x=node_ptrs.data()[j],
1403 y=
1404 x->prior()->next()!=node_impl_type::base_pointer_from(x)&&
1405 x->prior()->next()->prior()!=x?
1406 node_impl_type::pointer_from(x->prior()->next()):x;
1407 node_alg::unlink_range(y,x);
1408 if(buc==prev_buc)node_alg::append_range(y,x,end_);
1409 else node_alg::link_range(y,x,buckets.at(buc),end_);
1410 prev_buc=buc;
1411 }
1412 }
1413 BOOST_RETHROW;
1414 }
1415 BOOST_CATCH_END
1416 }
1417
1418 end_->prior()=cpy_end->prior()!=cpy_end?cpy_end->prior():end_;
1419 end_->next()=cpy_end->next();
1420 end_->prior()->next()->prior()=end_->next()->prior()->prior()=end_;
1421 buckets.swap(buckets_cpy);
1422 calculate_max_load();
1423 }
1424
1425 bool in_place(node_impl_pointer x,key_param_type k,std::size_t buc)const
1426 {
1427 return in_place(x,k,buc,Category());
1428 }
1429
1430 bool in_place(
1431 node_impl_pointer x,key_param_type k,std::size_t buc,
1432 hashed_unique_tag)const
1433 {
1434 bool found=false;
1435 for(node_impl_pointer y=buckets.at(buc)->prior();
1436 y!=node_impl_pointer(0);y=node_alg::after_local(y)){
1437 if(y==x)found=true;
1438 else if(eq_(k,key(node_type::from_impl(y)->value())))return false;
1439 }
1440 return found;
1441 }
1442
1443 bool in_place(
1444 node_impl_pointer x,key_param_type k,std::size_t buc,
1445 hashed_non_unique_tag)const
1446 {
1447 bool found=false;
1448 int range_size=0;
1449 for(node_impl_pointer y=buckets.at(buc)->prior();y!=node_impl_pointer(0);){
1450 if(node_alg::is_first_of_group(y)){ /* group of 3 or more */
1451 if(y==x){
1452 /* in place <-> equal to some other member of the group */
1453 return eq_(
1454 k,
1455 key(node_type::from_impl(
1456 node_impl_type::pointer_from(y->next()))->value()));
1457 }
1458 else{
1459 node_impl_pointer z=
1460 node_alg::after_local(y->next()->prior()); /* end of range */
1461 if(eq_(k,key(node_type::from_impl(y)->value()))){
1462 if(found)return false; /* x lies outside */
1463 do{
1464 if(y==x)return true;
1465 y=node_alg::after_local(y);
1466 }while(y!=z);
1467 return false; /* x not found */
1468 }
1469 else{
1470 if(range_size==1&&!found)return false;
1471 if(range_size==2)return found;
1472 range_size=0;
1473 y=z; /* skip range (and potentially x, too, which is fine) */
1474 }
1475 }
1476 }
1477 else{ /* group of 1 or 2 */
1478 if(y==x){
1479 if(range_size==1)return true;
1480 range_size=1;
1481 found=true;
1482 }
1483 else if(eq_(k,key(node_type::from_impl(y)->value()))){
1484 if(range_size==0&&found)return false;
1485 if(range_size==1&&!found)return false;
1486 if(range_size==2)return false;
1487 ++range_size;
1488 }
1489 else{
1490 if(range_size==1&&!found)return false;
1491 if(range_size==2)return found;
1492 range_size=0;
1493 }
1494 y=node_alg::after_local(y);
1495 }
1496 }
1497 return found;
1498 }
1499
1500 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
1501 void detach_iterators(node_type* x)
1502 {
1503 iterator it=make_iterator(x);
1504 safe_mode::detach_equivalent_iterators(it);
1505 }
1506 #endif
1507
1508 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
1509 std::pair<iterator,bool> emplace_impl(BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
1510 {
1511 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
1512 std::pair<final_node_type*,bool>p=
1513 this->final_emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
1514 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
1515 }
1516
1517 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
1518 iterator emplace_hint_impl(
1519 iterator position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
1520 {
1521 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
1522 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
1523 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
1524 std::pair<final_node_type*,bool>p=
1525 this->final_emplace_hint_(
1526 static_cast<final_node_type*>(position.get_node()),
1527 BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
1528 return make_iterator(p.first);
1529 }
1530
1531 template<
1532 typename CompatibleHash,typename CompatiblePred
1533 >
1534 iterator find(
1535 const key_type& k,
1536 const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
1537 {
1538 return find(k,hash,eq,mpl::false_());
1539 }
1540
1541 template<
1542 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
1543 >
1544 iterator find(
1545 const CompatibleKey& k,
1546 const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
1547 {
1548 std::size_t buc=buckets.position(hash(k));
1549 for(node_impl_pointer x=buckets.at(buc)->prior();
1550 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
1551 if(eq(k,key(node_type::from_impl(x)->value()))){
1552 return make_iterator(node_type::from_impl(x));
1553 }
1554 }
1555 return end();
1556 }
1557
1558 template<
1559 typename CompatibleHash,typename CompatiblePred
1560 >
1561 size_type count(
1562 const key_type& k,
1563 const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
1564 {
1565 return count(k,hash,eq,mpl::false_());
1566 }
1567
1568 template<
1569 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
1570 >
1571 size_type count(
1572 const CompatibleKey& k,
1573 const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
1574 {
1575 std::size_t buc=buckets.position(hash(k));
1576 for(node_impl_pointer x=buckets.at(buc)->prior();
1577 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
1578 if(eq(k,key(node_type::from_impl(x)->value()))){
1579 size_type res=0;
1580 node_impl_pointer y=end_of_range(x);
1581 do{
1582 ++res;
1583 x=node_alg::after(x);
1584 }while(x!=y);
1585 return res;
1586 }
1587 }
1588 return 0;
1589 }
1590
1591 template<
1592 typename CompatibleHash,typename CompatiblePred
1593 >
1594 std::pair<iterator,iterator> equal_range(
1595 const key_type& k,
1596 const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
1597 {
1598 return equal_range(k,hash,eq,mpl::false_());
1599 }
1600
1601 template<
1602 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
1603 >
1604 std::pair<iterator,iterator> equal_range(
1605 const CompatibleKey& k,
1606 const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
1607 {
1608 std::size_t buc=buckets.position(hash(k));
1609 for(node_impl_pointer x=buckets.at(buc)->prior();
1610 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
1611 if(eq(k,key(node_type::from_impl(x)->value()))){
1612 return std::pair<iterator,iterator>(
1613 make_iterator(node_type::from_impl(x)),
1614 make_iterator(node_type::from_impl(end_of_range(x))));
1615 }
1616 }
1617 return std::pair<iterator,iterator>(end(),end());
1618 }
1619
1620 key_from_value key;
1621 hasher hash_;
1622 key_equal eq_;
1623 bucket_array_type buckets;
1624 float mlf;
1625 size_type max_load;
1626
1627 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
1628 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
1629 #pragma parse_mfunc_templ reset
1630 #endif
1631 };
1632
1633 /* comparison */
1634
1635 template<
1636 typename KeyFromValue,typename Hash,typename Pred,
1637 typename SuperMeta,typename TagList,typename Category
1638 >
1639 bool operator==(
1640 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
1641 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
1642 {
1643 return x.equals(y);
1644 }
1645
1646 template<
1647 typename KeyFromValue,typename Hash,typename Pred,
1648 typename SuperMeta,typename TagList,typename Category
1649 >
1650 bool operator!=(
1651 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
1652 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
1653 {
1654 return !(x==y);
1655 }
1656
1657 /* specialized algorithms */
1658
1659 template<
1660 typename KeyFromValue,typename Hash,typename Pred,
1661 typename SuperMeta,typename TagList,typename Category
1662 >
1663 void swap(
1664 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
1665 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
1666 {
1667 x.swap(y);
1668 }
1669
1670 } /* namespace multi_index::detail */
1671
1672 /* hashed index specifiers */
1673
1674 template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
1675 struct hashed_unique
1676 {
1677 typedef typename detail::hashed_index_args<
1678 Arg1,Arg2,Arg3,Arg4> index_args;
1679 typedef typename index_args::tag_list_type::type tag_list_type;
1680 typedef typename index_args::key_from_value_type key_from_value_type;
1681 typedef typename index_args::hash_type hash_type;
1682 typedef typename index_args::pred_type pred_type;
1683
1684 template<typename Super>
1685 struct node_class
1686 {
1687 typedef detail::hashed_index_node<Super,detail::hashed_unique_tag> type;
1688 };
1689
1690 template<typename SuperMeta>
1691 struct index_class
1692 {
1693 typedef detail::hashed_index<
1694 key_from_value_type,hash_type,pred_type,
1695 SuperMeta,tag_list_type,detail::hashed_unique_tag> type;
1696 };
1697 };
1698
1699 template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
1700 struct hashed_non_unique
1701 {
1702 typedef typename detail::hashed_index_args<
1703 Arg1,Arg2,Arg3,Arg4> index_args;
1704 typedef typename index_args::tag_list_type::type tag_list_type;
1705 typedef typename index_args::key_from_value_type key_from_value_type;
1706 typedef typename index_args::hash_type hash_type;
1707 typedef typename index_args::pred_type pred_type;
1708
1709 template<typename Super>
1710 struct node_class
1711 {
1712 typedef detail::hashed_index_node<
1713 Super,detail::hashed_non_unique_tag> type;
1714 };
1715
1716 template<typename SuperMeta>
1717 struct index_class
1718 {
1719 typedef detail::hashed_index<
1720 key_from_value_type,hash_type,pred_type,
1721 SuperMeta,tag_list_type,detail::hashed_non_unique_tag> type;
1722 };
1723 };
1724
1725 } /* namespace multi_index */
1726
1727 } /* namespace boost */
1728
1729 /* Boost.Foreach compatibility */
1730
1731 template<
1732 typename KeyFromValue,typename Hash,typename Pred,
1733 typename SuperMeta,typename TagList,typename Category
1734 >
1735 inline boost::mpl::true_* boost_foreach_is_noncopyable(
1736 boost::multi_index::detail::hashed_index<
1737 KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>*&,
1738 boost_foreach_argument_dependent_lookup_hack)
1739 {
1740 return 0;
1741 }
1742
1743 #undef BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT
1744 #undef BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF
1745
1746 #endif