Construct bloom filter from existing buckets.

Fixes #2.
This commit is contained in:
Jason Davies 2013-04-30 12:04:41 +01:00
parent 9412f20e4c
commit 318ebca0a8
2 changed files with 18 additions and 9 deletions

View File

@ -5,21 +5,30 @@
var typedArrays = typeof ArrayBuffer !== "undefined";
// Creates a new bloom filter with *m* bits and *k* hashing functions.
// 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.
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);
var n = Math.ceil(m / 32),
i = -1;
if (typedArrays) {
var kbytes = 1 << Math.ceil(Math.log(Math.ceil(Math.log(m) / Math.LN2 / 8)) / Math.LN2),
array = kbytes === 1 ? Uint8Array : kbytes === 2 ? Uint16Array : Uint32Array,
kbuffer = new ArrayBuffer(kbytes * k);
this.buckets = new Int32Array(n);
kbuffer = new ArrayBuffer(kbytes * k),
buckets = this.buckets = new Int32Array(n);
if (a) while (++i < n) buckets[i] = a[i];
this._locations = new array(kbuffer);
} else {
var buckets = this.buckets = [],
i = -1;
while (++i < n) buckets[i] = 0;
var buckets = this.buckets = [];
if (a) while (++i < n) buckets[i] = a[i];
else while (++i < n) buckets[i] = 0;
this._locations = [];
}
}

View File

@ -1,6 +1,6 @@
{
"name": "bloomfilter",
"version": "0.0.11",
"version": "0.0.12",
"description": "Fast bloom filter in JavaScript.",
"keywords": [
"bloom filter",
@ -17,7 +17,7 @@
},
"main": "bloomfilter.js",
"devDependencies": {
"vows": "0.6.0"
"vows": "0.7.0"
},
"scripts": {
"test": "./node_modules/vows/bin/vows"