Categorizer/Util.pm

filter: Statementstmt 66.6% 36 / 54 Branchbran 57.1% 16 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 6 totaltotal 59.6% 59 / 99 CC 18 SCAR 19.9
CC
18
Cov
63%
min
20.0
average
12.0
random_elements
6.0
CRAP 7.3
« Categorizer/Collection/SingleFile.pm ↑ Summary Categorizer/Learner/Boolean.pm »
1 package AI::Categorizer::Util;
2  
3 12 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 @EXPORT_OK = qw(intersection average max min random_elements binary_search);
6  
7 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # It's possible that this can be a class - something like
10 #
11 # $e = Evaluate->new(); $e->correct([...]); $e->assigned([...]); print $e->precision;
12  
13 # A simple binary search
14 5 ▶sub binary_search {
Subroutine binary_search: called CC=3 SCAR=11.0
Pod undocumented
15 5 my ($arr, $target) = @_;
16 5 my ($low, $high) = (0, scalar @$arr);
17 12 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶ while ( $low < $high ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low < $high) 10 5 15
19 10 my $cur = ($low + $high)/2;
20 10 ▶ if ( $arr->[$cur] < $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->[$cur] < $target) { } 4 6 10
21 4 $low = $cur + 1;
22 } else {
23 6 $high = $cur;
24 }
25 }
26 5 return $low;
27 }
28  
29 4 ▶sub max {
Subroutine max: called CC=4 SCAR=13.9
Pod undocumented
30 4 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
31 4 my $max = shift;
32 4 foreach (@_) {
33 22 ▶ $max = $_ if $_ > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 1 21 22
34 }
35 4 return $max;
36 }
37  
38 0 ▶sub min {
Subroutine min: not called CC=4 SCAR=30.0
Pod undocumented
39 0 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
40 0 my $min = shift;
41 0 foreach (@_) {
42 0 ▶ $min = $_ if $_ > $min;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $min 0 0 0
43 }
44 0 return $min;
45 }
46  
47 0 ▶sub average {
Subroutine average: not called CC=3 SCAR=24.8
Pod undocumented
48 0 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
49 0 my $total;
50 0 $total += $_ foreach @_;
51 0 return $total/@_;
52 }
53  
54 0 ▶sub intersection {
Subroutine intersection: not called CC=2 SCAR=17.9
Pod undocumented
55 0 my ($one, $two) = @_;
56 0 $two = _hashify($two);
57  
58 return UNIVERSAL::isa($one, 'HASH') ? # Accept hash or array for $one
59 0 grep {exists $two->{$_}} keys %$one :
60 0 ▶ grep {exists $two->{$_}} @$one;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($one, 'HASH') ? : 0 0 0
61 }
62  
63 0 ▶sub _hashify {
Subroutine _hashify: not called CC=2 SCAR=17.9
64 0 ▶ return $_[0] if UNIVERSAL::isa($_[0], 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "HASH") 0 0 0
65 0 return {map {$_=>1} @{$_[0]}};
66 }
67  
68 2 ▶sub random_elements {
Subroutine random_elements: called CC=6 SCAR=17.9
Pod undocumented
69 2 my ($a_ref, $n) = @_;
70 2 ▶ return @$a_ref if $n >= @$a_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= @$a_ref 0 2 2
71
72 2 ▶ my ($select, $mode) = ($n < @$a_ref/2) ? ($n, 'include') : (@$a_ref - $n, 'exclude');
Branch: 1 branch
BranchTrueFalseTotal
$n < @$a_ref / 2 ? : 1 1 2
73  
74 2 my %i;
75 2 ▶ $i{int rand @$a_ref} = 1 while keys(%i) < $select;
Branch: 1 branch
BranchTrueFalseTotal
keys %i < $select and do { $i{int rand @$a_ref} = 1 } 6 2 8
76  
77 2 ▶ return @{$a_ref}[keys %i] if $mode eq 'include';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "include" 1 1 2
78 1 ▶ return map {$i{$_} ? () : $a_ref->[$_]} 0..$#$a_ref;
Branch: 1 branch
BranchTrueFalseTotal
$i{$_} ? : 3 7 10
79 }
80  
81 12 1;
« Categorizer/Collection/SingleFile.pm ↑ Summary Categorizer/Learner/Boolean.pm »