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

use altrep in vctrs class #25

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(format,sparse_vector)
S3method(vec_arith,sparse_vector)
S3method(vec_math,sparse_vector)
S3method(vec_ptype_abbr,sparse_vector)
export(new_sparse_real)
export(new_sparse_vector)
export(sparse_to_tibble)
Expand Down
89 changes: 41 additions & 48 deletions R/vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
#' @examples
#' new_sparse_vector(1, 4, 10)
new_sparse_vector <- function(values, positions, length) {
vec_assert(values, double())
vec_assert(values, double())
vec_assert(values, double())
res <- seq(length)
res <- new_sparse_real(values, positions, length)
res <- new_vctr(res, class = "sparse_vector")
attr(res, "values") <- values
attr(res, "positions") <- positions
res
}

Expand All @@ -34,58 +29,56 @@ new_sparse_vector <- function(values, positions, length) {
#' @examples
#' sparse_vector(1, 4, 10)
sparse_vector <- function(values = double(),
positions = double(),
length = double()) {
positions = integer(),
length = integer()) {
new_sparse_vector(values, positions, length)
}

#' @export
format.sparse_vector <- function(x, ...) {
out <- rep(0, length(x))
out[attr(x, "positions")] <- attr(x, "values")
out
vec_data(x)
}

#' @export
vec_ptype_abbr.sparse_vector <- function(x, ...) {
"spvtr"
}
#' @export
# vec_ptype_abbr.sparse_vector <- function(x, ...) {
# "spvtr"
# }

#' @export
vec_math.sparse_vector <- function(.fn, .x, ...) {
switch(
.fn,
sum = sum(attr(.x, "values")),
prod = ifelse(length(attr(.x, "values")) != length(.x), 0, prod(attr(.x, "values"))),
mean = sum(attr(.x, "values")) / length(.x),
vec_math_base(.fn, .x, ...)
)
}

sparse_vector_addition <- function(x, y) {
res <- y

values <- attr(x, "values")
positions <- attr(x, "positions")

overlap <- positions %in% attr(res, "positions")
# vec_math.sparse_vector <- function(.fn, .x, ...) {
# switch(
# .fn,
# sum = sum(attr(.x, "values")),
# prod = ifelse(length(attr(.x, "values")) != length(.x), 0, prod(attr(.x, "values"))),
# mean = sum(attr(.x, "values")) / length(.x),
# vec_math_base(.fn, .x, ...)
# )
#}

res_loc <- match(positions[overlap], attr(res, "positions"))

attr(res, "values")[res_loc] <- attr(res, "values")[res_loc] + values[overlap]

attr(res, "values") <- c(attr(res, "values"), values[!overlap])

attr(res, "positions") <- c(attr(res, "positions"), positions[!overlap])
res
}
#sparse_vector_addition <- function(x, y) {
# res <- y
#
# values <- attr(x, "values")
# positions <- attr(x, "positions")
#
# overlap <- positions %in% attr(res, "positions")
#
# res_loc <- match(positions[overlap], attr(res, "positions"))
#
# attr(res, "values")[res_loc] <- attr(res, "values")[res_loc] + values[overlap]
#
# attr(res, "values") <- c(attr(res, "values"), values[!overlap])
#
# attr(res, "positions") <- c(attr(res, "positions"), positions[!overlap])
# res
#}

#' @export
#' @method vec_arith sparse_vector
vec_arith.sparse_vector <- function(op, x, y, ...) {
switch(
op,
"+" = sparse_vector_addition(x, y),
stop_incompatible_op(op, x, y)
)
}
#vec_arith.sparse_vector <- function(op, x, y, ...) {
# switch(
# op,
# "+" = sparse_vector_addition(x, y),
# stop_incompatible_op(op, x, y)
# )
#}
2 changes: 1 addition & 1 deletion man/sparse_vector.Rd

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

Loading