Skip to content

Commit c5f7a9f

Browse files
committed
Merge pull request #252 from RcppCore/feature/faster-strsxp-subsetting
faster strsxp subsetting (cc @romainfrancois, #250)
2 parents ada14a2 + 4b4cc30 commit c5f7a9f

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2015-02-06 Kevin Ushey <[email protected]>
2+
3+
* inst/include/Rcpp/vector/Subsetter.h: compare CHARSXP pointers
4+
rather than string contents in subsetter
5+
16
2015-02-03 JJ Allaire <[email protected]>
27

38
* src/attributes.cpp: Simplify generated attributes code for

inst/include/Rcpp/vector/Subsetter.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,17 @@ class SubsetProxy {
141141
indices.reserve(rhs_n);
142142
SEXP names = Rf_getAttrib(lhs, R_NamesSymbol);
143143
if (Rf_isNull(names)) stop("names is null");
144-
for (int i=0; i < rhs_n; ++i) {
145-
indices.push_back( find(names, CHAR( STRING_ELT(rhs, i) )) );
144+
SEXP* namesPtr = STRING_PTR(names);
145+
SEXP* rhsPtr = STRING_PTR(rhs);
146+
for (int i = 0; i < rhs_n; ++i) {
147+
SEXP* match = std::find(namesPtr, namesPtr + lhs_n, *(rhsPtr + i));
148+
if (match == namesPtr + lhs_n)
149+
stop("not found");
150+
indices.push_back(match - namesPtr);
146151
}
147152
indices_n = indices.size();
148153
}
149154

150-
int find(const RHS_t& names, const char* str) {
151-
for (int i=0; i < lhs_n; ++i) {
152-
if (strcmp( CHAR( STRING_ELT( names, i) ), str) == 0) return i;
153-
}
154-
stop("no name found");
155-
return -1;
156-
}
157-
158155
void get_indices( traits::identity< traits::int2type<LGLSXP> > t ) {
159156
indices.reserve(rhs_n);
160157
if (lhs_n != rhs_n) {

0 commit comments

Comments
 (0)