Skip to content

Commit 349c8a6

Browse files
committed
Some tweaks to the subset class (allow const/ref, add operator SEXP
1 parent 041a4c0 commit 349c8a6

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

inst/include/Rcpp/vector/Subsetter.h

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,12 @@ class SubsetProxy {
103103
return *this;
104104
}
105105

106-
template <int OtherRTYPE, template <class> class OtherStoragePolicy>
107-
operator Vector<OtherRTYPE, OtherStoragePolicy>() const {
108-
int n = indices.size();
109-
Vector<OtherRTYPE, OtherStoragePolicy> output = no_init(n);
110-
for (int i=0; i < n; ++i) {
111-
output[i] = lhs[ indices[i] ];
112-
}
113-
SEXP names = Rf_getAttrib(lhs, R_NamesSymbol);
114-
if (!Rf_isNull(names)) {
115-
Shield<SEXP> out_names( Rf_allocVector(STRSXP, n) );
116-
for (int i=0; i < n; ++i) {
117-
SET_STRING_ELT(out_names, i, STRING_ELT(names, indices[i]));
118-
}
119-
Rf_setAttrib(output, R_NamesSymbol, out_names);
120-
}
121-
Rf_copyMostAttrib(lhs, output);
122-
return output;
106+
operator Vector<RTYPE, StoragePolicy>() const {
107+
return get_vec();
108+
}
109+
110+
operator SEXP() const {
111+
return wrap( get_vec() );
123112
}
124113

125114
private:
@@ -184,6 +173,24 @@ class SubsetProxy {
184173
}
185174
}
186175
}
176+
177+
Vector<RTYPE, StoragePolicy> get_vec() const {
178+
int n = indices.size();
179+
Vector<RTYPE, StoragePolicy> output = no_init(n);
180+
for (int i=0; i < n; ++i) {
181+
output[i] = lhs[ indices[i] ];
182+
}
183+
SEXP names = Rf_getAttrib(lhs, R_NamesSymbol);
184+
if (!Rf_isNull(names)) {
185+
Shield<SEXP> out_names( Rf_allocVector(STRSXP, n) );
186+
for (int i=0; i < n; ++i) {
187+
SET_STRING_ELT(out_names, i, STRING_ELT(names, indices[i]));
188+
}
189+
Rf_setAttrib(output, R_NamesSymbol, out_names);
190+
}
191+
Rf_copyMostAttrib(lhs, output);
192+
return output;
193+
}
187194

188195
LHS_t& lhs;
189196
const RHS_t& rhs;

inst/include/Rcpp/vector/Vector.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ class Vector :
325325
);
326326
}
327327

328+
template <int RHS_RTYPE, bool RHS_NA, typename RHS_T>
329+
const SubsetProxy<RTYPE, StoragePolicy, RHS_RTYPE, RHS_NA, RHS_T>
330+
operator[](const VectorBase<RHS_RTYPE, RHS_NA, RHS_T>& rhs) const {
331+
return SubsetProxy<RTYPE, StoragePolicy, RHS_RTYPE, RHS_NA, RHS_T>(
332+
const_cast< Vector<RTYPE, StoragePolicy>& >(*this),
333+
rhs
334+
);
335+
}
336+
328337
Vector& sort(){
329338
typename traits::storage_type<RTYPE>::type* start = internal::r_vector_start<RTYPE>( Storage::get__() ) ;
330339
std::sort(

inst/unitTests/cpp/Subset.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ NumericVector subset_test_assign(NumericVector x) {
5151
x[ x > 0 ] = 0;
5252
return x;
5353
}
54+
55+
// [[Rcpp::export]]
56+
NumericVector subset_test_constref(NumericVector const& x, IntegerVector const& y) {
57+
return x[y];
58+
}

0 commit comments

Comments
 (0)