Skip to content

Commit 807cf8c

Browse files
committed
Merge pull request #349 from fplaza/subsetter-patch
Allow assignment of size 1 with Subsetter
2 parents e4868b8 + 10abfdb commit 807cf8c

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2015-09-01 Florian Plaza Onate <[email protected]>
2+
* inst/include/Rcpp/vector/Subsetter.h: Allow logical subsets to be
3+
assigned to a vector of size 1
4+
(pull request #349, discussed in issue #345)
5+
* inst/unitTests/cpp/Subset.cpp: Add unit test for above
6+
* inst/unitTests/runit.subset.R: Ditto
7+
18
2015-08-31 Dirk Eddelbuettel <[email protected]>
29

310
* DESCRIPTION (Version, Date): Roll Version and Date

inst/include/Rcpp/vector/Subsetter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class SubsetProxy {
5252
template <int OtherRTYPE, template <class> class OtherStoragePolicy>
5353
SubsetProxy& operator=(const Vector<OtherRTYPE, OtherStoragePolicy>& other) {
5454
int n = other.size();
55-
if (indices_n != n) stop("index error");
55+
5656
if (n == 1) {
57-
for (int i=0; i < n; ++i) {
57+
for (int i=0; i < indices_n; ++i) {
5858
lhs[ indices[i] ] = other[0];
5959
}
6060
} else if (n == indices_n) {
@@ -65,7 +65,7 @@ class SubsetProxy {
6565
stop("index error");
6666
}
6767
return *this;
68-
}
68+
}
6969

7070
// Enable e.g. x[y] = 1;
7171
// TODO: std::enable_if<primitive> with C++11

inst/unitTests/cpp/Subset.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,13 @@ NumericVector subset_assign_subset5(NumericVector x) {
9292
return y;
9393
}
9494

95+
// [[Rcpp::export]]
96+
NumericVector subset_assign_vector_size_1(NumericVector x, int i) {
97+
NumericVector y(1);
98+
y[0]=i;
99+
100+
x[x < 4] = y;
101+
102+
return x;
103+
}
104+

inst/unitTests/runit.subset.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ if (.runThisTest) {
8484
checkIdentical(subset_assign_subset4(seq(2, 4, 0.2)), c(2L,2L,2L,2L,2L,3L,0L,0L,0L,0L,0L))
8585

8686
checkException(subset_assign_subset5(1:6), msg = "index error")
87+
88+
checkIdentical(subset_assign_vector_size_1(1:6,7), c(7,7,7,4,5,6))
8789
}
8890

8991
}

0 commit comments

Comments
 (0)