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