File Coverage

blib/lib/Math/NumSeq/ReReplace.pm
Criterion Covered Total %
statement 49 53 92.4
branch 4 8 50.0
condition 4 9 44.4
subroutine 14 15 93.3
pod 5 5 100.0
total 76 90 84.4


line stmt bran cond sub pod time code
1             # Copyright 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              
19              
20             # Benoit Cloitre followups
21             # http://sci.tech-archive.net/Archive/sci.math.research/2004-10/0224.html
22             # http://sci.tech-archive.net/Archive/sci.math.research/2004-11/0015.html
23             # http://sci.tech-archive.net/Archive/sci.math.research/2004-11/0021.html
24             #
25              
26              
27             package Math::NumSeq::ReReplace;
28 1     1   1005 use 5.004;
  1         3  
29 1     1   4 use strict;
  1         1  
  1         24  
30              
31 1     1   3 use vars '$VERSION','@ISA';
  1         1  
  1         67  
32             $VERSION = 72;
33              
34 1     1   6 use Math::NumSeq 7; # v.7 for _is_infinite()
  1         15  
  1         55  
35             @ISA = ('Math::NumSeq');
36             *_is_infinite = \&Math::NumSeq::_is_infinite;
37              
38             # uncomment this to run the ### lines
39             #use Smart::Comments;
40              
41             # use constant name => Math::NumSeq::__('Repeated Replacement');
42 1     1   6 use constant description => Math::NumSeq::__('Sequence of repeated replacements.');
  1         1  
  1         3  
43 1     1   3 use constant values_min => 1;
  1         1  
  1         78  
44             sub values_max {
45 0     0 1 0 my ($self) = @_;
46 0         0 my $stage = $self->{'stage'};
47 0 0       0 return ($stage < 0 ? undef : $stage+1);
48             }
49 1     1   4 use constant i_start => 1;
  1         2  
  1         42  
50 1     1   3 use constant characteristic_smaller => 1;
  1         2  
  1         34  
51 1     1   3 use constant characteristic_integer => 1;
  1         1  
  1         47  
52              
53 1         253 use constant parameter_info_array =>
54             [
55             { name => 'stage',
56             share_key => 'stage_neg1',
57             type => 'integer',
58             default => '-1',
59             width => 4,
60             minimum => -1,
61             # description => Math::NumSeq::__('...'),
62             },
63 1     1   4 ];
  1         2  
64              
65             #------------------------------------------------------------------------------
66             # 'A100002'
67             # 0 1 2 3 4 5 6 7 8 9
68             # 1, 2, 1, 2, 3, 3, 1, 2, 4, 4, 3, 4, 1, 2, 5, 5, 3, 5, 1, 2, 4, 5, 3, 4,
69             # 1 2 1 2
70              
71             # cf A100287 - first occurrence of n
72             # A101224
73             #
74             my @oeis_anum = ('A100002', # -1 all stages
75             # 0 all-ones, but A000012 has OFFSET=0
76             # 1 is 1,2 rep, but A040001 has OFFSET=0
77             );
78             sub oeis_anum {
79 1     1 1 3 my ($self) = @_;
80 1         3 return $oeis_anum[$self->{'stage'}+1];
81             }
82              
83             #------------------------------------------------------------------------------
84              
85             sub rewind {
86 3     3 1 363 my ($self) = @_;
87 3         11 $self->{'i'} = $self->i_start;
88 3         11 my $count = $self->{'count'} = [undef, [], []];
89             }
90             sub next {
91 24     24 1 357 my ($self) = @_;
92             ### ReReplace next(): $self->{'i'}
93              
94 24         15 my $stage = $self->{'stage'};
95 24 50       32 if ($stage == 0) {
96 0         0 return ($self->{'i'}++,
97             1);
98             }
99              
100 24         19 my $count = $self->{'count'};
101             ### $count
102              
103 24         15 my $value = 1;
104 24         26 for my $level (2 .. $#$count) {
105             ### $level
106             ### $value
107             ### count: ($count->[$level]->[$value]||0) + 1
108              
109 86 100       112 if (++$count->[$level]->[$value] >= $level) {
110 26         17 $count->[$level]->[$value] = 0;
111 26         21 $value = $level;
112             }
113             }
114              
115 24 50 33     45 if ($value >= $#$count-1
      66        
116             && ($stage < 0 || $value < $stage)) {
117 8         7 push @$count, [ @{$count->[-1]} ]; # array copy
  8         13  
118             ### extended to: $count
119             }
120              
121             ### return: $value
122 24         31 return ($self->{'i'}++,
123             $value);
124             }
125              
126             sub pred {
127 12     12 1 32 my ($self, $value) = @_;
128             ### Runs pred(): $value
129              
130 12   33     29 return ($value >= 1
131             && $value == int($value));
132             }
133              
134             1;
135             __END__