bloom-filters
Advanced tools
Comparing version 1.3.4 to 1.3.6
{ | ||
"name": "bloom-filters", | ||
"version": "1.3.4", | ||
"version": "1.3.6", | ||
"description": "JS implementation of probabilistic data structures: Bloom Filter (and its derived), HyperLogLog, Count-Min Sketch, Top-K and MinHash", | ||
@@ -5,0 +5,0 @@ "main": "dist/api.js", |
@@ -359,3 +359,3 @@ # Bloom-Filters | ||
* `add(element: string) -> void`: add a new occurence of an element to the sketch. | ||
* `add(element: string, count: number = 1) -> void`: add one or more new occurences of an element to the sketch. | ||
* `values() -> Array<TopkElement>`: get the top-k values as an array of objects. | ||
@@ -370,3 +370,3 @@ * `iterator() -> Iterator<TopkElement>`: get the top-k values as an iterator that yields objects. | ||
// push some occurrences in the multiset | ||
// push occurrences one-at-a-time in the multiset | ||
topk.add('alice') | ||
@@ -376,2 +376,6 @@ topk.add('bob') | ||
// or, equally, push multiple occurrences at-once in the multiset | ||
// topk.add('alice', 2) | ||
// topk.add('bob', 1) | ||
// print the top k values | ||
@@ -378,0 +382,0 @@ for(let item of topk.values()) { |
@@ -149,3 +149,6 @@ /* file: topk.ts | ||
@Field() | ||
@Field<CountMinSketch>( | ||
(sketch: CountMinSketch) => sketch.saveAsJSON(), | ||
(json: any) => CountMinSketch.fromJSON(json) | ||
) | ||
private _sketch: CountMinSketch | ||
@@ -179,4 +182,7 @@ | ||
*/ | ||
add (element: string): void { | ||
this._sketch.update(element) | ||
add (element: string, count: number = 1): void { | ||
if (0 >= count) { | ||
throw (`count must be > 0 (was ${count})`) | ||
} | ||
this._sketch.update(element, count) | ||
const frequency = this._sketch.count(element) | ||
@@ -183,0 +189,0 @@ |
@@ -45,2 +45,61 @@ /* file : topk-test.js | ||
describe('#add', () => { | ||
it('should produce equivalent TopK estimations when using count parameter', () => { | ||
const k = 3 | ||
const errorRate = 0.001 | ||
const accuracy = 0.999 | ||
let freqTable = {} | ||
/* | ||
* Add items to the traditional one-at-a-time variant while concurrently | ||
* building a frequency table to be used for the all-at-once variant. | ||
*/ | ||
const topkOneAtATime = new TopK(k, errorRate, accuracy) | ||
for (const item of lessThanOrEqualTestCaseItems) { | ||
topkOneAtATime.add(item) | ||
if (!Object.hasOwnProperty.call(freqTable, item)) { | ||
freqTable[`${item}`] = 0 | ||
} | ||
++freqTable[`${item}`] | ||
} | ||
/* Ensure the built frequency table is correct. */ | ||
const expectedFreqTable = lessThanOrEqualTestCaseItems.reduce( | ||
function (acc, curr) { | ||
if (!Object.hasOwnProperty.call(acc, curr)) { | ||
acc[`${curr}`] = 1 | ||
} else { | ||
++acc[`${curr}`] | ||
} | ||
return acc | ||
}, {}) | ||
freqTable.should.to.deep.equal(expectedFreqTable) | ||
/* Build a version of TopK using the frequency as count */ | ||
const topkAllAtOnce = new TopK(k, errorRate, accuracy) | ||
for (const [item, freq] of Object.entries(freqTable)) { | ||
topkAllAtOnce.add(item, freq) | ||
} | ||
const topkOneAtATimeValues = topkOneAtATime.values() | ||
const topkOneAtATimeKeys = topkOneAtATimeValues.map(({value}) => value) | ||
const topkAllAtOnceValues = topkAllAtOnce.values() | ||
const topkAllAtOnceKeys = topkAllAtOnceValues.map(({value}) => value) | ||
/* Make sure all expected lengths match */ | ||
expectedTop.should.to.have.lengthOf(k) | ||
topkOneAtATimeKeys.should.to.have.lengthOf(expectedTop.length) | ||
topkAllAtOnceKeys.should.to.have.lengthOf(topkOneAtATimeKeys.length) | ||
/* Make sure all expected keys match */ | ||
topkOneAtATimeKeys.should.to.deep.equal(expectedTop) | ||
topkAllAtOnceKeys.should.to.deep.equal(topkOneAtATimeKeys) | ||
/* Make sure the objects themselves match */ | ||
topkAllAtOnceValues.should.to.deep.equal(topkOneAtATimeValues) | ||
}) | ||
}) | ||
describe('#values', () => { | ||
@@ -230,3 +289,24 @@ it('should produce valid TopK estimations when there are fewer than K items', () => { | ||
}) | ||
it('should update an imported TopK', ()=>{ | ||
const exported = topk.saveAsJSON() | ||
const newSketch = TopK.fromJSON(exported) | ||
newSketch.add("alice") | ||
topk.add("alice") | ||
newSketch._k.should.equal(topk._k) | ||
newSketch._errorRate.should.equal(topk._errorRate) | ||
newSketch._accuracy.should.equal(topk._accuracy) | ||
newSketch._seed.should.equal(topk._seed) | ||
// inner count min sketch | ||
newSketch._sketch._columns.should.equal(topk._sketch._columns) | ||
newSketch._sketch._rows.should.equal(topk._sketch._rows) | ||
newSketch._sketch._allSums.should.equal(topk._sketch._allSums) | ||
newSketch._sketch._seed.should.equal(topk._sketch._seed) | ||
newSketch._sketch._matrix.should.deep.equal(topk._sketch._matrix) | ||
// inner MinHeap | ||
newSketch._heap._content.should.deep.equal(topk._heap._content) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
527
233765
42
4980