Skip to content

Commit

Permalink
Merge pull request #36 from Callidon/fix-34
Browse files Browse the repository at this point in the history
Fix 34
  • Loading branch information
Callidon authored Jul 7, 2021
2 parents bd3ed71 + a4b2500 commit 9beb38b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,17 @@ export function getDistinctIndices (element: HashableInput, size: number, number
if (seed === undefined) {
seed = getDefaultSeed()
}
function getDistinctIndicesBis (n: number, elem: HashableInput, size: number, count: number, indexes: Array<number> = []): Array<number> {
if (indexes.length === count) {
return indexes
} else {
const hashes = hashTwice(elem, true, seed! + size % n)
const ind = doubleHashing(n, hashes.first, hashes.second, size)
if (indexes.includes(ind)) {
// console.log('generate index: %d for %s', ind, elem)
return getDistinctIndicesBis(n + 1, elem, size, count, indexes)
} else {
// console.log('already found: %d for %s', ind, elem)
indexes.push(ind)
return getDistinctIndicesBis(n + 1, elem, size, count, indexes)
}
const indexes: Array<number> = []
let n = 1
while (indexes.length < number) {
const hashes = hashTwice(element, true, seed! + size + n)
const ind = doubleHashing(n, hashes.first, hashes.second, size)
if (!indexes.includes(ind)) {
indexes.push(ind)
}
n++
}
return getDistinctIndicesBis(1, element, size, number)
return indexes
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ SOFTWARE.

require('chai').should()
const utils = require('../dist/utils.js')
const { BloomFilter } = require('../dist/api.js')
const XXH = require('xxhashjs')
const { range } = require('lodash')
const seed = utils.getDefaultSeed()

describe('Utils', () => {
Expand Down Expand Up @@ -111,4 +113,28 @@ describe('Utils', () => {
utils.isEmptyBuffer(Buffer.allocUnsafe(10).fill(1)).should.equal(false)
})
})

describe('#getDistinctIndices', () => {
const key = "da5e21f8a67c4163f1a53ef43515bd027967da305ecfc741b2c3f40f832b7f82"
it('should return <number> distinct indices on the interval [0, size)', () => {
const desiredIndices = 10000
const result = range(0, desiredIndices, 1)
try{
const indices = utils.getDistinctIndices(key, desiredIndices, desiredIndices).sort((a, b) => a - b)
indices.should.deep.equal(result)
} catch (e) {
throw Error("it should not throw: " + e)
}
})
it('should the issue be fixed', () => {
try{
const filter = new BloomFilter(39, 28);
filter.add(key);
filter.has(key).should.be.true
} catch (e) {
throw Error("it should not throw: " + e)
}

})
})
})

0 comments on commit 9beb38b

Please sign in to comment.