uniquedouble | R Documentation |
Uses a reversible one-to-one mapping to converts non-negative 64-bit integers, produced via the bit64
package, to double-precision floating-point values for faster handling of 64-bit integer indices, e.g. comparison of indices, count number of unique indices, etc. The double values are completely different from the corresponding integer values.
uniquedouble(x, inverse = FALSE)
x |
If inverse= |
inverse |
Logical flag. If |
Scalar/vector/array of double values, if inverse=FALSE
, or integer64 values, if inverse=TRUE
.
Danail Obreschkow
# produce example vector of non-negative 64-bit integers
n = 1e3
x = c(bit64::as.integer64(0:n),
bit64::as.integer64('9218868437227405311')+c(-n:n),
bit64::lim.integer64()[2]-c(0:n))
# map this vector onto unique doubles
y = uniquedouble(x)
# check if the double-vector can be inverted back to the integer-vector
cat(sprintf("Check inversion = %s.\n", all(x==uniquedouble(y, inverse=TRUE))))
# measure the time taken to compare all the int64 values
tick("Compare original integer64 values. ")
comparison = TRUE
for(i in seq_along(x)) comparison = comparison & x[i]==x[i]
tock(comparison, fmt="Time taken = %.3fs. Test result = %s.\n")
# measure the time taken to compare all the corresponding double values
tick("Compare corresponding double values. ")
comparison = TRUE
for(i in seq_along(y)) comparison = comparison & y[i]==y[i]
tock(comparison, fmt="Time taken = %.3fs. Test result = %s.\n")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.