File Coverage

blib/lib/Math/NumSeq/Base/Sparse.pm
Criterion Covered Total %
statement 36 38 94.7
branch 2 4 50.0
condition 5 6 83.3
subroutine 9 9 100.0
pod 3 3 100.0
total 55 60 91.6


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde
2              
3             # This file is part of Math-NumSeq.
4             #
5             # Math-NumSeq is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Math-NumSeq is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Math-NumSeq. If not, see .
17              
18             package Math::NumSeq::Base::Sparse;
19 30     30   374 use 5.004;
  30         67  
20 30     30   95 use strict;
  30         34  
  30         551  
21              
22 30     30   81 use vars '$VERSION', '@ISA';
  30         34  
  30         1358  
23             $VERSION = 72;
24 30     30   1846 use Math::NumSeq;
  30         35  
  30         1074  
25             @ISA = ('Math::NumSeq');
26             *_is_infinite = \&Math::NumSeq::_is_infinite;
27              
28             # uncomment this to run the ### lines
29             #use Smart::Comments;
30              
31              
32 30     30   99 use constant characteristic_sparse => 1;
  30         30  
  30         8340  
33              
34             sub new {
35 27     27 1 23191 my $class = shift;
36             ### Sparse new() ...
37 27         132 return $class->SUPER::new (pred_array => [],
38             pred_hash => {},
39             pred_value => -1,
40             @_);
41             }
42             sub ith {
43 66     66 1 113 my ($self, $i) = @_;
44              
45 66 50       79 if (_is_infinite($i)) {
46 0         0 return $i;
47             }
48              
49             ### pred_array last: $#{$self->{'pred_array'}}
50 66         46 while ($#{$self->{'pred_array'}} < $i) {
  270         908  
51 204         202 _extend ($self);
52             }
53             ### pred_array: $self->{'pred_array'}
54 66         96 return $self->{'pred_array'}->[$i];
55             }
56              
57             sub pred {
58 4087     4087 1 10083 my ($self, $value) = @_;
59             ### Sparse pred(): $value
60 4087 50       4768 if (_is_infinite($value)) {
61 0         0 return 0;
62             }
63 4087   100     10614 while ($self->{'pred_value'} < $value
64             || $self->{'pred_value'} < 10) {
65 64         64 _extend ($self);
66             }
67             ### pred_hash: $self->{'pred_hash'}
68             ### Sparse pred result: exists($self->{'pred_hash'}->{$value})
69 4087         7574 return exists($self->{'pred_hash'}->{$value});
70             }
71              
72             sub _extend {
73 268     268   180 my ($self) = @_;
74             ### Sparse _extend()
75 268   66     364 my $iter = ($self->{'pred_iter'} ||= do {
76             ### Sparse create pred_iter
77 6         10 my $class = ref $self;
78 6         27 my $it = $class->new (%$self);
79             # while ($self->{'pred_value'} < 10) {
80             # my ($i, $pred_value) = $it->next;
81             # $self->{'pred_hash'}->{$self->{'pred_value'}=$pred_value} = undef;
82             # }
83             # ### $it
84 6         23 $it
85             });
86 268         363 my ($i, $value) = $iter->next;
87             ### $i
88             ### $value
89 268         219 $self->{'pred_value'} = $value;
90 268         371 $self->{'pred_array'}->[$i - $self->i_start] = $value;
91 268         554 $self->{'pred_hash'}->{$value} = undef;
92             }
93              
94             1;
95             __END__