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   164 use strict;
  33         44  
  33         1101  
5 33     33   143 use warnings;
  33         38  
  33         902  
6 33     33   130 use Carp;
  33         45  
  33         1879  
7              
8 33     33   153 use base 'Statistics::Basic::_OneVectorBase';
  33         37  
  33         10184  
9              
10             sub new {
11 12     12 1 1452 my $class = shift;
12              
13 12 50       28 warn "[new $class]\n" if $Statistics::Basic::DEBUG >= 2;
14              
15 12         28 my $this = bless {}, $class;
16 12 50       13 my $vector = eval { Statistics::Basic::Vector->new(@_) } or croak $@;
  12         143  
17 12 100       31 my $c = $vector->_get_computer("median"); return $c if defined $c;
  12         28  
18              
19 9         244 $this->{v} = $vector;
20              
21 9         23 $vector->_set_computer( median => $this );
22              
23 9         28 return $this;
24             }
25              
26             sub _recalc {
27 13     13   14 my $this = shift;
28 13         19 my $v = $this->{v};
29 13         30 my $cardinality = $v->query_size;
30              
31 13         30 delete $this->{recalc_needed};
32 13         16 delete $this->{_value};
33 13 50       33 return unless $cardinality > 0;
34 13 50       27 return unless $v->query_filled; # only applicable in certain circumstances
35              
36 13         32 my @v = (sort {$a <=> $b} ($v->query));
  195         194  
37 13         35 my $center = int($cardinality/2);
38              
39 33     33   177 { no warnings 'uninitialized'; ## no critic
  33         47  
  33         4718  
  13         14  
40 13 100       29 if ($cardinality%2) {
41 10         21 $this->{_value} = $v[$center];
42              
43             } else {
44 3         13 $this->{_value} = ($v[$center] + $v[$center-1])/2;
45             }
46             }
47              
48 13 50       26 warn "[recalc " . ref($this) . "] vector[int($cardinality/2)] = $this->{_value}\n" if $Statistics::Basic::DEBUG;
49              
50 13         30 return;
51             }
52              
53             1;