Skip to content

Commit

Permalink
Remove if(FALSE) regions (#138)
Browse files Browse the repository at this point in the history
* convert if(FALSE) region to tests

* More unreachable code

* nolint false positive
  • Loading branch information
MichaelChirico authored Jan 21, 2025
1 parent cb4552b commit 297e407
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 254 deletions.
3 changes: 1 addition & 2 deletions .lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ linters = all_linters(
spaces_left_parentheses_linter = NULL,
todo_comment_linter = NULL,
undesirable_function_linter = NULL,
undesirable_operator_linter = NULL,
unreachable_code_linter = NULL
undesirable_operator_linter = NULL
)
2 changes: 1 addition & 1 deletion R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ nunique.integer64 <- function(x, ...){
has.cache <- FALSE
else if (exists("nunique", envir=env, inherits=FALSE))
return(get("nunique", envir=env, inherits=FALSE))
else
else # nolint: unreachable_code_linter. TODO(r-lib/lintr#2710): Re-enable.
has.cache <- TRUE
if (is.sorted(x)){
ret <- .Call(C_r_ram_integer64_sortnut
Expand Down
85 changes: 0 additions & 85 deletions R/hash64.R
Original file line number Diff line number Diff line change
Expand Up @@ -413,88 +413,3 @@ runif64 <- function(n, min=lim.integer64()[1L], max=lim.integer64()[2L], replace
}
ret
}

# nocov start
if (FALSE){

require(bit64)
require(microbenchmark)
n <- 1000000L
print(microbenchmark(runif64(n, 1.0, n), times=20L))
for (m in c(1.0, 2.0, 4.0, 8.0, 16.0)){
print(microbenchmark(runif64(n, 1.0, n*m, replace=FALSE), times=20L))
print(microbenchmark(sample(n*m, n, replace=FALSE), times=20L))
}
print(microbenchmark(runif64(n, 1.0, replace=FALSE), times=20L))


library(bit64)
n <- 10000000L
x <- as.integer64(sample(n, n, TRUE))
t1 <- system.time({h <- hashmap(x)})[3L]
t2 <- system.time({value <- hashuni(h)})[3L]
t3 <- system.time({count <- hashtab(h)})[3L]
t4 <- system.time({ret1 <- list(values=value, counts=count)})[3L]
t1+t2+t3+t4
system.time({ret2 <- hashmaptab(x)})[3L]
identical(ret1,ret2)


x <- as.integer64(sample(n, n, TRUE))

system.time({
ret2 <- hashmaptab(x)
cv2 <- sum(ret2$counts[ret2$counts > 1.0])
})[3L]

system.time({
s <- clone(x)
na.count <- ramsort(s, has.na = TRUE, na.last = FALSE, decreasing = FALSE, stable = FALSE, optimize = "time")
cv <- .Call(C_r_ram_integer64_sortnut, x = s, PACKAGE = "bit64")[[2L]]
})

cv
cv2


nunique(x)
length(value)
length(count)
length(t1$value)
length(t1$count)
value
t1
count

s <- clone(x); o <- seq_along(x); ramsortorder(s, o)
t2 <- sortordertab(s,o)
length(s)
length(t2)




library(bit64)
n <- 1000000L
r <- runif64(n, lim.integer64()[1L], lim.integer64()[2L])
identical(r, as.integer64(as.bitstring(r)))
cbind(r,as.integer64(as.bitstring(r)))
cbind(as.bitstring(r),as.bitstring(as.integer64(as.bitstring(r))))

#sum(duplicated(r))
#table.integer64(r)
#range(r)
log2(abs(range(r)))

x <- seq(0.0, 1.0, 0.1)
y <- quantile.integer64(r, x)
z <- diff(y)
plot(log2(z), type="b",ylim=c(0.0, max(log2(z))))


n <- 10000000L
system.time(runif(n))
system.time(runif64(n))

}
# nocov end
137 changes: 0 additions & 137 deletions R/integer64.R
Original file line number Diff line number Diff line change
Expand Up @@ -543,51 +543,6 @@ all.equal.integer64 <- function (
} else msg
}

# nocov start
if (FALSE){
require(bit64)
a <- as.integer64(1L)
b <- 10L
oldClass(a) <- c("j", oldClass(a))
oldClass(b) <- c("j", oldClass(b))
all.equal(a,b)

a <- 1.0
b <- 10L
oldClass(a) <- c("j", oldClass(a))
oldClass(b) <- c("j", oldClass(b))
all.equal(a,b)

a <- as.integer64(9e17)
b <- 9e18
oldClass(a) <- c("j", oldClass(a))
oldClass(b) <- c("j", oldClass(b))
all.equal(a,b)

a <- as.integer64(9e18)
b <- 9e19
oldClass(a) <- c("j", oldClass(a))
oldClass(b) <- c("j", oldClass(b))
all.equal(a,b)

a <- as.integer64(c(1L,NA))
b <- as.integer(c(1L,NA))
all.equal(a,b)

a <- as.integer64(c(1L,NA))
b <- as.double(c(1L,NA))
all.equal(a,b)

a <- as.integer64(c(1.0,Inf))
b <- as.integer(c(1.0,Inf))
all.equal(a,b)

a <- as.integer64(c(1.0,Inf))
b <- as.double(c(1.0,Inf))
all.equal(a,b)
}
# nocov end

# TODO(R>=4.2.0): Consider restoring extptr.as.ref= to the signature.
#' @rdname identical.integer64
#' @exportS3Method identical integer64
Expand Down Expand Up @@ -647,46 +602,6 @@ plusclass <- function(class, whichclass){
whichclass
}

# nocov start
if (FALSE){
# version until 0.9-7
binattr <- function(e1, e2) {
d1 <- dim(e1)
d2 <- dim(e2)
n1 <- length(e1)
n2 <- length(e2)
if (length(d1)) {
if (length(d2)) {
if (!identical(dim(e1),dim(e2)))
stop("non-conformable arrays")
} else {
if (n2>n1)
stop("length(e2) does not match dim(e1)")
if (n1%%n2)
warning("length(e1) not a multiple length(e2)")
}
attributes(e1)
} else if (length(d2)) {
if (n1>n2)
stop("length(e1) does not match dim(n2)")
if (n2%%n1)
warning("length(e2) not a multiple length(e1)")
attributes(e2)
} else {
if (n1<n2) {
if (n2%%n1)
warning("length(e2) not a multiple length(e1)")
} else {
# nolint next: unnecessary_nesting_linter. Good parallelism.
if (n1%%n2)
warning("length(e1) not a multiple length(e2)")
}
attributes(e1)
}
}
}
# nocov end

# Version of Leonardo Silvestri
#' @rdname xor.integer64
#' @export
Expand Down Expand Up @@ -763,37 +678,6 @@ binattr <- function(e1, e2) {
# x
# }



# nocov start
if (FALSE){
x <- integer64(0L)
y <- integer64(0L)
#dim(x) <- c(2L,2L)
dim(y) <- c(0L,0L)
dimnames(y) <- list(character(0L),character(0L))
#dim(x) <- c(1L,4L)
#dim(y) <- c(4L,1L)

attr(x,"x") <- "x"
attr(y,"y") <- "y"

z <- x - y
z
dim(z)
dimnames(z)

z <- y - x
z
dim(z)
dimnames(z)

ret <- "integer64(0L)"
attributes(ret) <- list(dim = c(0L, 0L), class = character(0L), dimnames = list(NULL,NULL))

}
# nocov end

#' @rdname bit64-package
#' @param length length of vector using [integer()]
#' @return `integer64` returns a vector of 'integer64', i.e.,
Expand Down Expand Up @@ -988,27 +872,6 @@ str.integer64 <- function(object
invisible()
}

# nocov start
if (FALSE){
require(microbenchmark)
require(bit64)
x <- runif64(10000000L)
microbenchmark(x[TRUE], times=10L)
microbenchmark(x[NA], times=10L)
i <- seq_along(x)
i[1L] <- NA
microbenchmark(x[i], times=10L)
i <- rep(TRUE, length(x))
i[1L] <- NA
microbenchmark(x[i], times=10L)
i <- seq_along(x)
microbenchmark(x[i], times=10L)
i <- rep(TRUE, length(x))
microbenchmark(x[i], times=10L)

}
# nocov end

#' @rdname extract.replace.integer64
#' @export
`[.integer64` <- function(x, i, ...) {
Expand Down
29 changes: 0 additions & 29 deletions R/sort64.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,35 +476,6 @@ ramorder.integer64 <- function (x
#' @name sort.integer64
NULL

# nocov start
if (FALSE){
library(bit64)
x <- as.integer64(c(sample.int(10000000L),NA))
#system.time(sortcache(x))[3]
# system.time(ordercache(x))[3]
system.time(sortordercache(x))[3L]

# system.time(s <- sort(x, na.last=FALSE, decreasing=FALSE))[3]
# stopifnot(identical(s, {xs<-clone(x);ramsort(xs, na.last=FALSE, decreasing=FALSE);xs}))
# system.time(s <- sort(x, na.last=TRUE, decreasing=FALSE))[3]
# stopifnot(identical(s, {xs<-clone(x);ramsort(xs, na.last=TRUE, decreasing=FALSE);xs}))
# system.time(s <- sort(x, na.last=FALSE, decreasing=TRUE))[3]
# stopifnot(identical(s, {xs<-clone(x);ramsort(xs, na.last=FALSE, decreasing=TRUE);xs}))
# system.time(s <- sort(x, na.last=TRUE, decreasing=TRUE))[3]
# stopifnot(identical(s, {xs<-clone(x);ramsort(xs, na.last=TRUE, decreasing=TRUE);xs}))

system.time(o <- order.integer64(x, na.last=FALSE, decreasing=FALSE))[3L]
stopifnot(identical(o, {xo<-seq_along(x);ramorder(x, xo, na.last=FALSE, decreasing=FALSE);xo}))
system.time(o <- order.integer64(x, na.last=TRUE, decreasing=FALSE))[3L]
stopifnot(identical(o, {xo<-seq_along(x);ramorder(x, xo, na.last=TRUE, decreasing=FALSE);xo}))
system.time(o <- order.integer64(x, na.last=FALSE, decreasing=TRUE))[3L]
stopifnot(identical(o, {xo<-seq_along(x);ramorder(x, xo, na.last=FALSE, decreasing=TRUE);xo}))
system.time(o <- order.integer64(x, na.last=TRUE, decreasing=TRUE))[3L]
stopifnot(identical(o, {xo<-seq_along(x);ramorder(x, xo, na.last=TRUE, decreasing=TRUE);xo}))

}
# nocov end

#' @rdname sort.integer64
#' @export
sort.integer64 <- function(x
Expand Down
80 changes: 80 additions & 0 deletions tests/testthat/test-hash64.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,83 @@ test_that("runif64 behaves as expected", {
x = runif64(100000L, -5L, 5L)
expect_true(all(x >= -5L & x <= 5L))
})

# require(bit64)
# require(microbenchmark)
# n <- 1000000L
# print(microbenchmark(runif64(n, 1.0, n), times=20L))
# for (m in c(1.0, 2.0, 4.0, 8.0, 16.0)){
# print(microbenchmark(runif64(n, 1.0, n*m, replace=FALSE), times=20L))
# print(microbenchmark(sample(n*m, n, replace=FALSE), times=20L))
# }
# print(microbenchmark(runif64(n, 1.0, replace=FALSE), times=20L))


# library(bit64)
# n <- 10000000L
# x <- as.integer64(sample(n, n, TRUE))
# t1 <- system.time({h <- hashmap(x)})[3L]
# t2 <- system.time({value <- hashuni(h)})[3L]
# t3 <- system.time({count <- hashtab(h)})[3L]
# t4 <- system.time({ret1 <- list(values=value, counts=count)})[3L]
# t1+t2+t3+t4
# system.time({ret2 <- hashmaptab(x)})[3L]
# identical(ret1,ret2)


# x <- as.integer64(sample(n, n, TRUE))

# system.time({
# ret2 <- hashmaptab(x)
# cv2 <- sum(ret2$counts[ret2$counts > 1.0])
# })[3L]

# system.time({
# s <- clone(x)
# na.count <- ramsort(s, has.na = TRUE, na.last = FALSE, decreasing = FALSE, stable = FALSE, optimize = "time")
# cv <- .Call(C_r_ram_integer64_sortnut, x = s, PACKAGE = "bit64")[[2L]]
# })

# cv
# cv2


# nunique(x)
# length(value)
# length(count)
# length(t1$value)
# length(t1$count)
# value
# t1
# count

# s <- clone(x); o <- seq_along(x); ramsortorder(s, o)
# t2 <- sortordertab(s,o)
# length(s)
# length(t2)




# library(bit64)
# n <- 1000000L
# r <- runif64(n, lim.integer64()[1L], lim.integer64()[2L])
# identical(r, as.integer64(as.bitstring(r)))
# cbind(r,as.integer64(as.bitstring(r)))
# cbind(as.bitstring(r),as.bitstring(as.integer64(as.bitstring(r))))

# #sum(duplicated(r))
# #table.integer64(r)
# #range(r)
# log2(abs(range(r)))

# x <- seq(0.0, 1.0, 0.1)
# y <- quantile.integer64(r, x)
# z <- diff(y)
# plot(log2(z), type="b",ylim=c(0.0, max(log2(z))))


# n <- 10000000L
# system.time(runif(n))
# system.time(runif64(n))

Loading

0 comments on commit 297e407

Please sign in to comment.