2 , setPref } = require 'prefs'
6 exports.BloomFilter = BloomFilter;
7 exports.fnv_1a = fnv_1a;
8 exports.fnv_1a_b = fnv_1a_b;
10 var typedArrays = typeof ArrayBuffer !== "undefined";
12 // Creates a new bloom filter. If *m* is an array-like object, with a length
13 // property, then the bloom filter is loaded with data from the array, where
14 // each element is a 32-bit integer. Otherwise, *m* should specify the
15 // number of bits. *k* specifies the number of hashing functions.
16 function BloomFilter(m, k) {
18 if (typeof m !== "number") a = m, m = a.length * 32;
22 var n = Math.ceil(m / 32),
26 var kbytes = 1 << Math.ceil(Math.log(Math.ceil(Math.log(m) / Math.LN2 / 8)) / Math.LN2),
27 array = kbytes === 1 ? Uint8Array : kbytes === 2 ? Uint16Array : Uint32Array,
28 kbuffer = new ArrayBuffer(kbytes * k),
29 buckets = this.buckets = new Int32Array(n);
30 if (a) while (++i < n) buckets[i] = a[i];
31 this._locations = new array(kbuffer);
33 var buckets = this.buckets = [];
34 if (a) while (++i < n) buckets[i] = a[i];
35 else while (++i < n) buckets[i] = 0;
40 // See http://willwhim.wordpress.com/2011/09/03/producing-n-hash-functions-by-hashing-only-once/
41 BloomFilter.prototype.locations = function(v) {
50 r[i] = x < 0 ? (x + m) : x;
56 BloomFilter.prototype.add = function(v) {
57 var l = this.locations(v + ""),
60 buckets = this.buckets;
61 while (++i < k) buckets[Math.floor(l[i] / 32)] |= 1 << (l[i] % 32);
64 BloomFilter.prototype.test = function(v) {
65 var l = this.locations(v + ""),
69 buckets = this.buckets;
72 if ((buckets[Math.floor(b / 32)] & (1 << (b % 32))) === 0) {
79 // Estimated cardinality.
80 BloomFilter.prototype.size = function() {
81 var buckets = this.buckets,
83 for (var i = 0, n = buckets.length; i < n; ++i) bits += popcnt(buckets[i]);
84 return -this.m * Math.log(1 - bits / this.m) / this.k;
87 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
89 v -= (v >> 1) & 0x55555555;
90 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
91 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
94 // Fowler/Noll/Vo hashing.
103 if (d = c & 0xff000000) {
105 a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
107 if (d = c & 0xff0000) {
109 a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
111 if (d = c & 0xff00) {
113 a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
116 a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
118 // From http://home.comcast.net/~bretm/hash/6.html
124 return a & 0xffffffff;
127 // One additional iteration of FNV, given a hash.
128 function fnv_1a_b(a) {
129 a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
135 return a & 0xffffffff;
137 })(typeof exports !== "undefined" ? exports : this);
140 class DummyBloomFilter
147 class SerializableBloomFilter extends exports.BloomFilter
148 constructor: (@prefName, m, k) ->
149 if data = getPref(@prefName)
150 super(JSON.parse(data), k)
155 data = JSON.stringify([].slice.call(@buckets))
156 setPref(@prefName, data)
159 exports.SerializableBloomFilter = SerializableBloomFilter
160 exports.DummyBloomFilter = DummyBloomFilter