一个有意思的类型实现,可惜是个半成品

template<int N,typename H,typename T>
struct red_blue_boll{
    typedef std::map<H,typename red_blue_boll<N-1,H,T>::red_blue_type > red_blue_type;
public:
    template<int COUNTS>
    static void fill_rb_boll_data(red_blue_type& dst,const H (&vals)[COUNTS]){
        const H& val_index = vals[COUNTS - N - 1];
        typename red_blue_boll<N-1,H,T>::red_blue_type& top_dst = dst[val_index];
        red_blue_boll<N-1,H,T>::fill_rb_boll_data(boost::ref(top_dst),vals);
    }
public:
    static int get_leaf_counts(const red_blue_type& dst, int leal){
        if(N == leal){
            return dst.size();
        }
        typename red_blue_type::const_iterator b = dst.begin();
        typename red_blue_type::const_iterator e = dst.end();
        int count = 0;
        for(; b != e;++b){
            count += red_blue_boll<N-1,H,T>::get_leaf_counts(b->second,leal);
        }
        return count;
    }
    static int calc_number_counts(const red_blue_type& dst,int leal,std::map<int,int>& rst){
        typename red_blue_type::const_iterator b = dst.begin();
        typename red_blue_type::const_iterator e = dst.end();
        if(N == leal){
            for(; b != e; ++b){
                rst[b->first] += red_blue_boll<N-1,H,T>::get_leaf_counts(b->second,0);
            }
            return rst.size();
        }else{
            int count = 0;
            for(; b !=e ; ++b){
                count += red_blue_boll<N-1,H,T>::calc_number_counts(b->second,leal,boost::ref(rst));
            }
            return count;
        }
    }     
public:
    template<int COUNTS>
    static void set_rb_boll_inst(const cp_data (&src)[COUNTS],red_blue_type& dst){
        for(int i = 0; i < COUNTS; ++i){
            fill_rb_boll_data(boost::ref(dst),src[i].vals);
        }
    }
};
template<typename H,typename T>
struct red_blue_boll<0,H,T>{
    typedef T  red_blue_type;
public:
    template<int COUNTS>
    static void fill_rb_boll_data(red_blue_type& dst,const H (&vals)[COUNTS]){
        dst.push_back(vals[COUNTS - 1]);
    }
    static int get_leaf_counts(const red_blue_type& dst, int leal){
        return dst.size();
    }
    static int calc_number_counts(const red_blue_type& dst,int leal,std::map<int,int>& rst){
        red_blue_type::const_iterator b = dst.begin();
        red_blue_type::const_iterator e = dst.end();
        for(; b != e ; ++b){
            rst[*b] += 1;
        }
        return rst.size();
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值