Skip to content

Commit

Permalink
refrain from using std::string which seems to f**k up encoding. tidyv…
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Jul 9, 2015
1 parent 7e784e2 commit e6b1ad5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ BEGIN_RCPP
END_RCPP
}
// combine_vars
SEXP combine_vars(std::vector<std::string> vars, ListOf<IntegerVector> xs);
SEXP combine_vars(CharacterVector vars, ListOf<IntegerVector> xs);
RcppExport SEXP dplyr_combine_vars(SEXP varsSEXP, SEXP xsSEXP) {
BEGIN_RCPP
Rcpp::RObject __result;
Rcpp::RNGScope __rngScope;
Rcpp::traits::input_parameter< std::vector<std::string> >::type vars(varsSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type vars(varsSEXP);
Rcpp::traits::input_parameter< ListOf<IntegerVector> >::type xs(xsSEXP);
__result = Rcpp::wrap(combine_vars(vars, xs));
return __result;
Expand Down
28 changes: 12 additions & 16 deletions src/combine_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int vector_sign(IntegerVector x) {
class VarList {

std::vector<int> out_indx;
std::vector<std::string> out_name;
std::vector<String> out_name;

int find(int i) {
std::vector<int>::iterator pos = std::find(out_indx.begin(), out_indx.end(), i);
Expand All @@ -37,10 +37,7 @@ class VarList {
}

public:
VarList(int n) {
out_indx = std::vector<int>();
out_name = std::vector<std::string>();

VarList(int n) : out_indx(), out_name() {
out_indx.reserve(n);
out_name.reserve(n);
}
Expand All @@ -49,7 +46,7 @@ class VarList {
return find(i) != -1;
}

void add(int i, std::string name) {
void add(int i, String name) {
out_indx.push_back(i);
out_name.push_back(name);
}
Expand All @@ -60,7 +57,7 @@ class VarList {
out_indx.erase(out_indx.begin() + pos);
out_name.erase(out_name.begin() + pos);
}
void update(int i, std::string name) {
void update(int i, String name) {
int pos = find(i);
if (pos == -1) {
add(i, name);
Expand All @@ -72,28 +69,27 @@ class VarList {
operator SEXP() {
IntegerVector out(out_indx.begin(), out_indx.end());
CharacterVector out_names(out_name.begin(), out_name.end());
out.attr("names") = out_names;

out.names() = out_names;
return out;
}
};

// [[Rcpp::export]]
SEXP combine_vars(std::vector<std::string> vars, ListOf<IntegerVector> xs) {
SEXP combine_vars(CharacterVector vars, ListOf<IntegerVector> xs) {
VarList selected(vars.size());

if (xs.size() == 0)
return IntegerVector::create();

// Workaround bug in ListOf<>; can't access attributes
SEXP raw_names = Rf_getAttrib(xs, Rf_mkString("names"));
std::vector<std::string> xs_names;
CharacterVector xs_names;
if (raw_names == R_NilValue) {
xs_names = std::vector<std::string>(xs.size(), "");
xs_names = CharacterVector(xs.size(), "" );
} else {
xs_names = as< std::vector<std::string> >(raw_names);
xs_names = raw_names ;
}

// If first component is negative, pre-fill with existing vars
if (vector_sign(xs[0]) == -1) {
for (int j = 0; j < vars.size(); ++j) {
Expand Down Expand Up @@ -125,7 +121,7 @@ SEXP combine_vars(std::vector<std::string> vars, ListOf<IntegerVector> xs) {
}
}
} else if (has_names) {
std::vector<std::string> names = as<std::vector<std::string> >(x.attr("names"));
CharacterVector names = x.names() ;
for (int j = 0; j < x.size(); ++j) {
selected.update(x[j], names[j]);
}
Expand Down

0 comments on commit e6b1ad5

Please sign in to comment.