1 /* Copyright 2003-2007 Joaquín M López Muñoz.
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.
8 * The internal implementation of red-black trees is based on that of SGI STL
11 * Copyright (c) 1996,1997
12 * Silicon Graphics Computer Systems, Inc.
14 * Permission to use, copy, modify, distribute and sell this software
15 * and its documentation for any purpose is hereby granted without fee,
16 * provided that the above copyright notice appear in all copies and
17 * that both that copyright notice and this permission notice appear
18 * in supporting documentation. Silicon Graphics makes no
19 * representations about the suitability of this software for any
20 * purpose. It is provided "as is" without express or implied warranty.
24 * Hewlett-Packard Company
26 * Permission to use, copy, modify, distribute and sell this software
27 * and its documentation for any purpose is hereby granted without fee,
28 * provided that the above copyright notice appear in all copies and
29 * that both that copyright notice and this permission notice appear
30 * in supporting documentation. Hewlett-Packard Company makes no
31 * representations about the suitability of this software for any
32 * purpose. It is provided "as is" without express or implied warranty.
36 #ifndef BOOST_MULTI_INDEX_ORDERED_INDEX_HPP
37 #define BOOST_MULTI_INDEX_ORDERED_INDEX_HPP
39 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
43 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
45 #include <boost/call_traits.hpp>
46 #include <boost/detail/no_exceptions_support.hpp>
47 #include <boost/detail/workaround.hpp>
48 #include <boost/iterator/reverse_iterator.hpp>
49 #include <boost/mpl/push_front.hpp>
50 #include <boost/multi_index/detail/access_specifier.hpp>
51 #include <boost/multi_index/detail/bidir_node_iterator.hpp>
52 #include <boost/multi_index/detail/modify_key_adaptor.hpp>
53 #include <boost/multi_index/detail/ord_index_node.hpp>
54 #include <boost/multi_index/detail/ord_index_ops.hpp>
55 #include <boost/multi_index/detail/safe_ctr_proxy.hpp>
56 #include <boost/multi_index/detail/safe_mode.hpp>
57 #include <boost/multi_index/detail/scope_guard.hpp>
58 #include <boost/multi_index/detail/unbounded.hpp>
59 #include <boost/multi_index/detail/value_compare.hpp>
60 #include <boost/multi_index/ordered_index_fwd.hpp>
61 #include <boost/ref.hpp>
62 #include <boost/tuple/tuple.hpp>
65 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
66 #include <boost/archive/archive_exception.hpp>
67 #include <boost/bind.hpp>
68 #include <boost/multi_index/detail/duplicates_iterator.hpp>
69 #include <boost/throw_exception.hpp>
72 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
73 #define BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT \
74 detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
75 detail::make_obj_guard(*this,&ordered_index::check_invariant_); \
76 BOOST_JOIN(check_invariant_,__LINE__).touch();
78 #define BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT
83 namespace multi_index{
87 /* ordered_index adds a layer of ordered indexing to a given Super */
89 /* Most of the implementation of unique and non-unique indices is
90 * shared. We tell from one another on instantiation time by using
94 struct ordered_unique_tag{};
95 struct ordered_non_unique_tag{};
98 typename KeyFromValue,typename Compare,
99 typename SuperMeta,typename TagList,typename Category
102 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
104 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
105 #if BOOST_WORKAROUND(BOOST_MSVC,<1300)
106 ,public safe_ctr_proxy_impl<
108 ordered_index_node<typename SuperMeta::type::node_type> >,
109 ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category> >
111 ,public safe_mode::safe_container<
112 ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category> >
117 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
118 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
119 /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
120 * lifetime of const references bound to temporaries --precisely what
124 #pragma parse_mfunc_templ off
127 typedef typename SuperMeta::type super;
130 typedef ordered_index_node<
131 typename super::node_type> node_type;
136 typedef typename KeyFromValue::result_type key_type;
137 typedef typename node_type::value_type value_type;
138 typedef KeyFromValue key_from_value;
139 typedef Compare key_compare;
140 typedef value_comparison<
141 value_type,KeyFromValue,Compare> value_compare;
142 typedef tuple<key_from_value,key_compare> ctor_args;
143 typedef typename super::final_allocator_type allocator_type;
144 typedef typename allocator_type::reference reference;
145 typedef typename allocator_type::const_reference const_reference;
147 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
148 #if BOOST_WORKAROUND(BOOST_MSVC,<1300)
149 typedef safe_mode::safe_iterator<
150 bidir_node_iterator<node_type>,
152 bidir_node_iterator<node_type> > > iterator;
154 typedef safe_mode::safe_iterator<
155 bidir_node_iterator<node_type>,
156 ordered_index> iterator;
159 typedef bidir_node_iterator<node_type> iterator;
162 typedef iterator const_iterator;
164 typedef std::size_t size_type;
165 typedef std::ptrdiff_t difference_type;
166 typedef typename allocator_type::pointer pointer;
167 typedef typename allocator_type::const_pointer const_pointer;
169 boost::reverse_iterator<iterator> reverse_iterator;
171 boost::reverse_iterator<const_iterator> const_reverse_iterator;
172 typedef TagList tag_list;
175 typedef typename super::final_node_type final_node_type;
176 typedef tuples::cons<
178 typename super::ctor_args_list> ctor_args_list;
179 typedef typename mpl::push_front<
180 typename super::index_type_list,
181 ordered_index>::type index_type_list;
182 typedef typename mpl::push_front<
183 typename super::iterator_type_list,
184 iterator>::type iterator_type_list;
185 typedef typename mpl::push_front<
186 typename super::const_iterator_type_list,
187 const_iterator>::type const_iterator_type_list;
188 typedef typename super::copy_map_type copy_map_type;
190 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
191 typedef typename super::index_saver_type index_saver_type;
192 typedef typename super::index_loader_type index_loader_type;
196 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
197 #if BOOST_WORKAROUND(BOOST_MSVC,<1300)
198 typedef safe_ctr_proxy_impl<
199 bidir_node_iterator<node_type>,
200 ordered_index> safe_super;
202 typedef safe_mode::safe_container<ordered_index> safe_super;
206 typedef typename call_traits<
207 value_type>::param_type value_param_type;
208 typedef typename call_traits<
209 key_type>::param_type key_param_type;
213 /* construct/copy/destroy
214 * Default and copy ctors are in the protected section as indices are
215 * not supposed to be created on their own. No range ctor either.
218 ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& operator=(
219 const ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& x)
221 this->final()=x.final();
225 allocator_type get_allocator()const
227 return this->final().get_allocator();
232 iterator begin(){return make_iterator(leftmost());}
233 const_iterator begin()const{return make_iterator(leftmost());}
234 iterator end(){return make_iterator(header());}
235 const_iterator end()const{return make_iterator(header());}
236 reverse_iterator rbegin(){return make_reverse_iterator(end());}
237 const_reverse_iterator rbegin()const{return make_reverse_iterator(end());}
238 reverse_iterator rend(){return make_reverse_iterator(begin());}
239 const_reverse_iterator rend()const{return make_reverse_iterator(begin());}
243 bool empty()const{return this->final_empty_();}
244 size_type size()const{return this->final_size_();}
245 size_type max_size()const{return this->final_max_size_();}
249 std::pair<iterator,bool> insert(value_param_type x)
251 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
252 std::pair<final_node_type*,bool> p=this->final_insert_(x);
253 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
256 iterator insert(iterator position,value_param_type x)
258 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
259 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
260 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
261 std::pair<final_node_type*,bool> p=this->final_insert_(
262 x,static_cast<final_node_type*>(position.get_node()));
263 return make_iterator(p.first);
266 template<typename InputIterator>
267 void insert(InputIterator first,InputIterator last)
269 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
271 for(;first!=last;++first)hint=insert(hint,*first);
274 iterator erase(iterator position)
276 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
277 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
278 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
279 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
280 this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
284 size_type erase(key_param_type x)
286 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
287 std::pair<iterator,iterator> p=equal_range(x);
289 while(p.first!=p.second){
290 p.first=erase(p.first);
296 iterator erase(iterator first,iterator last)
298 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
299 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
300 BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
301 BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
302 BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
303 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
310 bool replace(iterator position,value_param_type x)
312 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
313 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
314 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
315 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
316 return this->final_replace_(
317 x,static_cast<final_node_type*>(position.get_node()));
320 template<typename Modifier>
321 bool modify(iterator position,Modifier mod)
323 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
324 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
325 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
326 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
328 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
329 /* MSVC++ 6.0 optimizer on safe mode code chokes if this
330 * this is not added. Left it for all compilers as it does no
337 return this->final_modify_(
338 mod,static_cast<final_node_type*>(position.get_node()));
341 template<typename Modifier>
342 bool modify_key(iterator position,Modifier mod)
344 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
345 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
346 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
347 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
349 position,modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key));
352 void swap(ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& x)
354 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
355 this->final_swap_(x.final());
360 BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
361 this->final_clear_();
366 key_from_value key_extractor()const{return key;}
367 key_compare key_comp()const{return comp;}
368 value_compare value_comp()const{return value_compare(key,comp);}
372 /* Internally, these ops rely on const_iterator being the same
376 template<typename CompatibleKey>
377 iterator find(const CompatibleKey& x)const
379 return make_iterator(ordered_index_find(header(),key,x,comp));
382 template<typename CompatibleKey,typename CompatibleCompare>
384 const CompatibleKey& x,const CompatibleCompare& comp)const
386 return make_iterator(ordered_index_find(header(),key,x,comp));
389 template<typename CompatibleKey>
390 size_type count(const CompatibleKey& x)const
392 return count(x,comp);
395 template<typename CompatibleKey,typename CompatibleCompare>
396 size_type count(const CompatibleKey& x,const CompatibleCompare& comp)const
398 std::pair<iterator,iterator> p=equal_range(x,comp);
399 size_type n=std::distance(p.first,p.second);
403 template<typename CompatibleKey>
404 iterator lower_bound(const CompatibleKey& x)const
406 return make_iterator(ordered_index_lower_bound(header(),key,x,comp));
409 template<typename CompatibleKey,typename CompatibleCompare>
410 iterator lower_bound(
411 const CompatibleKey& x,const CompatibleCompare& comp)const
413 return make_iterator(ordered_index_lower_bound(header(),key,x,comp));
416 template<typename CompatibleKey>
417 iterator upper_bound(const CompatibleKey& x)const
419 return make_iterator(ordered_index_upper_bound(header(),key,x,comp));
422 template<typename CompatibleKey,typename CompatibleCompare>
423 iterator upper_bound(
424 const CompatibleKey& x,const CompatibleCompare& comp)const
426 return make_iterator(ordered_index_upper_bound(header(),key,x,comp));
429 template<typename CompatibleKey>
430 std::pair<iterator,iterator> equal_range(
431 const CompatibleKey& x)const
433 return equal_range(x,comp);
436 template<typename CompatibleKey,typename CompatibleCompare>
437 std::pair<iterator,iterator> equal_range(
438 const CompatibleKey& x,const CompatibleCompare& comp)const
440 return std::pair<iterator,iterator>(
441 lower_bound(x,comp),upper_bound(x,comp));
446 template<typename LowerBounder,typename UpperBounder>
447 std::pair<iterator,iterator>
448 range(LowerBounder lower,UpperBounder upper)const
450 std::pair<iterator,iterator> p(
451 lower_range(lower),upper_range(upper));
452 if(p.second!=end()&&(p.first==end()||comp(key(*p.second),key(*p.first)))){
458 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
459 ordered_index(const ctor_args_list& args_list,const allocator_type& al):
460 super(args_list.get_tail(),al),
461 key(tuples::get<0>(args_list.get_head())),
462 comp(tuples::get<1>(args_list.get_head()))
468 const ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& x):
471 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
478 /* Copy ctor just takes the key and compare objects from x. The rest is
479 * done in subsequent call to copy_().
485 /* the container is guaranteed to be empty by now */
488 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
489 iterator make_iterator(node_type* node){return iterator(node,this);}
490 const_iterator make_iterator(node_type* node)const
491 {return const_iterator(node,const_cast<ordered_index*>(this));}
493 iterator make_iterator(node_type* node){return iterator(node);}
494 const_iterator make_iterator(node_type* node)const
495 {return const_iterator(node);}
499 const ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& x,
500 const copy_map_type& map)
506 header()->color()=x.header()->color();
508 node_type* root_cpy=map.find(static_cast<final_node_type*>(x.root()));
509 header()->parent()=root_cpy->impl();
511 node_type* leftmost_cpy=map.find(
512 static_cast<final_node_type*>(x.leftmost()));
513 header()->left()=leftmost_cpy->impl();
515 node_type* rightmost_cpy=map.find(
516 static_cast<final_node_type*>(x.rightmost()));
517 header()->right()=rightmost_cpy->impl();
519 typedef typename copy_map_type::const_iterator copy_map_iterator;
520 for(copy_map_iterator it=map.begin(),it_end=map.end();it!=it_end;++it){
521 node_type* org=it->first;
522 node_type* cpy=it->second;
524 cpy->color()=org->color();
526 ordered_index_node_impl* parent_org=org->parent();
527 if(!parent_org)cpy->parent()=0;
529 node_type* parent_cpy=map.find(
530 static_cast<final_node_type*>(node_type::from_impl(parent_org)));
531 cpy->parent()=parent_cpy->impl();
532 if(parent_org->left()==org->impl()){
533 parent_cpy->left()=cpy->impl();
535 else if(parent_org->right()==org->impl()){
536 /* header() does not satisfy this nor the previous check */
537 parent_cpy->right()=cpy->impl();
541 if(!org->left())cpy->left()=0;
542 if(!org->right())cpy->right()=0;
549 node_type* insert_(value_param_type v,node_type* x)
552 if(!link_point(key(v),inf,Category())){
553 return node_type::from_impl(inf.pos);
556 node_type* res=static_cast<node_type*>(super::insert_(v,x));
558 ordered_index_node_impl::link(
559 x->impl(),inf.side,inf.pos,header()->impl());
564 node_type* insert_(value_param_type v,node_type* position,node_type* x)
567 if(!hinted_link_point(key(v),position,inf,Category())){
568 return node_type::from_impl(inf.pos);
571 node_type* res=static_cast<node_type*>(super::insert_(v,position,x));
573 ordered_index_node_impl::link(
574 x->impl(),inf.side,inf.pos,header()->impl());
579 void erase_(node_type* x)
581 ordered_index_node_impl::rebalance_for_erase(
582 x->impl(),header()->parent(),header()->left(),header()->right());
585 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
590 void delete_all_nodes_()
592 delete_all_nodes(root());
600 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
601 safe_super::detach_dereferenceable_iterators();
605 void swap_(ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& x)
607 std::swap(key,x.key);
608 std::swap(comp,x.comp);
610 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
617 bool replace_(value_param_type v,node_type* x)
619 if(in_place(v,x,Category())){
620 return super::replace_(v,x);
624 node_type::increment(next);
626 ordered_index_node_impl::rebalance_for_erase(
627 x->impl(),header()->parent(),header()->left(),header()->right());
631 if(link_point(key(v),inf,Category())&&super::replace_(v,x)){
632 ordered_index_node_impl::link(
633 x->impl(),inf.side,inf.pos,header()->impl());
636 ordered_index_node_impl::restore(
637 x->impl(),next->impl(),header()->impl());
641 ordered_index_node_impl::restore(
642 x->impl(),next->impl(),header()->impl());
648 bool modify_(node_type* x)
652 b=in_place(x->value(),x,Category());
660 ordered_index_node_impl::rebalance_for_erase(
661 x->impl(),header()->parent(),header()->left(),header()->right());
664 if(!link_point(key(x->value()),inf,Category())){
667 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
672 ordered_index_node_impl::link(
673 x->impl(),inf.side,inf.pos,header()->impl());
678 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
688 if(!super::modify_(x)){
689 ordered_index_node_impl::rebalance_for_erase(
690 x->impl(),header()->parent(),header()->left(),header()->right());
692 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
701 ordered_index_node_impl::rebalance_for_erase(
702 x->impl(),header()->parent(),header()->left(),header()->right());
704 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
713 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
716 template<typename Archive>
718 Archive& ar,const unsigned int version,const index_saver_type& sm)const
720 save_(ar,version,sm,Category());
723 template<typename Archive>
724 void load_(Archive& ar,const unsigned int version,const index_loader_type& lm)
726 load_(ar,version,lm,Category());
730 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
731 /* invariant stuff */
733 bool invariant_()const
735 if(size()==0||begin()==end()){
736 if(size()!=0||begin()!=end()||
737 header()->left()!=header()->impl()||
738 header()->right()!=header()->impl())return false;
741 if((size_type)std::distance(begin(),end())!=size())return false;
743 std::size_t len=ordered_index_node_impl::black_count(
744 leftmost()->impl(),root()->impl());
745 for(const_iterator it=begin(),it_end=end();it!=it_end;++it){
746 node_type* x=it.get_node();
747 node_type* left_x=node_type::from_impl(x->left());
748 node_type* right_x=node_type::from_impl(x->right());
751 if((left_x&&left_x->color()==red)||
752 (right_x&&right_x->color()==red))return false;
754 if(left_x&&comp(key(x->value()),key(left_x->value())))return false;
755 if(right_x&&comp(key(right_x->value()),key(x->value())))return false;
756 if(!left_x&&!right_x&&
757 ordered_index_node_impl::black_count(
758 x->impl(),root()->impl())!=len)
762 if(leftmost()->impl()!=
763 ordered_index_node_impl::minimum(root()->impl()))
765 if(rightmost()->impl()!=
766 ordered_index_node_impl::maximum(root()->impl()))
770 return super::invariant_();
774 /* This forwarding function eases things for the boost::mem_fn construct
775 * in BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT. Actually,
776 * final_check_invariant is already an inherited member function of
779 void check_invariant_()const{this->final_check_invariant_();}
783 node_type* header()const{return this->final_header();}
784 node_type* root()const{return node_type::from_impl(header()->parent());}
785 node_type* leftmost()const{return node_type::from_impl(header()->left());}
786 node_type* rightmost()const{return node_type::from_impl(header()->right());}
788 void empty_initialize()
790 header()->color()=red;
791 /* used to distinguish header() from root, in iterator.operator++ */
793 header()->parent()=0;
794 header()->left()=header()->impl();
795 header()->right()=header()->impl();
800 ordered_index_side side;
801 ordered_index_node_impl* pos;
804 bool link_point(key_param_type k,link_info& inf,ordered_unique_tag)
806 node_type* y=header();
811 c=comp(k,key(x->value()));
812 x=node_type::from_impl(c?x->left():x->right());
821 else node_type::decrement(yy);
824 if(comp(key(yy->value()),k)){
825 inf.side=c?to_left:to_right;
835 bool link_point(key_param_type k,link_info& inf,ordered_non_unique_tag)
837 node_type* y=header();
842 c=comp(k,key(x->value()));
843 x=node_type::from_impl(c?x->left():x->right());
845 inf.side=c?to_left:to_right;
850 bool hinted_link_point(
851 key_param_type k,node_type* position,link_info& inf,ordered_unique_tag)
853 if(position->impl()==header()->left()){
854 if(size()>0&&comp(k,key(position->value()))){
856 inf.pos=position->impl();
859 else return link_point(k,inf,ordered_unique_tag());
861 else if(position==header()){
862 if(comp(key(rightmost()->value()),k)){
864 inf.pos=rightmost()->impl();
867 else return link_point(k,inf,ordered_unique_tag());
870 node_type* before=position;
871 node_type::decrement(before);
872 if(comp(key(before->value()),k)&&comp(k,key(position->value()))){
873 if(before->right()==0){
875 inf.pos=before->impl();
880 inf.pos=position->impl();
884 else return link_point(k,inf,ordered_unique_tag());
888 bool hinted_link_point(
889 key_param_type k,node_type* position,link_info& inf,ordered_non_unique_tag)
891 if(position->impl()==header()->left()){
892 if(size()>0&&!comp(key(position->value()),k)){
894 inf.pos=position->impl();
897 else return link_point(k,inf,ordered_non_unique_tag());
899 else if(position==header()){
900 if(!comp(k,key(rightmost()->value()))){
902 inf.pos=rightmost()->impl();
905 else return link_point(k,inf,ordered_non_unique_tag());
908 node_type* before=position;
909 node_type::decrement(before);
910 if (!comp(k,key(before->value()))&&!comp(key(position->value()),k)){
911 if(before->right()==0){
913 inf.pos=before->impl();
918 inf.pos=position->impl();
922 else return link_point(k,inf,ordered_non_unique_tag());
926 void delete_all_nodes(node_type* x)
930 delete_all_nodes(node_type::from_impl(x->left()));
931 delete_all_nodes(node_type::from_impl(x->right()));
932 this->final_delete_node_(static_cast<final_node_type*>(x));
935 bool in_place(value_param_type v,node_type* x,ordered_unique_tag)
940 node_type::decrement(y);
941 if(!comp(key(y->value()),key(v)))return false;
945 node_type::increment(y);
946 return y==header()||comp(key(v),key(y->value()));
949 bool in_place(value_param_type v,node_type* x,ordered_non_unique_tag)
954 node_type::decrement(y);
955 if(comp(key(v),key(y->value())))return false;
959 node_type::increment(y);
960 return y==header()||!comp(key(y->value()),key(v));
963 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
964 void detach_iterators(node_type* x)
966 iterator it=make_iterator(x);
967 safe_mode::detach_equivalent_iterators(it);
971 template<typename LowerBounder>
972 iterator lower_range(LowerBounder lower)const
974 node_type* y=header();
978 if(lower(key(z->value()))){
980 z=node_type::from_impl(z->left());
982 else z=node_type::from_impl(z->right());
985 return make_iterator(y);
988 iterator lower_range(unbounded_type)const
993 template<typename UpperBounder>
994 iterator upper_range(UpperBounder upper)const
996 node_type* y=header();
1000 if(!upper(key(z->value()))){
1002 z=node_type::from_impl(z->left());
1004 else z=node_type::from_impl(z->right());
1007 return make_iterator(y);
1010 iterator upper_range(unbounded_type)const
1015 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
1016 template<typename Archive>
1018 Archive& ar,const unsigned int version,const index_saver_type& sm,
1019 ordered_unique_tag)const
1021 super::save_(ar,version,sm);
1024 template<typename Archive>
1026 Archive& ar,const unsigned int version,const index_loader_type& lm,
1029 super::load_(ar,version,lm);
1032 template<typename Archive>
1034 Archive& ar,const unsigned int version,const index_saver_type& sm,
1035 ordered_non_unique_tag)const
1037 typedef duplicates_iterator<node_type,value_compare> dup_iterator;
1040 dup_iterator(begin().get_node(),end().get_node(),value_comp()),
1041 dup_iterator(end().get_node(),value_comp()),
1043 super::save_(ar,version,sm);
1046 template<typename Archive>
1048 Archive& ar,const unsigned int version,const index_loader_type& lm,
1049 ordered_non_unique_tag)
1052 ::boost::bind(&ordered_index::rearranger,this,_1,_2),
1054 super::load_(ar,version,lm);
1057 void rearranger(node_type* position,node_type *x)
1059 if(!position||comp(key(position->value()),key(x->value()))){
1060 position=lower_bound(key(x->value())).get_node();
1062 else if(comp(key(x->value()),key(position->value()))){
1063 /* inconsistent rearrangement */
1065 archive::archive_exception(
1066 archive::archive_exception::other_exception));
1068 else node_type::increment(position);
1071 ordered_index_node_impl::rebalance_for_erase(
1072 x->impl(),header()->parent(),header()->left(),header()->right());
1073 ordered_index_node_impl::restore(
1074 x->impl(),position->impl(),header()->impl());
1077 #endif /* serialization */
1082 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
1083 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
1084 #pragma parse_mfunc_templ reset
1091 typename KeyFromValue1,typename Compare1,
1092 typename SuperMeta1,typename TagList1,typename Category1,
1093 typename KeyFromValue2,typename Compare2,
1094 typename SuperMeta2,typename TagList2,typename Category2
1097 const ordered_index<KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1>& x,
1098 const ordered_index<KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2>& y)
1100 return x.size()==y.size()&&std::equal(x.begin(),x.end(),y.begin());
1104 typename KeyFromValue1,typename Compare1,
1105 typename SuperMeta1,typename TagList1,typename Category1,
1106 typename KeyFromValue2,typename Compare2,
1107 typename SuperMeta2,typename TagList2,typename Category2
1110 const ordered_index<KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1>& x,
1111 const ordered_index<KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2>& y)
1113 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
1117 typename KeyFromValue1,typename Compare1,
1118 typename SuperMeta1,typename TagList1,typename Category1,
1119 typename KeyFromValue2,typename Compare2,
1120 typename SuperMeta2,typename TagList2,typename Category2
1123 const ordered_index<KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1>& x,
1124 const ordered_index<KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2>& y)
1130 typename KeyFromValue1,typename Compare1,
1131 typename SuperMeta1,typename TagList1,typename Category1,
1132 typename KeyFromValue2,typename Compare2,
1133 typename SuperMeta2,typename TagList2,typename Category2
1136 const ordered_index<KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1>& x,
1137 const ordered_index<KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2>& y)
1143 typename KeyFromValue1,typename Compare1,
1144 typename SuperMeta1,typename TagList1,typename Category1,
1145 typename KeyFromValue2,typename Compare2,
1146 typename SuperMeta2,typename TagList2,typename Category2
1149 const ordered_index<KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1>& x,
1150 const ordered_index<KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2>& y)
1156 typename KeyFromValue1,typename Compare1,
1157 typename SuperMeta1,typename TagList1,typename Category1,
1158 typename KeyFromValue2,typename Compare2,
1159 typename SuperMeta2,typename TagList2,typename Category2
1162 const ordered_index<KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1>& x,
1163 const ordered_index<KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2>& y)
1168 /* specialized algorithms */
1171 typename KeyFromValue,typename Compare,
1172 typename SuperMeta,typename TagList,typename Category
1175 ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& x,
1176 ordered_index<KeyFromValue,Compare,SuperMeta,TagList,Category>& y)
1181 } /* namespace multi_index::detail */
1183 /* ordered_index specifiers */
1185 template<typename Arg1,typename Arg2,typename Arg3>
1186 struct ordered_unique
1188 typedef typename detail::ordered_index_args<
1189 Arg1,Arg2,Arg3> index_args;
1190 typedef typename index_args::tag_list_type::type tag_list_type;
1191 typedef typename index_args::key_from_value_type key_from_value_type;
1192 typedef typename index_args::compare_type compare_type;
1194 template<typename Super>
1197 typedef detail::ordered_index_node<Super> type;
1200 template<typename SuperMeta>
1203 typedef detail::ordered_index<
1204 key_from_value_type,compare_type,
1205 SuperMeta,tag_list_type,detail::ordered_unique_tag> type;
1209 template<typename Arg1,typename Arg2,typename Arg3>
1210 struct ordered_non_unique
1212 typedef detail::ordered_index_args<
1213 Arg1,Arg2,Arg3> index_args;
1214 typedef typename index_args::tag_list_type::type tag_list_type;
1215 typedef typename index_args::key_from_value_type key_from_value_type;
1216 typedef typename index_args::compare_type compare_type;
1218 template<typename Super>
1221 typedef detail::ordered_index_node<Super> type;
1224 template<typename SuperMeta>
1227 typedef detail::ordered_index<
1228 key_from_value_type,compare_type,
1229 SuperMeta,tag_list_type,detail::ordered_non_unique_tag> type;
1233 } /* namespace multi_index */
1235 } /* namespace boost */
1237 #undef BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT