ordered_index.hpp
来自「support vector clustering for vc++」· HPP 代码 · 共 1,240 行 · 第 1/3 页
HPP
1,240 行
template<typename CompatibleKey>
iterator upper_bound(const CompatibleKey& x)const
{
return make_iterator(ordered_index_upper_bound(header(),key,x,comp));
}
template<typename CompatibleKey,typename CompatibleCompare>
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<iterator,iterator> equal_range(
const CompatibleKey& x)const
{
return equal_range(x,comp);
}
template<typename CompatibleKey,typename CompatibleCompare>
std::pair<iterator,iterator> equal_range(
const CompatibleKey& x,const CompatibleCompare& comp)const
{
return std::pair<iterator,iterator>(
lower_bound(x,comp),upper_bound(x,comp));
}
/* range */
template<typename LowerBounder,typename UpperBounder>
std::pair<iterator,iterator>
range(LowerBounder lower,UpperBounder upper)const
{
std::pair<iterator,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),
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,SuperMeta,TagList,Category>& x):
super(x),
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
safe_super(),
#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,SuperMeta,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)
{
link_info inf;
if(!link_point(key(v),inf,Category())){
return node_type::from_impl(inf.pos);
}
node_type* res=static_cast<node_type*>(super::insert_(v,x));
if(res==x){
ordered_index_node_impl::link(
x->impl(),inf.side,inf.pos,header()->impl());
}
return res;
}
node_type* insert_(value_param_type v,node_type* position,node_type* x)
{
link_info inf;
if(!hinted_link_point(key(v),position,inf,Category())){
return node_type::from_impl(inf.pos);
}
node_type* res=static_cast<node_type*>(super::insert_(v,position,x));
if(res==x){
ordered_index_node_impl::link(
x->impl(),inf.side,inf.pos,header()->impl());
}
return res;
}
void erase_(node_type* x)
{
ordered_index_node_impl::rebalance_for_erase(
x->impl(),header()->parent(),header()->left(),header()->right());
super::erase_(x);
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
detach_iterators(x);
#endif
}
void delete_all_nodes_()
{
delete_all_nodes(root());
}
void clear_()
{
super::clear_();
empty_initialize();
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
safe_super::detach_dereferenceable_iterators();
#endif
}
void swap_(ordered_index<KeyFromValue,Compare,SuperMeta,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(in_place(v,x,Category())){
return super::replace_(v,x);
}
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{
link_info inf;
if(link_point(key(v),inf,Category())&&super::replace_(v,x)){
ordered_index_node_impl::link(
x->impl(),inf.side,inf.pos,header()->impl());
return true;
}
ordered_index_node_impl::restore(
x->impl(),next->impl(),header()->impl());
return false;
}
BOOST_CATCH(...){
ordered_index_node_impl::restore(
x->impl(),next->impl(),header()->impl());
BOOST_RETHROW;
}
BOOST_CATCH_END
}
bool modify_(node_type* x)
{
bool b;
BOOST_TRY{
b=in_place(x->value(),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{
link_info inf;
if(!link_point(key(x->value()),inf,Category())){
super::erase_(x);
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
detach_iterators(x);
#endif
return false;
}
ordered_index_node_impl::link(
x->impl(),inf.side,inf.pos,header()->impl());
}
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_DISABLE_SERIALIZATION)
/* serialization */
template<typename Archive>
void save_(
Archive& ar,const unsigned int version,const index_saver_type& sm)const
{
save_(ar,version,sm,Category());
}
template<typename Archive>
void load_(Archive& ar,const unsigned int version,const index_loader_type& lm)
{
load_(ar,version,lm,Category());
}
#endif
#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()!=
ordered_index_node_impl::maximum(root()->impl()))
return false;
}
return super::invariant_();
}
/* This forwarding function eases things for the boost::mem_fn construct
* in BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT. Actually,
* final_check_invariant is already an inherited member function of
* ordered_index.
*/
void check_invariant_()const{this->final_check_invariant_();}
#endif
private:
node_type* header()const{return this->final_header();}
node_type* root()const{return node_type::from_impl(header()->parent());}
node_type* leftmost()const{return node_type::from_impl(header()->left());}
node_type* rightmost()const{return node_type::from_impl(header()->right());}
void empty_initialize()
{
header()->color()=red;
/* used to distinguish header() from root, in iterator.operator++ */
header()->parent()=0;
header()->left()=header()->impl();
header()->right()=header()->impl();
}
struct link_info
{
ordered_index_side side;
ordered_index_node_impl* pos;
};
bool link_point(key_param_type k,link_info& inf,ordered_unique_tag)
{
node_type* y=header();
node_type* x=root();
bool c=true;
while(x){
y=x;
c=comp(k,key(x->value()));
x=node_type::from_impl(c?x->left():x->right());
}
node_type* yy=y;
if(c){
if(yy==leftmost()){
inf.side=to_left;
inf.pos=y->impl();
return true;
}
else node_type::decrement(yy);
}
if(comp(key(yy->value()),k)){
inf.side=c?to_left:to_right;
inf.pos=y->impl();
return true;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?