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   154 use strict;
  33         44  
  33         1193  
5 33     33   161 use warnings;
  33         46  
  33         808  
6 33     33   145 use Carp;
  33         48  
  33         2314  
7              
8             our $tag_number = 0;
9              
10 33     33   177 use Statistics::Basic;
  33         58  
  33         362  
11 33     33   189 use base 'Statistics::Basic::Vector';
  33         42  
  33         29748  
12              
13             # new {{{
14             sub new {
15 13     13 1 33 my $class = shift;
16 13 50       18 my $that = eval { Statistics::Basic::Vector->new(@_) } or croak $@;
  13         46  
17 13 50       41 croak "input vector must be supplied to ComputedVector" unless defined $that;
18              
19 13         63 my $this = bless { tag=>(--$tag_number), c=>{}, input_vector=>$that, output_vector=>Statistics::Basic::Vector->new() }, $class;
20 13         41 $this->_recalc_needed;
21              
22 13         48 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 53 my $this = shift;
39 12 50       17 my $cref = shift; croak "cref should be a code reference" unless ref($cref) eq "CODE";
  12         37  
40              
41 12         19 $this->{computer} = $cref;
42              
43 12         31 my $a = Scalar::Util::refaddr($this);
44 12         53 $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         34 delete $this->{recalc_needed};
54              
55 18 100       56 if( ref( my $c = $this->{computer} ) eq "CODE" ) {
56 15         54 $this->{output_vector}->set_vector( [$c->($this->{input_vector}->query)] );
57              
58             } else {
59 3         14 $this->{output_vector}->set_vector( [$this->{input_vector}->query] );
60             }
61              
62 18 50       54 warn "[recalc " . ref($this) . "]\n" if $Statistics::Basic::DEBUG;
63 18         62 $this->_inform_computers_of_change;
64              
65 18         20 return;
66             }
67             # }}}
68             # _recalc_needed {{{
69             sub _recalc_needed {
70 33     33   37 my $this = shift;
71 33         393 $this->{recalc_needed} = 1;
72              
73 33 50       64 warn "[recalc_needed " . ref($this) . "]\n" if $Statistics::Basic::DEBUG;
74              
75 33         79 return;
76             }
77             # }}}
78             # query_size {{{
79             sub query_size {
80 18     18 1 390 my $this = shift;
81              
82 18 100       54 $this->_recalc if $this->{recalc_needed};
83              
84 18         57 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 65 my $this = shift;
94              
95 35 100       86 $this->_recalc if $this->{recalc_needed};
96              
97 35         81 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 16 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       19 $this->_recalc if $this->{recalc_needed};
110              
111 12         28 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 317 sub insert { my $this = shift; $this->{input_vector}->insert (@_); return $this }
  1         6  
  1         2  
118 1     1 1 6 sub ginsert { my $this = shift; $this->{input_vector}->ginsert (@_); return $this }
  1         5  
  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 824 sub set_vector { my $this = shift; $this->{input_vector}->set_vector(@_); return $this }
  2         9  
  2         4  
121              
122             1;