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)
6 * See http://www.boost.org/libs/multi_index for library home page.
9 #ifndef BOOST_MULTI_INDEX_RANDOM_ACCESS_INDEX_HPP
10 #define BOOST_MULTI_INDEX_RANDOM_ACCESS_INDEX_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
18 #include <boost/bind.hpp>
19 #include <boost/call_traits.hpp>
20 #include <boost/detail/no_exceptions_support.hpp>
21 #include <boost/detail/workaround.hpp>
22 #include <boost/foreach_fwd.hpp>
23 #include <boost/iterator/reverse_iterator.hpp>
24 #include <boost/move/core.hpp>
25 #include <boost/move/utility.hpp>
26 #include <boost/mpl/bool.hpp>
27 #include <boost/mpl/not.hpp>
28 #include <boost/mpl/push_front.hpp>
29 #include <boost/multi_index/detail/access_specifier.hpp>
30 #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
31 #include <boost/multi_index/detail/index_node_base.hpp>
32 #include <boost/multi_index/detail/rnd_node_iterator.hpp>
33 #include <boost/multi_index/detail/rnd_index_node.hpp>
34 #include <boost/multi_index/detail/rnd_index_ops.hpp>
35 #include <boost/multi_index/detail/rnd_index_ptr_array.hpp>
36 #include <boost/multi_index/detail/safe_mode.hpp>
37 #include <boost/multi_index/detail/scope_guard.hpp>
38 #include <boost/multi_index/detail/vartempl_support.hpp>
39 #include <boost/multi_index/random_access_index_fwd.hpp>
40 #include <boost/throw_exception.hpp>
41 #include <boost/tuple/tuple.hpp>
42 #include <boost/type_traits/is_integral.hpp>
48 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
49 #include<initializer_list>
52 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
53 #include <boost/multi_index/detail/rnd_index_loader.hpp>
56 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
57 #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(x) \
58 detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
59 detail::make_obj_guard(x,&random_access_index::check_invariant_); \
60 BOOST_JOIN(check_invariant_,__LINE__).touch();
61 #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT \
62 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(*this)
64 #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(x)
65 #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT
70 namespace multi_index{
74 /* random_access_index adds a layer of random access indexing
78 template<typename SuperMeta,typename TagList>
79 class random_access_index:
80 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
82 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
83 ,public safe_mode::safe_container<
84 random_access_index<SuperMeta,TagList> >
88 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
89 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
90 /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
91 * lifetime of const references bound to temporaries --precisely what
95 #pragma parse_mfunc_templ off
98 typedef typename SuperMeta::type super;
101 typedef random_access_index_node<
102 typename super::node_type> node_type;
105 typedef typename node_type::impl_type node_impl_type;
106 typedef random_access_index_ptr_array<
107 typename super::final_allocator_type> ptr_array;
108 typedef typename ptr_array::pointer node_impl_ptr_pointer;
113 typedef typename node_type::value_type value_type;
114 typedef tuples::null_type ctor_args;
115 typedef typename super::final_allocator_type allocator_type;
116 typedef typename allocator_type::reference reference;
117 typedef typename allocator_type::const_reference const_reference;
119 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
120 typedef safe_mode::safe_iterator<
121 rnd_node_iterator<node_type>,
122 random_access_index> iterator;
124 typedef rnd_node_iterator<node_type> iterator;
127 typedef iterator const_iterator;
129 typedef std::size_t size_type;
130 typedef std::ptrdiff_t difference_type;
131 typedef typename allocator_type::pointer pointer;
132 typedef typename allocator_type::const_pointer const_pointer;
134 boost::reverse_iterator<iterator> reverse_iterator;
136 boost::reverse_iterator<const_iterator> const_reverse_iterator;
137 typedef TagList tag_list;
140 typedef typename super::final_node_type final_node_type;
141 typedef tuples::cons<
143 typename super::ctor_args_list> ctor_args_list;
144 typedef typename mpl::push_front<
145 typename super::index_type_list,
146 random_access_index>::type index_type_list;
147 typedef typename mpl::push_front<
148 typename super::iterator_type_list,
149 iterator>::type iterator_type_list;
150 typedef typename mpl::push_front<
151 typename super::const_iterator_type_list,
152 const_iterator>::type const_iterator_type_list;
153 typedef typename super::copy_map_type copy_map_type;
155 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
156 typedef typename super::index_saver_type index_saver_type;
157 typedef typename super::index_loader_type index_loader_type;
161 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
162 typedef safe_mode::safe_container<
163 random_access_index> safe_super;
166 typedef typename call_traits<
167 value_type>::param_type value_param_type;
169 /* Needed to avoid commas in BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL
173 typedef std::pair<iterator,bool> emplace_return_type;
177 /* construct/copy/destroy
178 * Default and copy ctors are in the protected section as indices are
179 * not supposed to be created on their own. No range ctor either.
182 random_access_index<SuperMeta,TagList>& operator=(
183 const random_access_index<SuperMeta,TagList>& x)
185 this->final()=x.final();
189 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
190 random_access_index<SuperMeta,TagList>& operator=(
191 std::initializer_list<value_type> list)
198 template <class InputIterator>
199 void assign(InputIterator first,InputIterator last)
201 assign_iter(first,last,mpl::not_<is_integral<InputIterator> >());
204 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
205 void assign(std::initializer_list<value_type> list)
207 assign(list.begin(),list.end());
211 void assign(size_type n,value_param_type value)
213 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
215 for(size_type i=0;i<n;++i)push_back(value);
218 allocator_type get_allocator()const BOOST_NOEXCEPT
220 return this->final().get_allocator();
225 iterator begin()BOOST_NOEXCEPT
226 {return make_iterator(node_type::from_impl(*ptrs.begin()));}
227 const_iterator begin()const BOOST_NOEXCEPT
228 {return make_iterator(node_type::from_impl(*ptrs.begin()));}
230 end()BOOST_NOEXCEPT{return make_iterator(header());}
232 end()const BOOST_NOEXCEPT{return make_iterator(header());}
234 rbegin()BOOST_NOEXCEPT{return boost::make_reverse_iterator(end());}
235 const_reverse_iterator
236 rbegin()const BOOST_NOEXCEPT{return boost::make_reverse_iterator(end());}
238 rend()BOOST_NOEXCEPT{return boost::make_reverse_iterator(begin());}
239 const_reverse_iterator
240 rend()const BOOST_NOEXCEPT{return boost::make_reverse_iterator(begin());}
242 cbegin()const BOOST_NOEXCEPT{return begin();}
244 cend()const BOOST_NOEXCEPT{return end();}
245 const_reverse_iterator
246 crbegin()const BOOST_NOEXCEPT{return rbegin();}
247 const_reverse_iterator
248 crend()const BOOST_NOEXCEPT{return rend();}
250 iterator iterator_to(const value_type& x)
252 return make_iterator(node_from_value<node_type>(&x));
255 const_iterator iterator_to(const value_type& x)const
257 return make_iterator(node_from_value<node_type>(&x));
262 bool empty()const BOOST_NOEXCEPT{return this->final_empty_();}
263 size_type size()const BOOST_NOEXCEPT{return this->final_size_();}
264 size_type max_size()const BOOST_NOEXCEPT{return this->final_max_size_();}
265 size_type capacity()const BOOST_NOEXCEPT{return ptrs.capacity();}
267 void reserve(size_type n)
269 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
275 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
276 ptrs.shrink_to_fit();
279 void resize(size_type n)
281 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
283 for(size_type m=n-size();m--;)
284 this->final_emplace_(BOOST_MULTI_INDEX_NULL_PARAM_PACK);
285 else if(n<size())erase(begin()+n,end());
288 void resize(size_type n,value_param_type x)
290 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
291 if(n>size())for(size_type m=n-size();m--;)this->final_insert_(x);
292 else if(n<size())erase(begin()+n,end());
295 /* access: no non-const versions provided as random_access_index
296 * handles const elements.
299 const_reference operator[](size_type n)const
301 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(n<size(),safe_mode::out_of_bounds);
302 return node_type::from_impl(*ptrs.at(n))->value();
305 const_reference at(size_type n)const
307 if(n>=size())throw_exception(std::out_of_range("random access index"));
308 return node_type::from_impl(*ptrs.at(n))->value();
311 const_reference front()const{return operator[](0);}
312 const_reference back()const{return operator[](size()-1);}
316 BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
317 emplace_return_type,emplace_front,emplace_front_impl)
319 std::pair<iterator,bool> push_front(const value_type& x)
320 {return insert(begin(),x);}
321 std::pair<iterator,bool> push_front(BOOST_RV_REF(value_type) x)
322 {return insert(begin(),boost::move(x));}
323 void pop_front(){erase(begin());}
325 BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
326 emplace_return_type,emplace_back,emplace_back_impl)
328 std::pair<iterator,bool> push_back(const value_type& x)
329 {return insert(end(),x);}
330 std::pair<iterator,bool> push_back(BOOST_RV_REF(value_type) x)
331 {return insert(end(),boost::move(x));}
332 void pop_back(){erase(--end());}
334 BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG(
335 emplace_return_type,emplace,emplace_impl,iterator,position)
337 std::pair<iterator,bool> insert(iterator position,const value_type& x)
339 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
340 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
341 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
342 std::pair<final_node_type*,bool> p=this->final_insert_(x);
343 if(p.second&&position.get_node()!=header()){
344 relocate(position.get_node(),p.first);
346 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
349 std::pair<iterator,bool> insert(iterator position,BOOST_RV_REF(value_type) x)
351 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
352 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
353 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
354 std::pair<final_node_type*,bool> p=this->final_insert_rv_(x);
355 if(p.second&&position.get_node()!=header()){
356 relocate(position.get_node(),p.first);
358 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
361 void insert(iterator position,size_type n,value_param_type x)
363 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
364 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
365 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
369 if(push_back(x).second)++s;
373 relocate(position,end()-s,end());
377 relocate(position,end()-s,end());
380 template<typename InputIterator>
381 void insert(iterator position,InputIterator first,InputIterator last)
383 insert_iter(position,first,last,mpl::not_<is_integral<InputIterator> >());
386 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
387 void insert(iterator position,std::initializer_list<value_type> list)
389 insert(position,list.begin(),list.end());
393 iterator erase(iterator position)
395 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
396 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
397 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
398 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
399 this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
403 iterator erase(iterator first,iterator last)
405 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
406 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
407 BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
408 BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
409 BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
410 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
411 difference_type n=last-first;
412 relocate(end(),first,last);
413 while(n--)pop_back();
417 bool replace(iterator position,const value_type& x)
419 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
420 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
421 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
422 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
423 return this->final_replace_(
424 x,static_cast<final_node_type*>(position.get_node()));
427 bool replace(iterator position,BOOST_RV_REF(value_type) x)
429 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
430 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
431 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
432 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
433 return this->final_replace_rv_(
434 x,static_cast<final_node_type*>(position.get_node()));
437 template<typename Modifier>
438 bool modify(iterator position,Modifier mod)
440 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
441 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
442 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
443 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
445 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
446 /* MSVC++ 6.0 optimizer on safe mode code chokes if this
447 * this is not added. Left it for all compilers as it does no
454 return this->final_modify_(
455 mod,static_cast<final_node_type*>(position.get_node()));
458 template<typename Modifier,typename Rollback>
459 bool modify(iterator position,Modifier mod,Rollback back_)
461 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
462 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
463 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
464 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
466 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
467 /* MSVC++ 6.0 optimizer on safe mode code chokes if this
468 * this is not added. Left it for all compilers as it does no
475 return this->final_modify_(
476 mod,back_,static_cast<final_node_type*>(position.get_node()));
479 void swap(random_access_index<SuperMeta,TagList>& x)
481 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
482 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(x);
483 this->final_swap_(x.final());
486 void clear()BOOST_NOEXCEPT
488 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
489 this->final_clear_();
492 /* list operations */
494 void splice(iterator position,random_access_index<SuperMeta,TagList>& x)
496 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
497 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
498 BOOST_MULTI_INDEX_CHECK_DIFFERENT_CONTAINER(*this,x);
499 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
500 iterator first=x.begin(),last=x.end();
504 if(push_back(*first).second){
505 first=x.erase(first);
512 relocate(position,end()-n,end());
516 relocate(position,end()-n,end());
520 iterator position,random_access_index<SuperMeta,TagList>& x,iterator i)
522 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
523 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
524 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(i);
525 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(i);
526 BOOST_MULTI_INDEX_CHECK_IS_OWNER(i,x);
527 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
528 if(&x==this)relocate(position,i);
530 if(insert(position,*i).second){
532 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
533 /* MSVC++ 6.0 optimizer has a hard time with safe mode, and the following
534 * workaround is needed. Left it for all compilers as it does no
538 x.erase(x.make_iterator(i.get_node()));
548 iterator position,random_access_index<SuperMeta,TagList>& x,
549 iterator first,iterator last)
551 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
552 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
553 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
554 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
555 BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,x);
556 BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,x);
557 BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
558 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
559 if(&x==this)relocate(position,first,last);
564 if(push_back(*first).second){
565 first=x.erase(first);
572 relocate(position,end()-n,end());
576 relocate(position,end()-n,end());
580 void remove(value_param_type value)
582 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
585 random_access_index_remove<node_type>(
587 ::boost::bind(std::equal_to<value_type>(),::boost::arg<1>(),value)));
588 while(n--)pop_back();
591 template<typename Predicate>
592 void remove_if(Predicate pred)
594 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
596 end()-make_iterator(random_access_index_remove<node_type>(ptrs,pred));
597 while(n--)pop_back();
602 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
605 random_access_index_unique<node_type>(
606 ptrs,std::equal_to<value_type>()));
607 while(n--)pop_back();
610 template <class BinaryPredicate>
611 void unique(BinaryPredicate binary_pred)
613 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
616 random_access_index_unique<node_type>(ptrs,binary_pred));
617 while(n--)pop_back();
620 void merge(random_access_index<SuperMeta,TagList>& x)
623 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
626 random_access_index_inplace_merge<node_type>(
627 get_allocator(),ptrs,ptrs.at(s),std::less<value_type>());
631 template <typename Compare>
632 void merge(random_access_index<SuperMeta,TagList>& x,Compare comp)
635 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
638 random_access_index_inplace_merge<node_type>(
639 get_allocator(),ptrs,ptrs.at(s),comp);
645 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
646 random_access_index_sort<node_type>(
647 get_allocator(),ptrs,std::less<value_type>());
650 template <typename Compare>
651 void sort(Compare comp)
653 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
654 random_access_index_sort<node_type>(
655 get_allocator(),ptrs,comp);
658 void reverse()BOOST_NOEXCEPT
660 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
661 node_impl_type::reverse(ptrs.begin(),ptrs.end());
664 /* rearrange operations */
666 void relocate(iterator position,iterator i)
668 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
669 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
670 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(i);
671 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(i);
672 BOOST_MULTI_INDEX_CHECK_IS_OWNER(i,*this);
673 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
674 if(position!=i)relocate(position.get_node(),i.get_node());
677 void relocate(iterator position,iterator first,iterator last)
679 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
680 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
681 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
682 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
683 BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
684 BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
685 BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
686 BOOST_MULTI_INDEX_CHECK_OUTSIDE_RANGE(position,first,last);
687 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
688 if(position!=last)relocate(
689 position.get_node(),first.get_node(),last.get_node());
692 template<typename InputIterator>
693 void rearrange(InputIterator first)
695 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
696 for(node_impl_ptr_pointer p0=ptrs.begin(),p0_end=ptrs.end();
697 p0!=p0_end;++first,++p0){
698 const value_type& v1=*first;
699 node_impl_ptr_pointer p1=node_from_value<node_type>(&v1)->up();
707 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
709 const ctor_args_list& args_list,const allocator_type& al):
710 super(args_list.get_tail(),al),
711 ptrs(al,header()->impl(),0)
715 random_access_index(const random_access_index<SuperMeta,TagList>& x):
718 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
722 ptrs(x.get_allocator(),header()->impl(),x.size())
724 /* The actual copying takes place in subsequent call to copy_().
729 const random_access_index<SuperMeta,TagList>& x,do_not_copy_elements_tag):
730 super(x,do_not_copy_elements_tag()),
732 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
736 ptrs(x.get_allocator(),header()->impl(),0)
740 ~random_access_index()
742 /* the container is guaranteed to be empty by now */
745 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
746 iterator make_iterator(node_type* node){return iterator(node,this);}
747 const_iterator make_iterator(node_type* node)const
748 {return const_iterator(node,const_cast<random_access_index*>(this));}
750 iterator make_iterator(node_type* node){return iterator(node);}
751 const_iterator make_iterator(node_type* node)const
752 {return const_iterator(node);}
756 const random_access_index<SuperMeta,TagList>& x,const copy_map_type& map)
758 for(node_impl_ptr_pointer begin_org=x.ptrs.begin(),
759 begin_cpy=ptrs.begin(),
760 end_org=x.ptrs.end();
761 begin_org!=end_org;++begin_org,++begin_cpy){
763 static_cast<node_type*>(
765 static_cast<final_node_type*>(
766 node_type::from_impl(*begin_org))))->impl();
767 (*begin_cpy)->up()=begin_cpy;
773 template<typename Variant>
774 final_node_type* insert_(
775 value_param_type v,final_node_type*& x,Variant variant)
778 final_node_type* res=super::insert_(v,x,variant);
779 if(res==x)ptrs.push_back(static_cast<node_type*>(x)->impl());
783 template<typename Variant>
784 final_node_type* insert_(
785 value_param_type v,node_type* position,final_node_type*& x,Variant variant)
788 final_node_type* res=super::insert_(v,position,x,variant);
789 if(res==x)ptrs.push_back(static_cast<node_type*>(x)->impl());
793 void erase_(node_type* x)
795 ptrs.erase(x->impl());
798 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
803 void delete_all_nodes_()
805 for(node_impl_ptr_pointer x=ptrs.begin(),x_end=ptrs.end();x!=x_end;++x){
806 this->final_delete_node_(
807 static_cast<final_node_type*>(node_type::from_impl(*x)));
816 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
817 safe_super::detach_dereferenceable_iterators();
821 void swap_(random_access_index<SuperMeta,TagList>& x)
825 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
832 void swap_elements_(random_access_index<SuperMeta,TagList>& x)
836 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
840 super::swap_elements_(x);
843 template<typename Variant>
844 bool replace_(value_param_type v,node_type* x,Variant variant)
846 return super::replace_(v,x,variant);
849 bool modify_(node_type* x)
852 if(!super::modify_(x)){
853 ptrs.erase(x->impl());
855 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
864 ptrs.erase(x->impl());
866 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
875 bool modify_rollback_(node_type* x)
877 return super::modify_rollback_(x);
880 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
883 template<typename Archive>
885 Archive& ar,const unsigned int version,const index_saver_type& sm)const
887 sm.save(begin(),end(),ar,version);
888 super::save_(ar,version,sm);
891 template<typename Archive>
893 Archive& ar,const unsigned int version,const index_loader_type& lm)
896 typedef random_access_index_loader<node_type,allocator_type> loader;
898 loader ld(get_allocator(),ptrs);
901 &loader::rearrange,&ld,::boost::arg<1>(),::boost::arg<2>()),
903 } /* exit scope so that ld frees its resources */
904 super::load_(ar,version,lm);
908 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
909 /* invariant stuff */
911 bool invariant_()const
913 if(size()>capacity())return false;
914 if(size()==0||begin()==end()){
915 if(size()!=0||begin()!=end())return false;
919 for(const_iterator it=begin(),it_end=end();;++it,++s){
920 if(*(it.get_node()->up())!=it.get_node()->impl())return false;
923 if(s!=size())return false;
926 return super::invariant_();
929 /* This forwarding function eases things for the boost::mem_fn construct
930 * in BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT. Actually,
931 * final_check_invariant is already an inherited member function of index.
933 void check_invariant_()const{this->final_check_invariant_();}
937 node_type* header()const{return this->final_header();}
939 static void relocate(node_type* position,node_type* x)
941 node_impl_type::relocate(position->up(),x->up());
944 static void relocate(node_type* position,node_type* first,node_type* last)
946 node_impl_type::relocate(
947 position->up(),first->up(),last->up());
950 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
951 void detach_iterators(node_type* x)
953 iterator it=make_iterator(x);
954 safe_mode::detach_equivalent_iterators(it);
958 template <class InputIterator>
959 void assign_iter(InputIterator first,InputIterator last,mpl::true_)
961 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
963 for(;first!=last;++first)this->final_insert_ref_(*first);
966 void assign_iter(size_type n,value_param_type value,mpl::false_)
968 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
970 for(size_type i=0;i<n;++i)push_back(value);
973 template<typename InputIterator>
975 iterator position,InputIterator first,InputIterator last,mpl::true_)
977 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
980 for(;first!=last;++first){
981 if(this->final_insert_ref_(*first).second)++s;
985 relocate(position,end()-s,end());
989 relocate(position,end()-s,end());
993 iterator position,size_type n,value_param_type x,mpl::false_)
995 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
996 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
997 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
1001 if(push_back(x).second)++s;
1005 relocate(position,end()-s,end());
1009 relocate(position,end()-s,end());
1012 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
1013 std::pair<iterator,bool> emplace_front_impl(
1014 BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
1016 return emplace_impl(begin(),BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
1019 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
1020 std::pair<iterator,bool> emplace_back_impl(
1021 BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
1023 return emplace_impl(end(),BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
1026 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
1027 std::pair<iterator,bool> emplace_impl(
1028 iterator position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
1030 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
1031 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
1032 BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
1033 std::pair<final_node_type*,bool> p=
1034 this->final_emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
1035 if(p.second&&position.get_node()!=header()){
1036 relocate(position.get_node(),p.first);
1038 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
1043 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
1044 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
1045 #pragma parse_mfunc_templ reset
1052 typename SuperMeta1,typename TagList1,
1053 typename SuperMeta2,typename TagList2
1056 const random_access_index<SuperMeta1,TagList1>& x,
1057 const random_access_index<SuperMeta2,TagList2>& y)
1059 return x.size()==y.size()&&std::equal(x.begin(),x.end(),y.begin());
1063 typename SuperMeta1,typename TagList1,
1064 typename SuperMeta2,typename TagList2
1067 const random_access_index<SuperMeta1,TagList1>& x,
1068 const random_access_index<SuperMeta2,TagList2>& y)
1070 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
1074 typename SuperMeta1,typename TagList1,
1075 typename SuperMeta2,typename TagList2
1078 const random_access_index<SuperMeta1,TagList1>& x,
1079 const random_access_index<SuperMeta2,TagList2>& y)
1085 typename SuperMeta1,typename TagList1,
1086 typename SuperMeta2,typename TagList2
1089 const random_access_index<SuperMeta1,TagList1>& x,
1090 const random_access_index<SuperMeta2,TagList2>& y)
1096 typename SuperMeta1,typename TagList1,
1097 typename SuperMeta2,typename TagList2
1100 const random_access_index<SuperMeta1,TagList1>& x,
1101 const random_access_index<SuperMeta2,TagList2>& y)
1107 typename SuperMeta1,typename TagList1,
1108 typename SuperMeta2,typename TagList2
1111 const random_access_index<SuperMeta1,TagList1>& x,
1112 const random_access_index<SuperMeta2,TagList2>& y)
1117 /* specialized algorithms */
1119 template<typename SuperMeta,typename TagList>
1121 random_access_index<SuperMeta,TagList>& x,
1122 random_access_index<SuperMeta,TagList>& y)
1127 } /* namespace multi_index::detail */
1129 /* random access index specifier */
1131 template <typename TagList>
1132 struct random_access
1134 BOOST_STATIC_ASSERT(detail::is_tag<TagList>::value);
1136 template<typename Super>
1139 typedef detail::random_access_index_node<Super> type;
1142 template<typename SuperMeta>
1145 typedef detail::random_access_index<
1146 SuperMeta,typename TagList::type> type;
1150 } /* namespace multi_index */
1152 } /* namespace boost */
1154 /* Boost.Foreach compatibility */
1156 template<typename SuperMeta,typename TagList>
1157 inline boost::mpl::true_* boost_foreach_is_noncopyable(
1158 boost::multi_index::detail::random_access_index<SuperMeta,TagList>*&,
1159 boost_foreach_argument_dependent_lookup_hack)
1164 #undef BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT
1165 #undef BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF