NumSeq/OEIS/File.pm

filter: Statementstmt 25.5% 103 / 404 Branchbran 11.2% 23 / 204 Conditioncond 11.0% 11 / 100 MC/DCmcdc 6.7% 4 / 59 Subroutinesub 51.2% 21 / 41 Podpod 80.0% 8 / 10 totaltotal 20.7% 170 / 818 CC 143 SCAR 49.7
CC
143
Cov
19%
_analyze
600.0
ith
272.0
_read_internal_txt
132.7
CRAP 143.3
« NumSeq/JugglerSteps.pm ↑ Summary NumSeq/PisanoPeriodSteps.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2016, 2018, 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  
19 # http://oeis.org/wiki/Clear-cut_examples_of_keywords
20 #
21 # ENHANCE-ME: share most of the a-file/b-file reading with Math::NumSeq::File
22 #
23 # FIXME: When using a b-file only, does i_start come out right? Meaning the
24 # first index in the file.
25  
26 package Math::NumSeq::OEIS::File;
27 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 $VERSION = 75;
36  
37 2 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 @ISA = ('Math::NumSeq');
39 2 *_to_bigint = \&Math::NumSeq::_to_bigint;
40  
41 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 $VERSION = 75;
43  
44 2 ▶eval q{use Scalar::Util 'weaken'; 1}
Subroutine BEGIN: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
eval "use Scalar::Util 'weaken'; 1" or eval "sub weaken { \$_[0] = undef }; 1 " or die "Oops, error making a weaken() fallback: $@" 2 0 2
Condition33%1/3
eval "use Scalar::Util 'weaken'; 1" or eval "sub weaken { \$_[0] = undef }; 1 "
l!l&&r!l&&!r
200
MC/DC0%0/2
eval "use Scalar::Util 'weaken'; 1" or eval "sub weaken { \$_[0] = undef }; 1 "
eval "use Scalar::Util 'weaken'; 1" eval "sub weaken { \$_[0] = undef }; 1 "
Truth table33%1/3
A or B
A= eval "use Scalar::Util 'weaken'; 1"B= eval "sub weaken { \$_[0] = undef }; 1 "
ABresult
1X1
011
000
45 || eval q{sub weaken { $_[0] = undef }; 1 }
46 || die "Oops, error making a weaken() fallback: $@";
47  
48 # uncomment this to run the ### lines
49 # use Smart::Comments;
50  
51  
52 # use constant name => Math::NumSeq::__('OEIS File');
53 2 ▶use Math::NumSeq::OEIS;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 *parameter_info_array = \&Math::NumSeq::OEIS::parameter_info_array;
55  
56 2 ▶use constant::defer _HAVE_ENCODE => sub {
Subroutine BEGIN: called CC=1 SCAR=2.2
Subroutine __ANON__: not called CC=2 SCAR=17.9
57 0 ▶ eval { require Encode; 1 } || 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Encode; 1 } } or 0 0 0 0
58 2 };
59  
60 0 ▶sub description {
Subroutine description: not called CC=3 SCAR=24.8
Pod documented
61 0 my ($class_or_self) = @_;
62 0 ▶ if (ref $class_or_self && defined $class_or_self->{'description'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_self and defined $class_or_self->{'description'}) { } 0 0 0
Condition0%0/3
ref $class_or_self and defined $class_or_self->{"description"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $class_or_self and defined $class_or_self->{"description"}
ref $class_or_self defined $class_or_self->{"description"}
Truth table0%0/3
A and B
A= ref $class_or_selfB= defined $class_or_self->{"description"}
ABresult
0X0
100
111
63 # instance
64 0 return $class_or_self->{'description'};
65 } else {
66 # class
67 0 return Math::NumSeq::__('OEIS sequence from file.');
68 }
69 }
70  
71 0 ▶sub values_min {
Subroutine values_min: not called CC=1 SCAR=6.9
Pod documented
72 0 my ($self) = @_;
73 ### OEIS-File values_min() ...
74 0 return _analyze($self)->{'values_min'};
75 }
76 0 ▶sub values_max {
Subroutine values_max: not called CC=1 SCAR=6.9
Pod documented
77 0 my ($self) = @_;
78 ### OEIS-File values_max() ...
79 0 return _analyze($self)->{'values_max'};
80 }
81  
82 2 my %analyze_characteristics = (increasing => 1,
83 increasing_from_i => 1,
84 non_decreasing => 1,
85 non_decreasing_from_i => 1,
86 smaller => 1,
87 );
88 0 ▶sub characteristic {
Subroutine characteristic: not called CC=2 SCAR=17.9
Pod documented
89 0 my ($self, $key) = @_;
90 0 ▶ if ($analyze_characteristics{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($analyze_characteristics{$key}) 0 0 0
91 0 _analyze($self);
92 }
93 0 return shift->SUPER::characteristic(@_);
94 }
95  
96 246 ▶sub oeis_dir {
Subroutine oeis_dir: called CC=1 SCAR=0.0
Pod undocumented
97 246 require File::HomeDir;
98 246 return File::Spec->catfile (File::HomeDir->my_home, 'OEIS');
99 }
100 0 ▶sub anum_to_bfile {
Subroutine anum_to_bfile: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my ($anum, $prefix) = @_;
102 0 ▶ $prefix ||= 'b';
Condition0%0/2
$prefix ||= "b"
l!l
00
MC/DC0%0/1
$prefix ||= "b"
$prefix
Truth table0%0/2
A
A= $prefix
Aresult
11
00
103 0 $anum =~ s/^A/$prefix/;
104 0 return "$anum.txt";
105 }
106  
107 #------------------------------------------------------------------------------
108 # Keep track of all instances which exist and on an ithread CLONE re-open
109 # any filehandles in the instances, so they have their own independent file
110 # positions in the new thread.
111  
112 2 my %instances;
113 65 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
114 65 my ($self) = @_;
115 65 delete $instances{$self+0};
116 }
117 0 ▶sub CLONE {
Subroutine CLONE: not called CC=5 SCAR=34.0
118 0 my ($class) = @_;
119 0 foreach my $self (values %instances) {
120 0 ▶ next unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
121 0 ▶ next unless $self->{'fh'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fh"} 0 0 0
122 0 my $pos = _tell($self);
123 0 my $fh = gensym;
124 0 ▶ if (open $fh, "< $self->{'filename'}") {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, "< $self->{'filename'}") { } 0 0 0
125 0 $self->{'fh'} = $fh;
126 0 _seek ($self, $pos);
127 } else {
128 0 delete $self->{'fh'};
129 0 delete $self->{'filename'};
130 }
131 }
132 }
133  
134 #------------------------------------------------------------------------------
135  
136 # The length in decimal digits of the biggest value which fits in a plain
137 # Perl integer. For example on a 32-bit system this is 9 since 9 digit
138 # numbers such as "999_999_999" are the biggest which fit a signed IV
139 # (+2^31).
140 #
141 # The IV size is probed rather than using ~0 since under "perl -Minteger"
142 # have ~0 as -1 rather than the biggest UV ... except "use integer" is not
143 # normally global.
144 #
145 # The NV size is applied to the limit too since not sure should trust values
146 # to stay in IV or UV. This means on a 64-bit integer with 53-bit NV
147 # "double" the limit is 53-bits.
148 #
149 2 ▶use constant 1.02 _MAX_DIGIT_LENGTH => do {
Subroutine BEGIN: called CC=5 SCAR=16.1
150 ### ~0 is: ~0
151  
152 2 my $iv = 0;
153 2 for (1 .. 256) {
154 130 my $new = ($iv << 1) | 1;
155 130 ▶ unless ($new > $iv && ($new & 1) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new > $iv and ($new & 1) == 1) 2 128 130
Condition66%2/3
$new > $iv and ($new & 1) == 1
!ll&&!rl&&r
20128
MC/DC50%1/2
$new > $iv and ($new & 1) == 1
$new > $iv ($new & 1) == 1
Truth table66%2/3
A and B
A= $new > $ivB= ($new & 1) == 1
ABresult
0X0
100
111
156 2 last;
157 }
158 128 $iv = $new;
159 }
160 ### $iv
161  
162 2 require POSIX;
163 2 my $nv = POSIX::FLT_RADIX() ** (POSIX::DBL_MANT_DIG()-5);
164 ### $nv
165  
166 2 my $iv_len = length($iv) - 1;
167 2 my $nv_len = length($nv) - 1;
168 2 ▶ ($iv_len < $nv_len ? $iv_len : $nv_len) # smaller of the two lengths;
Branch: 1 branch
BranchTrueFalseTotal
$iv_len < $nv_len ? : 0 2 2
169 2 };
170 ### _MAX_DIGIT_LENGTH: _MAX_DIGIT_LENGTH()
171  
172  
173 #------------------------------------------------------------------------------
174  
175 # special case a000000.txt files to exclude
176 #
177 2 my %afile_exclude
178 = (
179 # a003849.txt has replication level words rather than the individual
180 # sequence values.
181 'a003849.txt' => 1,
182  
183 # a027750.txt is unflattened divisors as lists.
184 # Its first line is a correct looking "1 1" so _afile_is_good() doesn't
185 # notice.
186 'a027750.txt' => 1,
187 );
188  
189  
190 # Fields:
191 # fh File handle ref, if reading B-file or A-file
192 #
193 # next_seek File pos to seek $fh for next() to read from.
194 # ith() sets this when it moves the file position.
195 #
196 # array Arrayref of values if using .internal or .html.
197 # array_pos Index 0,1,2,... of next value of $array to return by next().
198 #
199 # i Next $i for next() to return.
200 # When reading a file this is ignored, use the file i instead.
201  
202 65 ▶sub new {
Subroutine new: called CC=15 SCAR=36.1
Pod documented
203 ### OEIS-File new() ...
204 65 my $self = shift->SUPER::new(@_);
205  
206 65 delete $self->{'next_seek'}; # no initial seek
207 65 $self->{'characteristic'}->{'integer'} = 1;
208  
209 65 my $anum = $self->{'anum'};
210 65 (my $num = $anum) =~ s/^A//;
211 65 ▶ if (defined(my $filename = $self->{'_b_filename'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filename = $self->{'_b_filename'})) { } 0 65 65
212 0 $self->{'filename'} = $filename; # the B-file or A-file name
213 0 my $fh = gensym();
214 0 $self->{'fh'} = $fh;
215 0 ▶ open $fh, "< $filename"
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "< $filename" or croak("Cannot open ${filename}: ", $!) 0 0 0
216 or croak "Cannot open $filename: ", $!;
217 0 $self->{'fh_i'} = $self->i_start; # at first entry
218  
219 } else {
220 65 foreach my $basefile ("a$num.txt",
221 "b$num.txt"
222 ) {
223 130 ▶ next if $afile_exclude{$basefile};
Branch: 1 branch
BranchTrueFalseTotal
if $afile_exclude{$basefile} 10 120 130
224  
225 120 ▶ next if $self->{'_dont_use_afile'} && $basefile =~ /^a/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dont_use_afile"} and $basefile =~ /^a/ 44 76 120
Condition100%3/3
$self->{"_dont_use_afile"} and $basefile =~ /^a/
!ll&&!rl&&r
245244
MC/DC100%2/2
$self->{"_dont_use_afile"} and $basefile =~ /^a/
$self->{"_dont_use_afile"} $basefile =~ /^a/
Truth table100%3/3
A and B
A= $self->{"_dont_use_afile"}B= $basefile =~ /^a/
ABresult
0X0
100
111
226 76 ▶ next if $self->{'_dont_use_bfile'} && $basefile =~ /^b/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dont_use_bfile"} and $basefile =~ /^b/ 39 37 76
Condition66%2/3
$self->{"_dont_use_bfile"} and $basefile =~ /^b/
!ll&&!rl&&r
37039
MC/DC50%1/2
$self->{"_dont_use_bfile"} and $basefile =~ /^b/
$self->{"_dont_use_bfile"} $basefile =~ /^b/
Truth table66%2/3
A and B
A= $self->{"_dont_use_bfile"}B= $basefile =~ /^b/
ABresult
0X0
100
111
227  
228 37 my $filename = File::Spec->catfile (oeis_dir(), $basefile);
229 ### $filename
230 37 my $fh = gensym();
231 37 ▶ if (! open $fh, "< $filename") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "< $filename") 37 0 37
232 ### cannot open: $!
233 37 next;
234 }
235  
236 0 $self->{'filename'} = $filename; # the B-file or A-file name
237 0 $self->{'fh'} = $fh;
238 0 ▶ if (! _afile_is_good($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_afile_is_good($self)) 0 0 0
239 ### this afile not good ...
240 0 close delete $self->{'fh'};
241 0 delete $self->{'filename'};
242 0 next;
243 }
244 ### opened: $fh
245 0 $self->{'fh_i'} = $self->i_start; # at first entry
246 0 last;
247 }
248 }
249  
250  
251 65 ▶ my $have_info = (_read_internal_txt($self, $anum)
Condition33%2/6
_read_internal_txt($self, $anum) || _read_internal_html($self, $anum) || _read_html($self, $anum)
l!l&&r!l&&!r
0065
_read_internal_txt($self, $anum) or _read_internal_html($self, $anum)
l!l&&r!l&&!r
0065
MC/DC0%0/3
_read_internal_txt($self, $anum) || _read_internal_html($self, $anum) || _read_html($self, $anum)
_read_internal_txt($self, $anum) _read_internal_html($self, $anum) _read_html($self, $anum)
Truth table25%1/4
A or B || C
A= _read_internal_txt($self, $anum)B= _read_internal_html($self, $anum)C= _read_html($self, $anum)
ABCresult
1XX1
01X1
0011
0000
252 || _read_internal_html($self, $anum)
253 || _read_html($self, $anum));
254  
255 65 ▶ if (! $have_info && ! $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_info or $self->{"fh"}) 65 0 65
Condition33%1/3
$have_info or $self->{"fh"}
l!l&&r!l&&!r
0065
MC/DC0%0/2
$have_info or $self->{"fh"}
$have_info $self->{"fh"}
Truth table33%1/3
A or B
A= $have_infoB= $self->{"fh"}
ABresult
1X1
011
000
256 65 croak 'OEIS file(s) not found for A-number "',$anum,'"';
257 }
258  
259 0 weaken($instances{$self+0} = $self);
260 0 return $self;
261 }
262  
263 0 ▶sub _analyze {
Subroutine _analyze: not called CC=24 SCAR=64.0
264 0 my ($self) = @_;
265  
266 0 ▶ if ($self->{'analyze_done'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"analyze_done"}) 0 0 0
267 0 return $self;
268 }
269 0 $self->{'analyze_done'} = 1;
270  
271 ### _analyze() ...
272  
273 0 my $i_start = $self->i_start;
274 0 my ($i, $value);
275 0 my ($prev_i, $prev_value);
276  
277 0 my $values_min;
278 0 my $values_max;
279 0 my $increasing_from_i = $i_start;
280 0 my $non_decreasing_from_i = $i_start;
281 0 my $strictly_smaller_count = 0;
282 0 my $smaller_count = 0;
283 0 my $total_count = 0;
284  
285 0 ▶ my $analyze = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
286 ### $prev_value
287 ### $value
288 0 ▶ if (! defined $values_min || $value < $values_min) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $values_min or $value < $values_min) 0 0 0
Condition0%0/3
not defined $values_min or $value < $values_min
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $values_min or $value < $values_min
not defined $values_min $value < $values_min
Truth table0%0/3
not(A) or B
A= not defined $values_minB= $value < $values_min
ABresult
1X1
011
000
289 0 $values_min = $value;
290 }
291 0 ▶ if (! defined $values_max || $value > $values_max) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $values_max or $value > $values_max) 0 0 0
Condition0%0/3
not defined $values_max or $value > $values_max
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $values_max or $value > $values_max
not defined $values_max $value > $values_max
Truth table0%0/3
not(A) or B
A= not defined $values_maxB= $value > $values_max
ABresult
1X1
011
000
292 0 $values_max = $value;
293 }
294  
295 0 ▶ if (defined $prev_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_value) 0 0 0
296 0 my $cmp = ($value <=> $prev_value);
297 0 ▶ if ($cmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp < 0) 0 0 0
298 # value < $prev_value
299 0 $increasing_from_i = $i;
300 0 $non_decreasing_from_i = $i;
301 }
302 0 ▶ if ($cmp <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp <= 0) 0 0 0
303 # value <= $prev_value
304 0 $increasing_from_i = $i;
305 }
306 }
307  
308 0 $total_count++;
309 0 $smaller_count += (abs($value) <= $i);
310 0 $strictly_smaller_count += ($value < $i);
311  
312 0 $prev_i = $value;
313 0 $prev_value = $value;
314 0 };
315  
316 0 ▶ if (my $fh = $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->{'fh'}) { } 0 0 0
317 0 my $oldpos = _tell($self);
318 0 ▶ while (($i, $value) = _readline($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _readline($self)) 0 0 0
319 0 $analyze->($value);
320 0 ▶ last if $total_count > 200;
Branch: 1 branch
BranchTrueFalseTotal
if $total_count > 200 0 0 0
321 }
322 0 _seek ($self, $oldpos);
323 } else {
324 0 $i = $i_start;
325 0 foreach (@{$self->{'array'}}) {
326 0 $i++;
327 0 $value = $_;
328 0 $analyze->();
329 }
330 }
331  
332 0 ▶ my $range_is_small = (defined $values_max
Condition0%0/3
defined $values_max && $values_max - $values_min <= 16
!ll&&!rl&&r
000
MC/DC0%0/2
defined $values_max && $values_max - $values_min <= 16
defined $values_max $values_max - $values_min <= 16
Truth table0%0/3
A && B
A= defined $values_maxB= $values_max - $values_min <= 16
ABresult
0X0
100
111
333 && $values_max - $values_min <= 16);
334 ### $range_is_small
335  
336 # "full" means whole sequence in sample values
337 # "sign" means negatives in sequence
338 0 ▶ if (! defined $self->{'values_min'}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"values_min"} and $range_is_small || $self->{"characteristic"}{"OEIS_full"} || !$self->{"characteristic"}{"OEIS_sign"}) 0 0 0
Condition0%0/9
not defined $self->{"values_min"} and $range_is_small || $self->{"characteristic"}{"OEIS_full"} || !$self->{"characteristic"}{"OEIS_sign"}
!ll&&!rl&&r
000
$range_is_small or $self->{"characteristic"}{"OEIS_full"} or not $self->{"characteristic"}{"OEIS_sign"}
l!l&&r!l&&!r
000
$range_is_small or $self->{"characteristic"}{"OEIS_full"}
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $self->{"values_min"} and $range_is_small || $self->{"characteristic"}{"OEIS_full"} || !$self->{"characteristic"}{"OEIS_sign"}
not defined $self->{"values_min"} $range_is_small $self->{"characteristic"}{"OEIS_full"} not $self->{"characteristic"}{"OEIS_sign"}
Truth table0%0/5
not(A) and B or C or not(D)
A= not defined $self->{"values_min"}B= $range_is_smallC= $self->{"characteristic"}{"OEIS_full"}D= not $self->{"characteristic"}{"OEIS_sign"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
339 && ($range_is_small
340 || $self->{'characteristic'}->{'OEIS_full'}
341 || ! $self->{'characteristic'}->{'OEIS_sign'})) {
342 ### set values_min: $values_min
343 0 $self->{'values_min'} = $values_min;
344 }
345 0 ▶ if (! defined $self->{'values_max'}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"values_max"} and $range_is_small || $self->{"characteristic"}{"OEIS_full"}) 0 0 0
Condition0%0/6
not defined $self->{"values_max"} and $range_is_small || $self->{"characteristic"}{"OEIS_full"}
!ll&&!rl&&r
000
$range_is_small or $self->{"characteristic"}{"OEIS_full"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $self->{"values_max"} and $range_is_small || $self->{"characteristic"}{"OEIS_full"}
not defined $self->{"values_max"} $range_is_small $self->{"characteristic"}{"OEIS_full"}
Truth table0%0/4
not(A) and B or C
A= not defined $self->{"values_max"}B= $range_is_smallC= $self->{"characteristic"}{"OEIS_full"}
ABCresult
0XX0
1000
11X1
1011
346 && ($range_is_small
347 || $self->{'characteristic'}->{'OEIS_full'})) {
348 ### set values_max: $values_max
349 0 $self->{'values_max'} = $values_max;
350 }
351  
352 0 ▶ $self->{'characteristic'}->{'smaller'}
Condition0%0/6
$total_count == 0 || $smaller_count / $total_count >= 0.9 && $strictly_smaller_count > 0
l!l&&r!l&&!r
000
$smaller_count / $total_count >= "0.9" and $strictly_smaller_count > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$total_count == 0 || $smaller_count / $total_count >= 0.9 && $strictly_smaller_count > 0
$total_count == 0 $smaller_count / $total_count >= "0.9" $strictly_smaller_count > 0
Truth table0%0/4
A || B and C
A= $total_count == 0B= $smaller_count / $total_count >= "0.9"C= $strictly_smaller_count > 0
ABCresult
1XX1
0111
00X0
0100
353 = ($total_count == 0
354 || ($smaller_count / $total_count >= .9
355 && $strictly_smaller_count > 0));
356 ### decide smaller: $self->{'characteristic'}->{'smaller'}
357  
358 ### $increasing_from_i
359 0 ▶ if (defined $prev_i && $increasing_from_i < $prev_i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_i and $increasing_from_i < $prev_i) 0 0 0
Condition0%0/3
defined $prev_i and $increasing_from_i < $prev_i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prev_i and $increasing_from_i < $prev_i
defined $prev_i $increasing_from_i < $prev_i
Truth table0%0/3
A and B
A= defined $prev_iB= $increasing_from_i < $prev_i
ABresult
0X0
100
111
360 0 ▶ if ($increasing_from_i - $i_start < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($increasing_from_i - $i_start < 20) 0 0 0
361 0 $self->{'characteristic'}->{'increasing_from_i'} = $increasing_from_i;
362 }
363 0 ▶ if ($increasing_from_i == $i_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($increasing_from_i == $i_start) 0 0 0
364 0 $self->{'characteristic'}->{'increasing'} = 1;
365 }
366 }
367  
368 ### $non_decreasing_from_i
369 0 ▶ if (defined $prev_i && $non_decreasing_from_i < $prev_i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_i and $non_decreasing_from_i < $prev_i) 0 0 0
Condition0%0/3
defined $prev_i and $non_decreasing_from_i < $prev_i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prev_i and $non_decreasing_from_i < $prev_i
defined $prev_i $non_decreasing_from_i < $prev_i
Truth table0%0/3
A and B
A= defined $prev_iB= $non_decreasing_from_i < $prev_i
ABresult
0X0
100
111
370 0 ▶ if ($non_decreasing_from_i - $i_start < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($non_decreasing_from_i - $i_start < 20) 0 0 0
371 0 $self->{'characteristic'}->{'non_decreasing_from_i'} = $non_decreasing_from_i;
372 }
373 0 ▶ if ($non_decreasing_from_i == $i_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($non_decreasing_from_i == $i_start) 0 0 0
374 0 $self->{'characteristic'}->{'non_decreasing'} = 1;
375 }
376 }
377  
378 0 return $self;
379 }
380  
381 # # compare $x <=> $y but in strings in case they're bigger than IV or NV
382 # # my $cmp = _value_cmp ($value, $prev_value);
383 # sub _value_cmp {
384 # my ($x, $y) = @_;
385 # ### _value_cmp(): "$x $y"
386 # ### cmp: $x cmp $y
387 #
388 # my $x_neg = substr($x,0,1) eq '-';
389 # my $y_neg = substr($y,0,1) eq '-';
390 # ### $x_neg
391 # ### $y_neg
392 #
393 # return ($y_neg <=> $x_neg
394 # || ($x_neg ? -1 : 1) * (length($x) <=> length($y)
395 # || $x cmp $y));
396 # }
397  
398 0 ▶sub _seek {
Subroutine _seek: not called CC=2 SCAR=17.9
399 0 my ($self, $pos) = @_;
400 0 ▶ seek ($self->{'fh'}, $pos, 0)
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"fh"}, $pos, 0 or croak(qq[Cannot seek $self->{"filename"}: $!]) 0 0 0
401 or croak "Cannot seek $self->{'filename'}: $!";
402 }
403 0 ▶sub _tell {
Subroutine _tell: not called CC=2 SCAR=17.9
404 0 my ($self) = @_;
405 0 my $pos = tell $self->{'fh'};
406 0 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 0 0 0
407 0 croak "Cannot tell file position $self->{'filename'}: $!";
408 }
409 0 return $pos;
410 }
411  
412 65 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
413 65 my ($self) = @_;
414 ### OEIS-File rewind() ...
415  
416 65 $self->{'i'} = $self->i_start;
417 65 $self->{'array_pos'} = 0;
418 65 $self->{'next_seek'} = 0;
419 }
420  
421 0 ▶sub next {
Subroutine next: not called CC=4 SCAR=30.0
Pod documented
422 0 my ($self) = @_;
423 ### OEIS-File next(): "i=$self->{'i'}"
424  
425 0 my $value;
426 0 ▶ if (my $fh = $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->{'fh'}) { } 0 0 0
427 ### from readline ...
428 0 ▶ if (defined (my $pos = delete $self->{'next_seek'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pos = delete $self->{"next_seek"})) 0 0 0
429 ### seek to: $pos
430 0 _seek($self, $pos);
431 }
432 0 return _readline($self);
433  
434 } else {
435 ### from array ...
436 0 ▶ my ($value) = _array_value($self, $self->{'array_pos'}++)
Branch: 1 branch
BranchTrueFalseTotal
my($value) = _array_value($self, $self->{"array_pos"}++) or return 0 0 0
437 or return;
438 0 return ($self->{'i'}++, $value);
439 }
440 }
441  
442 # Return $self->{'array'}->[$pos], or no values if $pos past end of array.
443 # Array values are promoted to BigInt if necessary.
444 0 ▶sub _array_value {
Subroutine _array_value: not called CC=4 SCAR=30.0
445 0 my ($self, $pos) = @_;
446 ### _array_value(): $pos
447  
448 0 my $array = $self->{'array'};
449 0 ▶ if ($pos > $#$array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > $array->$#*) 0 0 0
450 ### past end of array ...
451 0 return;
452 }
453 0 my $value = $array->[$pos];
454  
455 # large values as Math::BigInt
456 # initially $array has strings, make bigint objects when required
457 0 ▶ if (! ref $value && length($value) > _MAX_DIGIT_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value and length $value > 14) 0 0 0
Condition0%0/3
not ref $value and length $value > 14
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $value and length $value > 14
not ref $value length $value > 14
Truth table0%0/3
not(A) and B
A= not ref $valueB= length $value > 14
ABresult
0X0
100
111
458 0 $value = $array->[$pos] = _to_bigint($value);
459 }
460 ### $value
461 0 return $value;
462 }
463  
464 # Read a line from an open B-file or A-file, return ($i,$value).
465 # At EOF return empty ().
466 #
467 0 ▶sub _readline {
Subroutine _readline: not called CC=5 SCAR=34.0
468 0 my ($self) = @_;
469 0 my $fh = $self->{'fh'};
470 0 ▶ while (defined (my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
471 0 chomp $line;
472 0 $line =~ tr/\r//d; # delete CR if CRLF line endings, eg. b009000.txt
473 ### $line
474  
475 0 ▶ if ($line =~ /^\s*(#|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(#|$)/) 0 0 0
476 ### ignore blank or comment ...
477 # comment lines with "#" eg. b002182.txt
478 0 next;
479 }
480  
481 # leading whitespace allowed as per b195467.txt
482 0 ▶ if (my ($i, $value) = ($line =~ /^\s*
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /^\s* ([0-9]+) # i [ \t]+ (-?[0-9]+) # value [ \t]* $/x) 0 0 0
483 ([0-9]+) # i
484 [ \t]+
485 (-?[0-9]+) # value
486 [ \t]*
487 $/x)) {
488 ### _readline: "$i $value"
489 0 ▶ if (length($value) > _MAX_DIGIT_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > 14) 0 0 0
490 0 $value = _to_bigint($value);
491 }
492 0 $self->{'fh_i'} = $i+1;
493 0 return ($i, $value);
494 }
495 }
496 0 undef $self->{'fh_i'};
497 0 return;
498 }
499  
500 # Return true if the a000000.txt file in $self->{'fh'} looks good.
501 # Various a-files are source code or tables rather than sequence values.
502 #
503 0 ▶sub _afile_is_good {
Subroutine _afile_is_good: not called CC=7 SCAR=40.3
504 0 my ($self) = @_;
505 0 my $fh = $self->{'fh'};
506 0 my $good = 0;
507 0 my $prev_i;
508 0 ▶ while (defined (my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
509 0 chomp $line;
510 0 $line =~ tr/\r//d; # delete CR if CRLF line endings, eg. b009000.txt
511 ### $line
512  
513 0 ▶ if ($line =~ /^\s*(#|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(#|$)/) 0 0 0
514 ### ignore blank or comment ...
515 0 next;
516 }
517  
518 # Must have line like "0 123". Can have negative OFFSET and so index i,
519 # eg. A166242 (though that one doesn't have an A-file).
520 0 ▶ my ($i,$value) = ($line =~ /^(-?[0-9]+) # i
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^(-?[0-9]+) # i [ \t]+ (-?[0-9]+) # value [ \t]* $/x or last 0 0 0
521 [ \t]+
522 (-?[0-9]+) # value
523 [ \t]*
524 $/x)
525 or last;
526  
527 0 ▶ if (defined $prev_i && $i != $prev_i+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_i and $i != $prev_i + 1) 0 0 0
Condition0%0/3
defined $prev_i and $i != $prev_i + 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prev_i and $i != $prev_i + 1
defined $prev_i $i != $prev_i + 1
Truth table0%0/3
A and B
A= defined $prev_iB= $i != $prev_i + 1
ABresult
0X0
100
111
528 ### bad A-file, initial "i" values not consecutive ...
529 0 last;
530 }
531 0 $prev_i = $i;
532  
533 0 $good++;
534 0 ▶ if ($good >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($good >= 3) 0 0 0
535 ### three good lines, A-file is good ...
536 0 _seek ($self, 0);
537 0 return 1;
538 }
539 }
540 0 return 0;
541 }
542  
543 65 ▶sub _read_internal_txt {
Subroutine _read_internal_txt: called CC=14 SCAR=48.9
544 65 my ($self, $anum) = @_;
545 ### _read_internal_txt(): $anum
546  
547 65 ▶ return 0 if $self->{'_dont_use_internal'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dont_use_internal"} 13 52 65
548  
549 52 foreach my $basefile ("$anum.internal.txt") {
550 52 ▶ my ($fullname, $contents) = _slurp_oeis_file($self,$basefile)
Branch: 1 branch
BranchTrueFalseTotal
() = _slurp_oeis_file($self, $basefile) or next 0 52 52
551 or next;
552 0 ▶ if (_HAVE_ENCODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_HAVE_ENCODE()) 0 0 0
553 # "Internal" text format is utf-8.
554 0 $contents = Encode::decode('utf-8', $contents, Encode::FB_PERLQQ());
555 }
556  
557 ### $contents
558  
559 # eg. "%O A007318 0,5" is OFFSET=0
560 0 my $offset;
561 0 ▶ if ($contents =~ /^%O\s+\Q$anum\E\s+(-?\d+)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /^%O\s+\Q$anum\E\s+(-?\d+)/m) { } 0 0 0
562 0 $offset = $1;
563 ### %O line: $offset
564 } else {
565 0 $offset = 0;
566 }
567  
568 # eg. "%N A007318 Pascal's triangle ..."
569 0 ▶ if ($contents =~ m{^%N\s+\Q$anum\E\s+(.*)}im) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /^%N\s+\Q$anum\E\s+(.*)/im) { } 0 0 0
570 0 _set_description ($self, $1);
571 } else {
572 ### description not matched ...
573 }
574  
575 # eg. "%K A007318 nonn,tabl,nice,easy,core,look,hear,changed"
576 0 ▶ _set_characteristics ($self,
Condition0%0/3
$contents =~ /^%K\s+\Q$anum\E\s+(.*)/im && $1
!ll&&!rl&&r
000
MC/DC0%0/2
$contents =~ /^%K\s+\Q$anum\E\s+(.*)/im && $1
$contents =~ /^%K\s+\Q$anum\E\s+(.*)/im $1
Truth table0%0/3
A && B
A= $contents =~ /^%K\s+\Q$anum\E\s+(.*)/imB= $1
ABresult
0X0
100
111
577 $contents =~ /^%K\s+\Q$anum\E\s+(.*)/im && $1);
578  
579 # the eishelp1.html says
580 # %V,%W,%X lines for signed sequences
581 # %S,%T,%U lines for non-negative sequences
582 # though now %S is signed and unsigned both is it?
583 #
584 0 ▶ if (! $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 0 0 0
585 0 my @samples;
586 # capital %STU etc, but any case <tt>
587 0 ▶ while ($contents =~ m{^%[VWX]\s+\Q$anum\E\s+(.*)}mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /^%[VWX]\s+\Q$anum\E\s+(.*)/gm) 0 0 0
588 0 push @samples, $1;
589 }
590 0 ▶ unless (@samples) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@samples) 0 0 0
591 0 ▶ while ($contents =~ m{^%[STU]\s+\Q$anum\E\s+(.*)}mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /^%[STU]\s+\Q$anum\E\s+(.*)/gm) 0 0 0
592 0 push @samples, $1;
593 }
594 0 ▶ unless (@samples) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@samples) 0 0 0
595 0 croak "Oops list of values not found in ",$self->{'filename'};
596 }
597 }
598 # join multiple lines of samples
599 0 _split_sample_values ($self, join(', ',@samples));
600 }
601  
602 # %O "OFFSET" is subscript of first number.
603 # Or for digit expansions it's the number of terms before the decimal
604 # point, per http://oeis.org/eishelp2.html#RO
605 #
606 ### digits: $self->{'characteristic'}->{'digits'}
607 0 ▶ unless ($self->{'characteristic'}->{'digits'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"characteristic"}{"digits"}) 0 0 0
608 0 $self->{'i'} = $self->{'i_start'} = $offset;
609 }
610 ### i: $self->{'i'}
611 ### i_start: $self->{'i_start'}
612  
613 0 return 1; # success
614 }
615  
616 52 return 0; # file not found
617 }
618  
619 65 ▶sub _read_internal_html {
Subroutine _read_internal_html: called CC=13 SCAR=47.2
620 65 my ($self, $anum) = @_;
621 ### _read_internal_html(): $anum
622  
623 65 ▶ return 0 if $self->{'_dont_use_internal'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dont_use_internal"} 13 52 65
624  
625 52 foreach my $basefile ("$anum.internal.html") {
626 52 ▶ my ($fullname, $contents) = _slurp_oeis_file($self,$basefile)
Branch: 1 branch
BranchTrueFalseTotal
() = _slurp_oeis_file($self, $basefile) or next 0 52 52
627 or next;
628 # "Internal" files are served as html with a <meta> charset indicator
629 0 $contents = _decode_html_charset($contents);
630 ### $contents
631  
632 0 my $offset;
633 0 ▶ if ($contents =~ /(^|<tt>)%O\s+(\d+)/im) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /(^|<tt>)%O\s+(\d+)/im) { } 0 0 0
634 0 $offset = $2;
635 ### %O line: $offset
636 } else {
637 0 $offset = 0;
638 }
639  
640 0 ▶ if ($contents =~ m{(^|<tt>)%N (.*?)(<tt>|$)}im) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /(^|<tt>)%N (.*?)(<tt>|$)/im) { } 0 0 0
641 0 _set_description ($self, $2);
642 } else {
643 ### description not matched ...
644 }
645  
646 0 ▶ _set_characteristics ($self,
Condition0%0/3
$contents =~ /(^|<tt>)%K (.*?)(<tt>|$)/im && $2
!ll&&!rl&&r
000
MC/DC0%0/2
$contents =~ /(^|<tt>)%K (.*?)(<tt>|$)/im && $2
$contents =~ /(^|<tt>)%K (.*?)(<tt>|$)/im $2
Truth table0%0/3
A && B
A= $contents =~ /(^|<tt>)%K (.*?)(<tt>|$)/imB= $2
ABresult
0X0
100
111
647 $contents =~ /(^|<tt>)%K (.*?)(<tt>|$)/im
648 && $2);
649  
650 # the eishelp1.html says
651 # %V,%W,%X lines for signed sequences
652 # %S,%T,%U lines for non-negative sequences
653 # though now %S is signed and unsigned both is it?
654 #
655 0 ▶ if (! $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 0 0 0
656 0 my @samples;
657 # capital %STU etc, but any case <tt>
658 0 ▶ while ($contents =~ m{(^|<[tT][tT]>)%[VWX] (.*?)(</[tT][tT]>|$)}mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ m[(^|<[tT][tT]>)%[VWX] (.*?)(</[tT][tT]>|$)]gm) 0 0 0
659 0 push @samples, $2;
660 }
661 0 ▶ unless (@samples) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@samples) 0 0 0
662 0 ▶ while ($contents =~ m{(^|<[tT][tT]>)%[STU] (.*?)(</[tT][tT]>|$)}mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ m[(^|<[tT][tT]>)%[STU] (.*?)(</[tT][tT]>|$)]gm) 0 0 0
663 0 push @samples, $2;
664 }
665 0 ▶ unless (@samples) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@samples) 0 0 0
666 0 croak "Oops list of values not found in ",$self->{'filename'};
667 }
668 }
669 # join multiple lines of samples
670 0 _split_sample_values ($self, join(', ',@samples));
671 }
672  
673 # %O "OFFSET" is subscript of first number.
674 # Or for digit expansions it's the number of terms before the decimal
675 # point, per http://oeis.org/eishelp2.html#RO
676 #
677 0 ▶ unless ($self->{'characteristic'}->{'digits'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"characteristic"}{"digits"}) 0 0 0
678 0 $self->{'i'} = $self->{'i_start'} = $offset;
679 }
680 ### i: $self->{'i'}
681 ### i_start: $self->{'i_start'}
682  
683 0 return 1; # success
684 }
685  
686 52 return 0; # file not found
687 }
688  
689 # Fill $self with contents of ~/OEIS/A000000.html but various fragile greps
690 # of the html.
691 # Return 1 if .html or .htm file exists, 0 if not.
692 #
693 65 ▶sub _read_html {
Subroutine _read_html: called CC=9 SCAR=38.5
694 65 my ($self, $anum) = @_;
695 ### _read_html(): $anum
696  
697 65 ▶ return 0 if $self->{'_dont_use_html'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dont_use_html"} 13 52 65
698  
699 52 foreach my $basefile ("$anum.html", "$anum.htm") {
700 104 ▶ my ($fullname, $contents) = _slurp_oeis_file($self,$basefile)
Branch: 1 branch
BranchTrueFalseTotal
() = _slurp_oeis_file($self, $basefile) or next 0 104 104
701 or next;
702 0 $contents = _decode_html_charset($contents);
703  
704 0 ▶ if ($contents =~
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ m[${anum}[ \t]*\r?\n.*? # target anum <td[^>]*>\s*(?:</td>)? # <td ...></td> empty <td[^>]*> # <td ...> \s* (.*?) # text through to ... (<br>|</?td) # <br> or </td> or <td> ]six) { } 0 0 0
705 m{$anum[ \t]*\r?\n.*? # target anum
706 <td[^>]*>\s*(?:</td>)? # <td ...></td> empty
707 <td[^>]*> # <td ...>
708 \s*
709 (.*?) # text through to ...
710 (<br>|</?td) # <br> or </td> or <td>
711 }isx) {
712 0 _set_description ($self, $1);
713 } else {
714 ### description not matched ...
715 }
716  
717 0 ▶ my $offset = ($contents =~ /OFFSET.*?<[tT][tT]>(\d+)/s
Condition0%0/3
$contents =~ /OFFSET.*?<[tT][tT]>(\d+)/s && $1
!ll&&!rl&&r
000
MC/DC0%0/2
$contents =~ /OFFSET.*?<[tT][tT]>(\d+)/s && $1
$contents =~ /OFFSET.*?<[tT][tT]>(\d+)/s $1
Truth table0%0/3
A && B
A= $contents =~ /OFFSET.*?<[tT][tT]>(\d+)/sB= $1
ABresult
0X0
100
111
718 && $1);
719 ### $offset
720  
721 # fragile grep out of the html ...
722 0 my $keywords;
723 0 ▶ if ($contents =~ m{KEYWORD.*?<[tT][tT][^>]*>(.*?)</[tT][tT]>}s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ m[KEYWORD.*?<[tT][tT][^>]*>(.*?)</[tT][tT]>]s) { } 0 0 0
724 0 $keywords = $1;
725 ### html keywords match: $keywords
726 } else {
727 # die "Oops, KEYWORD not matched: $anum";
728 }
729 0 _set_characteristics ($self, $keywords);
730  
731 0 ▶ if (! $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 0 0 0
732 # fragile grep out of the html ...
733 0 $contents =~ s{>graph</a>.*}{};
734 0 $contents =~ m{.*<tt>([^<]+)</tt>}i;
735 0 my $list = $1;
736 0 _split_sample_values ($self, $list);
737 }
738  
739 # %O "OFFSET" is subscript of first number, but for digit expansions
740 # it's the position of the decimal point
741 # http://oeis.org/eishelp2.html#RO
742 0 ▶ if (! $self->{'characteristic'}->{'digits'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"characteristic"}{"digits"}) 0 0 0
743 0 $self->{'i'} = $self->{'i_start'} = $offset;
744 }
745 ### i: $self->{'i'}
746 ### i_start: $self->{'i_start'}
747  
748 0 return 1;
749 }
750 52 return 0;
751 }
752  
753 # Return the contents of ~/OEIS/$filename.
754 # $filename is like "A000000.html" to be taken relative to oeis_dir().
755 # If $filename cannot be read then return undef.
756 208 ▶sub _slurp_oeis_file {
Subroutine _slurp_oeis_file: called CC=4 SCAR=22.6
757 208 my ($self,$filename) = @_;
758 208 $filename = File::Spec->catfile (oeis_dir(), $filename);
759 ### $filename
760  
761 208 ▶ if (! open FH, "< $filename") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FH, "< $filename") 208 0 208
762 ### cannot open file: $!
763 208 return;
764 }
765 0 my $contents = do { local $/; <FH> }; # slurp
766 0 ▶ close FH
Branch: 1 branch
BranchTrueFalseTotal
close FH or return 0 0 0
767 or return;
768 0 ▶ $self->{'filename'} ||= $filename;
Condition0%0/3
$self->{"filename"} ||= $filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"filename"} ||= $filename
$self->{"filename"} $filename
Truth table0%0/3
A ||= B
A= $self->{"filename"}B= $filename
ABresult
1X1
011
000
769 0 return ($filename, $contents);
770 }
771  
772 0 ▶sub _set_description {
Subroutine _set_description: not called CC=2 SCAR=17.9
773 0 my ($self, $description) = @_;
774 ### _set_description(): $description
775  
776 0 $description =~ s/\s+$//; # trailing whitespace
777 0 $description =~ s/\s+/ /g; # collapse whitespace
778 0 $description =~ s/<[^>]*?>//sg; # tags <foo ...>
779 0 $description =~ s/&lt;/</ig; # unentitize <
780 0 $description =~ s/&gt;/>/ig; # unentitize >
781 0 $description =~ s/&amp;/&/ig; # unentitize &
782 0 $description =~ s/&#(\d+);/chr($1)/ge; # unentitize numeric ' and "
783  
784 # ENHANCE-ME: maybe __x() if made available, or an sprintf "... %s" would
785 # be enough ...
786 0 $description .= "\n";
787 0 ▶ if ($self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fh'}) { } 0 0 0
788 $description .= sprintf(Math::NumSeq::__('Values from B-file %s'),
789 0 $self->{'filename'})
790 } else {
791 $description .= sprintf(Math::NumSeq::__('Values from %s'),
792 0 $self->{'filename'})
793 }
794 0 $self->{'description'} = $description;
795 }
796  
797 0 ▶sub _set_characteristics {
Subroutine _set_characteristics: not called CC=11 SCAR=48.8
798 0 my ($self, $keywords) = @_;
799 ### _set_characteristics()
800 ### $keywords
801  
802 0 ▶ if (! defined $keywords) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $keywords) 0 0 0
803 0 return; # if perhaps match of .html failed
804 }
805  
806 0 $keywords =~ s{<[^>]*>}{}g; # <foo ...> tags
807 ### $keywords
808  
809 0 ▶ foreach my $key (split /[, \t]+/, ($keywords||'')) {
Condition0%0/2
$keywords || ''
l!l
00
MC/DC0%0/1
$keywords || ''
$keywords
Truth table0%0/2
A
A= $keywords
Aresult
11
00
810 ### $key
811 0 $self->{'characteristic'}->{"OEIS_$key"} = 1;
812 }
813  
814 # if ($self->{'characteristic'}->{'OEIS_cofr'}) {
815 # $self->{'characteristic'}->{'continued_fraction'} = 1;
816 # }
817  
818 # "cons" means decimal digits of a constant
819 # but don't reckon A000012 all-ones that way
820 # "base" means non-decimal, it seems, maybe
821 0 ▶ if ($self->{'characteristic'}->{'OEIS_cons'}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"characteristic"}{"OEIS_cons"} and not $self->{"characteristic"}{"OEIS_base"} and $self->{"anum"} ne "A000012" and $self->{"anum"} ne "A000035") 0 0 0
Condition0%0/9
$self->{"characteristic"}{"OEIS_cons"} and not $self->{"characteristic"}{"OEIS_base"} and $self->{"anum"} ne "A000012" and $self->{"anum"} ne "A000035"
!ll&&!rl&&r
000
$self->{"characteristic"}{"OEIS_cons"} and not $self->{"characteristic"}{"OEIS_base"} and $self->{"anum"} ne "A000012"
!ll&&!rl&&r
000
$self->{"characteristic"}{"OEIS_cons"} and not $self->{"characteristic"}{"OEIS_base"}
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"characteristic"}{"OEIS_cons"} and not $self->{"characteristic"}{"OEIS_base"} and $self->{"anum"} ne "A000012" and $self->{"anum"} ne "A000035"
$self->{"characteristic"}{"OEIS_cons"} not $self->{"characteristic"}{"OEIS_base"} $self->{"anum"} ne "A000012" $self->{"anum"} ne "A000035"
Truth table0%0/5
A and not(B) and C and D
A= $self->{"characteristic"}{"OEIS_cons"}B= not $self->{"characteristic"}{"OEIS_base"}C= $self->{"anum"} ne "A000012"D= $self->{"anum"} ne "A000035"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
822 && ! $self->{'characteristic'}->{'OEIS_base'}
823 && $self->{'anum'} ne 'A000012'
824 && $self->{'anum'} ne 'A000035') { # 0,1 rep = mod 2
825 0 $self->{'values_min'} = 0;
826 0 $self->{'values_max'} = 9;
827 0 $self->{'characteristic'}->{'digits'} = 10;
828 }
829  
830 0 ▶ if (defined (my $description = $self->{'description'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $description = $self->{"description"})) 0 0 0
831 0 ▶ if ($description =~ /expansion of .* in base (\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description =~ /expansion of .* in base (\d+)/i) 0 0 0
832 0 $self->{'values_min'} = 0;
833 0 $self->{'values_max'} = $1 - 1;
834 0 $self->{'characteristic'}->{'digits'} = $1;
835 }
836 0 ▶ if ($description =~ /^number of /i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description =~ /^number of /i) 0 0 0
837 0 $self->{'characteristic'}->{'count'} = 1;
838 }
839 }
840 }
841  
842 0 ▶sub _split_sample_values {
Subroutine _split_sample_values: not called CC=4 SCAR=30.0
843 0 my ($self, $str) = @_;
844 ### _split_sample_values(): $str
845 0 ▶ unless (defined $str && $str =~ m{^([0-9,-]|\s)+$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str and $str =~ /^([0-9,-]|\s)+$/) 0 0 0
Condition0%0/3
defined $str and $str =~ /^([0-9,-]|\s)+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $str and $str =~ /^([0-9,-]|\s)+$/
defined $str $str =~ /^([0-9,-]|\s)+$/
Truth table0%0/3
A and B
A= defined $strB= $str =~ /^([0-9,-]|\s)+$/
ABresult
0X0
100
111
846 0 ▶ croak "Oops list of sample values not recognised in ",$self->{'filename'},"\n",
Branch: 1 branch
BranchTrueFalseTotal
defined $str ? : 0 0 0
847 (defined $str ? $str : ());
848 }
849 0 $self->{'array'} = [ split /[, \t\r\n]+/, $str ];
850 }
851  
852 0 ▶sub _decode_html_charset {
Subroutine _decode_html_charset: not called CC=3 SCAR=24.8
853 0 my ($contents) = @_;
854  
855 # eg. <META http-equiv="content-type" content="text/html; charset=utf-8">
856 # HTTP::Message has a blob of code for this, using the full HTTP::Parser,
857 # but a slack regexp should be enough for OEIS pages.
858 #
859 0 ▶ if (_HAVE_ENCODE
Branch: 1 branch
BranchTrueFalseTotal
if (&_HAVE_ENCODE() and $contents =~ /<META[^>]+ http-equiv=[^>]+ content-type[^>]+ charset=([a-z0-9-_]+)/six) { } 0 0 0
Condition0%0/3
&_HAVE_ENCODE() and $contents =~ /<META[^>]+ http-equiv=[^>]+ content-type[^>]+ charset=([a-z0-9-_]+)/six
!ll&&!rl&&r
000
MC/DC0%0/2
&_HAVE_ENCODE() and $contents =~ /<META[^>]+ http-equiv=[^>]+ content-type[^>]+ charset=([a-z0-9-_]+)/six
&_HAVE_ENCODE() $contents =~ /<META[^>]+ http-equiv=[^>]+ content-type[^>]+ charset=([a-z0-9-_]+)/six
Truth table0%0/3
A and B
A= &_HAVE_ENCODE()B= $contents =~ /<META[^>]+ http-equiv=[^>]+ content-type[^>]+ charset=([a-z0-9-_]+)/six
ABresult
0X0
100
111
860 && $contents =~ m{<META[^>]+
861 http-equiv=[^>]+
862 content-type[^>]+
863 charset=([a-z0-9-_]+)}isx) {
864 0 return Encode::decode($1, $contents, Encode::FB_PERLQQ());
865 } else {
866 0 return $contents;
867 }
868 }
869  
870 #------------------------------------------------------------------------------
871  
872 # Similar bsearch to Search::Dict, but Search::Dict doesn't allow for
873 # comment lines at the start of the file or blank lines at the end.
874 #
875 #use Smart::Comments;
876  
877 0 ▶sub ith {
Subroutine ith: not called CC=16 SCAR=56.1
Pod documented
878 0 my ($self, $i) = @_;
879 ### ith(): "$i cf fh_i=".($self->{'fh_i'} || -999)
880  
881 0 ▶ if (my $fh = $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->{'fh'}) { } 0 0 0
882 0 ▶ if (! defined $self->{'next_seek'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"next_seek"}) 0 0 0
883 0 $self->{'next_seek'} = tell($fh);
884 }
885  
886 0 ▶ if (defined $self->{'fh_i'} && $i <= $self->{'fh_i'} + 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fh_i"} and $i <= $self->{"fh_i"} + 20) 0 0 0
Condition0%0/3
defined $self->{"fh_i"} and $i <= $self->{"fh_i"} + 20
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"fh_i"} and $i <= $self->{"fh_i"} + 20
defined $self->{"fh_i"} $i <= $self->{"fh_i"} + 20
Truth table0%0/3
A and B
A= defined $self->{"fh_i"}B= $i <= $self->{"fh_i"} + 20
ABresult
0X0
100
111
887 ### fh_i is target ...
888 0 ▶ if (my ($line_i, $value) = _readline($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _readline($self)) 0 0 0
889 0 ▶ if ($line_i == $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_i == $i) 0 0 0
890 0 return $value;
891 }
892 }
893 }
894  
895 0 my $lo = 0;
896 0 my $hi = -s $fh;
897 0 for (;;) {
898 ### at: "lo=$lo hi=$hi consider mid=".int(($lo+$hi)/2)
899 0 my $mid = int(($lo+$hi)/2);
900 0 _seek ($self, $mid);
901  
902 0 ▶ if (! defined(readline $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined readline $fh) 0 0 0
903 ### mid is EOF ...
904 0 last;
905 }
906 ### skip partial line to: tell($fh)
907 0 $mid = tell($fh);
908 0 ▶ if ($mid >= $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid >= $hi) 0 0 0
909 0 last;
910 }
911  
912 0 ▶ my ($line_i,$value) = _readline($self)
Branch: 1 branch
BranchTrueFalseTotal
() = _readline($self) or last 0 0 0
913 or last; # only blank lines between $mid and EOF, go linear
914  
915 ### $line_i
916 ### $value
917 0 ▶ if ($line_i == $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_i == $i) 0 0 0
918 ### found by binary search ...
919 0 return $value;
920 }
921 0 ▶ if ($line_i < $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_i < $i) { } 0 0 0
922 ### line_i before the target, advance lo ...
923 0 $lo = tell($fh);
924 } else {
925 ### line_i after target, reduce hi ...
926 0 $hi = $mid;
927 }
928 }
929  
930 0 _seek ($self, $lo);
931 0 for (;;) {
932 0 ▶ my ($line_i,$value) = _readline($self)
Branch: 1 branch
BranchTrueFalseTotal
() = _readline($self) or last 0 0 0
933 or last;
934 0 ▶ if ($line_i == $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_i == $i) 0 0 0
935 ### found by linear search ...
936 0 $self->{'fh_i'} = $line_i+1;
937 0 return $value;
938 }
939 0 ▶ if ($line_i > $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_i > $i) 0 0 0
940 0 return undef;
941 }
942 }
943 0 return undef;
944  
945 } else {
946 0 $i -= $self->i_start;
947 0 ▶ unless ($i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i >= 0) 0 0 0
948 0 return undef; # negative or NaN
949 }
950 0 return $self->{'array'}->[$i];
951 }
952 }
953  
954 2 1;
955 __END__
« NumSeq/JugglerSteps.pm ↑ Summary NumSeq/PisanoPeriodSteps.pm »