| 1 | package Geohash; | ||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | our @EXPORT_OK = qw( ADJ_TOP ADJ_RIGHT ADJ_LEFT ADJ_BOTTOM ); | |||||||||||||||||||||||||||
| 8 | 9 | our %EXPORT_TAGS = (adjacent => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=9 SCAR=24.8
| |||||||||||||||||||||||||||||
| 11 | 9 | my @classes = qw( Geo::Hash::XS Geohash::backendPP ); | |||||||||||||||||||||||||||
| 12 | 9 | ▶ | if (my $backend = $ENV{PERL_GEOHASH_BACKEND}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | if ($backend eq 'Geo::Hash') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 14 | 0 | @classes = qw( Geohash::backendPP ); | |||||||||||||||||||||||||||
| 15 | } elsif ($backend eq '+Geo::Hash') { | ||||||||||||||||||||||||||||
| 16 | 0 | @classes = qw( Geo::Hash ); | |||||||||||||||||||||||||||
| 17 | } else { | ||||||||||||||||||||||||||||
| 18 | 0 | @classes = ( $backend ); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 9 | local $@; | |||||||||||||||||||||||||||
| 23 | 9 | my $class; | |||||||||||||||||||||||||||
| 24 | 9 | for (@classes) { | |||||||||||||||||||||||||||
| 25 | 18 | $class = $_; | |||||||||||||||||||||||||||
| 26 | 18 | ▶ | last if $class eq 'Geohash::backendPP'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | eval "use $class";## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 28 | 9 | ▶ | last unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 9 | ▶ | unless ($class eq 'Geohash::backendPP') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 11 | ▶ | sub backend_class { $class } | ||||||||||||||||||||||||||
|
Subroutine
backend_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 9 | ▶ | *ADJ_RIGHT = sub { &{"$class\::ADJ_RIGHT"} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 9 | ▶ | *ADJ_LEFT = sub { &{"$class\::ADJ_LEFT"} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 9 | ▶ | *ADJ_TOP = sub { &{"$class\::ADJ_TOP"} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 9 | ▶ | *ADJ_BOTTOM = sub { &{"$class\::ADJ_BOTTOM"} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 8 | my($class) = @_; | |||||||||||||||||||||||||||
| 45 | 8 | my $backend = $class->backend_class->new; | |||||||||||||||||||||||||||
| 46 | 8 | bless { | |||||||||||||||||||||||||||
| 47 | backend => $backend, | ||||||||||||||||||||||||||||
| 48 | }, $class; | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 9 | for my $method (qw/ encode decode decode_to_interval adjacent neighbors precision /) { | |||||||||||||||||||||||||||
| 53 | 58 | ▶ | my $code = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 58 | my $self = shift; | |||||||||||||||||||||||||||
| 55 | 58 | $self->{backend}->$method(@_); | |||||||||||||||||||||||||||
| 56 | 54 | }; | |||||||||||||||||||||||||||
| 57 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 54 | *{$method} = $code; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | my @ENC = qw( | |||||||||||||||||||||||||||
| 63 | 0 1 2 3 4 5 6 7 8 9 b c d e f g h j k m n p q r s t u v w x y z | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | 9 | my %ENC_MAP = map { $_ => 1 } @ENC; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 223 | ▶ | sub _merge_strip_last_char { | ||||||||||||||||||||||||||
|
Subroutine
_merge_strip_last_char: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 68 | 223 | my($self, $geohash) = @_; | |||||||||||||||||||||||||||
| 69 | 223 | my @results; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 223 | ▶ | if (length($geohash || '') < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$geohash || ''
MC/DC0%0/1
$geohash || ''
$geohash
Truth table50%1/2
A
A= $geohash
| |||||||||||||||||||||||||||||
| 72 | 2 | return ($geohash); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 221 | my($parent_geohash, $last_char) = $geohash =~ /^(.+)(.)$/; | |||||||||||||||||||||||||||
| 76 | 221 | ▶ | if ($last_char eq $ENC[0]) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 77 | 7 | $self->{cache}{$parent_geohash}{$last_char}++; | |||||||||||||||||||||||||||
| 78 | } elsif ($last_char eq $ENC[-1]) { | ||||||||||||||||||||||||||||
| 79 | 7 | $self->{cache}{$parent_geohash}{$last_char}++; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 7 | ▶ | if (scalar(keys %{ $self->{cache}{$parent_geohash} }) == scalar(@ENC)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 6 | push @results, $self->_merge_strip_last_char($parent_geohash); | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 1 | push @results, map { "$parent_geohash$_" } keys %{ $self->{cache}{$parent_geohash} }; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 7 | delete $self->{cache}{$parent_geohash}; | |||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 207 | ▶ | if ($self->{cache}{$parent_geohash} && $ENC_MAP{$last_char}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"cache"}{$parent_geohash} and $ENC_MAP{$last_char}
MC/DC50%1/2
$self->{"cache"}{$parent_geohash} and $ENC_MAP{$last_char}
$self->{"cache"}{$parent_geohash}
$ENC_MAP{$last_char}
Truth table66%2/3
A and B
A= $self->{"cache"}{$parent_geohash}B= $ENC_MAP{$last_char}
| |||||||||||||||||||||||||||||
| 90 | 199 | $self->{cache}{$parent_geohash}{$last_char}++; | |||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 8 | push @results, $geohash; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 221 | return @results; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 3 | ▶ | sub merge { | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 3 | my @geohashes = sort @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | $self->{cache} = +{}; | |||||||||||||||||||||||||||
| 104 | 3 | my @results; | |||||||||||||||||||||||||||
| 105 | 3 | for my $geohash (@geohashes) { | |||||||||||||||||||||||||||
| 106 | 217 | push @results, $self->_merge_strip_last_char($geohash); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 3 | delete $self->{cache}; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | sort @results; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub split { | ||||||||||||||||||||||||||
|
Subroutine
split: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 1 | my($self, $geohash) = @_; | |||||||||||||||||||||||||||
| 115 | 1 | map { "$geohash$_" } @ENC; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 6 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 6 | my($self, $geohash) = @_; | |||||||||||||||||||||||||||
| 120 | 6 | ▶ | $geohash && $geohash =~ /^[0123456789bcdefghjkmnpqrstuvwxyz]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | package Geohash::backendPP; | ||||||||||||||||||||||||||||
| 126 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 9 | ▶ | use parent 'Geo::Hash'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # https://github.com/yappo/Geo--Hash/tree/feature-geo_hash_xs | ||||||||||||||||||||||||||||
| 132 | 9 | ▶ | use constant ADJ_RIGHT => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 133 | 9 | ▶ | use constant ADJ_LEFT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | 9 | ▶ | use constant ADJ_TOP => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 9 | ▶ | use constant ADJ_BOTTOM => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 9 | my @NEIGHBORS = ( | |||||||||||||||||||||||||||
| 138 | [ "bc01fg45238967deuvhjyznpkmstqrwx", "p0r21436x8zb9dcf5h7kjnmqesgutwvy" ], | ||||||||||||||||||||||||||||
| 139 | [ "238967debc01fg45kmstqrwxuvhjyznp", "14365h7k9dcfesgujnmqp0r2twvyx8zb" ], | ||||||||||||||||||||||||||||
| 140 | [ "p0r21436x8zb9dcf5h7kjnmqesgutwvy", "bc01fg45238967deuvhjyznpkmstqrwx" ], | ||||||||||||||||||||||||||||
| 141 | [ "14365h7k9dcfesgujnmqp0r2twvyx8zb", "238967debc01fg45kmstqrwxuvhjyznp" ] | ||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 9 | my @BORDERS = ( | |||||||||||||||||||||||||||
| 145 | [ "bcfguvyz", "prxz" ], | ||||||||||||||||||||||||||||
| 146 | [ "0145hjnp", "028b" ], | ||||||||||||||||||||||||||||
| 147 | [ "prxz", "bcfguvyz" ], | ||||||||||||||||||||||||||||
| 148 | [ "028b", "0145hjnp" ] | ||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 83 | ▶ | sub adjacent { | ||||||||||||||||||||||||||
|
Subroutine
adjacent: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 152 | 83 | my ( $self, $hash, $where ) = @_; | |||||||||||||||||||||||||||
| 153 | 83 | my $hash_len = length $hash; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 83 | ▶ | croak "PANIC: hash too short!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | unless $hash_len >= 1; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 83 | my $base; | |||||||||||||||||||||||||||
| 159 | my $last_char; | ||||||||||||||||||||||||||||
| 160 | 83 | my $type = $hash_len % 2; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 83 | ▶ | if ( $hash_len == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 4 | $base = ''; | |||||||||||||||||||||||||||
| 164 | 4 | $last_char = $hash; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||
| 167 | 79 | ( $base, $last_char ) = $hash =~ /^(.+)(.)$/; | |||||||||||||||||||||||||||
| 168 | 79 | ▶ | if ($BORDERS[$where][$type] =~ /$last_char/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 13 | my $tmp = $self->adjacent($base, $where); | |||||||||||||||||||||||||||
| 170 | 13 | substr($base, 0, length($tmp)) = $tmp; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 83 | return $base . $ENC[ index($NEIGHBORS[$where][$type], $last_char) ]; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 4 | ▶ | sub neighbors { | ||||||||||||||||||||||||||
|
Subroutine
neighbors: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 177 | 4 | my ( $self, $hash, $around, $offset ) = @_; | |||||||||||||||||||||||||||
| 178 | 4 | ▶ | $around ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$around ||= 1
MC/DC100%1/1
$around ||= 1
$around
Truth table100%2/2
A
A= $around
| |||||||||||||||||||||||||||||
| 179 | 4 | ▶ | $offset ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$offset ||= 0
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 4 | my $last_hash = $hash; | |||||||||||||||||||||||||||
| 182 | 4 | my $i = 1; | |||||||||||||||||||||||||||
| 183 | 4 | ▶ | while ( $offset-- > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 1 | my $top = $self->adjacent( $last_hash, ADJ_TOP ); | |||||||||||||||||||||||||||
| 185 | 1 | my $left = $self->adjacent( $top, ADJ_LEFT ); | |||||||||||||||||||||||||||
| 186 | 1 | $last_hash = $left; | |||||||||||||||||||||||||||
| 187 | 1 | $i++; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 4 | my @list; | |||||||||||||||||||||||||||
| 191 | 4 | ▶ | while ( $around-- > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 5 | my $max = 2 * $i - 1; | |||||||||||||||||||||||||||
| 193 | 5 | $last_hash = $self->adjacent( $last_hash, ADJ_TOP ); | |||||||||||||||||||||||||||
| 194 | 5 | push @list, $last_hash; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 5 | for ( 0..( $max - 1 ) ) { | |||||||||||||||||||||||||||
| 197 | 9 | $last_hash = $self->adjacent( $last_hash, ADJ_RIGHT ); | |||||||||||||||||||||||||||
| 198 | 9 | push @list, $last_hash; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 5 | for ( 0..$max ) { | |||||||||||||||||||||||||||
| 202 | 14 | $last_hash = $self->adjacent( $last_hash, ADJ_BOTTOM ); | |||||||||||||||||||||||||||
| 203 | 14 | push @list, $last_hash; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 5 | for ( 0..$max ) { | |||||||||||||||||||||||||||
| 207 | 14 | $last_hash = $self->adjacent( $last_hash, ADJ_LEFT ); | |||||||||||||||||||||||||||
| 208 | 14 | push @list, $last_hash; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 5 | for ( 0..$max ) { | |||||||||||||||||||||||||||
| 212 | 14 | $last_hash = $self->adjacent( $last_hash, ADJ_TOP ); | |||||||||||||||||||||||||||
| 213 | 14 | push @list, $last_hash; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | 5 | $i++; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 4 | return @list; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 9 | 1; | |||||||||||||||||||||||||||
| 223 | __END__ | ||||||||||||||||||||||||||||