File Coverage

blib/lib/Math/NumSeq/Base/Sparse.pm
Criterion Covered Total %
statement 37 39 94.8
branch 2 4 50.0
condition 5 6 83.3
subroutine 9 9 100.0
pod 3 3 100.0
total 56 61 91.8


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   506 use 5.004;
  30         98  
  30         1282  
20 30     30   147 use strict;
  30         59  
  30         1008  
21              
22 30     30   165 use vars '$VERSION', '@ISA';
  30         58  
  30         1935  
23             $VERSION = 71;
24 30     30   3170 use Math::NumSeq;
  30         51  
  30         1458  
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   215 use constant characteristic_sparse => 1;
  30         75  
  30         12192  
33              
34             sub new {
35 27     27 1 48607 my $class = shift;
36             ### Sparse new() ...
37 27         225 return $class->SUPER::new (pred_array => [],
38             pred_hash => {},
39             pred_value => -1,
40             @_);
41             }
42             sub ith {
43 66     66 1 287 my ($self, $i) = @_;
44              
45 66 50       295 if (_is_infinite($i)) {
46 0         0 return $i;
47             }
48              
49             ### pred_array last: $#{$self->{'pred_array'}}
50 66         131 while ($#{$self->{'pred_array'}} < $i) {
  270         7934  
51 204         405 _extend ($self);
52             }
53             ### pred_array: $self->{'pred_array'}
54 66         448 return $self->{'pred_array'}->[$i];
55             }
56              
57             sub pred {
58 4087     4087 1 20725 my ($self, $value) = @_;
59             ### Sparse pred(): $value
60 4087 50       9041 if (_is_infinite($value)) {
61 0         0 return 0;
62             }
63 4087   100     33244 while ($self->{'pred_value'} < $value
64             || $self->{'pred_value'} < 10) {
65 64         107 _extend ($self);
66             }
67             ### pred_hash: $self->{'pred_hash'}
68             ### Sparse pred result: exists($self->{'pred_hash'}->{$value})
69 4087         17947 return exists($self->{'pred_hash'}->{$value});
70             }
71              
72             sub _extend {
73 268     268   567 my ($self) = @_;
74             ### Sparse _extend()
75 268   66     1243 my $iter = ($self->{'pred_iter'} ||= do {
76             ### Sparse create pred_iter
77 6         17 my $class = ref $self;
78 6         52 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         41 $it
85             });
86 268         1065 my ($i, $value) = $iter->next;
87             ### $i
88             ### $value
89 268         623 $self->{'pred_value'} = $value;
90 268         1083 $self->{'pred_array'}->[$i - $self->i_start] = $value;
91 268         1534 $self->{'pred_hash'}->{$value} = undef;
92             }
93              
94             1;
95             __END__