用fusion :: vector初始化状态折叠

时间:2017-01-01 14:32:45

标签: c++11 boost fold boost-fusion boost-hana

将boost :: fusion :: fold与std :: vector初始状态一起使用可以正常工作。

#include <vector>
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/fold.hpp>
struct A;
struct B;
struct C;
int ia = 1, ib = 2, ic = 3;
namespace fusion = boost::fusion;

template< typename StdInteratorT>
struct initialize_map_with_std_vector 
{
    template<class T>
    StdInteratorT operator()(StdInteratorT& i, T& val) {
        val = *i;
        return ++i;
    }
};

void use_fold_with_std_vector() 
{
    using M = fusion::map<fusion::pair<A, int>, fusion::pair<B, char>, fusion::pair<C, double>>;
    M fm;
    std::vector<int> sv = { ia, ib, ic };
    auto state = sv.begin();
    fusion::fold(fm, state, initialize_map_with_std_vector<std::vector<int>::iterator>());
}

int main() { use_fold_with_std_vector(); }

但是当将相同的概念应用于fusion :: vector时,代码无法使用错误进行编译 no instance of overloaded function "boost::fusion::fold" matches the argument

    #include <vector>
    #include <boost/fusion/container/map.hpp>
    #include <boost/fusion/container/vector.hpp>
    #include <boost/fusion/include/fold.hpp>
    #include <boost/fusion/algorithm/iteration/fold.hpp>
    namespace fusion = boost::fusion;

    struct A;
    struct B;
    struct C;
    std::vector<int> va = { 4, 5, 6 };
    std::vector<char> vb = { 'a', 'b', 'c' };
    std::vector<double> vc = { 10., 11., 12. };    

    template< typename FusionIteratorT, typename Ret>
    struct initialize_map_with_fusion_vector {

        template<typename T>
        Ret operator()(FusionIteratorT& i,  T& val) {
            val = fusion::deref(i);
            return fusion::next(i);
        } 
    };

    void use_fold_with_fusion_vector() {
        using M = fusion::map<fusion::pair<A, std::vector<int>>, fusion::pair<B, std::vector<char>>, fusion::pair<C, std::vector<double>>>;
        using V = fusion::vector<std::vector<int>, std::vector<char>, std::vector<double>>;
        auto fm = M();
        auto fv = V(va, vb, vc);
        using FusionIteratorT = decltype(fusion::begin(fv));
        using Ret = fusion::result_of::next<fusion::result_of::begin<decltype(fv)>>;
        fusion::fold(fm, fusion::begin(fv),
            initialize_map_with_fusion_vector<FusionIteratorT, Ret > ());
}

int main() {use_fold_with_fusion_vector();}

如何使用fusion :: vector初始化器进行fusion :: fold工作?

1 个答案:

答案 0 :(得分:0)

有关使用Boost.Hana的一点需要注意的是bars_df <- structure(list(key = c("time_2", "time_2", "time_2", "time_2", "time_2", "time_2", "time_1", "time_1", "time_1", "time_1", "time_1", "time_1", "time_3", "time_3", "time_3", "time_3", "time_3", "time_3" ), val = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("0", "1", "2", "3", "4", "5"), class = "factor"), sum = c(0, 147, 144, 63, 512, 30, 0, 100, 302, 168, 412, 35, 0, 51, 56, 84, 252, 20), prop = c(0, 0.164, 0.161, 0.07, 0.571, 0.033, 0, 0.098, 0.297, 0.165, 0.405, 0.034, 0, 0.11, 0.121, 0.181, 0.544, 0.043)), .Names = c("key", "val", "sum", "prop"), row.names = c(NA, -18L), class = "data.frame") 要求在编译时知道容器的长度,因此lines_df在没有运行时的情况下不能很好地工作检查。

考虑这个例子:     

lines_df <- structure(list(code = c("0-0", "0-1", "0-2", "0-3", "0-4", "0-5", 
"1-0", "1-1", "1-2", "1-3", "1-4", "1-5", "2-0", "2-1", "2-2", 
"2-3", "2-4", "2-5", "3-0", "3-1", "3-2", "3-3", "3-4", "3-5", 
"4-0", "4-1", "4-2", "4-3", "4-4", "4-5", "5-0", "5-1", "5-2", 
"5-3", "5-4", "5-5", "0-0", "0-1", "0-2", "0-3", "0-4", "0-5", 
"1-0", "1-1", "1-2", "1-3", "1-4", "1-5", "2-0", "2-1", "2-2", 
"2-3", "2-4", "2-5", "3-0", "3-1", "3-2", "3-3", "3-4", "3-5", 
"4-0", "4-1", "4-2", "4-3", "4-4", "4-5", "5-0", "5-1", "5-2", 
"5-3", "5-4", "5-5"), shift = c("shift_1", "shift_1", "shift_1", 
"shift_1", "shift_1", "shift_1", "shift_1", "shift_1", "shift_1", 
"shift_1", "shift_1", "shift_1", "shift_1", "shift_1", "shift_1", 
"shift_1", "shift_1", "shift_1", "shift_1", "shift_1", "shift_1", 
"shift_1", "shift_1", "shift_1", "shift_1", "shift_1", "shift_1", 
"shift_1", "shift_1", "shift_1", "shift_1", "shift_1", "shift_1", 
"shift_1", "shift_1", "shift_1", "shift_2", "shift_2", "shift_2", 
"shift_2", "shift_2", "shift_2", "shift_2", "shift_2", "shift_2", 
"shift_2", "shift_2", "shift_2", "shift_2", "shift_2", "shift_2", 
"shift_2", "shift_2", "shift_2", "shift_2", "shift_2", "shift_2", 
"shift_2", "shift_2", "shift_2", "shift_2", "shift_2", "shift_2", 
"shift_2", "shift_2", "shift_2", "shift_2", "shift_2", "shift_2", 
"shift_2", "shift_2", "shift_2"), sig = c("+", NA, NA, NA, NA, 
NA, NA, NA, "-", "-", NA, NA, NA, NA, "+", NA, NA, NA, NA, NA, 
NA, "+", "-", NA, NA, "-", NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, "+", NA, "+", "+", NA, NA, "-", 
"-", NA, NA, NA, NA, NA, NA, "+", NA, NA, NA, NA, NA, NA, "+", 
NA, NA, NA, NA, NA, NA, NA)), .Names = c("code", "shift", "sig"
), row.names = c(NA, -72L), class = "data.frame")

输出:

hana::Foldable