Skip to content

Commit

Permalink
finish off enumerate
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Oct 5, 2024
1 parent 937049e commit 8076917
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
62 changes: 60 additions & 2 deletions inst/ext/renv.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ static SEXP renv_dependencies_recurse(SEXP object,
#define ENUMERATE_DISPATCH_IMPL(__TYPE__, __GET__, __SET__, __EXTRACT__, __COERCE__) \
do \
{ \
\
R_xlen_t n = Rf_xlength(x); \
SEXP output = PROTECT(Rf_allocVector(__TYPE__, n)); \
Rf_setAttrib(output, R_NamesSymbol, names); \
Expand Down Expand Up @@ -177,7 +176,6 @@ static SEXP renv_dependencies_recurse(SEXP object,
#define ENUMERATE_DISPATCH_ENVSXP_IMPL(__TYPE__, __EXTRACT__, __SET__, __COERCE__) \
do \
{ \
\
R_xlen_t n = Rf_xlength(names); \
SEXP output = PROTECT(Rf_allocVector(__TYPE__, n)); \
Rf_setAttrib(output, R_NamesSymbol, names); \
Expand Down Expand Up @@ -214,6 +212,66 @@ static SEXP enumerate(SEXP x,
switch (TYPEOF(x))
{

case INTSXP:
{
SEXP names = PROTECT(Rf_getAttrib(x, R_NamesSymbol));
switch (TYPEOF(type))
{
case INTSXP:
ENUMERATE_DISPATCH(INTSXP, INTSXP);
case REALSXP:
ENUMERATE_DISPATCH(INTSXP, REALSXP);
case LGLSXP:
ENUMERATE_DISPATCH(INTSXP, LGLSXP);
case STRSXP:
ENUMERATE_DISPATCH(INTSXP, INTSXP);
case NILSXP:
ENUMERATE_DISPATCH(INTSXP, VECSXP);
}

break;
}

case REALSXP:
{
SEXP names = PROTECT(Rf_getAttrib(x, R_NamesSymbol));
switch (TYPEOF(type))
{
case INTSXP:
ENUMERATE_DISPATCH(REALSXP, REALSXP);
case REALSXP:
ENUMERATE_DISPATCH(REALSXP, REALSXP);
case LGLSXP:
ENUMERATE_DISPATCH(REALSXP, LGLSXP);
case STRSXP:
ENUMERATE_DISPATCH(REALSXP, REALSXP);
case NILSXP:
ENUMERATE_DISPATCH(REALSXP, VECSXP);
}

break;
}

case LGLSXP:
{
SEXP names = PROTECT(Rf_getAttrib(x, R_NamesSymbol));
switch (TYPEOF(type))
{
case INTSXP:
ENUMERATE_DISPATCH(LGLSXP, LGLSXP);
case REALSXP:
ENUMERATE_DISPATCH(LGLSXP, LGLSXP);
case LGLSXP:
ENUMERATE_DISPATCH(LGLSXP, LGLSXP);
case STRSXP:
ENUMERATE_DISPATCH(LGLSXP, LGLSXP);
case NILSXP:
ENUMERATE_DISPATCH(LGLSXP, VECSXP);
}

break;
}

case STRSXP:
{

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-enumerate.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@

test_that("enumerate() handles identity maps", {

data <- list(
as.integer(1:10),
as.numeric(1:10),
as.logical(1:10),
letters[1:10]
)

for (i in seq_along(data)) {
expect_identical(
enumerate(data[[i]], function(key, value) value, FUN.VALUE = data[[i]]),
data[[i]]
)
}

})

test_that("enumerate() works as expected", {

zip <- function(key, value) list(key, value)
Expand Down

0 comments on commit 8076917

Please sign in to comment.