📄 ordered_index.hpp
字号:
return count(x,comp); } template<typename CompatibleKey,typename CompatibleCompare> size_type count(const CompatibleKey& x,const CompatibleCompare& comp)const { std::pair<const_iterator, const_iterator> p=equal_range(x,comp); size_type n=std::distance(p.first,p.second); return n; } template<typename CompatibleKey> const_iterator lower_bound(const CompatibleKey& x)const { return make_iterator(ordered_index_lower_bound(header(),key,x,comp)); } template<typename CompatibleKey,typename CompatibleCompare> const_iterator lower_bound( const CompatibleKey& x,const CompatibleCompare& comp)const { return make_iterator(ordered_index_lower_bound(header(),key,x,comp)); } template<typename CompatibleKey> const_iterator upper_bound(const CompatibleKey& x)const { return make_iterator(ordered_index_upper_bound(header(),key,x,comp)); } template<typename CompatibleKey,typename CompatibleCompare> const_iterator upper_bound( const CompatibleKey& x,const CompatibleCompare& comp)const { return make_iterator(ordered_index_upper_bound(header(),key,x,comp)); } template<typename CompatibleKey> std::pair<const_iterator,const_iterator> equal_range( const CompatibleKey& x)const { return equal_range(x,comp); } template<typename CompatibleKey,typename CompatibleCompare> std::pair<const_iterator,const_iterator> equal_range( const CompatibleKey& x,const CompatibleCompare& comp)const { return std::pair<const_iterator,const_iterator>( lower_bound(x,comp),upper_bound(x,comp)); } /* range */ /* no need to provide non-const version as * ordered_index::iterator==ordered_index::const_iterator */ template<typename LowerBounder,typename UpperBounder> std::pair<const_iterator,const_iterator> range(LowerBounder lower,UpperBounder upper)const { std::pair<const_iterator,const_iterator> p( lower_range(lower),upper_range(upper)); if(p.second!=end()&&(p.first==end()||comp(key(*p.second),key(*p.first)))){ p.second=p.first; } return p; }BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS: ordered_index(const ctor_args_list& args_list,const allocator_type& al): Super(args_list.get_tail(),al),#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)&&\ BOOST_WORKAROUND(BOOST_MSVC,<1300) safe_super(final_header()),#endif key(tuples::get<0>(args_list.get_head())), comp(tuples::get<1>(args_list.get_head())) { empty_initialize(); } ordered_index( const ordered_index<KeyFromValue,Compare,Super,TagList,Category>& x): Super(x),#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)&&\ BOOST_WORKAROUND(BOOST_MSVC,<1300) safe_super(final_header()),#endif key(x.key), comp(x.comp) { /* Copy ctor just takes the key and compare objects from x. The rest is * done in subsequent call to copy_(). */ } ~ordered_index() { /* the container is guaranteed to be empty by now */ }#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE) iterator make_iterator(node_type* node){return iterator(node,this);} const_iterator make_iterator(node_type* node)const {return const_iterator(node,const_cast<ordered_index*>(this));}#else iterator make_iterator(node_type* node){return iterator(node);} const_iterator make_iterator(node_type* node)const {return const_iterator(node);}#endif void copy_( const ordered_index<KeyFromValue,Compare,Super,TagList,Category>& x, const copy_map_type& map) { if(!x.root()){ empty_initialize(); } else{ header()->color()=x.header()->color(); node_type* root_cpy=map.find(static_cast<final_node_type*>(x.root())); header()->parent()=root_cpy->impl(); node_type* leftmost_cpy=map.find( static_cast<final_node_type*>(x.leftmost())); header()->left()=leftmost_cpy->impl(); node_type* rightmost_cpy=map.find( static_cast<final_node_type*>(x.rightmost())); header()->right()=rightmost_cpy->impl(); typedef typename copy_map_type::const_iterator copy_map_iterator; for(copy_map_iterator it=map.begin(),it_end=map.end();it!=it_end;++it){ node_type* org=it->first; node_type* cpy=it->second; cpy->color()=org->color(); ordered_index_node_impl* parent_org=org->parent(); if(!parent_org)cpy->parent()=0; else{ node_type* parent_cpy=map.find( static_cast<final_node_type*>(node_type::from_impl(parent_org))); cpy->parent()=parent_cpy->impl(); if(parent_org->left()==org->impl()){ parent_cpy->left()=cpy->impl(); } else if(parent_org->right()==org->impl()){ /* header() does not satisfy this nor the previous check */ parent_cpy->right()=cpy->impl(); } } if(!org->left())cpy->left()=0; if(!org->right())cpy->right()=0; } } Super::copy_(x,map); } node_type* insert_(value_param_type v,node_type* x) { node_type* res=link2(key(v),x,Category()); if(res!=x)return res; else{ BOOST_TRY{ res=static_cast<node_type*>(Super::insert_(v,x)); if(res!=x){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); } return res; } BOOST_CATCH(...){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); BOOST_RETHROW; } BOOST_CATCH_END } } node_type* insert_(value_param_type v,node_type* position,node_type* x) { node_type* res=link3(key(v),position,x,Category()); if(res!=x)return res; else{ BOOST_TRY{ res=static_cast<node_type*>(Super::insert_(v,position,x)); if(res!=x){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); } return res; } BOOST_CATCH(...){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); BOOST_RETHROW; } BOOST_CATCH_END } } void erase_(node_type* x) { Super::erase_(x); ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right());#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE) detach_iterators(x);#endif } void swap_(ordered_index<KeyFromValue,Compare,Super,TagList,Category>& x) { std::swap(key,x.key); std::swap(comp,x.comp);#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE) safe_super::swap(x);#endif Super::swap_(x); } bool replace_(value_param_type v,node_type* x) { if(!comp(key(v),key(x->value))&& !comp(key(x->value),key(v))){ return Super::replace_(v,x); } node_type* prior=x; node_type::decrement(prior); node_type* next=x; node_type::increment(next); ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); BOOST_TRY{ if(link2(key(v),x,Category())!=x){ ordered_index_node_impl::restore( x->impl(),prior->impl(),next->impl(),header()->impl()); return false; } BOOST_TRY{ if(!Super::replace_(v,x)){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); ordered_index_node_impl::restore( x->impl(),prior->impl(),next->impl(),header()->impl()); return false; } else return true; } BOOST_CATCH(...){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); BOOST_RETHROW; } BOOST_CATCH_END } BOOST_CATCH(...){ ordered_index_node_impl::restore( x->impl(),prior->impl(),next->impl(),header()->impl()); BOOST_RETHROW; } BOOST_CATCH_END } bool modify_(node_type* x) { bool b; BOOST_TRY{ b=in_place(x,Category()); } BOOST_CATCH(...){ erase_(x); BOOST_RETHROW; } BOOST_CATCH_END if(!b){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right()); BOOST_TRY{ if(link2(key(x->value),x,Category())!=x){ Super::erase_(x);#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE) detach_iterators(x);#endif return false; } } BOOST_CATCH(...){ Super::erase_(x);#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE) detach_iterators(x);#endif BOOST_RETHROW; } BOOST_CATCH_END } BOOST_TRY{ if(!Super::modify_(x)){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right());#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE) detach_iterators(x);#endif return false; } else return true; } BOOST_CATCH(...){ ordered_index_node_impl::rebalance_for_erase( x->impl(),header()->parent(),header()->left(),header()->right());#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE) detach_iterators(x);#endif BOOST_RETHROW; } BOOST_CATCH_END }#if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING) /* invariant stuff */ bool invariant_()const { if(size()==0||begin()==end()){ if(size()!=0||begin()!=end()|| header()->left()!=header()->impl()|| header()->right()!=header()->impl())return false; } else{ if((size_type)std::distance(begin(),end())!=size())return false; std::size_t len=ordered_index_node_impl::black_count( leftmost()->impl(),root()->impl()); for(const_iterator it=begin(),it_end=end();it!=it_end;++it){ node_type* x=it.get_node(); node_type* left_x=node_type::from_impl(x->left()); node_type* right_x=node_type::from_impl(x->right()); if(x->color()==red){ if((left_x&&left_x->color()==red)|| (right_x&&right_x->color()==red))return false; } if(left_x&&comp(key(x->value),key(left_x->value)))return false; if(right_x&&comp(key(right_x->value),key(x->value)))return false; if(!left_x&&!right_x&& ordered_index_node_impl::black_count( x->impl(),root()->impl())!=len) return false; } if(leftmost()->impl()!= ordered_index_node_impl::minimum(root()->impl())) return false; if(rightmost()->impl()!=
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -