File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change 8
8
-----
9
9
10
10
``` javascript
11
- var bloom = new BloomFilter (
11
+ const bloom = new BloomFilter (
12
12
32 * 256 , // number of bits to allocate.
13
13
16 // number of hash functions.
14
14
);
@@ -26,13 +26,17 @@ bloom.test("blah");
26
26
27
27
// Serialisation. Note that bloom.buckets may be a typed array,
28
28
// so we convert to a normal array first.
29
- var array = [].slice .call (bloom .buckets ),
30
- json = JSON .stringify (array);
29
+ const array = [].slice .call (bloom .buckets );
30
+ const json = JSON .stringify (array);
31
31
32
32
// Deserialisation. Note that the any array-like object is supported, but
33
33
// this will be used directly, so you may wish to use a typed array for
34
34
// performance.
35
- var bloom = new BloomFilter (array, 16 );
35
+ const bloom = new BloomFilter (array, 16 );
36
+
37
+ // Automatically pick {m, k} based on number of elements and target false
38
+ // positive error rate.
39
+ const bloom = BloomFilter .withTargetError (1_000_000 , 1e-6 );
36
40
```
37
41
38
42
Implementation
Original file line number Diff line number Diff line change 129
129
return - this . m * Math . log ( 1 - bits / this . m ) / this . k ;
130
130
} ;
131
131
132
+ BloomFilter . withTargetError = function ( n , error ) {
133
+ const m = Math . ceil ( - n * Math . log2 ( error ) / Math . LN2 ) ;
134
+ const k = Math . ceil ( Math . LN2 * m / n ) ;
135
+ return new BloomFilter ( m , k ) ;
136
+ } ;
137
+
132
138
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
133
139
function popcnt ( v ) {
134
140
v -= ( v >> 1 ) & 0x55555555 ;
You can’t perform that action at this time.
0 commit comments