Skip to content

Commit

Permalink
add sparse_default()
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed May 10, 2024
1 parent 884e002 commit 578ec5a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(coerce_to_sparse_matrix)
export(coerce_to_sparse_tibble)
export(is_sparse_double)
export(is_sparse_vector)
export(sparse_default)
export(sparse_double)
export(sparse_positions)
export(sparse_values)
Expand Down
25 changes: 23 additions & 2 deletions R/extractors.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#' Information extraction from sparse vectors
#'
#' Extract positions and values from sparse vectors without the need to
#' materialize vector.
#' Extract positions, values, and default from sparse vectors without the need
#' to materialize vector.
#'
#' @details
#'
#' `sparse_default()` returns `NA` when applied to non-sparse vectors. This is
#' done to have an indicator of non-sparsity.
#'
#' @param x vector to be extracted from.
#'
Expand All @@ -14,9 +19,14 @@
#'
#' sparse_positions(x_sparse)
#' sparse_values(x_sparse)
#' sparse_default(x_sparse)
#'
#' sparse_positions(x_dense)
#' sparse_values(x_dense)
#' sparse_default(x_dense)
#'
#' x_sparse_3 <- sparse_double(c(pi, 5, 0.1), c(2, 5, 10), 10, default = 3)
#' sparse_default(x_sparse_3)
#' @name extractors
NULL

Expand All @@ -39,3 +49,14 @@ sparse_values <- function(x) {

.Call(ffi_altrep_sparse_values, x)
}


#' @rdname extractors
#' @export
sparse_default <- function(x) {
if (!is_sparse_vector(x)) {
return(NA)
}

.Call(ffi_altrep_sparse_default, x)
}
15 changes: 13 additions & 2 deletions man/extractors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static const R_CallMethodDef CallEntries[] = {
1},
{"ffi_altrep_sparse_positions", (DL_FUNC) &ffi_altrep_sparse_positions, 1},
{"ffi_altrep_sparse_values", (DL_FUNC) &ffi_altrep_sparse_values, 1},
{"ffi_altrep_sparse_default", (DL_FUNC) &ffi_altrep_sparse_default, 1},
{"ffi_extract_altrep_class", (DL_FUNC) &ffi_extract_altrep_class, 1},
{NULL, NULL, 0}};

Expand Down
5 changes: 5 additions & 0 deletions src/sparse-extractors.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ SEXP ffi_altrep_sparse_values(SEXP x) {
SEXP out = extract_val(x);
return out;
}

SEXP ffi_altrep_sparse_default(SEXP x) {
SEXP out = extract_default(x);
return out;
}
2 changes: 2 additions & 0 deletions src/sparse-extractors.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ SEXP ffi_altrep_sparse_positions(SEXP x);

SEXP ffi_altrep_sparse_values(SEXP x);

SEXP ffi_altrep_sparse_default(SEXP x);

#endif
19 changes: 19 additions & 0 deletions tests/testthat/test-extractors.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,22 @@ test_that("sparse_values works with numeric vectors", {
101:200
)
})

test_that("sparse_default works with altrep_sparse_double", {
expect_identical(
sparse_default(sparse_double(1, 5, 10)),
0
)

expect_identical(
sparse_default(sparse_double(1, 5, 10, default = 11)),
11
)
})

test_that("sparse_values works with numeric vectors", {
expect_identical(
sparse_default(c(1, 6, 4, 2)),
NA
)
})

0 comments on commit 578ec5a

Please sign in to comment.