Sort::Packed - Sort records packed in a vector
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";
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.
sort
The following functions are available from the module:
sorts the records packed inside scalar $data.
$data
It uses the radixsort algorithm, usually the faster for data that is not partially ordered.
$template is a simplified pack template. It has to contain a type indicator optionally followed by an exclamation mark and/or a repetition count. For instance:
$template
pack
"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).
-n4
Sub-byte size or variable length types can not be used (for instance, bit b, hexadecimal h, unicode U or BER w types are forbidden).
b
h
U
w
Currently, templates containing several types (as for instance "nL") are not supported.
this subroutine is an alias for radixsort_packed.
radixsort_packed
this subroutine works as c<radixsort_packed> but uses the mergesort algorithm that can be faster for data that is not completely unordered.
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.
this is an alias for mergesort_packed_custom
mergesort_packed_custom
reverses the order of the records packed inside scalar $data.
shuffles the records packed inside scalar $data.
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.
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 (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.
To install Sort::Packed, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sort::Packed
CPAN shell
perl -MCPAN -e shell install Sort::Packed
For more information on module installation, please visit the detailed CPAN module installation guide.