use buffer for bloom filter.
This commit is contained in:
parent
d63f4af6ca
commit
3514eb7dec
@ -15,7 +15,7 @@ function Bloom(size, n, tweak) {
|
||||
if (!(this instanceof Bloom))
|
||||
return new Bloom(size, n, tweak);
|
||||
|
||||
this.filter = new Array(Math.ceil(size / 32));
|
||||
this.filter = new Buffer(Math.ceil(size / 8));
|
||||
this.size = size;
|
||||
this.n = n;
|
||||
this.tweak = tweak;
|
||||
@ -28,8 +28,7 @@ Bloom.prototype.hash = function hash(val, n) {
|
||||
};
|
||||
|
||||
Bloom.prototype.reset = function reset() {
|
||||
for (var i = 0; i < this.filter.length; i++)
|
||||
this.filter[i] = 0;
|
||||
this.filter.fill(0);
|
||||
};
|
||||
|
||||
Bloom.prototype.add = function add(val, enc) {
|
||||
@ -41,8 +40,9 @@ Bloom.prototype.add = function add(val, enc) {
|
||||
bit = this.hash(val, i);
|
||||
pos = 1 << (bit & 0x1f);
|
||||
shift = bit >> 5;
|
||||
shift *= 4;
|
||||
|
||||
this.filter[shift] |= pos;
|
||||
utils.writeU32(this.filter, utils.readU32(this.filter, shift) | pos, shift);
|
||||
}
|
||||
};
|
||||
|
||||
@ -55,8 +55,9 @@ Bloom.prototype.test = function test(val, enc) {
|
||||
bit = this.hash(val, i);
|
||||
pos = 1 << (bit & 0x1f);
|
||||
shift = bit >> 5;
|
||||
shift *= 4;
|
||||
|
||||
if ((this.filter[shift] & pos) === 0)
|
||||
if ((utils.readU32(this.filter, shift) & pos) === 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -64,21 +65,7 @@ Bloom.prototype.test = function test(val, enc) {
|
||||
};
|
||||
|
||||
Bloom.prototype.toBuffer = function toBuffer() {
|
||||
var bytes = Math.ceil(this.size / 8);
|
||||
var res = new Buffer(this.filter.length * 4);
|
||||
var i, w;
|
||||
|
||||
res.fill(0);
|
||||
|
||||
for (i = 0; i < this.filter.length; i++) {
|
||||
w = this.filter[i];
|
||||
res[i * 4] = w & 0xff;
|
||||
res[i * 4 + 1] = (w >> 8) & 0xff;
|
||||
res[i * 4 + 2] = (w >> 16) & 0xff;
|
||||
res[i * 4 + 3] = (w >> 24) & 0xff;
|
||||
}
|
||||
|
||||
return res.slice(0, bytes);
|
||||
return this.filter;
|
||||
};
|
||||
|
||||
function mul32(a, b) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user