| 1 | package Sort::Key::Multi; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | our $VERSION = '1.30'; | |||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use Sort::Key qw(multikeysorter multikeysorter_inplace); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Sort::Key::Types; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | our @CARP_NOT = qw(Sort::Key); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | my %sub; | |||||||||||||||||||
| 15 | 1 | my %type = qw( i integer | |||||||||||||||||||
| 16 | u unsigned_integer | ||||||||||||||||||||
| 17 | n number | ||||||||||||||||||||
| 18 | s string | ||||||||||||||||||||
| 19 | l locale); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | my $one_char_types = join('', keys %Sort::Key::Types::mktypes); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=8 SCAR=21.2
| |||||||||||||||||||||
| 24 | 2 | shift; | |||||||||||||||||||
| 25 | 2 | for my $name (@_) { | |||||||||||||||||||
| 26 | 2 | my $sub = $sub{$name}; | |||||||||||||||||||
| 27 | 2 | ▶ | unless (defined $sub) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 2 | ▶ | my ($types, $inplace) = $name =~ /^((?:r?[$one_char_types]\d*_*)+)keysort((?:_?inplace)?)$/o | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | or croak "invalid name for multikey sorter '$name'"; | ||||||||||||||||||||
| 30 | 2 | my @types; | |||||||||||||||||||
| 31 | 2 | ▶ | while ($types =~ /(r?)(.)(\d*)_*/g) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 3 | my ($r, $t, $n) = ($1, $2, $3); | |||||||||||||||||||
| 33 | 3 | ▶ | push @types, ( ($r ? '-' : '') . $type{$t} ) x ($n || 1); | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$n || 1
MC/DC0%0/1
$n || 1
$n
Truth table50%1/2
A
A= $n
| |||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | # print STDERR "$types => @types\n"; | ||||||||||||||||||||
| 36 | 2 | ▶ | if ($inplace) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | $sub = multikeysorter_inplace(@types); | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||
| 40 | 2 | $sub = multikeysorter(@types); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | 2 | my $caller = caller; | |||||||||||||||||||
| 44 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 2 | *{$caller."::".$name} = $sub; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 1 | 1; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head1 NAME | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | Sort::Key::Multi - simple multi-key sorts | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | use Sort::Key::Multi qw(sikeysort); | ||||||||||||||||||||
| 58 | my @data = qw(foo0 foo1 bar34 bar0 bar34 bar33 doz4) | ||||||||||||||||||||
| 59 | my @sisorted = sikeysort { /(\w+)(\d+)/} @data; | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Sort::Key::Multi creates multi-key sorting subroutines and exports them | ||||||||||||||||||||
| 64 | to the caller package. | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | The names of the sorters are of the form C<xxxkeysort> or | ||||||||||||||||||||
| 67 | C<xxxkeysort_inplace>, where C<xxx> determines the number and types of | ||||||||||||||||||||
| 68 | the keys as follows: | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =over 4 | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | + C<i> indicates an integer key, C<u> indicates an unsigned integer | ||||||||||||||||||||
| 73 | key, C<n> indicates a numeric key, C<s> indicates a string key and | ||||||||||||||||||||
| 74 | C<l> indicates a string key that obeys locale order configuration. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | + Type characters can be prefixed by C<r> to indicate reverse order. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | + A number following a type character indicates that the key type has | ||||||||||||||||||||
| 79 | to be repeated as many times (for instance C<i3> is equivalent to | ||||||||||||||||||||
| 80 | C<iii> and C<rs2> is equivalent to C<rsrs>). | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | + Underscores (C<_>) can be freely used between type indicators. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =back | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | For instance: | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | use Key::Sort::Multi qw(iirskeysort | ||||||||||||||||||||
| 89 | i2rskeysort | ||||||||||||||||||||
| 90 | i_i_rs__keysort | ||||||||||||||||||||
| 91 | i2rs_keysort); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | exports to the caller package fourth identical sorting functions that | ||||||||||||||||||||
| 94 | take two integer keys that are sorted in ascending order and one | ||||||||||||||||||||
| 95 | string key that is sorted in descending order. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | The generated sorters take as first argument a subroutine that is used | ||||||||||||||||||||
| 98 | to extract the keys from the values which are passed inside C<$_>, for | ||||||||||||||||||||
| 99 | example: | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | my @data = qw(1.3.foo 1.3.bar 2.3.bar 1.4.bar 1.7.foo); | ||||||||||||||||||||
| 102 | my @s = i2rs_keysort { split /\./, $_ } @data; | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =head1 SEE ALSO | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | For a more general multi-key sorter generator see L<Sort::Key::Maker>. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | Copyright (C) 2006, 2014 by Salvador FandiE<ntilde>o | ||||||||||||||||||||
| 111 | E<lt>sfandino@yahoo.comE<gt> | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 114 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||||||||||||||||||||
| 115 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||