Hilbert.pm

filter: Statementstmt 94.4% 85 / 90 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.4% 17 / 18 Podpod 100% 1 / 1 totaltotal 93.3% 113 / 121 CC 7 SCAR 5.0
CC
7
Cov
93%
_rot
3.0
BEGIN
2.3
_extract_side
2.0
CRAP 1.7
« Hilbert/BigInt.pm ↑ Summary
1 package Path::Hilbert;
2  
3 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our @EXPORT = qw( xy2d d2xy );
9  
10 2 our $VERSION = '2.000';
11  
12 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.4
13 2 ▶ if (eval "require Path::Hilbert::XS") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'require Path::Hilbert::XS') { } 0 2 2
14 0 *xy2d = \&Path::Hilbert::XS::xy2d;
15 0 *d2xy = \&Path::Hilbert::XS::d2xy;
16 }
17 else {
18 2 *xy2d = \&_xy2d;
19 2 *d2xy = \&_d2xy;
20 }
21 };
22  
23 # optional constructor if you want OO-style
24 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
25 0 my $class = shift;
26 0 my ($n) = @_;
27 0 return bless { n => $n } => $class;
28 }
29  
30 # convert (x,y) to d
31 1360 ▶sub _xy2d {
Subroutine _xy2d: called CC=2 SCAR=6.9
32 1360 my ($side, $x, $y) = @_;
33 1360 my $n = _valid_n($side);
34 1360 my ($X, $Y) = map { int($_ + 0.5) } ($x, $y);
35 1360 my $D;
36 {
37 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1360 my $d = 0;
39 1360 my ($x, $y) = map { int($_) } ($X, $Y);
40 1360 ▶ for (my $s = $n / 2; $s > 0; $s /= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 0) 19720 1360 21080
41 19720 my $rx = ($x & $s) > 0;
42 19720 my $ry = ($y & $s) > 0;
43 19720 $d += $s * $s * ((3 * $rx) ^ $ry);
44 19720 ($x, $y) = _rot($s, $x, $y, $rx, $ry);
45 }
46 2 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1360 $D = $d;
48 }
49 1360 return $D * _side_scale($side);
50 }
51  
52 # convert d to (x,y)
53 4080 ▶sub _d2xy {
Subroutine _d2xy: called CC=2 SCAR=6.9
54 4080 my ($side, $d) = @_;
55 4080 my $n = _valid_n($side);
56 4080 my $T = int($d + 0.5);
57 4080 my ($X, $Y);
58 {
59 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 4080 my ($x, $y) = (0, 0);
61 4080 my $t = int($T);
62 4080 ▶ for (my $s = 1; $s < $n; $s *= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s < $n) 59160 4080 63240
63 59160 my $rx = 1 & ($t / 2);
64 59160 my $ry = 1 & ($t ^ $rx);
65 59160 ($x, $y) = _rot($s, $x, $y, $rx, $ry);
66 59160 $x += $s * $rx;
67 59160 $y += $s * $ry;
68 59160 $t /= 4;
69 }
70 2 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 4080 ($X, $Y) = ($x, $y);
72 }
73 4080 return map { _side_scale($side) * $_ } ($X, $Y);
74 }
75  
76 # rotate/flip a quadrant appropriately
77 78880 ▶sub _rot {
Subroutine _rot: called CC=3 SCAR=11.0
78 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 78880 my ($n, $x, $y, $rx, $ry) = map { int($_) } @_;
80 78880 ▶ if (!$ry) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ry) 72268 6612 78880
81 72268 ▶ if ($rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx) 2448 69820 72268
82 2448 $x = $n - 1 - $x;
83 2448 $y = $n - 1 - $y;
84 }
85 72268 ($x, $y) = ($y, $x);
86 }
87 78880 return ($x, $y);
88 }
89  
90 14960 ▶sub _valid_n {
Subroutine _valid_n: called CC=1 SCAR=0.0
91 14960 my $n = _extract_side(shift(@_));
92 2 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 14960 my $rv = 2 ** int((log($n) / log(2)) + 0.5);
94 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 14960 return int($rv);
96 }
97  
98 24480 ▶sub _extract_side {
Subroutine _extract_side: called CC=2 SCAR=7.1
99 24480 my ($n) = @_;
100 24480 ▶ $n = $n->{ n } if ref($n);
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 24480 24480
101 24480 return $n;
102 }
103  
104 9520 ▶sub _side_scale {
Subroutine _side_scale: called CC=1 SCAR=0.0
105 9520 my $side = _extract_side(shift(@_));
106 9520 my $n = _valid_n($side);
107 9520 return $side / $n;
108 }
109  
110 2 1;
111  
112 __END__
« Hilbert/BigInt.pm ↑ Summary