Skip to content

Commit 3c72b6d

Browse files
committed
Merge pull request #352 from thirdwing/master
Enable e.g. 'a[b > 3] = b[b > 3]'
2 parents 17ef95e + 101fad8 commit 3c72b6d

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

inst/include/Rcpp/vector/Subsetter.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ class SubsetProxy {
9797
return *this;
9898
}
9999

100+
template <int RTYPE_OTHER, template <class> class StoragePolicyOther,int RHS_RTYPE_OTHER, bool RHS_NA_OTHER, typename RHS_T_OTHER>
101+
SubsetProxy& operator=(const SubsetProxy<RTYPE_OTHER, StoragePolicyOther, RHS_RTYPE_OTHER, RHS_NA_OTHER, RHS_T_OTHER>& other) {
102+
103+
Vector<RTYPE_OTHER, StoragePolicyOther> other_vec = other;
104+
*this = other_vec;
105+
return *this;
106+
}
107+
108+
SubsetProxy& operator=(const SubsetProxy& other) {
109+
if (other.indices_n == 1) {
110+
for (int i=0; i < indices_n; ++i) {
111+
lhs[ indices[i] ] = other.lhs[other.indices[0]];
112+
}
113+
}
114+
else if (indices_n == other.indices_n) {
115+
for (int i=0; i < indices_n; ++i)
116+
lhs[ indices[i] ] = other.lhs[other.indices[i]];
117+
}
118+
else {
119+
stop("index error");
120+
}
121+
return *this;
122+
}
123+
100124
operator Vector<RTYPE, StoragePolicy>() const {
101125
return get_vec();
102126
}

inst/unitTests/cpp/Subset.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,39 @@ NumericVector subset_test_assign(NumericVector x) {
5656
NumericVector subset_test_constref(NumericVector const& x, IntegerVector const& y) {
5757
return x[y];
5858
}
59+
60+
// [[Rcpp::export]]
61+
NumericVector subset_assign_subset(NumericVector x) {
62+
NumericVector y(x.size());
63+
y[x > 3] = x[x > 3];
64+
return y;
65+
}
66+
67+
// [[Rcpp::export]]
68+
NumericVector subset_assign_subset2(NumericVector x) {
69+
NumericVector y(x.size());
70+
y[x <= 3] = x[x > 3];
71+
return y;
72+
}
73+
74+
// [[Rcpp::export]]
75+
NumericVector subset_assign_subset3(NumericVector x) {
76+
NumericVector y(x.size());
77+
y[x <= 3] = x[3];
78+
return y;
79+
}
80+
81+
// [[Rcpp::export]]
82+
IntegerVector subset_assign_subset4(NumericVector x) {
83+
IntegerVector y(x.size());
84+
y[x <= 3] = x[x <= 3];
85+
return y;
86+
}
87+
88+
// [[Rcpp::export]]
89+
NumericVector subset_assign_subset5(NumericVector x) {
90+
NumericVector y(x.size());
91+
y[x < 3] = x[x >= 4];
92+
return y;
93+
}
94+

inst/unitTests/runit.subset.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ if (.runThisTest) {
7575
y <- subset_test_int(x, 0L)
7676
checkIdentical( attr(y, "foo"), "bar" )
7777

78+
checkIdentical(subset_assign_subset(1:6), c(0,0,0,4,5,6))
79+
80+
checkIdentical(subset_assign_subset2(1:6), c(4,5,6,0,0,0))
81+
82+
checkIdentical(subset_assign_subset3(1:6), c(4,4,4,0,0,0))
83+
84+
checkIdentical(subset_assign_subset4(seq(2, 4, 0.2)), c(2L,2L,2L,2L,2L,3L,0L,0L,0L,0L,0L))
85+
86+
checkException(subset_assign_subset5(1:6), msg = "index error")
7887
}
7988

8089
}

0 commit comments

Comments
 (0)