lib/Sort/MergeSort.pm

filter: Statementstmt 93.2% 55 / 59 Branchbran 84.6% 22 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 89.2% 83 / 93 CC 15 SCAR 18.7
CC
15
Cov
91%
__ANON__
9.0
mergesort
7.1
__ANON__
2.0
CRAP 6.5
↑ Summary blib/lib/Sort/MergeSort/Iterator.pm »
1  
2 package Sort::MergeSort;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Sort::MergeSort::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require Exporter;
8 1 our @ISA = qw(Exporter);
9 1 our @EXPORT = qw(mergesort);
10  
11 1 our $VERSION = 0.31;
12  
13 1 our $max_array = 64;
14  
15 sub mergesort
16 33 ▶{
Subroutine mergesort: called CC=7 SCAR=19.6
Pod undocumented
17 33 my ($compare, @inputs) = @_;
18  
19 #printf "Number of inputs: %d\n", scalar(@inputs);
20  
21 #
22 # Since we'll be using splice, don't use really wide arrays
23 #
24  
25 33 ▶ if (@inputs > $max_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (@inputs > $Sort::MergeSort::max_array) 5 28 33
26 5 my @new_array;
27 5 ▶ while (@inputs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@inputs) 21 5 26
28 21 push(@new_array, mergesort($compare, splice(@inputs, 0, $max_array)));
29 }
30 5 return mergesort($compare, @new_array);
31 }
32  
33 28 ▶ if (@inputs == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@inputs == 1) { } 0 28 28
elsif (@inputs == 0) { } 0 28 28
34 0 return $inputs[0];
35 } elsif (@inputs == 0) {
36 0 ▶ return Sort::MergeSort::Iterator->new(sub { undef });
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 }
38  
39 28 my @data;
40  
41 28 for my $i (@inputs) {
42 153 my $first = <$i>;
43 153 ▶ next unless defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 0 153 153
44 153 push(@data, [ $first, $i ]);
45 }
46  
47 # Sort high to low so that the element we want can be
48 # pop()ed off the end. Cheaper than shift.
49  
50 28 @data = sort { $compare->($b->[0], $a->[0]) } @data;
51  
52 #print join(", ", map { $_->[0] } @data)."\n";
53  
54 44878 ▶ return Sort::MergeSort::Iterator->new(sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
55 44878 ▶ return undef unless @data;
Branch: 1 branch
BranchTrueFalseTotal
unless @data 28 44850 44878
56  
57 44850 my $popped = pop(@data);
58  
59 44850 my $retval = $popped->[0];
60 44850 my $iter = $popped->[1];
61 44850 my $new = $popped->[0] = <$iter>;
62 44850 ▶ return $retval unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 153 44697 44850
63  
64 # if ($compare->($new, $retval) < 0) {
65 # die "Unsorted inputs $new $retval";
66 # }
67  
68 44697 ▶ unless(@data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@data) 145 44552 44697
69 145 @data = $popped;
70 145 return $retval;
71 }
72  
73 44552 my $min = 0;
74 44552 my $max = $#data;
75  
76 44552 ▶ while ($max - $min >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max - $min >= 2) 87747 44552 132299
77 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 87747 my $mid = ($min + $max) / 2; # rounds down
79 87747 my $c = $compare->($data[$mid][0], $new);
80 #print "new=$new [ $min, $mid, $max ] = $c\n";
81 87747 ▶ if ($c > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c > 0) { } 46683 41064 87747
elsif ($c < 0) { } 41064 0 41064
82 46683 $min = $mid;
83 } elsif ($c < 0) {
84 41064 $max = $mid;
85 } else {
86 0 $max = $min = $mid;
87 }
88 }
89 44552 ▶ if ($compare->($data[$max][0], $new) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$compare($data[$max][0], $new) > 0) { } 7560 36992 44552
elsif (&$compare($data[$min][0], $new) > 0) { } 30025 6967 36992
90 7560 splice(@data, $max+1, 0, $popped);
91 } elsif ($compare->($data[$min][0], $new) > 0) {
92 30025 splice(@data, $max, 0, $popped);
93 } else {
94 6967 splice(@data, $min, 0, $popped);
95 }
96  
97 #print join(", ", map { $_->[0] } @data)."\n";
98 #die unless is_sorted(reverse map { $_->[0] } @data);
99  
100 44552 return $retval;
101 28 });
102 }
103  
104 #sub is_sorted
105 #{
106 # my $ok = 1;
107 # for my $i (1..$#_) {
108 # next if $_[$i] >= $_[$i-1];
109 # $ok = 0;
110 # }
111 # return $ok;
112 #}
113  
114 1 1;
115  
116 __END__
↑ Summary blib/lib/Sort/MergeSort/Iterator.pm »