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   145 use strict;
  33         43  
  33         1141  
5 33     33   189 use warnings;
  33         45  
  33         921  
6 33     33   143 use Carp;
  33         45  
  33         2198  
7              
8             our $tag_number = 0;
9              
10 33     33   226 use Statistics::Basic;
  33         46  
  33         396  
11 33     33   183 use base 'Statistics::Basic::Vector';
  33         49  
  33         27116  
12              
13             # new {{{
14             sub new {
15 13     13 1 35 my $class = shift;
16 13 50       19 my $that = eval { Statistics::Basic::Vector->new(@_) } or croak $@;
  13         49  
17 13 50       42 croak "input vector must be supplied to ComputedVector" unless defined $that;
18              
19 13         54 my $this = bless { tag=>(--$tag_number), c=>{}, input_vector=>$that, output_vector=>Statistics::Basic::Vector->new() }, $class;
20 13         43 $this->_recalc_needed;
21              
22 13         49 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 82 my $this = shift;
39 12 50       18 my $cref = shift; croak "cref should be a code reference" unless ref($cref) eq "CODE";
  12         42  
40              
41 12         23 $this->{computer} = $cref;
42              
43 12         37 my $a = Scalar::Util::refaddr($this);
44 12         66 $this->{input_vector}->_set_computer( "cvec_$a" => $this ); # sets recalc needed in this object
45              
46 12         24 return $this;
47             }
48             # }}}
49             # _recalc {{{
50             sub _recalc {
51 18     18   26 my $this = shift;
52              
53 18         47 delete $this->{recalc_needed};
54              
55 18 100       67 if( ref( my $c = $this->{computer} ) eq "CODE" ) {
56 15         61 $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       58 warn "[recalc " . ref($this) . "]\n" if $Statistics::Basic::DEBUG;
63 18         77 $this->_inform_computers_of_change;
64              
65 18         25 return;
66             }
67             # }}}
68             # _recalc_needed {{{
69             sub _recalc_needed {
70 33     33   44 my $this = shift;
71 33         458 $this->{recalc_needed} = 1;
72              
73 33 50       72 warn "[recalc_needed " . ref($this) . "]\n" if $Statistics::Basic::DEBUG;
74              
75 33         103 return;
76             }
77             # }}}
78             # query_size {{{
79             sub query_size {
80 18     18 1 99 my $this = shift;
81              
82 18 100       59 $this->_recalc if $this->{recalc_needed};
83              
84 18         64 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 69 my $this = shift;
94              
95 35 100       95 $this->_recalc if $this->{recalc_needed};
96              
97 35         86 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 103 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       36 $this->_recalc if $this->{recalc_needed};
110              
111 12         43 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 162 sub insert { my $this = shift; $this->{input_vector}->insert (@_); return $this }
  1         8  
  1         2  
118 1     1 1 6 sub ginsert { my $this = shift; $this->{input_vector}->ginsert (@_); return $this }
  1         6  
  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 271 sub set_vector { my $this = shift; $this->{input_vector}->set_vector(@_); return $this }
  2         11  
  2         5  
121              
122             1;