From 503ae1afdbe6812f5c1b3e76b586884fbb88e20d Mon Sep 17 00:00:00 2001 From: Jason Davies Date: Fri, 28 Feb 2014 15:39:32 +0000 Subject: [PATCH] Round *m* up to nearest multiple of 32. Fixes #9. --- bloomfilter.js | 7 ++++--- test/bloomfilter-test.js | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/bloomfilter.js b/bloomfilter.js index 4950155..44a1422 100644 --- a/bloomfilter.js +++ b/bloomfilter.js @@ -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), diff --git a/test/bloomfilter-test.js b/test/bloomfilter-test.js index 651f621..6cccab4 100644 --- a/test/bloomfilter-test.js +++ b/test/bloomfilter-test.js @@ -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); } } });