diff --git a/bloomfilter.js b/bloomfilter.js index 1f09b74..d3118e8 100644 --- a/bloomfilter.js +++ b/bloomfilter.js @@ -69,18 +69,26 @@ var n = v.length, a = 2166136261, c, + d, i = -1; while (++i < n) { c = v.charCodeAt(i); - a ^= (c & 0xff00) >> 8; - a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24); - // Trickery to work around 32-bit signed integers (we need unsigned). - a &= 0xffffffff; + if (d = c & 0xff000000) { + a ^= d >> 24; + a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24); + } + if (d = c & 0xff0000) { + a ^= d >> 16; + a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24); + } + if (d = c & 0xff00) { + a ^= d >> 8; + a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24); + } a ^= c & 0xff; a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24); - a &= 0xffffffff; } - return a; + return a & 0xffffffff; } // One additional iteration of FNV, given a hash. diff --git a/package.json b/package.json index c6912d4..6e8065c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bloomfilter.js", - "version": "0.0.2", + "version": "0.0.3", "description": "Fast bloom filter in JavaScript.", "keywords": [ "bloom filter", diff --git a/test/bloomfilter-test.js b/test/bloomfilter-test.js index 74319df..a501023 100644 --- a/test/bloomfilter-test.js +++ b/test/bloomfilter-test.js @@ -1,10 +1,14 @@ -var BloomFilter = require("../bloomfilter").BloomFilter; +var bf = require("../bloomfilter"), + BloomFilter = bf.BloomFilter, + fnv_1a = bf.fnv_1a; var vows = require("vows"), assert = require("assert"); var suite = vows.describe("bloomfilter"); +var jabberwocky = "`Twas brillig, and the slithy toves\n Did gyre and gimble in the wabe:\nAll mimsy were the borogoves,\n And the mome raths outgrabe.\n\n\"Beware the Jabberwock, my son!\n The jaws that bite, the claws that catch!\nBeware the Jubjub bird, and shun\n The frumious Bandersnatch!\"\n\nHe took his vorpal sword in hand:\n Long time the manxome foe he sought --\nSo rested he by the Tumtum tree,\n And stood awhile in thought.\n\nAnd, as in uffish thought he stood,\n The Jabberwock, with eyes of flame,\nCame whiffling through the tulgey wood,\n And burbled as it came!\n\nOne, two! One, two! And through and through\n The vorpal blade went snicker-snack!\nHe left it dead, and with its head\n He went galumphing back.\n\n\"And, has thou slain the Jabberwock?\n Come to my arms, my beamish boy!\nO frabjous day! Callooh! Callay!'\n He chortled in his joy.\n\n`Twas brillig, and the slithy toves\n Did gyre and gimble in the wabe;\nAll mimsy were the borogoves,\n And the mome raths outgrabe."; + suite.addBatch({ "bloom filter": { "basic": function() { @@ -15,6 +19,14 @@ suite.addBatch({ assert.equal(f.test(n1), true); assert.equal(f.test(n2), false); }, + "jabberwocky": function() { + var f = new BloomFilter(1000, 4), + n1 = jabberwocky, + n2 = jabberwocky + "\n"; + f.add(n1); + assert.equal(f.test(n1), true); + assert.equal(f.test(n2), false); + }, "basic uint32": function() { var f = new BloomFilter(1000, 4), n1 = "\u100", @@ -25,6 +37,23 @@ suite.addBatch({ assert.equal(f.test(n2), false); assert.equal(f.test(n3), false); } + }, + "fnv": { + "basic": function() { + // Test vectors from http://isthe.com/chongo/tech/comp/fnv/ + assert.equal(fnv_1a(""), 0x811c9dc5 & 0xffffffff); + assert.equal(fnv_1a("a"), 0xe40c292c & 0xffffffff); + assert.equal(fnv_1a("b"), 0xe70c2de5 & 0xffffffff); + assert.equal(fnv_1a("c"), 0xe60c2c52 & 0xffffffff); + assert.equal(fnv_1a("d"), 0xe10c2473 & 0xffffffff); + assert.equal(fnv_1a("e"), 0xe00c22e0 & 0xffffffff); + assert.equal(fnv_1a("f"), 0xe30c2799 & 0xffffffff); + assert.equal(fnv_1a("fo"), 0x6222e842 & 0xffffffff); + assert.equal(fnv_1a("foo"), 0xa9f37ed7 & 0xffffffff); + assert.equal(fnv_1a("foob"), 0x3f5076ef & 0xffffffff); + assert.equal(fnv_1a("fooba"), 0x39aaa18a & 0xffffffff); + assert.equal(fnv_1a("foobar"), 0xbf9cf968 & 0xffffffff); + } } });