【发布时间】:2017-03-15 21:38:07
【问题描述】:
我一直在尝试实现我自己的(简单)布隆过滤器,但我坚持使用散列,我理解多次散列项目并使用索引填充位数组的概念。
但是,我在哈希中看到大量冲突,我正在使用 1 种哈希算法(我尝试过 FNV、murmurhash 和现在的farmhash)和各种种子(基于当前的纳秒)。
我一定是做错了什么,我是按照information here 计算k 函数并设置相同数量的种子。
任何帮助都会很棒,谢谢。
const farmhash = require('farmhash');
class BloomFilter {
constructor(items, input)
{
const BITS_PER_ITEM = 15; //~0.1% false positive rate
this.m = Buffer.alloc(items.length * BITS_PER_ITEM); // setup bit array
this.k = Math.ceil(BITS_PER_ITEM * 0.7); // amount of hash functions we need to use
this.seeds = [];
this.input = input;
this.items = items;
this.setSeeds();
this.insertItems();
}
get time()
{
let hrTime = process.hrtime()
return hrTime[1];
}
setSeeds()
{
for(let i = 0; i <= this.k; i++) this.seeds.push(this.time);
}
insertItems()
{
console.log('Total buffer size: ' + this.m.length);
let collisions = 0;
this.items.forEach(value => {
this.getBufferIndices(value).map(index => {
if(this.m[index] === 1) collisions++;
this.m[index] = 1;
});
});
console.log('Total collisions: ' + collisions);
}
getBufferIndices(value)
{
let indicies = [];
this.seeds.forEach(seed => indicies.push(farmhash.hash32WithSeed(value, seed) % this.m.length));
return indicies;
}
}
module.exports = BloomFilter;
【问题讨论】:
-
请在您的问题中发布代码,而不仅仅是当前版本的链接。
-
@Bergi 我的错,已修复
标签: javascript node.js algorithm ecmascript-6 bloom-filter