Skip to content

Commit

Permalink
Added int64 methods and fixed a small bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Sep 21, 2024
1 parent 12308b7 commit 0947476
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Generated by roxygen2: do not edit by hand

S3method(base::as.character,vctrs_rcrd)
S3method(collapse::fmean,integer64)
S3method(collapse::fmedian,integer64)
S3method(collapse::fnobs,integer64)
S3method(collapse::fnth,integer64)
S3method(collapse::fsd,integer64)
S3method(collapse::funique,POSIXlt)
S3method(collapse::funique,vctrs_rcrd)
S3method(collapse::fvar,integer64)
S3method(cut,integer64)
S3method(is_na,POSIXlt)
S3method(is_na,data.frame)
Expand Down
2 changes: 0 additions & 2 deletions R/factors.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ factor_ <- function(
if (inherits(lvls, "data.frame")){
fct_lvls <- do.call(paste, c(lvls, list(sep = "_")))
} else if (is_int64){
# fct_lvls <- formatC(lvls, format = "f", drop0trailing = TRUE)
# fct_lvls <- format(lvls, scientific = FALSE, trim = TRUE)
fct_lvls <- cpp_format_numeric_as_int64(lvls)
} else {
fct_lvls <- as.character(lvls)
Expand Down
25 changes: 25 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ funique.POSIXlt <- function(x, sort = FALSE, ...){
out
}

#' @exportS3Method collapse::fmean
fmean.integer64 <- function(x, ...){
collapse::fmean(cpp_int64_to_double(x), ...)
}
#' @exportS3Method collapse::fmedian
fmedian.integer64 <- function(x, ...){
collapse::fmedian(cpp_int64_to_double(x), ...)
}
#' @exportS3Method collapse::fvar
fvar.integer64 <- function(x, ...){
collapse::fvar(cpp_int64_to_double(x), ...)
}
#' @exportS3Method collapse::fsd
fsd.integer64 <- function(x, ...){
collapse::fsd(cpp_int64_to_double(x), ...)
}
#' @exportS3Method collapse::fnth
fnth.integer64 <- function(x, ...){
collapse::fnth(cpp_int64_to_double(x), ...)
}
#' @exportS3Method collapse::fnobs
fnobs.integer64 <- function(x, ...){
collapse::fnobs(cpp_int64_to_double(x), ...)
}

n_dots <- function(...){
nargs()
}
Expand Down
4 changes: 3 additions & 1 deletion src/attrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ SEXP cpp_set_rm_attr(SEXP x, SEXP which) {
[[cpp11::register]]
SEXP cpp_set_add_attributes(SEXP x, SEXP attributes, bool add) {
int NP = 0;
if (Rf_isNull(attributes)){
if (Rf_isNull(attributes) ||
// is.null or empty list?
(TYPEOF(attributes) == VECSXP && Rf_length(attributes) == 0)){
if (add){
return x;
} else {
Expand Down
33 changes: 32 additions & 1 deletion src/scalars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@ SEXP cpp_val_replace(SEXP x, SEXP value, SEXP replace, bool recursive){
break;
}
case REALSXP: {
if (is_int64(x)){
SEXP temp = Rf_protect(Rf_allocVector(REALSXP, 0)); ++NP;
long long *p_out = INTEGER64_PTR(temp);

if (!is_int64(value)){
Rf_protect(value = Rf_coerceVector(value, REALSXP)); ++NP;
Rf_protect(value = cpp_numeric_to_int64(value)); ++NP;
}
if (!is_int64(replace)){
Rf_protect(replace = Rf_coerceVector(replace, REALSXP)); ++NP;
Rf_protect(replace = cpp_numeric_to_int64(replace)); ++NP;
}
long long val = INTEGER64_PTR(value)[0];
long long repl = INTEGER64_PTR(replace)[0];
long long *p_x = INTEGER64_PTR(x);
for (R_xlen_t i = 0; i < n; ++i){
eq = p_x[i] == val;
if (!any_eq && eq){
any_eq = true;
out = Rf_protect(Rf_duplicate(x)); ++NP;
// Change where pointer is pointing to
p_out = INTEGER64_PTR(out);
}
if (eq) p_out[i] = repl;
}
// Make sure to return x if there were no values to replace
if (!any_eq){
out = Rf_protect(x); ++NP;
}
} else {
SEXP temp = Rf_protect(Rf_allocVector(REALSXP, 0)); ++NP;
double *p_out = REAL(temp);
Rf_protect(value = Rf_coerceVector(value, REALSXP)); ++NP;
Expand Down Expand Up @@ -219,7 +249,8 @@ SEXP cpp_val_replace(SEXP x, SEXP value, SEXP replace, bool recursive){
out = Rf_protect(x); ++NP;
}
}
break;
}
break;
}
case STRSXP: {
Rf_protect(value = Rf_coerceVector(value, STRSXP)); ++NP;
Expand Down

0 comments on commit 0947476

Please sign in to comment.