blib/lib/Sort/Rank.pm

filter: Statementstmt 93.7% 45 / 48 Branchbran 68.7% 11 / 16 Conditioncond 66.6% 8 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 80.6% 75 / 93 CC 16 SCAR 19.3
CC
16
Cov
84%
rank_group
11.3
rank_sort
5.0
__ANON__
3.5
CRAP 6.9
↑ Summary
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
BranchTrueFalseTotal
if (ref $array eq "HASH") 0 5 5
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
BranchTrueFalseTotal
unless ref $array eq "ARRAY" 0 5 5
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
BranchTrueFalseTotal
unless ref $item eq "HASH" and exists $item->{"score"} 0 1007 1007
Condition33%1/3
ref $item eq "HASH" and exists $item->{"score"}
!ll&&!rl&&r
001007
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"}
ABresult
0X0
100
111
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"}; }
l!l&&r!l&&!r
320
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"}; }
ABresult
1X1
011
000
34  
35 5 ▶ croak "Key extractor must be a code ref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $extract eq "CODE" 0 5 5
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]
l!l&&r!l&&!r
3129136670
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]
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($i == 0 or $ar[$i][0] != $ar[$i - 1][0]) 2204 1803 4007
Condition100%3/3
$i == 0 or $ar[$i][0] != $ar[$i - 1][0]
l!l&&r!l&&!r
521991803
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]
ABresult
1X1
011
000
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
BranchTrueFalseTotal
wantarray ? : 1 4 5
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
BranchTrueFalseTotal
@$g > 1 ? : 3 1 4
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
BranchTrueFalseTotal
wantarray ? : 0 1 1
80 }
81  
82 3 1;
83 __END__
↑ Summary