Basic/Mode.pm

filter: Statementstmt 88.7% 55 / 62 Branchbran 50.0% 12 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 100% 2 / 2 totaltotal 79.2% 80 / 101 CC 14 SCAR 14.3
CC
14
Cov
78%
_recalc
7.0
__ANON__
6.0
is_multimodal
6.0
CRAP 4.2
« Basic/Vector.pm ↑ Summary Basic/Median.pm »
1  
2 package Statistics::Basic::Mode;
3  
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 33 ▶use Statistics::Basic;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use base 'Statistics::Basic::_OneVectorBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use overload
13 33 ▶ '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=1.4
Subroutine __ANON__: called CC=3 SCAR=13.1
14 1 ▶ defined( my $q = $_[0]->query ) or return "n/a";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $q = $_[0]->query) or return "n/a" 1 0 1
15 1 ▶ return $q if ref $q; # vectors interpolate themselves
Branch: 1 branch
BranchTrueFalseTotal
if ref $q 1 0 1
16 0 $Statistics::Basic::fmt->format_number($_[0]->query, $Statistics::Basic::IPRES);
17 },
18 0 ▶ '0+' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
19 0 my $q = $_[0]->query;
20 0 ▶ croak "result is multimodal and cannot be used as a number" if ref $q;
Branch: 1 branch
BranchTrueFalseTotal
if ref $q 0 0 0
21 0 $q;
22 },
23 33 fallback => 1; # tries to do what it would have done if this wasn't present.
24  
25 4 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
26 4 my $class = shift;
27  
28 4 ▶ warn "[new $class]\n" if $Statistics::Basic::DEBUG >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 2 0 4 4
29  
30 4 my $this = bless {}, $class;
31 4 ▶ my $vector = eval { Statistics::Basic::Vector->new(@_) } or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
my $vector = eval { do { "Statistics::Basic::Vector"->new(@_) } } or croak($@) 4 0 4
32 4 ▶ my $c = $vector->_get_computer("mode"); return $c if defined $c;
Branch: 1 branch
BranchTrueFalseTotal
if defined $c 0 4 4
33  
34 4 $this->{v} = $vector;
35  
36 4 $vector->_set_computer( mode => $this );
37  
38 4 return $this;
39 }
40  
41 7 ▶sub _recalc {
Subroutine _recalc: called CC=7 SCAR=19.5
42 7 my $this = shift;
43 7 my $v = $this->{v};
44 7 my $cardinality = $v->query_size;
45  
46 7 delete $this->{recalc_needed};
47 7 delete $this->{_value};
48 7 ▶ return unless $cardinality > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cardinality > 0 0 7 7
49 7 ▶ return unless $v->query_filled; # only applicable in certain circumstances
Branch: 1 branch
BranchTrueFalseTotal
unless $v->query_filled 0 7 7
50  
51 7 my %mode;
52 7 my $max = 0;
53  
54 7 for my $val ($v->query) {
55 33 ▶ no warnings 'uninitialized'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
56 51 my $t = ++ $mode{$val};
57 51 ▶ $max = $t if $t > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $t > $max 19 32 51
58 }
59 7 my @a = sort {$a<=>$b} grep { $mode{$_}==$max } keys %mode;
60  
61 7 ▶ $this->{_value} = ( (@a == 1) ? $a[0] : Statistics::Basic::Vector->new(\@a) );
Branch: 1 branch
BranchTrueFalseTotal
@a == 1 ? : 3 4 7
62  
63 7 ▶ warn "[recalc " . ref($this) . "] count of $this->{_value} = $max\n" if $Statistics::Basic::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG 0 7 7
64  
65 7 return;
66 }
67  
68 0 ▶sub is_multimodal {
Subroutine is_multimodal: not called CC=2 SCAR=17.9
Pod documented
69 0 my $this = shift;
70 0 my $that = $this->query;
71  
72 0 ▶ return (blessed($that) ? 1:0);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($that) ? : 0 0 0
73 }
74  
75 33 1;
« Basic/Vector.pm ↑ Summary Basic/Median.pm »