PlanePath/UlamWarburtonQuarter.pm

filter: Statementstmt 74.7% 181 / 242 Branchbran 60.1% 71 / 118 Conditioncond 61.5% 16 / 26 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 75.0% 21 / 28 Podpod 100% 15 / 15 totaltotal 69.5% 309 / 444 CC 73 SCAR 25.9
CC
73
Cov
69%
tree_n_to_subheight
56.0
xy_to_n
20.5
rect_to_n_range
12.4
CRAP 13.3
« PlanePath/PowerArray.pm ↑ Summary PlanePath.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath 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-PlanePath 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-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Math::PlanePath::UlamWarburtonQuarter;
20 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use List::Util 'sum';
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 = 128;
27 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 @ISA = ('Math::PlanePath');
29 2 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
30  
31 use Math::PlanePath::Base::Generic
32 2 'is_infinite',
33 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 use Math::PlanePath::Base::Digits
35 2 'round_down_pow',
36 'bit_split_lowtohigh',
37 'digit_split_lowtohigh',
38 2 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # uncomment this to run the ### lines
41 # use Smart::Comments;
42  
43  
44 2 use constant parameter_info_array =>
45 [
46 { name => 'parts',
47 share_key => 'parts_ulamwarburton_quarter',
48 display => 'Parts',
49 type => 'enum',
50 default => '1',
51 choices => ['1','octant','octant_up' ],
52 choices_display => ['1','Octant','Octant Up' ],
53 description => 'Which parts of the plane to fill.',
54 },
55 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
56 2 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 sub diffxy_minimum {
62 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
63 0 ▶ return ($self->{'parts'} eq 'octant' ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} eq 'octant' ? : 0 0 0
64 }
65 sub diffxy_maximum {
66 0 ▶ my ($self) = @_;
Subroutine diffxy_maximum: not called CC=2 SCAR=17.9
Pod documented
67 0 ▶ return ($self->{'parts'} eq 'octant_up' ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} eq 'octant_up' ? : 0 0 0
68 }
69  
70 # Minimum dir=0 at N=13 dX=2,dY=0.
71 # Maximum dir seems dX=13,dY=-9 at N=149 going top-left part to new bottom
72 # right diagonal.
73 2 my %dir_maximum_dxdy = (1 => [13,-9],
74 octant => [1,-1], # South-East
75 octant_up => [0,-1], # South
76 );
77 sub dir_maximum_dxdy {
78 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=1 SCAR=6.9
Pod documented
79 0 return @{$dir_maximum_dxdy{$self->{'parts'}}};
80 }
81  
82 sub tree_num_children_list {
83 0 ▶ my ($self) = @_;
Subroutine tree_num_children_list: not called CC=2 SCAR=17.9
Pod documented
84 0 ▶ return ($self->{'parts'} =~ /octant/
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parts'} =~ /octant/ ? : 0 0 0
85 ? (0, 1, 2, 3)
86 : (0, 1, 3));
87 }
88  
89 #------------------------------------------------------------------------------
90 sub new {
91 12 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=4 SCAR=14.3
Pod documented
92 12 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 8 4 12
93 8 $self->{'n_start'} = $self->default_n_start;
94 }
95 12 ▶ my $parts = ($self->{'parts'} ||= '1');
Condition50%1/2
$self->{"parts"} ||= 1
l!l
012
MC/DC0%0/1
$self->{"parts"} ||= 1
$self->{"parts"}
Truth table50%1/2
A
A= $self->{"parts"}
Aresult
11
00
96 12 ▶ if (! exists $dir_maximum_dxdy{$parts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $dir_maximum_dxdy{$parts}) 0 12 12
97 0 croak "Unrecognised parts option: ", $parts;
98 }
99 12 return $self;
100 }
101  
102 # 7 7 7 7
103 # 6 6
104 # 7 5 5 7
105 # 4
106 # 3 3 5 7
107 # 2 6
108 # 1 3 7 7
109 #
110 # 1+1+3=5
111 # 5+1+3*5=21
112 # 1+3 = 4
113 # 1+3+3+9 = 16
114 #
115 # 0
116 # 1 0 +1
117 # 2 1 +1 <- 1
118 # 3 2 +3
119 # 4 5 +1 <- 1 + 4 = 5
120 # 5 6 +3
121 # 6 9 +3
122 # 7 12 +9
123 # 8 21 <- 1 + 4 + 16 = 21
124  
125 # 1+3 = 4 power 2
126 # 1+3+3+9 = 16 power 3
127 # 1+3+3+9+3+9+9+27 = 64 power 4
128 #
129 # (1+4+16+...+4^(l-1)) = (4^l-1)/3
130 # l=1 total=(4-1)/3 = 1
131 # l=2 total=(16-1)/3 = 5
132 # l=3 total=(64-1)/3=63/3 = 21
133 #
134 # n = 1 + (4^l-1)/3
135 # n-1 = (4^l-1)/3
136 # 3n-3 = (4^l-1)
137 # 3n-2 = 4^l
138 #
139 # 3^0+3^1+3^1+3^2 = 1+3+3+9=16
140 # x+3x+3x+9x = 16x = 256
141 #
142 # 22
143 # 20 19 18 17
144 # 12 11
145 # 21 9 8 16
146 # 6
147 # 5 4 7 15
148 # 2 10
149 # 1 3 13 14
150 #
151  
152 sub n_to_xy {
153 117 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=10 SCAR=25.0
Pod documented
154 ### UlamWarburtonQuarter n_to_xy(): $n
155  
156 117 ▶ if ($n < $self->{'n_start'}) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $self->{"n_start"}) 0 117 117
157 117 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 117 117
158  
159 {
160 117 my $int = int($n);
161 ### $int
162 ### $n
163 117 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 117 117
164 0 my ($x1,$y1) = $self->n_to_xy($int);
165 0 my ($x2,$y2) = $self->n_to_xy($int+1);
166 0 my $frac = $n - $int; # inherit possible BigFloat
167 0 my $dx = $x2-$x1;
168 0 my $dy = $y2-$y1;
169 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
170 }
171 117 $n = $int; # BigFloat int() gives BigInt, use that
172 }
173  
174 117 $n = $n - $self->{'n_start'} + 1; # N=1 basis
175 117 ▶ if ($n == 1) { return (0,0); }
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) 5 112 117
176  
177 112 ▶ my ($depthsum, $nrem, $rowwidth) = _n1_to_depthsum_rem_width($self,$n)
Branch: 1 branch
BranchTrueFalseTotal
() = _n1_to_depthsum_rem_width($self, $n) or return $n, $n 0 112 112
178 or return ($n,$n); # N==nan or N==+inf
179  
180 ### assert: $nrem >= 0
181 ### assert: $nrem < $width
182 112 ▶ if ($self->{'parts'} eq 'octant_up') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parts"} eq "octant_up") 0 112 112
183 0 $nrem += ($rowwidth-1)/2;
184 ### assert: $nrem < $width
185 }
186  
187 112 my @ndigits = digit_split_lowtohigh($nrem,3);
188 112 my $dhigh = shift(@$depthsum) - 1; # highest term
189 112 my $x = 0;
190 112 my $y = 0;
191 112 foreach my $depthsum (reverse @$depthsum) { # depth terms low to high
192 258 my $ndigit = shift @ndigits; # N digits low to high
193 ### $depthsum
194 ### $ndigit
195  
196 258 $x += $depthsum;
197 258 $y += $depthsum;
198 ### depthsum to xy: "$x,$y"
199  
200 258 ▶ if ($ndigit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigit) { } 174 84 258
201 174 ▶ if ($ndigit == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigit == 2) 87 87 174
202 87 ($x,$y) = (-$y,$x); # rotate +90
203 }
204 } else {
205 # digit==0 (or undef when run out of @ndigits)
206 84 ($x,$y) = ($y,-$x); # rotate -90
207 }
208 ### rotate to: "$x,$y"
209 }
210  
211 ### final: "$x,$y"
212 112 return ($dhigh + $x, $dhigh + $y);
213 }
214  
215 sub xy_to_n {
216 560 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=20 SCAR=30.2
Pod documented
217 ### UlamWarburtonQuarter xy_to_n(): "$x, $y"
218  
219 560 $x = round_nearest ($x);
220 560 $y = round_nearest ($y);
221 560 my $parts = $self->{'parts'};
222 560 ▶ if ($y < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y < 0 or $x < ($parts eq "octant" ? $y : 0) or $parts eq "octant_up" and $x > $y) 115 231 346
$parts eq 'octant' ? : 0 346 346
Condition66%6/9
$y < 0 or $x < ($parts eq "octant" ? $y : 0) or $parts eq "octant_up" and $x > $y
l!l&&r!l&&!r
3290231
$y < 0 or $x < ($parts eq "octant" ? $y : 0)
l!l&&r!l&&!r
214115231
$parts eq "octant_up" and $x > $y
!ll&&!rl&&r
23100
MC/DC0%0/4
$y < 0 or $x < ($parts eq "octant" ? $y : 0) or $parts eq "octant_up" and $x > $y
$y < 0 $x < ($parts eq "octant" ? $y : 0) $parts eq "octant_up" $x > $y
Truth table40%2/5
A or B or C and D
A= $y < 0B= $x < ($parts eq "octant" ? $y : 0)C= $parts eq "octant_up"D= $x > $y
ABCDresult
1XXX1
01XX1
00111
000X0
00100
223 || $x < ($parts eq 'octant' ? $y : 0)
224 || ($parts eq 'octant_up' && $x > $y)) {
225 329 return undef;
226 }
227 231 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) 6 225 231
Condition100%3/3
$x == 0 and $y == 0
!ll&&!rl&&r
210156
MC/DC100%2/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table100%3/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
228 6 return $self->{'n_start'};
229 }
230 225 $x += 1; # pushed away by 1 ...
231 225 $y += 1;
232  
233 225 my ($len, $exp) = round_down_pow ($x + $y, 2);
234 225 ▶ if (is_infinite($exp)) { return $exp; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($exp)) 0 225 225
235  
236 225 my $depth
237 = my $n
238 = ($x * 0 * $y); # inherit bignum 0
239 225 my $rowwidth = $depth + 1;
240  
241 225 ▶ while ($exp-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp-- >= 0) 851 134 985
242 ### at: "$x,$y n=$n len=$len"
243  
244 # first quadrant square
245 ### assert: $x >= 0
246 ### assert: $y >= 0
247 # ### assert: $x < 2*$len
248 # ### assert: $y < 2*$len
249  
250 851 ▶ if ($x >= $len || $y >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $len or $y >= $len) 546 305 851
Condition100%3/3
$x >= $len or $y >= $len
l!l&&r!l&&!r
404142305
MC/DC100%2/2
$x >= $len or $y >= $len
$x >= $len $y >= $len
Truth table100%3/3
A or B
A= $x >= $lenB= $y >= $len
ABresult
1X1
011
000
251 # one of three quarters away from origin
252 # +---+---+
253 # | 2 | 1 |
254 # +---+---+
255 # | | 0 |
256 # +---+---+
257  
258 546 $x -= $len;
259 546 $y -= $len;
260 ### shift to: "$x,$y"
261  
262 546 ▶ if ($x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x) { } 367 179 546
263 367 ▶ unless ($y) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y) 46 321 367
264 46 return undef; # x==0, y!=0, nothing
265 }
266 } else {
267 179 ▶ if ($y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y) 45 134 179
268 45 return undef; # x!=0, y-=0, nothing
269 }
270 }
271  
272 455 $depth += $len;
273 455 ▶ if ($x || $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or $y) 321 134 455
Condition66%2/3
$x or $y
l!l&&r!l&&!r
3210134
MC/DC50%1/2
$x or $y
$x $y
Truth table66%2/3
A or B
A= $xB= $y
ABresult
1X1
011
000
274 321 $rowwidth *= 3;
275 321 $n *= 3;
276 321 ▶ if ($y < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y < 0) { } 104 217 321
elsif ($x >= 0) { } 103 114 217
277 ### bottom right, digit 0 ...
278 104 ($x,$y) = (-$y,$x); # rotate +90
279 } elsif ($x >= 0) {
280 ### top right, digit 1 ...
281 103 $n += 1;
282 } else {
283 ### top left, digit 2 ...
284 114 ($x,$y) = ($y,-$x); # rotate -90
285 114 $n += 2;
286 }
287 }
288 }
289  
290 760 $len /= 2;
291 }
292  
293 ### $n
294 ### $depth
295  
296 134 ▶ if ($self->{'parts'} eq 'octant_up') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parts"} eq "octant_up") 0 134 134
297 0 $n -= ($rowwidth-1)/2;
298 }
299  
300 134 return $n + $self->tree_depth_to_n($depth-1);
301 }
302  
303 # not exact
304 sub rect_to_n_range {
305 50 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=10 SCAR=25.2
Pod documented
306 ### UlamWarburtonQuarter rect_to_n_range(): "$x1,$y1 $x2,$y2"
307  
308 50 $x1 = round_nearest ($x1);
309 50 $y1 = round_nearest ($y1);
310 50 $x2 = round_nearest ($x2);
311 50 $y2 = round_nearest ($y2);
312  
313 50 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 50 50
314 50 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 50 50
315  
316 50 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 50 50
Condition33%1/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
0050
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0 $y2 < 0
Truth table33%1/3
A or B
A= $x2 < 0B= $y2 < 0
ABresult
1X1
011
000
317 0 return (1, 0); # all outside first quadrant
318 }
319  
320 50 ▶ if ($x1 < 0) { $x1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 50 50
321 50 ▶ if ($y1 < 0) { $y1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 50 50
322  
323 # level numbers
324 50 ▶ my $dlo = ($x1 > $y1 ? $x1 : $y1)+1;
Branch: 1 branch
BranchTrueFalseTotal
$x1 > $y1 ? : 24 26 50
325 50 ▶ my $dhi = ($x2 > $y2 ? $x2 : $y2);
Branch: 1 branch
BranchTrueFalseTotal
$x2 > $y2 ? : 24 26 50
326 ### $dlo
327 ### $dhi
328  
329 # round down to level=2^k numbers
330 50 ▶ if ($dlo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dlo) 50 0 50
331 50 ($dlo) = round_down_pow ($dlo,2);
332 }
333 50 ($dhi) = round_down_pow ($dhi,2);
334  
335 ### rounded to pow2: "$dlo ".(2*$dhi)
336  
337 50 return ($self->tree_depth_to_n($dlo-1),
338 $self->tree_depth_to_n(2*$dhi-1));
339 }
340  
341 #------------------------------------------------------------------------------
342 2 ▶use constant tree_num_roots => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
343  
344 # ENHANCE-ME: step by the bits, not by X,Y
345 sub tree_n_children {
346 9 ▶ my ($self, $n) = @_;
Subroutine tree_n_children: called CC=5 SCAR=16.1
Pod documented
347 9 ▶ if ($n < $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $self->{"n_start"}) 0 9 9
348 0 return;
349 }
350 9 my ($x,$y) = $self->n_to_xy($n);
351 9 my @ret;
352 9 my $dx = 1;
353 9 my $dy = 1;
354 9 foreach (1 .. 4) {
355 36 ▶ if (defined (my $n_child = $self->xy_to_n($x+$dx,$y+$dy))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n_child = $self->xy_to_n($x + $dx, $y + $dy))) 19 17 36
356 19 ▶ if ($n_child > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_child > $n) 11 8 19
357 11 push @ret, $n_child;
358 }
359 }
360 36 ($dx,$dy) = (-$dy,$dx); # rotate +90
361 }
362 9 return sort {$a<=>$b} @ret;
363 }
364 sub tree_n_parent {
365 12 ▶ my ($self, $n) = @_;
Subroutine tree_n_parent: called CC=5 SCAR=16.1
Pod documented
366 12 ▶ if ($n <= $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $self->{"n_start"}) 1 11 12
367 1 return undef;
368 }
369 11 my ($x,$y) = $self->n_to_xy($n);
370 11 my $dx = 1;
371 11 my $dy = 1;
372 11 foreach (1 .. 4) {
373 33 ▶ if (defined (my $n_parent = $self->xy_to_n($x+$dx,$y+$dy))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n_parent = $self->xy_to_n($x + $dx, $y + $dy))) 24 9 33
374 24 ▶ if ($n_parent < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_parent < $n) 11 13 24
375 11 return $n_parent;
376 }
377 }
378 22 ($dx,$dy) = (-$dy,$dx); # rotate +90
379 }
380 0 return undef;
381 }
382  
383 # level = depth+1 = 2^a + 2^b + 2^c + 2^d ... a>b>c>d...
384 # Ndepth = 1 + (-1
385 # + 4^a
386 # + 3 * 4^b
387 # + 3^2 * 4^c
388 # + 3^3 * 4^d + ...) / 3
389 sub tree_depth_to_n {
390 903 ▶ my ($self, $depth) = @_;
Subroutine tree_depth_to_n: called CC=6 SCAR=18.8
Pod documented
391 ### tree_depth_to_n(): $depth
392 903 ▶ if (is_infinite($depth)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($depth)) 0 903 903
393 0 return $depth;
394 }
395 903 ▶ unless ($depth >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($depth >= 0) 0 903 903
396 0 return undef;
397 }
398 903 my $n = $depth*0; # inherit bignum 0
399 903 my $pow3 = 1 + $n; # inherit bignum 1
400 903 foreach my $bit (reverse bit_split_lowtohigh($depth+1)) { # high to low
401 3800 $n *= 4;
402 3800 ▶ if ($bit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit) 1991 1809 3800
403 1991 $n += $pow3;
404 1991 $pow3 *= 3;
405 }
406 }
407 903 ▶ if ($self->{'parts'} =~ /octant/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parts'} =~ /octant/) { } 0 903 903
408 0 $n = ($n + (3*$depth-1))/6;
409 } else {
410 903 $n = ($n-1)/3;
411 }
412 903 return $n + $self->{'n_start'};
413 }
414  
415 sub tree_n_to_depth {
416 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_to_depth: not called CC=3 SCAR=24.8
Pod documented
417  
418 0 $n = int($n - $self->{'n_start'} + 1); # N=1 basis
419 0 ▶ if ($n < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 0 0
420 0 return undef;
421 }
422 0 ▶ (my $depthsum, $n) = _n1_to_depthsum_rem_width($self,$n)
Branch: 1 branch
BranchTrueFalseTotal
(my $depthsum, $n) = _n1_to_depthsum_rem_width($self, $n) or return $n 0 0 0
423 or return $n; # N==nan or N==+infinity
424 0 return sum(-1, @$depthsum);
425 }
426  
427 # Return ($aref, $remaining_n).
428 # sum(@$aref) = depth starting depth=1
429 #
430 # depth+1 = 2^k
431 # Ndepth(depth) = (4^k+2)/3
432 # 3N-2 = 4^k
433 # NdepthOct(depth) = ((4^k+2)/3 + 2^k)/2
434 # 6N-2 = 4^k + 3*2^k
435 #
436 sub _n1_to_depthsum_rem_width {
437 112 ▶ my ($self, $n) = @_;
Subroutine _n1_to_depthsum_rem_width: called CC=8 SCAR=23.1
438 ### _n1_to_depthsum_rem_width(): $n
439  
440 112 my $octant = ($self->{'parts'} =~ /octant/);
441 112 ▶ my ($power, $exp) = round_down_pow (($octant ? 6 : 3)*$n - 2, 4);
Branch: 1 branch
BranchTrueFalseTotal
$octant ? : 0 112 112
442 112 ▶ if (is_infinite($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($exp)) 0 112 112
443 0 return;
444 }
445  
446 ### $power
447 ### $exp
448 ### pow base: ($power - 1)/3 + 1
449  
450 {
451 112 my $sub = ($power + 2)/3; # (power-1)/3 + 1
452 112 ▶ if ($octant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($octant) 0 112 112
453 0 $sub = ($sub + 2**$exp) / 2;
454 ### prospective sub: $sub
455 ### assert: $sub == ($power + 3 * 2 ** $exp + 2)/6
456  
457 0 ▶ if ($sub > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub > $n) 0 0 0
458 0 $exp -= 1;
459 0 $power /= 4;
460 0 $sub = ($power + 3*2**$exp + 2)/6;
461 }
462 }
463 ### assert: $sub <= $n
464 112 $n -= $sub;
465 }
466 ### n less pow base: $n
467  
468 112 my @depthsum = (2**$exp);
469  
470 # find the cumulative levelpoints total <= $n, being the start of the
471 # level containing $n
472 #
473 112 my $factor = 1;
474 112 ▶ while (--$exp >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$exp >= 0) 383 112 495
475 383 $power /= 4;
476 383 my $sub = $power * $factor;
477 383 ▶ if ($octant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($octant) 0 383 383
478 0 $sub = ($sub + 2**$exp)/2;
479 }
480 ### $sub
481 383 my $rem = $n - $sub;
482  
483 ### $n
484 ### $power
485 ### $factor
486 ### consider subtract: $sub
487 ### $rem
488  
489 383 ▶ if ($rem >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rem >= 0) 258 125 383
490 258 $n = $rem;
491 258 push @depthsum, 2**$exp;
492 258 $factor *= 3;
493 }
494 }
495  
496 ### _n1_to_depthsum_rem_width() result ...
497 ### @depthsum
498 ### remaining n: $n
499 ### assert: $n >= 0
500 ### assert: $n < $factor
501  
502 112 return (\@depthsum, $n, $factor);
503 }
504  
505  
506 # at 0,2 turn and new height limit
507 # at 1 keep existing depth limit
508 # N=30 rem=1 = 0,1 depth=11=8+2+1=1011 width=9
509 #
510 sub tree_n_to_subheight {
511 0 ▶ my ($self, $n) = @_;
Subroutine tree_n_to_subheight: not called CC=7 SCAR=40.3
Pod documented
512 ### tree_n_to_subheight(): $n
513  
514 0 $n = int($n - $self->{'n_start'} + 1); # N=1 basis
515 0 ▶ if ($n < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 0 0
516 0 return undef;
517 }
518 0 ▶ my ($depthsum, $nrem, $rowwidth) = _n1_to_depthsum_rem_width($self,$n)
Branch: 1 branch
BranchTrueFalseTotal
() = _n1_to_depthsum_rem_width($self, $n) or return $n 0 0 0
519 or return $n; # N==nan or N==+infinity
520 ### $depthsum
521 ### $nrem
522  
523 0 ▶ if ($self->{'parts'} eq 'octant_up') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parts"} eq "octant_up") 0 0 0
524 0 $nrem += ($rowwidth-1)/2;
525 }
526  
527 0 my $sub = pop @$depthsum;
528 0 ▶ while (@$depthsum && _divrem_mutate($nrem,3) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$depthsum and _divrem_mutate($nrem, 3) == 1) 0 0 0
Condition0%0/3
@$depthsum and _divrem_mutate($nrem, 3) == 1
!ll&&!rl&&r
000
MC/DC0%0/2
@$depthsum and _divrem_mutate($nrem, 3) == 1
@$depthsum _divrem_mutate($nrem, 3) == 1
Truth table0%0/3
A and B
A= @$depthsumB= _divrem_mutate($nrem, 3) == 1
ABresult
0X0
100
111
529 0 $sub += pop @$depthsum;
530 }
531 0 ▶ if (@$depthsum) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$depthsum) { } 0 0 0
532 0 return $depthsum->[-1] - 1 - $sub;
533 } else {
534 0 return undef; # $nrem all 1-digits
535 }
536 }
537  
538 #------------------------------------------------------------------------------
539 # levels
540  
541 sub level_to_n_range {
542 27 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
543 27 return ($self->{'n_start'},
544 $self->tree_depth_to_n_end(2**($level+1) - 2));
545 }
546 sub n_to_level {
547 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=2 SCAR=17.9
Pod documented
548 0 my $depth = $self->tree_n_to_depth($n);
549 0 ▶ if (! defined $depth) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $depth) 0 0 0
550 0 my ($pow, $exp) = round_down_pow ($depth+1, 2);
551 0 return $exp;
552 }
553  
554 #------------------------------------------------------------------------------
555 2 1;
556 __END__
« PlanePath/PowerArray.pm ↑ Summary PlanePath.pm »