File Coverage

blib/lib/Statistics/Basic/Median.pm
Criterion Covered Total %
statement 41 41 100.0
branch 9 14 64.2
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 58 63 92.0


line stmt bran cond sub pod time code
1              
2             package Statistics::Basic::Median;
3              
4 33     33   215 use strict;
  33         48  
  33         1158  
5 33     33   147 use warnings;
  33         42  
  33         731  
6 33     33   127 use Carp;
  33         45  
  33         2038  
7              
8 33     33   175 use base 'Statistics::Basic::_OneVectorBase';
  33         46  
  33         10538  
9              
10             sub new {
11 12     12 1 487 my $class = shift;
12              
13 12 50       25 warn "[new $class]\n" if $Statistics::Basic::DEBUG >= 2;
14              
15 12         25 my $this = bless {}, $class;
16 12 50       12 my $vector = eval { Statistics::Basic::Vector->new(@_) } or croak $@;
  12         120  
17 12 100       28 my $c = $vector->_get_computer("median"); return $c if defined $c;
  12         27  
18              
19 9         204 $this->{v} = $vector;
20              
21 9         22 $vector->_set_computer( median => $this );
22              
23 9         27 return $this;
24             }
25              
26             sub _recalc {
27 13     13   12 my $this = shift;
28 13         19 my $v = $this->{v};
29 13         32 my $cardinality = $v->query_size;
30              
31 13         20 delete $this->{recalc_needed};
32 13         14 delete $this->{_value};
33 13 50       22 return unless $cardinality > 0;
34 13 50       23 return unless $v->query_filled; # only applicable in certain circumstances
35              
36 13         29 my @v = (sort {$a <=> $b} ($v->query));
  195         186  
37 13         21 my $center = int($cardinality/2);
38              
39 33     33   186 { no warnings 'uninitialized'; ## no critic
  33         63  
  33         4577  
  13         13  
40 13 100       21 if ($cardinality%2) {
41 10         19 $this->{_value} = $v[$center];
42              
43             } else {
44 3         10 $this->{_value} = ($v[$center] + $v[$center-1])/2;
45             }
46             }
47              
48 13 50       23 warn "[recalc " . ref($this) . "] vector[int($cardinality/2)] = $this->{_value}\n" if $Statistics::Basic::DEBUG;
49              
50 13         24 return;
51             }
52              
53             1;