Skip to content

Commit

Permalink
Merge pull request #349 from fplaza/subsetter-patch
Browse files Browse the repository at this point in the history
Allow assignment of size 1 with Subsetter
  • Loading branch information
eddelbuettel committed Sep 7, 2015
2 parents e4868b8 + 10abfdb commit 807cf8c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2015-09-01 Florian Plaza Onate <florian.plaza@jouy.inra.fr>
* inst/include/Rcpp/vector/Subsetter.h: Allow logical subsets to be
assigned to a vector of size 1
(pull request #349, discussed in issue #345)
* inst/unitTests/cpp/Subset.cpp: Add unit test for above
* inst/unitTests/runit.subset.R: Ditto

2015-08-31 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Roll Version and Date
Expand Down
6 changes: 3 additions & 3 deletions inst/include/Rcpp/vector/Subsetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class SubsetProxy {
template <int OtherRTYPE, template <class> class OtherStoragePolicy>
SubsetProxy& operator=(const Vector<OtherRTYPE, OtherStoragePolicy>& other) {
int n = other.size();
if (indices_n != n) stop("index error");

if (n == 1) {
for (int i=0; i < n; ++i) {
for (int i=0; i < indices_n; ++i) {
lhs[ indices[i] ] = other[0];
}
} else if (n == indices_n) {
Expand All @@ -65,7 +65,7 @@ class SubsetProxy {
stop("index error");
}
return *this;
}
}

// Enable e.g. x[y] = 1;
// TODO: std::enable_if<primitive> with C++11
Expand Down
10 changes: 10 additions & 0 deletions inst/unitTests/cpp/Subset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,13 @@ NumericVector subset_assign_subset5(NumericVector x) {
return y;
}

// [[Rcpp::export]]
NumericVector subset_assign_vector_size_1(NumericVector x, int i) {
NumericVector y(1);
y[0]=i;

x[x < 4] = y;

return x;
}

2 changes: 2 additions & 0 deletions inst/unitTests/runit.subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ if (.runThisTest) {
checkIdentical(subset_assign_subset4(seq(2, 4, 0.2)), c(2L,2L,2L,2L,2L,3L,0L,0L,0L,0L,0L))

checkException(subset_assign_subset5(1:6), msg = "index error")

checkIdentical(subset_assign_vector_size_1(1:6,7), c(7,7,7,4,5,6))
}

}

0 comments on commit 807cf8c

Please sign in to comment.