Fast low-level methods for sorting and ordering. The ..sortorder
methods do sorting and ordering at once, which requires more RAM
than ordering but is (almost) as fast as as sorting.
Usage
# S3 method for class 'integer64'
shellsort(x, has.na = TRUE, na.last = FALSE, decreasing = FALSE, ...)
# S3 method for class 'integer64'
shellsortorder(x, i, has.na = TRUE, na.last = FALSE, decreasing = FALSE, ...)
# S3 method for class 'integer64'
shellorder(x, i, has.na = TRUE, na.last = FALSE, decreasing = FALSE, ...)
# S3 method for class 'integer64'
mergesort(x, has.na = TRUE, na.last = FALSE, decreasing = FALSE, ...)
# S3 method for class 'integer64'
mergeorder(x, i, has.na = TRUE, na.last = FALSE, decreasing = FALSE, ...)
# S3 method for class 'integer64'
mergesortorder(x, i, has.na = TRUE, na.last = FALSE, decreasing = FALSE, ...)
# S3 method for class 'integer64'
quicksort(
x,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
restlevel = floor(1.5 * log2(length(x))),
...
)
# S3 method for class 'integer64'
quicksortorder(
x,
i,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
restlevel = floor(1.5 * log2(length(x))),
...
)
# S3 method for class 'integer64'
quickorder(
x,
i,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
restlevel = floor(1.5 * log2(length(x))),
...
)
# S3 method for class 'integer64'
radixsort(
x,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
radixbits = 8L,
...
)
# S3 method for class 'integer64'
radixsortorder(
x,
i,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
radixbits = 8L,
...
)
# S3 method for class 'integer64'
radixorder(
x,
i,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
radixbits = 8L,
...
)
# S3 method for class 'integer64'
ramsort(
x,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
stable = TRUE,
optimize = c("time", "memory"),
VERBOSE = FALSE,
...
)
# S3 method for class 'integer64'
ramsortorder(
x,
i,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
stable = TRUE,
optimize = c("time", "memory"),
VERBOSE = FALSE,
...
)
# S3 method for class 'integer64'
ramorder(
x,
i,
has.na = TRUE,
na.last = FALSE,
decreasing = FALSE,
stable = TRUE,
optimize = c("time", "memory"),
VERBOSE = FALSE,
...
)Arguments
- x
a vector to be sorted by
ramsort.integer64()andramsortorder.integer64(), i.e. the output ofsort.integer64()- has.na
boolean scalar defining whether the input vector might contain
NAs. If we know we don't have NAs, this may speed-up. Note that you risk a crash if there are unexpectedNAs withhas.na=FALSE- na.last
boolean scalar telling ramsort whether to sort
NAs last or first. Note that 'boolean' means that there is no third optionNAas insort()- decreasing
boolean scalar telling ramsort whether to sort increasing or decreasing
- ...
further arguments, passed from generics, ignored in methods
- i
integer positions to be modified by
ramorder.integer64()andramsortorder.integer64(), default is 1:n, in this case the output is similar toorder.integer64()- restlevel
number of remaining recursionlevels before
quicksortswitches from recursing toshellsort- radixbits
size of radix in bits
- stable
boolean scalar defining whether stable sorting is needed. Allowing non-stable may speed-up.
- optimize
by default ramsort optimizes for 'time' which requires more RAM, set to 'memory' to minimize RAM requirements and sacrifice speed
- VERBOSE
cat some info about chosen method
Details
See bit::ramsort()
Note
Note that these methods purposely violate the functional programming
paradigm: they are called for the side-effect of changing some of
their arguments. The sort-methods change x, the order-methods
change i, and the sortoder-methods change both x and i
See also
bit::ramsort() for the generic, ramsort.default for the methods
provided by package ff, sort.integer64() for the sort interface and
sortcache() for caching the work of sorting
Examples
x <- as.integer64(sample(c(rep(NA, 9), 1:9), 32, TRUE))
x
#> integer64
#> [1] <NA> 2 4 <NA> 9 8 6 3 1 2 <NA> <NA> <NA>
#> [14] 8 8 7 6 1 3 <NA> 6 8 2 8 <NA> 9
#> [27] 7 6 <NA> <NA> 7 5
message("ramsort example")
#> ramsort example
s <- bit::clone(x)
bit::ramsort(s)
#> Warning: Detected that 'ramsort.integer64' was called directly. Instead only call 'ramsort' and rely on S3 dispatch. To suppress this warning, e.g. if this is a false positive, use options(bit64.warn.exported.s3.method = FALSE). In the next version, this symbol will stop being exported.
#> [1] 9
message("s has been changed in-place - whether or not ramsort uses an in-place algorithm")
#> s has been changed in-place - whether or not ramsort uses an in-place algorithm
s
#> integer64
#> [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 1 1 2 2
#> [14] 2 3 3 4 5 6 6 6 6 7 7 7 8
#> [27] 8 8 8 8 9 9
message("ramorder example")
#> ramorder example
s <- bit::clone(x)
o <- seq_along(s)
bit::ramorder(s, o)
#> Warning: Detected that 'ramorder.integer64' was called directly. Instead only call 'ramorder' and rely on S3 dispatch. To suppress this warning, e.g. if this is a false positive, use options(bit64.warn.exported.s3.method = FALSE). In the next version, this symbol will stop being exported.
#> [1] 9
message("o has been changed in-place - s remains unchanged")
#> o has been changed in-place - s remains unchanged
s
#> integer64
#> [1] <NA> 2 4 <NA> 9 8 6 3 1 2 <NA> <NA> <NA>
#> [14] 8 8 7 6 1 3 <NA> 6 8 2 8 <NA> 9
#> [27] 7 6 <NA> <NA> 7 5
o
#> [1] 1 4 11 12 13 20 25 29 30 9 18 2 10 23 8 19 3 32 7 17 21 28
#> [23] 16 27 31 6 14 15 22 24 5 26
s[o]
#> integer64
#> [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 1 1 2 2
#> [14] 2 3 3 4 5 6 6 6 6 7 7 7 8
#> [27] 8 8 8 8 9 9
message("ramsortorder example")
#> ramsortorder example
o <- seq_along(s)
bit::ramsortorder(s, o)
#> Warning: Detected that 'ramsortorder.integer64' was called directly. Instead only call 'ramsortorder' and rely on S3 dispatch. To suppress this warning, e.g. if this is a false positive, use options(bit64.warn.exported.s3.method = FALSE). In the next version, this symbol will stop being exported.
#> [1] 9
message("s and o have both been changed in-place - this is much faster")
#> s and o have both been changed in-place - this is much faster
s
#> integer64
#> [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 1 1 2 2
#> [14] 2 3 3 4 5 6 6 6 6 7 7 7 8
#> [27] 8 8 8 8 9 9
o
#> [1] 1 4 11 12 13 20 25 29 30 9 18 2 10 23 8 19 3 32 7 17 21 28
#> [23] 16 27 31 6 14 15 22 24 5 26