bloomfilter
Advanced tools
Comparing version 0.0.12 to 0.0.14
@@ -11,3 +11,4 @@ (function(exports) { | ||
// 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) { | ||
@@ -17,6 +18,6 @@ var a; | ||
this.m = m; | ||
this.k = k; | ||
var n = Math.ceil(m / 32), | ||
i = -1; | ||
this.m = m = n * 32; | ||
this.k = k; | ||
@@ -77,2 +78,17 @@ if (typedArrays) { | ||
// Estimated cardinality. | ||
BloomFilter.prototype.size = function() { | ||
var buckets = this.buckets, | ||
bits = 0; | ||
for (var i = 0, n = buckets.length; i < n; ++i) bits += popcnt(buckets[i]); | ||
return -this.m * Math.log(1 - bits / this.m) / this.k; | ||
}; | ||
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel | ||
function popcnt(v) { | ||
v -= (v >> 1) & 0x55555555; | ||
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); | ||
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; | ||
} | ||
// Fowler/Noll/Vo hashing. | ||
@@ -79,0 +95,0 @@ function fnv_1a(v) { |
{ | ||
"name": "bloomfilter", | ||
"version": "0.0.12", | ||
"version": "0.0.14", | ||
"description": "Fast bloom filter in JavaScript.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -26,2 +26,12 @@ Bloom Filter | ||
// Serialisation. Note that bloom.buckets may be a typed array, | ||
// so we convert to a normal array first. | ||
var array = [].slice.call(bloom.buckets), | ||
json = JSON.stringify(array); | ||
// Deserialisation. Note that the any array-like object is supported, but | ||
// this will be used directly, so you may wish to use a typed array for | ||
// performance. | ||
var bloom = new BloomFilter(array, 3); | ||
Implementation | ||
@@ -28,0 +38,0 @@ -------------- |
7482
123
49