Skip to content

Commit 93f6838

Browse files
committed
incorporate @thirdwing's suggestion for sugar ops (closes #392)
1 parent 1a34a41 commit 93f6838

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ src/*.o
44
src/*.so
55
src/*.dll
66
.Rhistory
7+
.RData
78

89
## QtCreator
910
Rcpp.pro

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2015-11-06 Kevin Ushey <kevinushey@gmailcom>
2+
3+
* inst/include/Rcpp/vector/Subsetter.h: Add sugar math operators
4+
* inst/unitTests/runit.subset.R: Unit tests
5+
* inst/unitTests/cpp/Subset.cpp: Unit tests
6+
* inst/NEWS.Rd: NEWS entry
7+
18
2015-11-01 Qiang Kou <[email protected]>
29

310
* inst/include/Rcpp/vector/MatrixColumn.h: Fix overflow

inst/NEWS.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
\itemize{
2525
\item Added new Sugar function \code{cummin()}, \code{cummax()},
2626
\code{cumprod()} (PR \ghpr{389} by Nathan Russell fixing \ghit{388})
27+
\item Enabled sugar math operations for subsets; e.g. x[y] + x[z].
28+
(PR \ghpr{393}, implementing \ghit{392})
2729
}
2830
\item Changes in Rcpp Documentation:
2931
\itemize{

inst/include/Rcpp/vector/Subsetter.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,33 @@ class SubsetProxy {
226226
// because of the above, we keep track of the size
227227
int indices_n;
228228

229+
public:
230+
231+
#define RCPP_GENERATE_SUBSET_PROXY_OPERATOR(__OPERATOR__) \
232+
template <int RTYPE_OTHER, template <class> class StoragePolicyOther, \
233+
int RHS_RTYPE_OTHER, bool RHS_NA_OTHER, typename RHS_T_OTHER> \
234+
SubsetProxy& operator __OPERATOR__ ( \
235+
const SubsetProxy<RTYPE_OTHER, StoragePolicyOther, RHS_RTYPE_OTHER, \
236+
RHS_NA_OTHER, RHS_T_OTHER>& other) { \
237+
if (other.indices_n == 1) { \
238+
for (int i = 0; i < indices_n; ++i) \
239+
lhs[indices[i]] __OPERATOR__ ## = other.lhs[other.indices[0]]; \
240+
} else if (indices_n == other.indices_n) { \
241+
for (int i = 0; i < indices_n; ++i) \
242+
lhs[indices[i]] __OPERATOR__ ## = other.lhs[other.indices[i]]; \
243+
} else { \
244+
stop("index error"); \
245+
} \
246+
return *this; \
247+
}
248+
249+
RCPP_GENERATE_SUBSET_PROXY_OPERATOR(+)
250+
RCPP_GENERATE_SUBSET_PROXY_OPERATOR(-)
251+
RCPP_GENERATE_SUBSET_PROXY_OPERATOR(*)
252+
RCPP_GENERATE_SUBSET_PROXY_OPERATOR(/)
253+
254+
#undef RCPP_GENERATE_SUBSET_PROXY_OPERATOR
255+
229256
};
230257

231258
}

inst/unitTests/cpp/Subset.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,10 @@ NumericVector subset_assign_vector_size_1(NumericVector x, int i) {
102102
return x;
103103
}
104104

105+
// [[Rcpp::export]]
106+
NumericVector subset_sugar_add(NumericVector x, IntegerVector y)
107+
{
108+
NumericVector result = x[y] + x[y];
109+
return result;
110+
}
111+

inst/unitTests/runit.subset.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ if (.runThisTest) {
8686
checkException(subset_assign_subset5(1:6), msg = "index error")
8787

8888
checkIdentical(subset_assign_vector_size_1(1:6,7), c(7,7,7,4,5,6))
89+
90+
x <- rnorm(10)
91+
y <- sample(10, 5)
92+
checkIdentical(subset_sugar_add(x, y - 1L), x[y] + x[y])
8993
}
9094

9195
}

0 commit comments

Comments
 (0)