Skip to content

Commit

Permalink
Don't leak globals, fix tests, fix formatting, include package-lock
Browse files Browse the repository at this point in the history
  • Loading branch information
kdzwinel committed Aug 16, 2019
1 parent abcd4c1 commit 82addfb
Show file tree
Hide file tree
Showing 4 changed files with 1,962 additions and 128 deletions.
175 changes: 86 additions & 89 deletions bloom.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,122 @@
var JSBloom = {};

JSBloom.filter = function(items, target_prob) {
JSBloom.filter = function (items, target_prob) {

if (typeof items !== "number" || typeof target_prob !== "number" || target_prob >= 1) {
throw Error("Usage: new JSBloom.filter(items, target_probability)");
};

var BUFFER_LEN = (function() {
var buffer = Math.ceil((items * Math.log(target_prob)) / Math.log(1.0 / (Math.pow(2.0, Math.log(2.0)))));
var BUFFER_LEN = (function () {
var buffer = Math.ceil((items * Math.log(target_prob)) / Math.log(1.0 / (Math.pow(2.0, Math.log(2.0)))));

if ((buffer % 8) !== 0) {
buffer += 8 - (buffer % 8);
};

return buffer;
})(),
HASH_ROUNDS = Math.round(Math.log(2.0) * BUFFER_LEN / items),
bVector = new Uint8Array(BUFFER_LEN/8);

hashes = {
djb2: function(str) {
var hash = 5381;

for (var len = str.length, count = 0; count < len; count++) {
hash = hash * 33 ^ str.charCodeAt(count);
};

return (hash >>> 0) % BUFFER_LEN;
},
sdbm: function(str) {
var hash = 0;
if ((buffer % 8) !== 0) {
buffer += 8 - (buffer % 8);
};

for (var len = str.length, count = 0; count < len; count++) {
hash = str.charCodeAt(count) + (hash << 6) + (hash << 16) - hash;
};
return buffer;
})(),
HASH_ROUNDS = Math.round(Math.log(2.0) * BUFFER_LEN / items),
bVector = new Uint8Array(BUFFER_LEN / 8),

return (hash >>> 0) % BUFFER_LEN;
}
}
hashes = {
djb2: function (str) {
var hash = 5381;

addEntry = function(str) {
for (var len = str.length, count = 0; count < len; count++) {
hash = hash * 33 ^ str.charCodeAt(count);
};

var h1 = hashes.djb2(str)
var h2 = hashes.sdbm(str)
var added = false
for (var round = 0; round <= HASH_ROUNDS; round++) {
var new_hash = round == 0 ? h1
: round == 1 ? h2
: (h1 + (round * h2) + (round^2)) % BUFFER_LEN;
return (hash >>> 0) % BUFFER_LEN;
},
sdbm: function (str) {
var hash = 0;

var extra_indices = new_hash % 8,
index = ((new_hash - extra_indices) / 8);
for (var len = str.length, count = 0; count < len; count++) {
hash = str.charCodeAt(count) + (hash << 6) + (hash << 16) - hash;
};

if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
bVector[index] ^= (128 >> extra_indices - 1);
added = true;
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
bVector[index] ^= 1;
added = true;
return (hash >>> 0) % BUFFER_LEN;
}
},

};

return added;
}

addEntries = function(arr) {
for (var i = arr.length - 1; i >= 0; i--) {

addEntry(arr[i]);
addEntry = function (str) {
var h1 = hashes.djb2(str)
var h2 = hashes.sdbm(str)
var added = false
for (var round = 0; round <= HASH_ROUNDS; round++) {
var new_hash = round == 0 ? h1
: round == 1 ? h2
: (h1 + (round * h2) + (round ^ 2)) % BUFFER_LEN;

var extra_indices = new_hash % 8,
index = ((new_hash - extra_indices) / 8);

if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
bVector[index] ^= (128 >> extra_indices - 1);
added = true;
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
bVector[index] ^= 1;
added = true;
}

};
};

return true;
}
return added;
},

checkEntry = function(str) {
var index, extra_indices
var h1 = hashes.djb2(str)
addEntries = function (arr) {
for (var i = arr.length - 1; i >= 0; i--) {
addEntry(arr[i]);
};

extra_indices = h1 % 8;
index = ((h1 - extra_indices) / 8);
return true;
},

if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
return false;
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
return false;
}
checkEntry = function (str) {
var index, extra_indices
var h1 = hashes.djb2(str)

var h2 = hashes.sdbm(str)
extra_indices = h2 % 8;
index = ((h2 - extra_indices) / 8);
extra_indices = h1 % 8;
index = ((h1 - extra_indices) / 8);

if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
return false;
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
return false;
}
if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
return false;
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
return false;
}

for (var round = 2; round <= HASH_ROUNDS; round++) {
var new_hash = round==0?h1:round==1?h2:(h1 + (round * h2) + (round^2)) % BUFFER_LEN;
var extra_indices = new_hash % 8,
index = ((new_hash - extra_indices) / 8);
var h2 = hashes.sdbm(str)
extra_indices = h2 % 8;
index = ((h2 - extra_indices) / 8);

if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
return false;
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
return false;
}
};

return true;
}
for (var round = 2; round <= HASH_ROUNDS; round++) {
var new_hash = round == 0 ? h1 : round == 1 ? h2 : (h1 + (round * h2) + (round ^ 2)) % BUFFER_LEN;
var extra_indices = new_hash % 8,
index = ((new_hash - extra_indices) / 8);

if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
return false;
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
return false;
}
};

return true;
},

importData = function(data) {
bVector = data
}
importData = function (data) {
bVector = data
},

exportData = function() {
return bVector
}
exportData = function () {
return bVector
};

return {
info: {
Expand Down
Loading

0 comments on commit 82addfb

Please sign in to comment.