比较 Rcpp 中的两个值而不强制转换为特定类型
Comparing two values in Rcpp without casting to specific type
我试图使用 Rcpp 比较 C++ 中的两个通用 R 值。如何比较两个值而不将它们转换为 C++ 中的特定类型?
解释我的问题的代码如下,
require("Rcpp")
require("inline")
src <- "return wrap(x1 == x2);"
fun <- cxxfunction(signature(x1 = "SEXP", x2 = "SEXP"), src, plugin = "Rcpp")
fun("a", "a")
to_cmp <- "a"
fun(to_cmp, to_cmp)
它现在给出 FALSE
和 TRUE
我希望它产生 TRUE
和 TRUE
.
因为我的目标是用 C++ 实现数据结构,所以我更喜欢潜在的用户定义的 ==
方法。
可能的方法
我尝试过的一种方法是,
要求("Rcpp")
src <- '
Language call("\`==\`", x1, x2);
return call.eval();
'
fun <- cxxfunction(signature(x1 = "SEXP", x2 = "SEXP"), src, plugin = "Rcpp")
fun("a", "a")
to_cmp <- "a"
fun(to_cmp, to_cmp)
然而,当我 运行 这个我得到 Error: could not find function "`==`"
您使用通用 SEXP
输入对象标签的方向正确。为了让它工作,除了 TYPEOF()
之外还需要使用 C++ 模板。先验使比较函数中的正确向量创建与 Rcpp 糖挂钩,而后者使正确的检查和调度发生。
#include <Rcpp.h>
using namespace Rcpp;
template <int RTYPE>
Rcpp::LogicalVector compare_me(Rcpp::Vector<RTYPE> x, Rcpp::Vector<RTYPE> y) {
return x == y;
}
// [[Rcpp::export]]
Rcpp::LogicalVector compare_objects(SEXP x, SEXP y) {
if (TYPEOF(x) == TYPEOF(y)) {
switch (TYPEOF(x)) {
case INTSXP:
return compare_me<INTSXP>(x, y);
case REALSXP:
return compare_me<REALSXP>(x, y);
case STRSXP:
return compare_me<STRSXP>(x, y);
default:
Rcpp::stop("Type not supported");
}
} else {
Rcpp::stop("Objects are of different type");
}
// Never used, but necessary to avoid the compiler complaining
// about a missing return statement
return Rcpp::LogicalVector();
}
示例:
to_cmp <- "a"
compare_objects(to_cmp, to_cmp)
输出:
[1] TRUE
此外,以上是与 Rcpp::sourceCpp()
一起使用的。我鼓励您从使用 inline
切换到使用 Rcpp::cppFunction()
进行函数定义,因为它可以让您专注于计算而不是 设置。
我试图使用 Rcpp 比较 C++ 中的两个通用 R 值。如何比较两个值而不将它们转换为 C++ 中的特定类型?
解释我的问题的代码如下,
require("Rcpp")
require("inline")
src <- "return wrap(x1 == x2);"
fun <- cxxfunction(signature(x1 = "SEXP", x2 = "SEXP"), src, plugin = "Rcpp")
fun("a", "a")
to_cmp <- "a"
fun(to_cmp, to_cmp)
它现在给出 FALSE
和 TRUE
我希望它产生 TRUE
和 TRUE
.
因为我的目标是用 C++ 实现数据结构,所以我更喜欢潜在的用户定义的 ==
方法。
可能的方法
我尝试过的一种方法是,
要求("Rcpp")
src <- '
Language call("\`==\`", x1, x2);
return call.eval();
'
fun <- cxxfunction(signature(x1 = "SEXP", x2 = "SEXP"), src, plugin = "Rcpp")
fun("a", "a")
to_cmp <- "a"
fun(to_cmp, to_cmp)
然而,当我 运行 这个我得到 Error: could not find function "`==`"
您使用通用 SEXP
输入对象标签的方向正确。为了让它工作,除了 TYPEOF()
之外还需要使用 C++ 模板。先验使比较函数中的正确向量创建与 Rcpp 糖挂钩,而后者使正确的检查和调度发生。
#include <Rcpp.h>
using namespace Rcpp;
template <int RTYPE>
Rcpp::LogicalVector compare_me(Rcpp::Vector<RTYPE> x, Rcpp::Vector<RTYPE> y) {
return x == y;
}
// [[Rcpp::export]]
Rcpp::LogicalVector compare_objects(SEXP x, SEXP y) {
if (TYPEOF(x) == TYPEOF(y)) {
switch (TYPEOF(x)) {
case INTSXP:
return compare_me<INTSXP>(x, y);
case REALSXP:
return compare_me<REALSXP>(x, y);
case STRSXP:
return compare_me<STRSXP>(x, y);
default:
Rcpp::stop("Type not supported");
}
} else {
Rcpp::stop("Objects are of different type");
}
// Never used, but necessary to avoid the compiler complaining
// about a missing return statement
return Rcpp::LogicalVector();
}
示例:
to_cmp <- "a"
compare_objects(to_cmp, to_cmp)
输出:
[1] TRUE
此外,以上是与 Rcpp::sourceCpp()
一起使用的。我鼓励您从使用 inline
切换到使用 Rcpp::cppFunction()
进行函数定义,因为它可以让您专注于计算而不是 设置。