Round *m* up to nearest multiple of 32.

Fixes #9.
This commit is contained in:
Jason Davies 2014-02-28 15:39:32 +00:00
parent 2529639ace
commit 503ae1afdb
2 changed files with 6 additions and 5 deletions

View File

@ -8,15 +8,16 @@
// Creates a new bloom filter. If *m* is an array-like object, with a length
// property, then the bloom filter is loaded with data from the array, where
// each element is a 32-bit integer. Otherwise, *m* should specify the
// number of bits. *k* specifies the number of hashing functions.
// number of bits. Note that *m* is rounded up to the nearest multiple of
// 32. *k* specifies the number of hashing functions.
function BloomFilter(m, k) {
var a;
if (typeof m !== "number") a = m, m = a.length * 32;
this.m = m;
this.k = k;
var n = Math.ceil(m / 32),
i = -1;
this.m = m = n * 32;
this.k = k;
if (typedArrays) {
var kbytes = 1 << Math.ceil(Math.log(Math.ceil(Math.log(m) / Math.LN2 / 8)) / Math.LN2),

View File

@ -52,9 +52,9 @@ suite.addBatch({
"size": function() {
var f = new BloomFilter(1000, 4), i = -1;
while (++i < 100) f.add(i);
assert.inDelta(f.size(), 101.241308, 1e-6);
assert.inDelta(f.size(), 97.014763, 1e-6);
--i; while (++i < 1000) f.add(i);
assert.inDelta(f.size(), 1067.174487, 1e-6);
assert.inDelta(f.size(), 1007.549320, 1e-6);
}
}
});