I have some rather large arrays with hash values in them that looks like this:
ZjFhOTZjYzgtMmZhMy00YjdkLThmMDgtNjI4MjYxNTc3MGRlXw==ntyxntmzmzkw
I've done a binary search, using the node.js binary-search library, so my code looks like the following (although with c_array
being read in from a file of 60,000+ lines).
var bs = require("binary-search");
var c_arr = ["M2E0Mzk3YmItOWUzMC00ZmMwLWFhZDQtYTA0NTk0YWIwYjhjXw==mtg0mtq3nze2nw",
"M2E2NGZjNWEtMzkwYy00YzE4LTkzM2EtNDVmNjE1MjE2ZDViXw==ltcxmzk1otawng",
"M2ExZjE2NGItNzUwZi00YTU4LWI3OGMtZDVkNDA2YWE2MzRmXw==ltq3odgznja2oa",
"M2ExZjNmNDktODdhOS00ODJiLTg2NzQtM2NiODQ1Njc1ZmYzXw==lte0mzg2otgxmdk",
"M2EzNTExM2UtY2JmYS00ZjAzLTgwZmMtMjg4ZDJkZjA5YzJjXw==mtcxmjqxmdc1mq",
"M2EzZjkzZDQtODUwYS00ZjlkLTg3ZmQtZjliNTFjZmYxNjVhXw==mjgzodu3nji3",
"M2FkM2E5ZGYtOTFjMy00OGQwLTgxZTItOTlmZDg1ZjNmZmY5Xw==ltu0ntc1otqwmq"];
function numerify_ar(str){
return str.split('').map(function(s){
//Remember the array has to be the same as that returned from sorted string
return [",", "-", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "=", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"].indexOf(s)+1
});
}
function first_bigger(ar1,ar2){
//if array 1 is [0,0,0] and array 2 is [0,0,0,1] then it follows that array2 is a later word than array1.
for(var x = 0; x < ar1.length; x++){
if(x > ar2.length){
return true;
}
if(ar1[x] > ar2[x]){
return true;
}
if(ar1[x] < ar2[x]){
return false;
}
}
}
var test4 = bs(c_arr, "M2ExZjNmNDktODdhOS00ODJiLTg2NzQtM2NiODQ1Njc1ZmYzXw==lte0mzg2otgxmdk", function(a, b) {
var comp = first_bigger(numerify_ar(b),numerify_ar(a));
if( a === b){
return 0;
}
if(comp){
return -1;
}else{
return 1;
}
});
As said it works, but probably it can be improved on, so hoping for suggestions.