Skip to content

Commit 2637d78

Browse files
author
Qiang Kou
committed
unit test
1 parent f7a1399 commit 2637d78

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

inst/include/Rcpp/vector/Subsetter.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,27 @@ class SubsetProxy {
100100
template <int RTYPE_OTHER, template <class> class StoragePolicyOther,int RHS_RTYPE_OTHER, bool RHS_NA_OTHER, typename RHS_T_OTHER>
101101
SubsetProxy& operator=(const SubsetProxy<RTYPE_OTHER, StoragePolicyOther, RHS_RTYPE_OTHER, RHS_NA_OTHER, RHS_T_OTHER>& other) {
102102

103-
Vector<RTYPE, StoragePolicyOther> other_vec = other;
103+
Vector<RTYPE_OTHER, StoragePolicyOther> other_vec = other;
104104
*this = other_vec;
105105
return *this;
106106
}
107107

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+
108124
operator Vector<RTYPE, StoragePolicy>() const {
109125
return get_vec();
110126
}

inst/unitTests/cpp/Subset.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ 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+
}

inst/unitTests/runit.subset.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ 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+
7882
}
7983

8084
}

0 commit comments

Comments
 (0)