Skip to content

Commit

Permalink
Merge pull request #32 from EmilHvitfeldt/better-interface
Browse files Browse the repository at this point in the history
make user facing function not start with new_
  • Loading branch information
EmilHvitfeldt authored May 7, 2024
2 parents 4e4ef4d + 7777410 commit a3af0c4
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 134 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(new_sparse_double)
export(sparse_double)
import(rlang)
useDynLib(sparsevctrs, .registration = TRUE)
16 changes: 14 additions & 2 deletions R/altrep.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Create sparse numeric vector
#' Create sparse double vector
#'
#' @param value Numeric vector, values of non-zero entries.
#' @param position integer vector, indices of non-zero entries.
Expand All @@ -11,8 +11,16 @@
#' setting `options("sparsevctrs.verbose_materialize" = TRUE)` will print a
#' message each time a sparse vector has been forced to materialize.
#'
#' @examples
#' sparse_double(numeric(), integer(), 10)
#'
#' sparse_double(c(pi, 5, 0.1), c(2, 5, 10), 10)
#'
#' str(
#' sparse_double(c(pi, 5, 0.1), c(2, 5, 10), 1000000000)
#' )
#' @export
new_sparse_double <- function(value, position, length) {
sparse_double <- function(value, position, length) {
check_number_whole(length, min = 0)
if (!is.integer(length)) {
length <- as.integer(length)
Expand Down Expand Up @@ -127,6 +135,10 @@ new_sparse_double <- function(value, position, length) {
)
}

new_sparse_double(value, position, length)
}

new_sparse_double <- function(value, position, length) {
x <- list(
val = value,
pos = position,
Expand Down
6 changes: 3 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ remotes::install_github("emilhvitfeldt/sparsevctrs")
```{r}
library(sparsevctrs)
x <- new_sparse_vector(4, 7, 10)
x <- sparse_double(4, 7, 10)
x
sum(x)
new_sparse_vector(4, 7, 10) + new_sparse_vector(3, 2, 10)
sparse_double(4, 7, 10) + sparse_double(3, 2, 10)
```

This class is compatible with tibbles

```{r}
library(tibble)
tibble(x = sample(1:10), y = new_sparse_vector(1, 7, 10))
tibble(x = sample(1:10), y = sparse_double(1, 7, 10))
```

35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,35 @@ remotes::install_github("emilhvitfeldt/sparsevctrs")
``` r
library(sparsevctrs)

x <- new_sparse_vector(4, 7, 10)
x <- sparse_double(4, 7, 10)

x
#> <sparse_vector[10]>
#> [1] 0 0 0 0 0 0 4 0 0 0
sum(x)
#> [1] 4

new_sparse_vector(4, 7, 10) + new_sparse_vector(3, 2, 10)
#> <sparse_vector[10]>
#> [1] 0 3 0 0 0 0 4 0 0 0
sparse_double(4, 7, 10) + sparse_double(3, 2, 10)
#> [1] 3.456532e-314 2.847320e-314 2.714492e-314 2.927164e-314 4.940656e-323
#> [6] 0.000000e+00 0.000000e+00 3.000000e+00 0.000000e+00 0.000000e+00
```

This class is compatible with tibbles

``` r
library(tibble)

tibble(x = sample(1:10), y = new_sparse_vector(1, 7, 10))
tibble(x = sample(1:10), y = sparse_double(1, 7, 10))
#> # A tibble: 10 × 2
#> x y
#> <int> <spvtr>
#> 1 10 0
#> 2 6 0
#> 3 5 0
#> 4 4 0
#> 5 1 0
#> 6 8 0
#> 7 2 1
#> 8 7 0
#> 9 9 0
#> 10 3 0
#> x y
#> <int> <dbl>
#> 1 10 0
#> 2 6 0
#> 3 5 0
#> 4 4 0
#> 5 1 0
#> 6 8 0
#> 7 2 1
#> 8 7 0
#> 9 9 0
#> 10 3 0
```
19 changes: 14 additions & 5 deletions man/new_sparse_double.Rd → man/sparse_double.Rd

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

Loading

0 comments on commit a3af0c4

Please sign in to comment.