Rcpp调用的c++计数算法模板
c++ count algorithm template called by Rcpp
Rcpp调用的C++计数算法,可以计算向量中特定元素的出现次数。应该使用模板。我的尝试:
countRcpp <-'
#include<iostream>
#include<vector>
using namespace std;
int val = as<int>(y);
NumericVector xx(x);
NumericVector::iterator iter;
int m = 0;
for(iter = xx.begin(); iter < xx.end(); iter++){
if(*iter == val) ++m;}
return(wrap(m));
'
countf <- cxxfunction(signature(x = "numeric", y = "numeric"),
body = countRcpp,
plugin = "Rcpp")
set.seed (1005) # set seed for sampling
x <- sample(1:5L, size = 10, replace = T) #L is Long integer. Keep the nunmber as integer.
x # [1] 1 3 1 3 3 4 1 3 1 2
y <- 3L
y
countf(x,y)
incl <- '
#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
template <typename S_Type, typename T>
typename iterator_traits<S_Type>::difference_type
S_Type countR(S_Type first, S_Type last, const T & val){
typename iterator_traits<S_Type>::difference_type ret=0;
while (first !=last){
if(*first == val) ++ret;
++first;
}
return ret;
}
'
body_count <- '
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
NumericVector xx(x);
int n = xx.size();
NumericVector yy = xx + n;
int val = as<int>(y);
int pos = countR(xx, yy, val);
return wrap(pos);
'
countRcpp3 <- cxxfunction(signature(x = "numeric", y = "numeric"),
body = body_count,
includes = incl,
plugin = "Rcpp")
你能给我一些建议吗?或者你会推荐其他符合逻辑的任务吗?提前谢谢。
作为第一步,您可以提取具有签名的函数
int foo(Rcpp::IntegerVector x, int val)
从您的工作代码。然后,您可以将其泛化为对任何可迭代类型执行操作。签名:
template <typename T>
int foo(T x, typename std::iterator_traits<typename T::iterator>::value_type val)
然而,我们不能用R来称呼它。如果R中的函数应该作用于不同的类型,那么它必须将SEXP
作为参数。CCD_ 2然后可以用于确定R数据类型。将这些放在一起用于整数向量:
#include <Rcpp.h>
template <typename T>
int iter_count(T x, typename std::iterator_traits<typename T::iterator>::value_type val) {
int m = 0;
for(typename T::iterator iter = x.begin(); iter < x.end(); ++iter) {
if(*iter == val) ++m;
}
return m;
}
// [[Rcpp::export]]
int count(SEXP x, SEXP val) {
switch( TYPEOF(x) ) {
case INTSXP: {
return iter_count(Rcpp::as<Rcpp::IntegerVector>(x),
Rcpp::as<Rcpp::IntegerVector>(val)(0));
}
default: {
Rcpp::stop("incompatible SEXP encountered");
}
}
}
/*** R
set.seed (1005)
x <- sample(1:5L, size = 10, replace = T)
y <- 3L
count(x,y)
*/
我在这里使用Rcpp属性:将其保存为.cpp
文件并在其上使用Rcpp::sourceCpp("...")
顺便说一句,在你的代码中,这看起来很可疑:
NumericVector xx(x);
int n = xx.size();
NumericVector yy = xx + n;
你想要xx
的终点吗?然后使用不是NumericVector
的xx.end()
或end(xx)
。您的代码创建了一个新的NumericVector
,其中xx
的内容已按xx
的大小递增。这里使用Rcpp属性的等效代码:
Rcpp::cppFunction('NumericVector foo(NumericVector xx) {
int n = xx.size();
NumericVector yy = xx + n;
return yy;
}
')
set.seed(42)
foo(runif(3))
# [1] 3.914806 3.937075 3.286140
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- Rcpp调用的c++计数算法模板
- 如何在 c++ 中从 rosettacode 调用这个吴晓林的直线算法
- 通过指针算法调用结构的结构属性
- 如何转换多次调用自己的递归算法?
- 为什么此代码上的排序算法不调用类的交换版本?
- 使用指针算法调用结构内的结构数组
- 带有两个递归调用的递归算法的时间复杂性
- 如何使用"is_member_function_pointer"在算法中包含/排除成员函数调用
- 为什么排序调用比较函数的频率低于线性最小搜索算法
- 调用STL算法中的多个函数
- 如何在递归选择排序算法中调用类析构函数
- 对STL算法进行专门化,以便在可用时自动调用高效的容器成员函数
- 在调用C++/STL算法时,可视化消除了不必要的副本
- 对集合中的每个元素调用print的通用算法