File Coverage

blib/lib/Statistics/Basic/ComputedVector.pm
Criterion Covered Total %
statement 59 72 81.9
branch 12 20 60.0
condition n/a
subroutine 15 20 75.0
pod 12 12 100.0
total 98 124 79.0


line stmt bran cond sub pod time code
1              
2             package Statistics::Basic::ComputedVector;
3              
4 33     33   133 use strict;
  33         39  
  33         1091  
5 33     33   134 use warnings;
  33         36  
  33         645  
6 33     33   110 use Carp;
  33         41  
  33         2040  
7              
8             our $tag_number = 0;
9              
10 33     33   148 use Statistics::Basic;
  33         37  
  33         251  
11 33     33   154 use base 'Statistics::Basic::Vector';
  33         32  
  33         24199  
12              
13             # new {{{
14             sub new {
15 13     13 1 32 my $class = shift;
16 13 50       13 my $that = eval { Statistics::Basic::Vector->new(@_) } or croak $@;
  13         40  
17 13 50       37 croak "input vector must be supplied to ComputedVector" unless defined $that;
18              
19 13         50 my $this = bless { tag=>(--$tag_number), c=>{}, input_vector=>$that, output_vector=>Statistics::Basic::Vector->new() }, $class;
20 13         40 $this->_recalc_needed;
21              
22 13         45 return $this;
23             }
24             # }}}
25             # copy {{{
26             sub copy {
27 0     0 1 0 my $this = shift;
28 0         0 my $that = __PACKAGE__->new( $this->{input_vector} );
29 0         0 $that->{computer} = $this->{computer};
30              
31 0 0       0 warn "copied computedvector($this -> $that)\n" if $Statistics::Basic::DEBUG >= 3;
32              
33 0         0 return $that;
34             }
35             # }}}
36             # set_filter {{{
37             sub set_filter {
38 12     12 1 58 my $this = shift;
39 12 50       14 my $cref = shift; croak "cref should be a code reference" unless ref($cref) eq "CODE";
  12         41  
40              
41 12         28 $this->{computer} = $cref;
42              
43 12         33 my $a = Scalar::Util::refaddr($this);
44 12         60 $this->{input_vector}->_set_computer( "cvec_$a" => $this ); # sets recalc needed in this object
45              
46 12         22 return $this;
47             }
48             # }}}
49             # _recalc {{{
50             sub _recalc {
51 18     18   19 my $this = shift;
52              
53 18         37 delete $this->{recalc_needed};
54              
55 18 100       68 if( ref( my $c = $this->{computer} ) eq "CODE" ) {
56 15         53 $this->{output_vector}->set_vector( [$c->($this->{input_vector}->query)] );
57              
58             } else {
59 3         13 $this->{output_vector}->set_vector( [$this->{input_vector}->query] );
60             }
61              
62 18 50       55 warn "[recalc " . ref($this) . "]\n" if $Statistics::Basic::DEBUG;
63 18         61 $this->_inform_computers_of_change;
64              
65 18         17 return;
66             }
67             # }}}
68             # _recalc_needed {{{
69             sub _recalc_needed {
70 33     33   37 my $this = shift;
71 33         390 $this->{recalc_needed} = 1;
72              
73 33 50       66 warn "[recalc_needed " . ref($this) . "]\n" if $Statistics::Basic::DEBUG;
74              
75 33         83 return;
76             }
77             # }}}
78             # query_size {{{
79             sub query_size {
80 18     18 1 260 my $this = shift;
81              
82 18 100       55 $this->_recalc if $this->{recalc_needed};
83              
84 18         48 return $this->{output_vector}->query_size;
85             }
86              
87             # maybe deprecate this later
88             *size = \&query_size unless $ENV{TEST_AUTHOR};
89              
90             # }}}
91             # query {{{
92             sub query {
93 35     35 1 60 my $this = shift;
94              
95 35 100       82 $this->_recalc if $this->{recalc_needed};
96              
97 35         89 return $this->{output_vector}->query;
98             }
99             # }}}
100              
101 0     0 1 0 sub query_vector { return $_[0]{input_vector} }
102              
103             # query_filled {{{
104             sub query_filled {
105 12     12 1 19 my $this = shift;
106              
107             # even though this makes little sense, imo, we need to provide it since so many other objects call it
108              
109 12 50       31 $this->_recalc if $this->{recalc_needed};
110              
111 12         35 return $this->{input_vector}->query_filled;
112             }
113             # }}}
114              
115 0     0   0 sub _fix_size { croak "fix_size() makes no sense on computed vectors" }
116 0     0 1 0 sub set_size { my $this = shift; $this->{input_vector}->set_size (@_); return $this }
  0         0  
  0         0  
117 1     1 1 481 sub insert { my $this = shift; $this->{input_vector}->insert (@_); return $this }
  1         5  
  1         2  
118 1     1 1 3 sub ginsert { my $this = shift; $this->{input_vector}->ginsert (@_); return $this }
  1         4  
  1         2  
119 0     0 1 0 sub append { my $this = shift; $this->{input_vector}->append (@_); return $this }
  0         0  
  0         0  
120 2     2 1 638 sub set_vector { my $this = shift; $this->{input_vector}->set_vector(@_); return $this }
  2         9  
  2         2  
121              
122             1;