| 1 | package Path::Hilbert::BigInt; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.012; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Math::BigRat try => 'GMP,Pari,Calc'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Math::BigInt try => 'GMP,Pari,Calc'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT = qw( xy2d d2xy ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '2.000'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # optional constructor if you want OO-style | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 18 | 0 | my ($n) = @_; | |||||||||||||||||||||||||||
| 19 | 0 | return bless { n => $n } => $class; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # convert (x,y) to d | ||||||||||||||||||||||||||||
| 23 | 1360 | ▶ | sub xy2d { | ||||||||||||||||||||||||||
|
Subroutine
xy2d: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 1360 | my ($side, $x, $y) = @_; | |||||||||||||||||||||||||||
| 25 | 1360 | my $n = _valid_n($side); | |||||||||||||||||||||||||||
| 26 | 1360 | ($x, $y) = map { Math::BigInt->new("$_") } ($x, $y); | |||||||||||||||||||||||||||
| 27 | 1360 | my $d = Math::BigInt->bzero(); | |||||||||||||||||||||||||||
| 28 | 1360 | ▶ | for (my $s = $n->copy()->brsft(1); $s->bcmp(0) > 0; $s->brsft(1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 19720 | ▶ | my $rx = Math::BigInt->new($x->copy()->band($s)->bcmp(0) > 0 ? "1" : "0"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 19720 | ▶ | my $ry = Math::BigInt->new($y->copy()->band($s)->bcmp(0) > 0 ? "1" : "0"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 19720 | my $three_rx = $rx->copy()->bmul("3"); | |||||||||||||||||||||||||||
| 32 | 19720 | my $s_squared = $s->copy()->bpow("2"); | |||||||||||||||||||||||||||
| 33 | 19720 | $d->badd($s_squared->bmul($three_rx->bxor($ry))); | |||||||||||||||||||||||||||
| 34 | 19720 | ($x, $y) = _rot($s, $x, $y, $rx, $ry); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 1360 | return Math::BigRat->new($d); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # convert d to (x,y) | ||||||||||||||||||||||||||||
| 40 | 1360 | ▶ | sub d2xy { | ||||||||||||||||||||||||||
|
Subroutine
d2xy: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 1360 | my ($side, $d) = @_; | |||||||||||||||||||||||||||
| 42 | 1360 | my $n = _valid_n($side); | |||||||||||||||||||||||||||
| 43 | 1360 | my $t = Math::BigInt->new($d); | |||||||||||||||||||||||||||
| 44 | 1360 | my ($x, $y) = map { Math::BigInt->bzero() } (1 .. 2); | |||||||||||||||||||||||||||
| 45 | 1360 | ▶ | for (my $s = Math::BigInt->bone(); $s->bcmp($n) < 0; $s->blsft(1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 19720 | my $rx = $t->copy()->brsft(1)->band(Math::BigInt->bone()); | |||||||||||||||||||||||||||
| 47 | 19720 | my $ry = $t->copy()->bxor($rx)->band(Math::BigInt->bone()); | |||||||||||||||||||||||||||
| 48 | 19720 | ($x, $y) = _rot($s, $x, $y, $rx, $ry); | |||||||||||||||||||||||||||
| 49 | 19720 | my $Dx = $s->copy()->bmul($rx); | |||||||||||||||||||||||||||
| 50 | 19720 | my $Dy = $s->copy()->bmul($ry); | |||||||||||||||||||||||||||
| 51 | 19720 | ▶ | $Dx >= 0 ? $x->badd($Dx) : $x->bsub($Dx->copy()->babs()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 19720 | ▶ | $Dy >= 0 ? $y->badd($Dy) : $y->bsub($Dy->copy()->babs()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 19720 | $t->brsft(2); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 1360 | return map { Math::BigRat->new($_) } ($x, $y); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # rotate/flip a quadrant appropriately | ||||||||||||||||||||||||||||
| 59 | 39440 | ▶ | sub _rot { | ||||||||||||||||||||||||||
|
Subroutine
_rot: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 60 | 39440 | my ($n, $x, $y, $rx, $ry) = @_; | |||||||||||||||||||||||||||
| 61 | 39440 | ▶ | if (!$ry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 36134 | ▶ | if ($rx > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1224 | $x = $n - 1 - $x; | |||||||||||||||||||||||||||
| 64 | 1224 | $y = $n - 1 - $y; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 36134 | ($x, $y) = ($y, $x); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 39440 | return ($x, $y); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2720 | ▶ | sub _valid_n { | ||||||||||||||||||||||||||
|
Subroutine
_valid_n: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | 2720 | my $n = _extract_side(shift(@_)); | |||||||||||||||||||||||||||
| 73 | 2720 | ▶ | $n = 2 ** int((eval { (log($n) / log(2)) } || 0) + 0.5); | ||||||||||||||||||||||||||
|
Condition100%2/2
eval {
do {
log($n) / 0.69314718055994528623
}
} || 0
MC/DC100%1/1
eval {
do {
log($n) / 0.69314718055994528623
}
} || 0
eval {
do {
log($n) / 0.69314718055994528623
}
}
Truth table100%2/2
A
A= eval {
do {
log($n) / 0.69314718055994528623
}
}
| |||||||||||||||||||||||||||||
| 74 | 2720 | return Math::BigInt->new(int($n)); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2720 | ▶ | sub _extract_side { | ||||||||||||||||||||||||||
|
Subroutine
_extract_side: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 78 | 2720 | my ($side) = @_; | |||||||||||||||||||||||||||
| 79 | 2720 | ▶ | $side = $side->{ n } if ref($side) eq 'HASH' && exists $side->{ n }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $side eq "HASH" and exists $side->{"n"}
MC/DC0%0/2
ref $side eq "HASH" and exists $side->{"n"}
ref $side eq "HASH"
exists $side->{"n"}
Truth table33%1/3
A and B
A= ref $side eq "HASH"B= exists $side->{"n"}
| |||||||||||||||||||||||||||||
| 80 | 2720 | return $side; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | 1; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||