NAME
Sort::Packed - Sort records packed in a vector
SYNOPSIS
use Sort::Packed qw(sort_packed);
my $vector = pack 'l*' => 12, 435, 34, 56, 43, 7;
sort_packed l => $vector;
print join(', ', unpack('l*' => $vector)), "\n";
DESCRIPTION
This module allows to sort data packed in a perl scalar.
It is very fast and uses very little memory. Usually, it is one order of magnitude faster than unpacking the data and sorting it with perl sort
builtin.
EXPORT
The following functions are available from the module:
- radixsort_packed $template => $data
-
sorts the records packed inside scalar
$data
.It uses the radixsort algorithm, usually the faster for data that is not partially ordered.
$template
is a simplifiedpack
template. It has to contain a type indicator optionally followed by an exclamation mark and/or a repetition count. For instance:"n" => unsigned short in big-endian order "l!" => native signed long "L!4" => records of four native unsigned longs "C256" => records of 256 unsigned characters
The template can be prefixed by a minus sign to indicate descending order (for instance,
-n4
).Sub-byte size or variable length types can not be used (for instance, bit
b
, hexadecimalh
, unicodeU
or BERw
types are forbidden).Currently, templates containing several types (as for instance "nL") are not supported.
- sort_packed $template => $data
-
this subroutine is an alias for
radixsort_packed
. - mergesort_packed $template => $data
-
this subroutine works as c<radixsort_packed> but uses the mergesort algorithm that can be faster for data that is not completely unordered.
- mergesort_packed_custom { CMP($a, $b) } $template => $data;
-
this subroutine sorts the vector using a custom comparison routine as the perl builtin
sort
.For instance:
# sorts by integer value modulo 16: mergesort_packed_custom { ((unpack N => $a) % 16) <=> ((unpack N => $b) % 16) } N => $vector
Note that this function is very slow due to the Perl comparison function being called inside the O(NlogN) part of the mergesort algorithm.
- sort_packed_custom { CMP($a, $b) } $template => $data;
-
this is an alias for
mergesort_packed_custom
- reverse_packed $template => $data
-
reverses the order of the records packed inside scalar
$data
. - shuffle_packed $template => $data
-
shuffles the records packed inside scalar
$data
.
SEE ALSO
Perl builtins "pack" in perlfunc and "sort" in perlfunc.
My other sorting modules Sort::Key and Sort::Key::Radix.
The Wikipedia article about radix sort: http://en.wikipedia.org/wiki/Radix_sort.
BUGS AND SUPPORT
Send bug reports via the CPAN bug tracking system at http://rt.cpan.org or just drop my an e-mail with any problem you encounter while using this module.
COPYRIGHT AND LICENSE
Copyright (C) 2008, 2009, 2012, 2014 by Salvador Fandiño (sfandino@yahoo.com).
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.
Mergesort algorithm based on code from NetBSD, Copyright (c) 1992, 1993 The Regents of the University of California.