Skip to content

Commit

Permalink
[dims] speed up non consecutive dims (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jun 28, 2024
1 parent 9a9f894 commit bad89ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/helper_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ Rcpp::CharacterVector needed_cells(const std::string& range) {
return Rcpp::wrap(cells);
}

bool has_cell(const std::string& str, const Rcpp::CharacterVector& vec) {
return std::find(vec.begin(), vec.end(), str) != vec.end();
bool has_cell(const std::string& str, const std::unordered_set<std::string>& vec) {
return vec.find(str) != vec.end();
}

// provide a basic rbindlist for lists of named characters
Expand All @@ -353,18 +353,20 @@ SEXP dims_to_df(Rcpp::IntegerVector rows, Rcpp::CharacterVector cols, Rcpp::Null
SET_VECTOR_ELT(df, i, Rcpp::CharacterVector(nn, NA_STRING));
}

if (has_filled) {
if (has_filled && fill) {

std::vector<std::string> flld = Rcpp::as<std::vector<std::string>>(filled.get());
std::unordered_set<std::string> flls(flld.begin(), flld.end());

// with has_filled we always have to run this loop
for (size_t i = 0; i < kk; ++i) {
Rcpp::CharacterVector cvec = Rcpp::as<Rcpp::CharacterVector>(df[i]);
std::string coli = Rcpp::as<std::string>(cols[i]);
for (size_t j = 0; j < nn; ++j) {
std::string cell = coli + std::to_string(rows[j]);
if (!has_cell(cell, filled.get()))
cvec[j] = "";
else if (fill)
if (has_cell(cell, flls))
cvec[j] = coli + std::to_string(rows[j]);
// else cvec[j] = "";
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,19 @@ test_that("wb_dims(from_dims) positioning works", {
expect_equal(exp, got)

})

test_that("dims are quick", {

ddims <- dims_to_dataframe(
"A1:D5000",
fill = TRUE
)

edims <- dims_to_dataframe(
"A1:A5000,B1:B5000,C1:C5000,D1:D5000",
fill = TRUE
)

expect_equal(ddims, edims)

})

0 comments on commit bad89ff

Please sign in to comment.