Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add is_sparse_numeric #62

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(is_sparse_character)
export(is_sparse_double)
export(is_sparse_integer)
export(is_sparse_logical)
export(is_sparse_numeric)
export(is_sparse_vector)
export(sparse_character)
export(sparse_default)
Expand Down
20 changes: 17 additions & 3 deletions R/type-predicates.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#' @details
#' `is_sparse_vector()` is a general function that detects any type of sparse
#' vector created with this package. `is_sparse_double()`,
#' `is_sparse_integer()`, and `is_sparse_character()` are more specific
#' functions that only detects the type.
#' `is_sparse_integer()`, `is_sparse_character()`, and `is_sparse_logical()` are
#' more specific functions that only detects the type. `is_sparse_numeric()`
#' matches both sparse integers and doubles.
#'
#' @examples
#' x_sparse <- sparse_double(c(pi, 5, 0.1), c(2, 5, 10), 10)
Expand Down Expand Up @@ -47,7 +48,20 @@ is_sparse_vector <- function(x) {

res %in% valid
}


#' @rdname type-predicates
#' @export
is_sparse_numeric <- function(x) {
res <- .Call(ffi_extract_altrep_class, x)
if (is.null(res)) {
return(FALSE)
}

res <- as.character(res[[1]])

res == "altrep_sparse_double" || res == "altrep_sparse_integer"
}

#' @rdname type-predicates
#' @export
is_sparse_double <- function(x) {
Expand Down
8 changes: 6 additions & 2 deletions man/type-predicates.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test-type-predicates.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ test_that("is_sparse_vector works", {
expect_false(is_sparse_vector(NULL))
})

test_that("is_sparse_numeric works", {
expect_true(is_sparse_numeric(sparse_double(1, 1, 1)))
expect_true(is_sparse_numeric(sparse_integer(1, 1, 1)))

expect_false(is_sparse_numeric(c(1, 1, 1)))
expect_false(is_sparse_numeric(1:10))
expect_false(is_sparse_numeric(NULL))
})

test_that("is_sparse_double works", {
expect_true(is_sparse_double(sparse_double(1, 1, 1)))

Expand All @@ -30,3 +39,11 @@ test_that("is_sparse_character works", {
expect_false(is_sparse_character(1:10))
expect_false(is_sparse_character(NULL))
})

test_that("is_sparse_logical works", {
expect_true(is_sparse_logical(sparse_logical(TRUE, 1, 1)))

expect_false(is_sparse_logical(c(1, 1, 1)))
expect_false(is_sparse_logical(1:10))
expect_false(is_sparse_logical(NULL))
})
Loading