Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow assignment of size 1 with Subsetter #349

Merged
merged 5 commits into from
Sep 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}

}