| 1 | package Sort::Rank; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 8 | 3 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | 3 | our @EXPORT_OK = qw(rank_sort rank_group); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use version; our $VERSION = qv( '0.0.2' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | sub rank_group { | ||||||||||||||||||||||||||
|
Subroutine
rank_group: called CC=10 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 5 | my $array = shift; | |||||||||||||||||||||||||||
| 15 | 5 | my $extract = shift; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | if ( ref( $array ) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Turn a hash into an array | ||||||||||||||||||||||||||||
| 20 | 0 | my @a = map { [ $_, $array->{$_} ] } keys %$array; | |||||||||||||||||||||||||||
| 21 | 0 | $array = \@a; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | croak "rank_sort needs an array reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | unless ref $array eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Default score extraction sub | ||||||||||||||||||||||||||||
| 28 | 1007 | ▶ | $extract ||= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 29 | 1007 | my $item = shift; | |||||||||||||||||||||||||||
| 30 | croak "Array item must be a hash with a key called 'score'." | ||||||||||||||||||||||||||||
| 31 | 1007 | ▶ | unless ref( $item ) eq 'HASH' && exists $item->{score}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $item eq "HASH" and exists $item->{"score"}
MC/DC0%0/2
ref $item eq "HASH" and exists $item->{"score"}
ref $item eq "HASH"
exists $item->{"score"}
Truth table33%1/3
A and B
A= ref $item eq "HASH"B= exists $item->{"score"}
| |||||||||||||||||||||||||||||
| 32 | 1007 | return $item->{score}; | |||||||||||||||||||||||||||
| 33 | 5 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition66%2/3
$extract ||= sub {
my $item = shift();
croak("Array item must be a hash with a key called 'score'.") unless ref $item eq "HASH" and exists $item->{"score"};
return $item->{"score"};
}
MC/DC0%0/2
$extract ||= sub {
my $item = shift();
croak("Array item must be a hash with a key called 'score'.") unless ref $item eq "HASH" and exists $item->{"score"};
return $item->{"score"};
}
$extract
sub {
my $item = shift();
croak("Array item must be a hash with a key called 'score'.") unless ref $item eq "HASH" and exists $item->{"score"};
return $item->{"score"};
}
Truth table66%2/3
A ||= B
A= $extractB= sub {
my $item = shift();
croak("Array item must be a hash with a key called 'score'.") unless ref $item eq "HASH" and exists $item->{"score"};
return $item->{"score"};
}
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 5 | ▶ | croak "Key extractor must be a code ref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | unless ref( $extract ) eq 'CODE'; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 5 | my $pos = 1; | |||||||||||||||||||||||||||
| 39 | my @ar = sort { | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Sort on score then original position | ||||||||||||||||||||||||||||
| 42 | 34958 | ▶ | $b->[0] <=> $a->[0] | ||||||||||||||||||||||||||
|
Condition66%2/3
$Sort::Rank::b->[0] <=> $Sort::Rank::a->[0] || $Sort::Rank::a->[1] <=> $Sort::Rank::b->[1]
MC/DC0%0/2
$Sort::Rank::b->[0] <=> $Sort::Rank::a->[0] || $Sort::Rank::a->[1] <=> $Sort::Rank::b->[1]
$Sort::Rank::b->[0] <=> $Sort::Rank::a->[0]
$Sort::Rank::a->[1] <=> $Sort::Rank::b->[1]
Truth table66%2/3
A || B
A= $Sort::Rank::b->[0] <=> $Sort::Rank::a->[0]B= $Sort::Rank::a->[1] <=> $Sort::Rank::b->[1]
| |||||||||||||||||||||||||||||
| 43 | || $a->[1] <=> $b->[1] | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | map { | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Build array of score, original position, value | ||||||||||||||||||||||||||||
| 48 | 5 | [ $extract->( $_ ), $pos++, $_ ] | |||||||||||||||||||||||||||
| 49 | } @$array; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | my @out = (); | |||||||||||||||||||||||||||
| 52 | 5 | for my $i ( 0 .. $#ar ) { | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Need to start a new chunk? | ||||||||||||||||||||||||||||
| 55 | 4007 | ▶ | if ( $i == 0 || $ar[$i]->[0] != $ar[ $i - 1 ]->[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i == 0 or $ar[$i][0] != $ar[$i - 1][0]
MC/DC100%2/2
$i == 0 or $ar[$i][0] != $ar[$i - 1][0]
$i == 0
$ar[$i][0] != $ar[$i - 1][0]
Truth table100%3/3
A or B
A= $i == 0B= $ar[$i][0] != $ar[$i - 1][0]
| |||||||||||||||||||||||||||||
| 56 | 2204 | push @out, [ $i + 1 ]; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Add item to current chunk | ||||||||||||||||||||||||||||
| 60 | 4007 | push @{ $out[-1] }, $ar[$i]->[2]; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 5 | ▶ | return wantarray ? @out : \@out; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub rank_sort { | ||||||||||||||||||||||||||
|
Subroutine
rank_sort: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 1 | my @grp = rank_group( @_ ); | |||||||||||||||||||||||||||
| 68 | 1 | my @out = (); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Unwrap groups | ||||||||||||||||||||||||||||
| 71 | 1 | for my $g ( @grp ) { | |||||||||||||||||||||||||||
| 72 | 4 | my $rank = shift @$g; | |||||||||||||||||||||||||||
| 73 | 4 | ▶ | my $many = ( @$g > 1 ) ? '=' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 4 | for my $i ( @$g ) { | |||||||||||||||||||||||||||
| 75 | 7 | push @out, [ $rank, $many, $i ]; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | return wantarray ? @out : \@out; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | 1; | |||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||