File Coverage

blib/lib/Math/NumSeq/Base/IteratePred.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 45 47 95.7


line stmt bran cond sub pod time code
1             # Copyright 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::IteratePred;
19 4     4   75 use 5.004;
  4         16  
  4         357  
20 4     4   26 use strict;
  4         6  
  4         129  
21 4     4   21 use List::Util 'max';
  4         7  
  4         382  
22              
23 4     4   21 use vars '$VERSION';
  4         8  
  4         273  
24             $VERSION = 71;
25              
26             # uncomment this to run the ### lines
27             # use Smart::Comments;
28              
29 4     4   23 use constant characteristic_increasing => 1;
  4         6  
  4         259  
30 4     4   18 use constant characteristic_integer => 1;
  4         43  
  4         199  
31 4     4   19 use constant characteristic_smaller => 0;
  4         8  
  4         881  
32              
33             sub rewind {
34 29     29 0 6507 my ($self) = @_;
35             ### IteratePred rewind() ...
36              
37 29         229 $self->{'i'} = $self->i_start;
38 29         134 my $value = $self->values_min;
39 29 100       105 if (! defined $value) { die "Oops, no values_min() for predicate start"; }
  1         10  
40 28         325 $self->{'value'} = $value;
41              
42             ### i: $self->{'i'}
43             ### $value
44             }
45              
46             sub next {
47 202     202 0 4740 my ($self) = @_;
48             ### IteratePred next() at value: $self->{'value'}
49              
50 202         409 for (my $value = $self->{'value'}; ; $value++) {
51 1110 100       3400 if ($self->pred($value)) {
52 202         378 $self->{'value'} = $value+1;
53 202         740 return ($self->{'i'}++, $value);
54             }
55             }
56             }
57              
58             # Would have to scan all values to find correct i.
59             # sub seek_to_value {
60             # my ($self, $value) = @_;
61             # $value = int($value);
62             # $self->{'value'} = max ($value, $self->values_min);
63             # $self->{'i'} = ...
64             # }
65              
66             # Slow to scan through all values.
67             # sub ith {
68             # my ($self, $i) = @_;
69             # $i -= $self->i_start;
70             # my $value = $self->value_min - 1;
71             # while ($i >= 0) {
72             # $value++;
73             # if ($self->pred($value)) {
74             # $i--;
75             # }
76             # }
77             # return $value;
78             # }
79              
80              
81             1;
82             __END__