NumSeq/AsciiSelf.pm

filter: Statementstmt 98.0% 150 / 153 Branchbran 90.9% 20 / 22 Conditioncond 57.8% 11 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 94.7% 18 / 19 Podpod 85.7% 6 / 7 totaltotal 90.0% 208 / 231 CC 20 SCAR 13.1
CC
20
Cov
93%
values_max
12.0
rewind
5.0
ith
5.0
CRAP 3.7
« NumSeq/ConcatNumbers.pm ↑ Summary NumSeq/Catalan.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 # math-image --values=AsciiSelf --output=list
19  
20  
21 package Math::NumSeq::AsciiSelf;
22 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 $VERSION = 75;
27 2 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 @ISA = ('Math::NumSeq');
29 2 *_is_infinite = \&Math::NumSeq::_is_infinite;
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments;
33  
34  
35 # use constant name => Math::NumSeq::__('Ascii Self');
36 2 ▶use constant description => Math::NumSeq::__('Sequence is itself in ASCII.');
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use constant i_start => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use constant characteristic_increasing => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use constant characteristic_smaller => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 # use constant characteristic_charset => 'ASCII';
43 # sub characteristic_charset_digits {
44 # my ($self) = @_;
45 # return $self->{'radix'};
46 # }
47  
48 use Math::NumSeq::Base::Digits
49 2 ▶ 'parameter_info_array'; # radix parameter
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 {
52 2 my @values_min;
53 my @values_max;
54 2 $values_min[3] = 49;
55 2 $values_min[6] = 49;
56 2 $values_min[8] = $values_max[8] = 54;
57 2 $values_min[11] = 49;
58 2 $values_min[12] = 52;
59 2 $values_min[14] = 49;
60 2 $values_min[15] = 51;
61 2 $values_min[16] = $values_max[16] = 51; # only 51s
62 2 $values_min[20] = 50;
63 2 $values_min[21] = 50;
64 2 $values_min[23] = 50;
65 2 $values_min[24] = $values_max[24] = 50; # only 50s
66 2 $values_min[28] = 49;
67 2 $values_min[29] = 49;
68 2 $values_min[30] = 49;
69 2 $values_min[31] = 49;
70 2 $values_min[32] = 49;
71 2 $values_min[33] = 49;
72 2 $values_min[34] = 49;
73  
74 2 $values_max[6] = 50;
75 2 $values_max[11] = 65;
76 2 $values_max[12] = 52;
77 2 $values_max[13] = 67;
78 2 $values_max[14] = 68;
79 2 $values_max[15] = 67;
80 2 $values_max[16] = 51;
81 2 $values_max[17] = 71;
82 2 $values_max[18] = 72;
83 2 $values_max[19] = 73;
84 2 $values_max[20] = 72;
85 2 $values_max[21] = 70;
86 2 $values_max[22] = 76;
87 2 $values_max[23] = 75;
88 2 $values_max[24] = 50;
89 2 $values_max[25] = 79;
90 2 $values_max[26] = 80;
91 2 $values_max[27] = 81;
92 2 $values_max[28] = 77;
93 2 $values_max[29] = 83;
94 2 $values_max[30] = 82;
95 2 $values_max[31] = 80;
96 2 $values_max[32] = 80;
97 2 $values_max[33] = 87;
98 2 $values_max[34] = 78;
99 2 $values_max[35] = 89;
100  
101 1 ▶ sub values_min {
Subroutine values_min: called CC=2 SCAR=7.1
Pod documented
102 1 my ($self) = @_;
103 1 my $radix = $self->{'radix'};
104 1 ▶ return ($values_max[$radix] || 48);
Condition50%1/2
$values_max[$radix] || 48
l!l
01
MC/DC0%0/1
$values_max[$radix] || 48
$values_max[$radix]
Truth table50%1/2
A
A= $values_max[$radix]
Aresult
11
00
105 }
106 0 ▶ sub values_max {
Subroutine values_max: not called CC=3 SCAR=24.8
Pod documented
107 0 my ($self) = @_;
108 0 my $radix = $self->{'radix'};
109 0 ▶ return ($values_max[$radix]
Branch: 1 branch
BranchTrueFalseTotal
$radix <= 10 ? : 0 0 0
Condition0%0/3
$values_max[$radix] || $radix + ($radix <= 10 ? 47 : 55)
l!l&&r!l&&!r
000
MC/DC0%0/2
$values_max[$radix] || $radix + ($radix <= 10 ? 47 : 55)
$values_max[$radix] $radix + ($radix <= 10 ? 47 : 55)
Truth table0%0/3
A || B
A= $values_max[$radix]B= $radix + ($radix <= 10 ? 47 : 55)
ABresult
1X1
011
000
110 || $radix + ($radix <= 10 ? 47 : 65-10));
111 }
112 }
113  
114 #------------------------------------------------------------------------------
115  
116 # cf A109648 ascii with comma and space
117 2 my @oeis_anum;
118 2 $oeis_anum[10] = 'A109733';
119 # OEIS-Catalogue: A109733
120 1 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
121 1 my ($self) = @_;
122 1 return $oeis_anum[$self->{'radix'}];
123 }
124  
125 #------------------------------------------------------------------------------
126  
127 # ith() on radix 7 is wrong, report it as not available
128 134 ▶sub can {
Subroutine can: called CC=4 SCAR=13.9
Pod undocumented
129 134 my ($self, $name) = @_;
130 134 ▶ if ($name eq 'ith' && ref $self && $self->{'radix'} == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "ith" and ref $self and $self->{"radix"} == 7) 1 133 134
Condition83%5/6
$name eq "ith" and ref $self and $self->{"radix"} == 7
!ll&&!rl&&r
97361
$name eq "ith" and ref $self
!ll&&!rl&&r
97037
MC/DC66%2/3
$name eq "ith" and ref $self and $self->{"radix"} == 7
$name eq "ith" ref $self $self->{"radix"} == 7
Truth table75%3/4
A and B and C
A= $name eq "ith"B= ref $selfC= $self->{"radix"} == 7
ABCresult
0XX0
10X0
1100
1111
131 1 return undef;
132 }
133 133 return $self->SUPER::can($name);
134 }
135  
136 72 ▶sub rewind {
Subroutine rewind: called CC=5 SCAR=16.1
Pod documented
137 72 my ($self) = @_;
138 ### AsciiSelf rewind() ...
139 72 $self->{'i'} = $self->i_start;
140  
141 # FIXME: this is a pre-calculation rather than a rewind ...
142 72 my $radix = $self->{'radix'};
143 ### $radix;
144 72 my $start;
145 72 foreach my $digit (0 .. $radix-1) {
146 1298 my $ascii = _digit_to_ascii($digit);
147 1298 my $r = $self->{'map'}->[$ascii] = [ _radix_ascii($radix,$ascii) ];
148 1298 ▶ if ($r->[0] == $ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->[0] == $ascii) 82 1216 1298
149 82 ▶ $start ||= $r->[0];
Condition66%2/3
$start ||= $r->[0]
l!l&&r!l&&!r
10720
MC/DC0%0/2
$start ||= $r->[0]
$start $r->[0]
Truth table66%2/3
A ||= B
A= $startB= $r->[0]
ABresult
1X1
011
000
150 }
151 }
152 72 ▶ $start ||= 48;
Condition50%1/2
$start ||= 48
l!l
720
MC/DC0%0/1
$start ||= 48
$start
Truth table50%1/2
A
A= $start
Aresult
11
00
153 72 $self->{'width'} = scalar(@{$self->{'map'}->[48]});
154 72 $self->{'start'} = $start;
155 ### $start
156  
157 72 $self->{'state'} = [ $self->{'map'}->[$start] ];
158 72 $self->{'digits'} = [ -1 ]; # to start from 0 with preincrement
159 }
160  
161 1716 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
162 1716 my ($self) = @_;
163 ### AsciiSelf next(): "$self->{'i'}"
164 ### digits: $self->{'digits'}
165 ### state: $self->{'state'}
166  
167 1716 my $state = $self->{'state'};
168 1716 my $digits = $self->{'digits'};
169 1716 my $pos = 0;
170 1716 my $digit;
171 1716 for (;;) {
172 3128 ▶ if ($pos >= @$digits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= @$digits) 160 2968 3128
173 160 push @$state, $self->{'map'}->[$self->{'start'}];
174 160 push @$digits, ($digit = 1);
175  
176 ### extend at pos: $pos
177 ### extended digits: $digits
178 ### extended state: $state
179 160 last;
180 }
181 2968 $digit = ++($digits->[$pos]);
182 2968 ▶ if ($digit < scalar(@{$state->[$pos]})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit < scalar @{$state->[$pos];}) 1556 1412 2968
183 1556 last;
184 }
185 1412 $pos++;
186 }
187  
188 ### $pos
189 ### $digit
190  
191 1716 my $value = $state->[$pos]->[$digit];
192 1716 ▶ while ($pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 0) 1412 1716 3128
193 ### $value
194 1412 my $newtable = $state->[--$pos] = $self->{'map'}->[$value];
195 1412 $digits->[$pos] = 0;
196 1412 $value = $newtable->[0];
197 }
198  
199 ### now digits: $digits
200 ### now state: $state
201 ### final value: $value
202  
203 1716 return ($self->{'i'}++, $value);
204 }
205  
206 1727 ▶sub ith {
Subroutine ith: called CC=5 SCAR=16.1
Pod documented
207 1727 my ($self, $i) = @_;
208 ### AsciiSelf ith(): "$i"
209  
210 1727 ▶ if (_is_infinite($i) || ($i -= 1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($i) or ($i -= 1) < 0) 4 1723 1727
Condition66%2/3
_is_infinite($i) or ($i -= 1) < 0
l!l&&r!l&&!r
041723
MC/DC50%1/2
_is_infinite($i) or ($i -= 1) < 0
_is_infinite($i) ($i -= 1) < 0
Truth table66%2/3
A or B
A= _is_infinite($i)B= ($i -= 1) < 0
ABresult
1X1
011
000
211 4 return undef;
212 }
213  
214 1723 my $map = $self->{'map'};
215 1723 my $width = $self->{'width'};
216 1723 my @digits;
217 1723 ▶ while ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 7644 1723 9367
218 7644 push @digits, $i % $width;
219 7644 $i = int($i/$width);
220 }
221  
222 1723 my $value = $self->{'start'};
223 1723 ▶ while (@digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits) 7644 1723 9367
224 7644 my $digit = pop @digits;
225 7644 $value = $map->[$value]->[$digit];
226 }
227 1723 return $value;
228 }
229  
230 4698 ▶sub _radix_ascii {
Subroutine _radix_ascii: called CC=2 SCAR=6.9
231 4698 my ($radix, $n) = @_;
232 4698 my @digits;
233 4698 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 10431 4698 15129
234 10431 my $digit = ($n % $radix);
235 10431 push @digits, _digit_to_ascii($digit);
236 10431 $n = int($n/$radix);
237 }
238 4698 return reverse @digits;
239 }
240  
241 11735 ▶sub _digit_to_ascii {
Subroutine _digit_to_ascii: called CC=2 SCAR=6.9
242 11735 my ($digit) = @_;
243 ### assert: $digit >= 0
244 ### assert: $digit < 36
245 11735 ▶ return $digit + ($digit < 10 ? 48 : 65-10); # '0' or 'A'
Branch: 1 branch
BranchTrueFalseTotal
$digit < 10 ? : 9060 2675 11735
246 }
247  
248 2 1;
249 __END__
« NumSeq/ConcatNumbers.pm ↑ Summary NumSeq/Catalan.pm »