NumSeq/Base/IteratePred.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 95.8% 46 / 48 CC 3 SCAR 3.1
CC
3
Cov
100%
rewind
2.0
next
2.0
BEGIN
1.0
CRAP 1.4
« NumSeq/Tribonacci.pm ↑ Summary NumSeq/Base/IterateIth.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2016, 2019, 2020 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 <http://www.gnu.org/licenses/>.
17  
18 package Math::NumSeq::Base::IteratePred;
19 4 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 $VERSION = 75;
25  
26 # uncomment this to run the ### lines
27 # use Smart::Comments;
28  
29 4 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use constant characteristic_smaller => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 29 ▶sub rewind {
Subroutine rewind: called CC=2 SCAR=6.9
Pod undocumented
34 29 my ($self) = @_;
35 ### IteratePred rewind() ...
36  
37 29 $self->{'i'} = $self->i_start;
38 29 my $value = $self->values_min;
39 29 ▶ if (! defined $value) { die "Oops, no values_min() for predicate start"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 28 29
40 28 $self->{'value'} = $value;
41  
42 ### i: $self->{'i'}
43 ### $value
44 }
45  
46 202 ▶sub next {
Subroutine next: called CC=2 SCAR=6.9
Pod undocumented
47 202 my ($self) = @_;
48 ### IteratePred next() at value: $self->{'value'}
49  
50 202 for (my $value = $self->{'value'}; ; $value++) {
51 1110 ▶ if ($self->pred($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pred($value)) 202 908 1110
52 202 $self->{'value'} = $value+1;
53 202 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 4 1;
82 __END__
« NumSeq/Tribonacci.pm ↑ Summary NumSeq/Base/IterateIth.pm »