From d471e984199f04f3bab3bac3e122e1cc42c6aa8d Mon Sep 17 00:00:00 2001 From: Jason Davies Date: Sun, 4 Sep 2011 14:07:43 +0100 Subject: [PATCH] Fix a hashing bug. JavaScript doesn't support 64-bit integers, unfortunately, so instead I'm generating a single 32-bit hash and then taking the next iteration as the second hash. --- bloomfilter.js | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/bloomfilter.js b/bloomfilter.js index 7cb5534..2b7efc0 100644 --- a/bloomfilter.js +++ b/bloomfilter.js @@ -10,13 +10,24 @@ // See http://willwhim.wordpress.com/2011/09/03/producing-n-hash-functions-by-hashing-only-once/ BloomFilter.prototype.locations = function(v) { - var h = fnv64(v); - a = h & 0xffffffff, - b = (h & 0xffffffff00000000) >> 32, - k = this.k, + var k = this.k, + m = this.m, r = [], + n = v.length, + a = 2166136261, + b, + c, i = -1; - while (++i < k) r[i] = (a + b * i) % this.m; + // Fowler/Noll/Vo hashing. + while (++i < n) { + c = v.charCodeAt(i); + a ^= (c & 0xff00) >> 8; + a *= 16777619; + a ^= c & 0xff; + a *= 16777619; + } + b = a * 16777619; + i = -1; while (++i < k) r[i] = (a + b * i) % m; return r; }; @@ -42,21 +53,5 @@ return true; }; - // Fowler/Noll/Vo fast hash - function fnv64(d) { - var n = d.length, - h = 64, - i = -1, - c; - while (++i < n) { - c = d.charCodeAt(i); - h *= 1099511628211; - h ^= (c & 0xff00) >> 8; - h *= 1099511628211; - h ^= c & 0xff; - } - return h; - } - exports.BloomFilter = BloomFilter; })(typeof exports !== "undefined" ? exports : window);