PlanePath/HIndexing.pm

filter: Statementstmt 84.3% 145 / 172 Branchbran 72.4% 42 / 58 Conditioncond 42.8% 6 / 14 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 75.0% 15 / 20 Podpod 100% 5 / 5 totaltotal 77.5% 214 / 276 CC 35 SCAR 22.0
CC
35
Cov
79%
n_to_xy
15.0
n_to_level
12.0
xy_to_n
11.6
CRAP 9.1
« PlanePath/ImaginaryHalf.pm ↑ Summary PlanePath/CoprimeColumns.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 # http://theinf1.informatik.uni-jena.de/~niedermr/publications.html
20 #
21 # Rolf Niedermeier
22 # http://fpt.akt.tu-berlin.de/niedermr/publications.html
23 #
24 #
25 # H second part down per paper
26 # |
27 # | *--* * *-
28 # | | | | |
29 # | * *--* *
30 # | | |
31 # | * *--* *
32 # | | | | |
33 # | O * *--*
34 # |
35 # +------------
36 #
37 # eight similar to AlternatePaper
38 #
39 # |
40 # *--* *--* * *-
41 # | | | | | |
42 # --* * * *--* *--*
43 # | | |
44 # * * *--*--*--*
45 # | | |
46 # *--* * O *--*--*--*
47 # | |
48 # *--*--*--* * * *--*
49 # | | |
50 # *--*--*--* * * *-
51 # | | |
52 # *--* *--* * * *-
53 # | | | | | |
54 # *--* *--*
55 #
56  
57 package Math::PlanePath::HIndexing;
58 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 $VERSION = 128;
63  
64 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 @ISA = ('Math::PlanePath::Base::NSEW',
67 'Math::PlanePath');
68  
69 use Math::PlanePath::Base::Generic
70 1 'is_infinite',
71 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 use Math::PlanePath::Base::Digits
73 1 'round_down_pow',
74 'round_up_pow',
75 1 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
77  
78  
79 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use constant diffxy_maximum => 0; # upper octant X<=Y so X-Y<=0
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use constant 1.02 _UNDOCUMENTED__dxdy_list_at_n => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85  
86 #------------------------------------------------------------------------------
87  
88 sub n_to_xy {
89 278 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=15 SCAR=27.1
Pod documented
90 ### HIndexing n_to_xy(): $n
91  
92 278 ▶ if ($n < 0) { # negative
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 278 278
93 0 return;
94 }
95 278 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 278 278
96 0 return ($n,$n);
97 }
98  
99 {
100 # ENHANCE-ME: get direction without full N+1 calculation
101 278 my $int = int($n);
102 ### $int
103 ### $n
104 278 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 60 218 278
105 60 my ($x1,$y1) = $self->n_to_xy($int);
106 60 my ($x2,$y2) = $self->n_to_xy($int+1);
107 60 my $frac = $n - $int; # inherit possible BigFloat
108 60 my $dx = $x2-$x1;
109 60 my $dy = $y2-$y1;
110 60 return ($frac*$dx + $x1, $frac*$dy + $y1);
111 }
112 218 $n = $int; # BigFloat int() gives BigInt, use that
113 }
114  
115 218 my $low = _divrem_mutate ($n, 2);
116 ### $low
117 ### $n
118  
119 218 my @digits = digit_split_lowtohigh($n,4);
120 218 my $len = ($n*0 + 2) ** scalar(@digits); # inherit bignum 2
121  
122 218 my $x = 0;
123 218 my $y = 0;
124 218 my $rev = 0;
125 218 my $xinvert = 0;
126 218 my $yinvert = 0;
127 218 ▶ while (@digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits) 916 218 1134
128 916 my $digit = pop @digits;
129  
130 ### $len
131 ### $rev
132 ### $digit
133  
134 916 my $new_xinvert = $xinvert;
135 916 my $new_yinvert = $yinvert;
136 916 my $xo = 0;
137 916 my $yo = 0;
138 916 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) { } 441 475 916
139 441 ▶ if ($digit == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($digit == 1) { } 48 393 441
elsif ($digit == 2) { } 140 253 393
elsif ($digit == 3) { } 109 144 253
140 48 $xo = $len-1;
141 48 $yo = $len-1;
142 48 $rev ^= 1;
143 48 $new_yinvert = $yinvert ^ 1;
144 } elsif ($digit == 2) {
145 140 $xo = 2*$len-2;
146 140 $yo = 0;
147 140 $rev ^= 1;
148 140 $new_xinvert = $xinvert ^ 1;
149 } elsif ($digit == 3) {
150 109 $xo = $len;
151 109 $yo = $len;
152 }
153  
154 } else {
155 475 ▶ if ($digit == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($digit == 1) { } 159 316 475
elsif ($digit == 2) { } 153 163 316
elsif ($digit == 3) { } 74 89 163
156 159 $xo = $len-2;
157 159 $yo = $len;
158 159 $rev ^= 1;
159 159 $new_xinvert = $xinvert ^ 1;
160 } elsif ($digit == 2) {
161 153 $xo = 1;
162 153 $yo = 2*$len-1;
163 153 $rev ^= 1;
164 153 $new_yinvert = $yinvert ^ 1;
165 } elsif ($digit == 3) {
166 74 $xo = $len;
167 74 $yo = $len;
168 }
169 }
170  
171 ### $xo
172 ### $yo
173  
174 916 ▶ if ($xinvert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xinvert) { } 363 553 916
175 363 $x -= $xo;
176 } else {
177 553 $x += $xo;
178 }
179 916 ▶ if ($yinvert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yinvert) { } 248 668 916
180 248 $y -= $yo;
181 } else {
182 668 $y += $yo;
183 }
184  
185 916 $xinvert = $new_xinvert;
186 916 $yinvert = $new_yinvert;
187 916 $len /= 2;
188 }
189  
190 ### final: "$x,$y"
191  
192 218 ▶ if ($yinvert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yinvert) { } 81 137 218
193 81 $y -= $low;
194 } else {
195 137 $y += $low;
196 }
197  
198 ### is: "$x,$y"
199 218 return ($x, $y);
200 }
201  
202 # uncomment this to run the ### lines
203 #use Smart::Comments;
204  
205 sub xy_to_n {
206 186 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=11 SCAR=24.5
Pod documented
207 ### HIndexing xy_to_n(): "$x, $y"
208  
209 186 $x = round_nearest ($x);
210 186 $y = round_nearest ($y);
211  
212 186 ▶ if ($x < 0 || $y < 0 || $x > $y - ($y&1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0 or $x > $y - ($y & 1)) 0 186 186
Condition33%2/6
$x < 0 or $y < 0 or $x > $y - ($y & 1)
l!l&&r!l&&!r
00186
$x < 0 or $y < 0
l!l&&r!l&&!r
00186
MC/DC0%0/3
$x < 0 or $y < 0 or $x > $y - ($y & 1)
$x < 0 $y < 0 $x > $y - ($y & 1)
Truth table25%1/4
A or B or C
A= $x < 0B= $y < 0C= $x > $y - ($y & 1)
ABCresult
1XX1
01X1
0011
0000
213 0 return undef;
214 }
215 186 ▶ if (is_infinite($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 186 186
216 0 return $x;
217 }
218 186 my ($len, $level) = round_down_pow (int($y/1), 2);
219 ### $len
220 ### $level
221 186 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 186 186
222 0 return $level;
223 }
224  
225 186 my $n = 0;
226 186 my $npower = $len*$len/2;
227 186 my $rev = 0;
228 186 ▶ while (--$level >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$level >= 0) 981 186 1167
229 ### at: "$x,$y rev=$rev len=$len n=$n"
230 981 my $digit;
231 981 my $new_rev = $rev;
232 981 ▶ if ($y >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= $len) { } 614 367 981
233 614 $y -= $len;
234 614 ▶ if ($x >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $len) { } 494 120 614
235 ### digit 3 ...
236 494 $digit = 3;
237 494 $x -= $len;
238 } else {
239 120 my $yinv = $len-1-$y;
240 ### digit 1 or 2: "y reduce to $y, x cmp ".($yinv-($yinv&1))
241 120 ▶ if ($x > $yinv-($yinv&1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $yinv - ($yinv & 1)) { } 57 63 120
242 ### digit 2, x invert to: $len-1-$x
243 57 $digit = 2;
244 57 $x = $len-1-$x;
245 } else {
246 ### digit 1, y invert to: $yinv
247 63 $digit = 1;
248 63 $y = $yinv;
249 }
250 120 $new_rev ^= 1;
251 }
252 } else {
253 ### digit 0 ...
254 367 $digit = 0;
255 }
256  
257 981 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) 89 892 981
258 89 $digit = 3 - $digit;
259 ### reversed digit: $digit
260 }
261 981 $rev = $new_rev;
262  
263 ### add n: $npower*$digit
264 981 $n += $npower*$digit;
265 981 $len /= 2;
266 981 $npower /= 4;
267 }
268  
269 ### end at: "$x,$y n=$n rev=$rev"
270 ### assert: $x == 0
271 ### assert: $y == 0 || $y == 1
272  
273 186 return $n + $y^$rev;
274 }
275  
276 # not exact
277 sub rect_to_n_range {
278 67 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=7 SCAR=21.5
Pod documented
279  
280 67 $x1 = round_nearest ($x1);
281 67 $y1 = round_nearest ($y1);
282 67 $x2 = round_nearest ($x2);
283 67 $y2 = round_nearest ($y2);
284 67 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 67 67
285 67 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 67 67
286 ### HIndexing rect_to_n_range(): "$x1,$y1 to $x2,$y2"
287  
288 # y2 & 1 excluding the X=Y diagonal on odd Y rows
289 67 ▶ if ($x2 < 0 || $y2 < 0 || $x1 > $y2 - ($y2&1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0 or $x1 > $y2 - ($y2 & 1)) 0 67 67
Condition33%2/6
$x2 < 0 or $y2 < 0 or $x1 > $y2 - ($y2 & 1)
l!l&&r!l&&!r
0067
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
0067
MC/DC0%0/3
$x2 < 0 or $y2 < 0 or $x1 > $y2 - ($y2 & 1)
$x2 < 0 $y2 < 0 $x1 > $y2 - ($y2 & 1)
Truth table25%1/4
A or B or C
A= $x2 < 0B= $y2 < 0C= $x1 > $y2 - ($y2 & 1)
ABCresult
1XX1
01X1
0011
0000
290 0 return (1, 0);
291 }
292  
293 67 ▶ my ($len, $level) = round_down_pow (($y2||1), 2);
Condition100%2/2
$y2 || 1
l!l
643
MC/DC100%1/1
$y2 || 1
$y2
Truth table100%2/2
A
A= $y2
Aresult
11
00
294 67 return (0, 2*$len*$len-1);
295 }
296  
297  
298 #------------------------------------------------------------------------------
299  
300 sub level_to_n_range {
301 0 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: not called CC=1 SCAR=6.9
Pod documented
302 0 return (0, 2*4**$level - 1);
303 }
304 sub n_to_level {
305 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
306 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
307 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
308 0 $n = round_nearest($n);
309 0 _divrem_mutate ($n, 2);
310 0 my ($pow,$exp) = round_up_pow ($n+1, 4);
311 0 return $exp;
312 }
313  
314 sub _UNDOCUMENTED__level_to_area {
315 0 ▶ my ($self, $level) = @_;
Subroutine _UNDOCUMENTED__level_to_area: not called CC=1 SCAR=6.9
316 0 return (2**$level - 1)**2;
317 }
318 sub _UNDOCUMENTED__level_to_area_Y {
319 0 ▶ my ($self, $level) = @_;
Subroutine _UNDOCUMENTED__level_to_area_Y: not called CC=2 SCAR=17.9
320 0 ▶ if ($level == 0) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) 0 0 0
321 0 return 2**(2*$level-1) - 2**$level;
322 }
323 sub _UNDOCUMENTED__level_to_area_up {
324 0 ▶ my ($self, $level) = @_;
Subroutine _UNDOCUMENTED__level_to_area_up: not called CC=2 SCAR=17.9
325 0 ▶ if ($level == 0) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) 0 0 0
326 0 return 2**(2*$level-1) - 2**$level + 1;
327 }
328  
329 #------------------------------------------------------------------------------
330 1 1;
331 __END__
« PlanePath/ImaginaryHalf.pm ↑ Summary PlanePath/CoprimeColumns.pm »