API
[ LiveTrack24 API v2 Home ]
int GBase64 and deltaRLE
A compact way to represent signed integers and arrays of integers in URL friendly string format.JavaScript Demo
var data = [1387901778, 1387901781, 1387901784, 1387901787, 1387901790, 1387901793, 1387901796, 1387901799, 1387901802, 1387901805, 1387901808, 1387901811, 1387901814, 1387901817, 1387901820, 1387901823, 1387901826, 1387901829, 1387901832, 1387901835, 1387901838, 1387901841, 1387901844, 1387901847, 1387901850, 1387901853, 1387901856, 1387901859, 1387901862, 1387901865, 1387901868]; console.log( deltaRLE( data ) ); // outputs: "1iKrdi$u3"
JavaScript implementation
var mapGBase64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ()", mapGBase64Index = mapGBase64.slice(''); function intToGBase64(num) { var sign, str = ''; if ( num < 0 ) { num = -num; sign = "-"; } else { sign = ''; } do { str = mapGBase64.charAt(num & 0x3f) + str; num = Math.floor(num / 64); } while (num); return sign + str; } function gBase64ToInt(str) { var start, sign, num = 0; if ( str.charAt(0) == "-" ) { sign = -1; start = 1; } else { sign = 1; start = 0; } var pwr = Math.pow(2, (str.length - start - 1) * 6); for(var i = start; i < str.length; i++) { num += mapGBase64Index.indexOf(str.charAt(i)) * pwr; pwr = pwr / 64; } return sign * num; } // Compress array with Delta and RLE // function deltaRLE(data) { var rle = 0, times, dif, lastDif = null, last, res = '', lastRes; for (var i in data) { if ( !parseInt(i) ) { res += intToGBase64(data[i]); } else { dif = data[i] - last; // if ( ( parseInt(i) > 1 ) && ( lastDif == dif ) && ( rle < 63 ) ) // without === if ( ( lastDif === dif ) && ( rle < 63 ) ) { rle++; times = intToGBase64(rle); res = lastRes + ( ( dif == 0 ) ? "*" + times : ( dif > 0 ? "$" + times + intToGBase64(dif) : "_" + times + intToGBase64(-dif) ) ); } else { lastDif = dif; rle = 1; lastRes = res; res += ( dif == 0 ) ? "." : ( dif > 0 ? ":" + intToGBase64(dif) : "!" + intToGBase64(-dif) ); } } last = data[i]; } return res; }
PHP partial implementation
Note:If it is to be ported in a language or system that does not support 64bit bitwise operators use the Javascript version which is more 64bit friendly.function gBase64ToInt($str) { $mapBase64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ()"; if ( $str[0] == "-" ) { $sign = -1; $start = 1; } else { $sign = 1; $start = 0; } $num = 0; $pwr = (strlen($str) - $start - 1) * 6; for($i = $start; $i < strlen($str); $i++) { $num += strrpos($mapBase64, $str[$i]) * (1 << $pwr); $pwr = $pwr - 6; } return $sign * $num; }