Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upSugar operator+ and SubsetProxy #392
Comments
|
A somewhat casual observation from my usage is that the compiler has difficulties with complex and compound return statements. This more defensive variant works: #include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericVector SubsetAdd(Rcpp::NumericVector lhs, Rcpp::NumericVector rhs) {
Rcpp::LogicalVector idx = !Rcpp::is_na(lhs) & !Rcpp::is_na(rhs);
Rcpp::NumericVector a = lhs[idx];
Rcpp::NumericVector b = rhs[idx];
return a + b;
} as seen here R> library(Rcpp)
R> sourceCpp("/tmp/nrussell.cpp")
R> SubsetAdd(1:5, c(1,NA,3:5))
[1] 2 6 8 10
R> Good enough to close this? Fixing this would get pretty deep into the TMP bowels.... |
|
Yes of course; I had a feeling this would involve some serious template wizardry, but was hoping I had overlooked some obvious quick fix. I'll close this out. |
|
Reopening just because I think this should be possible + is worth doing. |
|
Add something like below into template <int RTYPE_OTHER, template <class> class StoragePolicyOther,int RHS_RTYPE_OTHER, bool RHS_NA_OTHER, typename RHS_T_OTHER>
SubsetProxy& operator+(const SubsetProxy<RTYPE_OTHER, StoragePolicyOther, RHS_RTYPE_OTHER, RHS_NA_OTHER, RHS_T_OTHER>& other) {
if (other.indices_n == 1) {
for (int i = 0; i < indices_n; ++i)
lhs[ indices[i] ] += other.lhs[other.indices[0]];
} else if (indices_n == other.indices_n) {
for (int i = 0; i < indices_n; ++i)
lhs[ indices[i] ] += other.lhs[other.indices[i]];
} else {
stop("index error");
}
return *this;
}If we support |
|
Indeed. I wonder if we could just generate all that code (either with a macro, or an R script, or otherwise). |
|
Well that was fast - and much more concise than I thought it would be. Very nice work guys. |
|
You guys roll. I was out "socialising" on a Friday... |
incorporate @thirdwing's suggestion for sugar ops (closes #392)
The following fails to compile (x86_64-redhat-linux, g++ 4.8.3, R 3.2.2),
with error
This compiles and evaluates correctly:
but surely is not ideal. I was unsuccessful in coming up with a fix for this, but hopefully someone can point me in the direction of a clever solution? (Presumably this functionality is desirable & possible.)