xs/geohash.xs

filter: Statementstmt 98.3% 176 / 179 Branchbran 84.0% 74 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.6% 250 / 267 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Geo/Hash/XS.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #define NEED_sv_2pv_flags_GLOBAL
5 #include "ppport.h"
6 #include <math.h>
7 #include "xshelper.h"
8  
9 static char PIECES[32] = {
10 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
11 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm',
12 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
13 };
14  
15 STATIC_INLINE
16 void
17 14 encode(char *buf, STRLEN precision, NV lat, NV lon) {
18 14 IV which = 0;
19 14 STRLEN count = 0;
20 NV
21 14 lat_min = -90,
22 14 lat_max = 90,
23 14 lon_min = -180,
24 14 lon_max = 180
25 ;
26  
27 111 ▶ while ( count < precision ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( count < precision ) { 97 14 111
28 IV i;
29 97 IV bits = 0;
30 582 ▶ for( i = 0; i < 5; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < 5; i++ ) { 485 97 582
31 IV bit;
32 485 ▶ if (which) {
Branch: 1 branch
BranchTrueFalseTotal
if (which) { 240 245 485
33 240 NV mid = (lat_max + lat_min) / 2;
34 240 bit = lat >= mid ? 1 : 0;
35 240 ▶ if ( bit ) { lat_min = mid; }
Branch: 1 branch
BranchTrueFalseTotal
if ( bit ) { lat_min = mid; } 101 139 240
36 139 else { lat_max = mid; }
37 } else {
38 245 NV mid = (lon_max + lon_min) / 2;
39 245 bit = lon >= mid ? 1 : 0;
40 245 ▶ if ( bit ) { lon_min = mid; }
Branch: 1 branch
BranchTrueFalseTotal
if ( bit ) { lon_min = mid; } 87 158 245
41 158 else { lon_max = mid; }
42 }
43 485 bits = ( ( bits << 1 ) | bit );
44 485 which ^= 1;
45 }
46  
47 97 buf[count] = PIECES[bits];
48 97 count++;
49 }
50  
51  
52 14 buf[count] = '\0';
53 14 }
54  
55 STATIC_INLINE
56 void
57 16 decode_to_interval(char *hash, STRLEN len, NV *lat_min_out, NV *lat_max_out, NV *lon_min_out, NV *lon_max_out) {
58 STRLEN i, j;
59 16 IV which = 0, min_or_max;
60 NV
61 16 lat_min = -90,
62 16 lat_max = 90,
63 16 lon_min = -180,
64 16 lon_max = 180
65 ;
66 124 ▶ for (i = 0; i < len; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++ ) { 108 16 124
67 IV bits;
68 108 int x = (int) hash[i];
69 108 ▶ if (x >= 48 && x <= 57) {
Branch: 2 branches
BranchTrueFalseTotal
if (x >= 48 && x <= 57) { 108 0 108
if (x >= 48 && x <= 57) { 48 60 108
70 48 bits = x - 48;
71 60 ▶ } else if ( x >= 98 && x <= 104 ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( x >= 98 && x <= 104 ) { 60 0 60
} else if ( x >= 98 && x <= 104 ) { 26 34 60
72 26 bits = x - 88;
73 34 ▶ } else if ( x >= 106 && x <= 107 ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( x >= 106 && x <= 107 ) { 34 0 34
} else if ( x >= 106 && x <= 107 ) { 2 32 34
74 2 bits = x - 89;
75 32 ▶ } else if ( x >= 109 && x <= 110 ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( x >= 109 && x <= 110 ) { 32 0 32
} else if ( x >= 109 && x <= 110 ) { 9 23 32
76 9 bits = x - 90;
77 23 ▶ } else if ( x >= 112 && x <= 122 ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( x >= 112 && x <= 122 ) { 23 0 23
} else if ( x >= 112 && x <= 122 ) { 23 0 23
78 23 bits = x - 91;
79 } else {
80 0 croak("Bad character '%c' in hash '%s'", hash[i], hash);
81 }
82  
83 648 ▶ for (j = 0; j < 5; j++){
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 5; j++){ 540 108 648
84 540 min_or_max = ( bits & 16 ) >> 4;
85 540 ▶ if (which) {
Branch: 1 branch
BranchTrueFalseTotal
if (which) { 267 273 540
86 267 NV mid = (lat_max + lat_min ) / 2;
87 267 ▶ if (min_or_max) { /* max */
Branch: 1 branch
BranchTrueFalseTotal
if (min_or_max) { /* max */ 113 154 267
88 113 lat_min = mid;
89 } else {
90 154 lat_max = mid;
91 }
92 } else {
93 273 NV mid = (lon_max + lon_min ) / 2;
94 273 ▶ if (min_or_max) { /* max */
Branch: 1 branch
BranchTrueFalseTotal
if (min_or_max) { /* max */ 99 174 273
95 99 lon_min = mid;
96 } else {
97 174 lon_max = mid;
98 }
99 }
100  
101 540 which ^= 1;
102 540 bits <<= 1;
103 }
104 }
105  
106 16 *lat_min_out = lat_min;
107 16 *lat_max_out = lat_max;
108 16 *lon_min_out = lon_min;
109 16 *lon_max_out = lon_max;
110 16 }
111  
112 STATIC_INLINE
113 void
114 14 decode(char *hash, IV len, NV *lat, NV *lon) {
115 14 NV lat_min = 0, lat_max = 0, lon_min = 0, lon_max = 0;
116 14 decode_to_interval(hash, len, &lat_min, &lat_max, &lon_min, &lon_max);
117 14 *lat = (lat_max + lat_min) / 2;
118 14 *lon = (lon_max + lon_min) / 2;
119 14 }
120  
121 static char* NEIGHBORS[4][2] = {
122 { "bc01fg45238967deuvhjyznpkmstqrwx", "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },
123 { "238967debc01fg45kmstqrwxuvhjyznp", "14365h7k9dcfesgujnmqp0r2twvyx8zb" },
124 { "p0r21436x8zb9dcf5h7kjnmqesgutwvy", "bc01fg45238967deuvhjyznpkmstqrwx" },
125 { "14365h7k9dcfesgujnmqp0r2twvyx8zb", "238967debc01fg45kmstqrwxuvhjyznp" }
126 };
127  
128 static char* BORDERS[4][2] = {
129 { "bcfguvyz", "prxz" },
130 { "0145hjnp", "028b" },
131 { "prxz", "bcfguvyz" },
132 { "028b", "0145hjnp" }
133 };
134  
135 #define LOG2_OF_10 3.32192809488736
136 #define LOG2_OF_180 7.49185309632967
137 #define LOG2_OF_360 8.49185309632968
138  
139 STATIC_INLINE
140 IV
141 32 bits_for_number(char *number) {
142 119 ▶ for(; *number != '\0'; number++){
Branch: 1 branch
BranchTrueFalseTotal
for(; *number != '\0'; number++){ 107 12 119
143 107 ▶ if(*number == '.'){
Branch: 1 branch
BranchTrueFalseTotal
if(*number == '.'){ 20 87 107
144 20 number++; /* skip dot */
145 20 return (IV) (strlen(number)) * LOG2_OF_10 + 1;
146 }
147 }
148 12 return 0;
149 }
150  
151 STATIC_INLINE
152 IV
153 16 precision(SV *lat, SV *lon) {
154 16 ▶ IV lab = bits_for_number(SvPV_nolen(lat)) + 8; /* 8 > log_2(180) */
Branch: 1 branch
BranchTrueFalseTotal
IV lab = bits_for_number(SvPV_nolen(lat)) + 8; /* 8 > log_2(180) */ 9 7 16
155 16 IV lob = bits_for_number(SvPV_nolen(lon)) + 9; /* 9 > log_2(360) */
156  
157 /* Though it seems I should use ceil(), I copied the logic from Geo::Hash */
158 16 return (IV) ( ( ( lab > lob ? lab : lob ) + 1 ) / 2.5 );
159 }
160  
161 enum GH_DIRECTION {
162 ADJ_RIGHT = 0,
163 ADJ_LEFT = 1,
164 ADJ_TOP = 2,
165 ADJ_BOTTOM = 3
166 };
167  
168 /* need to free this return value! */
169 #define HASHBASE_BUFSIZ 8192
170 STATIC_INLINE
171 char *
172 83 adjacent(char *hash, STRLEN hashlen, enum GH_DIRECTION direction) {
173 char base[HASHBASE_BUFSIZ];
174 83 char last_ch = hash[ hashlen - 1 ];
175 char *pos, *ret;
176 83 IV type = hashlen % 2;
177 IV base_len;
178  
179 83 ▶ if (hashlen < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (hashlen < 1) 0 83 83
180 0 croak("PANIC: hash too short!");
181 83 ▶ if (hashlen > HASHBASE_BUFSIZ)
Branch: 1 branch
BranchTrueFalseTotal
if (hashlen > HASHBASE_BUFSIZ) 0 83 83
182 0 croak("PANIC: hash too big!");
183  
184 83 memcpy(base, hash, hashlen - 1);
185 83 *(base + hashlen - 1) = '\0';
186  
187 83 ▶ if (hashlen > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (hashlen > 1) { 79 4 83
188 79 pos = strchr(BORDERS[direction][type], last_ch);
189 79 ▶ if (pos != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos != NULL) { 13 66 79
190 13 char *tmp = adjacent(base, strlen(base), direction);
191 13 strcpy(base, tmp);
192 13 Safefree(tmp);
193 }
194 }
195 83 base_len = strlen(base);
196 83 Newxz( ret, base_len + 2, char );
197 83 strcpy( ret, base );
198 83 ret[ base_len ] = PIECES[ strchr(NEIGHBORS[direction][type], last_ch) - NEIGHBORS[direction][type] ];
199 83 *(ret + base_len + 1) = '\0';
200 83 return ret;
201 }
202  
203 STATIC_INLINE
204 void
205 4 neighbors(char *hash, STRLEN hashlen, int around, int offset, char ***neighbors, int *nsize) {
206 char *xhash;
207 4 STRLEN xhashlen = hashlen;
208 4 int i = 1;
209  
210 4 Newxz( xhash, hashlen + 1, char );
211 4 Copy( hash, xhash, hashlen, char );
212 4 *(xhash + hashlen) = '\0';
213  
214 4 *nsize = ( (around + offset) * 2 + 1 ) * ( (around + offset) * 2 + 1 )
215 4 - (offset * 2 + 1) * (offset * 2 + 1);
216 4 Newxz( *neighbors, *nsize, char *);
217  
218 5 ▶ while ( offset > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( offset > 0 ) { 1 4 5
219 1 char *top = adjacent( xhash, xhashlen, ADJ_TOP );
220 1 char *left = adjacent( top, strlen(top), ADJ_LEFT );
221 1 Safefree(top);
222 1 Safefree(xhash);
223 1 xhash = left;
224 1 xhashlen = strlen(left);
225  
226 1 offset--;
227 1 i++;
228 }
229  
230 {
231 4 int m = 0;
232 4 char *tmp = xhash;
233 9 ▶ while (around-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (around-- > 0) { 5 4 9
234 int j;
235 /* going to insert this many neighbors */
236 5 (*neighbors)[m++] = adjacent(xhash, xhashlen, ADJ_TOP);
237  
238 14 ▶ for ( j = 0; j < 2 * i - 1; j ++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( j = 0; j < 2 * i - 1; j ++ ) { 9 5 14
239 9 xhash = (*neighbors)[m - 1];
240 9 xhashlen = strlen( xhash );
241 9 (*neighbors)[m++] = adjacent(xhash, xhashlen, ADJ_RIGHT);
242 }
243 19 ▶ for ( j = 0; j < 2 * i; j ++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( j = 0; j < 2 * i; j ++ ) { 14 5 19
244 14 xhash = (*neighbors)[m - 1];
245 14 xhashlen = strlen( xhash );
246 14 (*neighbors)[m++] = adjacent(xhash, xhashlen, ADJ_BOTTOM);
247 }
248 19 ▶ for ( j = 0; j < 2 * i; j ++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( j = 0; j < 2 * i; j ++ ) { 14 5 19
249 14 xhash = (*neighbors)[m - 1];
250 14 xhashlen = strlen( xhash );
251 14 (*neighbors)[m++] = adjacent(xhash, xhashlen, ADJ_LEFT);
252 }
253 19 ▶ for ( j = 0; j < 2 * i; j ++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( j = 0; j < 2 * i; j ++ ) { 14 5 19
254 14 xhash = (*neighbors)[m - 1];
255 14 xhashlen = strlen( xhash );
256 14 (*neighbors)[m++] = adjacent(xhash, xhashlen, ADJ_TOP);
257 }
258 5 i++;
259 5 xhash = (*neighbors)[m - 1];
260 5 xhashlen = strlen( xhash );
261 }
262 4 Safefree(tmp);
263 }
264 4 }
265  
266 MODULE = Geo::Hash::XS PACKAGE = Geo::Hash::XS
267  
268 PROTOTYPES: DISABLE
269  
270 SV *
271 encode(self, lat, lon, p = 0)
272 SV *self;
273 SV *lat;
274 SV *lon;
275 IV p;
276 PREINIT:
277 char *encoded;
278 CODE:
279 17 ▶ if (! looks_like_number(lat) || ! looks_like_number(lon) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (! looks_like_number(lat) || ! looks_like_number(lon) ) { 15 2 17
if (! looks_like_number(lat) || ! looks_like_number(lon) ) { 1 14 15
280 3 croak("encode() only works on degrees, not dms values");
281 }
282  
283 14 ▶ if (p <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p <= 0) { 7 7 14
284 7 p = precision(lat, lon);
285 }
286 PERL_UNUSED_VAR(self);
287  
288 14 Newxz(encoded, p + 1, char);
289 14 encode(encoded, p, SvNV(lat), SvNV(lon));
290  
291 14 RETVAL = newSV(0);
292 14 sv_setpv( RETVAL, encoded );
293 14 Safefree( encoded );
294 OUTPUT:
295 RETVAL
296  
297 void
298 decode_to_interval(self, hash)
299 SV *self;
300 char *hash;
301 PREINIT:
302 2 NV lat_min = 0, lat_max = 0, lon_min = 0, lon_max = 0;
303 2 STRLEN len = strlen(hash);
304 2 AV *lat_range = (AV *)sv_2mortal((SV *)newAV());
305 2 AV *lon_range = (AV *)sv_2mortal((SV *)newAV());
306 PPCODE:
307 PERL_UNUSED_VAR(self);
308 2 decode_to_interval(hash, len, &lat_min, &lat_max, &lon_min, &lon_max);
309  
310 2 av_push(lat_range, newSVnv(lat_max));
311 2 av_push(lat_range, newSVnv(lat_min));
312 2 av_push(lon_range, newSVnv(lon_max));
313 2 av_push(lon_range, newSVnv(lon_min));
314  
315 2 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)lat_range)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)lat_range))); 0 2 2
316 2 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)lon_range)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)lon_range))); 0 2 2
317  
318 void
319 decode(self, hash)
320 SV *self;
321 char *hash;
322 PREINIT:
323 14 NV lat = 0, lon = 0;
324 14 STRLEN len = strlen(hash);
325 PPCODE:
326 PERL_UNUSED_VAR(self);
327 14 decode(hash, len, &lat, &lon);
328 14 ▶ mXPUSHn(lat);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHn(lat); 0 14 14
329 14 ▶ mXPUSHn(lon);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHn(lon); 0 14 14
330  
331 IV
332 precision(self, lat, lon)
333 SV *self
334 SV *lat
335 SV *lon;
336 CODE:
337 PERL_UNUSED_VAR(self);
338 9 RETVAL = precision(lat, lon);
339 OUTPUT:
340 RETVAL
341  
342 SV *
343 adjacent(self, hash, direction)
344 SV *self;
345 char *hash;
346 int direction;
347 PREINIT:
348 char *adj;
349 CODE:
350 PERL_UNUSED_VAR(self);
351 12 adj = adjacent(hash, strlen(hash), direction);
352  
353 12 RETVAL = newSV(0);
354 12 sv_setpv(RETVAL, adj);
355 12 Safefree(adj);
356 OUTPUT:
357 RETVAL
358  
359 void
360 neighbors(self, hash, around = 1, offset = 0)
361 SV *self;
362 char *hash;
363 int around;
364 int offset;
365 PREINIT:
366 int i;
367 int nsize;
368 char **list;
369 PPCODE:
370 PERL_UNUSED_VAR(self);
371 4 neighbors(hash, strlen(hash), around, offset, &list, &nsize);
372  
373 60 ▶ for( i = 0; i < nsize; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < nsize; i++ ) { 56 4 60
374 56 ▶ mXPUSHp( list[i], strlen(list[i]) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp( list[i], strlen(list[i]) ); 0 56 56
375 }
376 60 ▶ for( i = 0; i < nsize; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < nsize; i++ ) { 56 4 60
377 56 Safefree(list[i]);
378 }
379 4 Safefree(list);
380  
381 IV
382 _constant()
383 ALIAS:
384 ADJ_TOP = ADJ_TOP
385 ADJ_RIGHT = ADJ_RIGHT
386 ADJ_LEFT = ADJ_LEFT
387 ADJ_BOTTOM = ADJ_BOTTOM
388 CODE:
389 12 ▶ RETVAL = ix;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ix; 0 12 12
390 OUTPUT:
391 RETVAL
392  
↑ Summary blib/lib/Geo/Hash/XS.pm »