5

What is the best way to pass an R vector of booleans to a C++ dynamic_bitset vector? Is there a way to use a pointer and the vector length to construct a dynamic_bitset object as would be possible for the vector class? Would you recommend using Rcpp ?
Thanks for your help and time...

4

1 回答 1

2

我会创建dynamic_bitset这样的:

#include <Rcpp.h>
#include <boost/dynamic_bitset.hpp>

using namespace Rcpp ;

// [[Rcpp::export]]
void create_dynamic_bitset( LogicalVector x ){  
    int n = x.size() ;
    boost::dynamic_bitset<> bs(n);
    for( int i=0; i<n; i++) bs[i] = x[i] ;

    // do something with the bitset
    for (boost::dynamic_bitset<>::size_type i = 0; i < x.size(); ++i)
        Rcout << x[i];
    Rcout << "\n";

}

在内部,R 逻辑向量只是int数组。所以没有更直接的方法来构造dynamic_bitset,你必须迭代。

另外,请注意 input 中的缺失值LogicalVector

或者,您可以将输入数据存储为原始向量(Rcpp 类RawVector),使用 adynamic_bitset<Rbyte>并使用块构造函数:

void create_dynamic_bitset( RawVector x ){  
    boost::dynamic_bitset<Rbyte> bs(x.begin(), x.end());
}
于 2013-07-30T06:27:17.387 回答