Fast aggregation functions for bit vectors.
# S3 method for bit
all(x, range = NULL, …)
# S3 method for bit
any(x, range = NULL, …)
# S3 method for bit
min(x, range = NULL, …)
# S3 method for bit
max(x, range = NULL, …)
# S3 method for bit
range(x, range = NULL, …)
# S3 method for bit
sum(x, range = NULL, …)
# S3 method for bit
summary(object, range = NULL, …)
# S3 method for bitwhich
all(x, …)
# S3 method for bitwhich
any(x, …)
# S3 method for bitwhich
min(x, …)
# S3 method for bitwhich
max(x, …)
# S3 method for bitwhich
range(x, …)
# S3 method for bitwhich
sum(x, …)
# S3 method for bitwhich
summary(object, …)
# S3 method for ri
all(x, …)
# S3 method for ri
any(x, …)
# S3 method for ri
min(x, …)
# S3 method for ri
max(x, …)
# S3 method for ri
range(x, …)
# S3 method for ri
sum(x, …)
# S3 method for ri
summary(object, …)
an object of class bit or bitwhich
an object of class bit
a ri
or an integer vector of length==2 giving a range restriction for chunked processing
formally required but not used
as expected
Bit summaries are quite fast because we use a double loop that fixes each word in a processor register. Furthermore we break out of looping as soon as possible.
# NOT RUN {
x <- as.bit(c(TRUE, TRUE))
all(x)
any(x)
min(x)
max(x)
range(x)
sum(x)
summary(x)
x <- as.bitwhich(c(TRUE, TRUE))
all(x)
any(x)
min(x)
max(x)
range(x)
sum(x)
summary(x)
# }
# NOT RUN {
n <- .Machine$integer.max
x <- !bit(n)
N <- 1000000L # batchsize
B <- n %/% N # number of batches
R <- n %% N # rest
message("Batched sum (52.5 sec on Centrino duo)")
system.time({
s <- 0L
for (b in 1:B){
s <- s + sum(x[((b-1L)*N+1L):(b*N)])
}
if (R)
s <- s + sum(x[(n-R+1L):n])
})
message("Batched sum saving repeated memory allocation for the return vector
(44.4 sec on Centrino duo)")
system.time({
s <- 0L
l <- logical(N)
for (b in 1:B){
.Call("R_bit_extract", x, length(x), ((b-1L)*N+1L):(b*N), l, PACKAGE = "bit")
s <- s + sum(l)
}
if (R)
s <- s + sum(x[(n-R+1L):n])
})
message("C-coded sum (3.1 sec on Centrino duo)")
system.time(sum(x))
# }
Run the code above in your browser using DataLab