Skip to content

Commit 6ff6619

Browse files
committed
Merge pull request #432 from thirdwing/subsetter
fix the error in subset under gc. close #431
2 parents b112a67 + cf89377 commit 6ff6619

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2016-01-30 Qiang Kou <[email protected]>
2+
3+
* inst//include/Rcpp/vector/Subsetter.h: fix the error under gc
4+
* inst/unitTests/cpp/Vector.cpp : Add tests
5+
* inst/unitTests/runit.Vector.R : Idem
6+
17
2016-01-29 Dirk Eddelbuettel <[email protected]>
28

39
* vignettes/Rcpp-FAQ.Rnw (Rcpp): Add new entry about required TeXlive

inst/include/Rcpp/vector/Subsetter.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ class SubsetProxy {
144144
#endif
145145

146146
void get_indices( traits::identity< traits::int2type<INTSXP> > t ) {
147-
indices = INTEGER(rhs);
148-
indices_n = rhs_n;
149-
check_indices(indices, rhs_n, lhs_n);
147+
indices.reserve(rhs_n);
148+
int* ptr = INTEGER(rhs);
149+
check_indices(ptr, rhs_n, lhs_n);
150+
for (int i=0; i < rhs_n; ++i) {
151+
indices.push_back( rhs[i] );
152+
}
153+
indices_n = rhs_n;
150154
}
151155

152156
void get_indices( traits::identity< traits::int2type<REALSXP> > t ) {
@@ -215,13 +219,7 @@ class SubsetProxy {
215219
int lhs_n;
216220
int rhs_n;
217221

218-
// we want to reuse the indices if an IntegerVector is passed in; otherwise,
219-
// we construct a std::vector<int> to hold the indices
220-
typename traits::if_<
221-
RHS_RTYPE == INTSXP,
222-
int*,
223-
std::vector<int>
224-
>::type indices;
222+
std::vector<int> indices;
225223

226224
// because of the above, we keep track of the size
227225
int indices_n;

inst/unitTests/cpp/Vector.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,8 @@ String vec_print_integer(IntegerVector v) {
823823
buf << v;
824824
return buf.str();
825825
}
826+
827+
// [[Rcpp::export]]
828+
IntegerVector vec_subset(IntegerVector x, IntegerVector y) {
829+
return x[y - 1];
830+
}

inst/unitTests/runit.Vector.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,5 +720,14 @@ if (.runThisTest) {
720720
s <- vec_print_character(v)
721721
checkEquals(s, '"a" "b" "c" "d"')
722722
}
723+
724+
test.IntegerVector.subset.under.gc <- function() {
725+
x <- 1:1E6
726+
y <- 1:1E6
727+
gctorture(TRUE)
728+
z <- vec_subset(x, y)
729+
gctorture(FALSE)
730+
checkEquals(x[y], z)
731+
}
723732
}
724733

0 commit comments

Comments
 (0)