Skip to content

Commit

Permalink
Update README and ensure count > 0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahharris committed Apr 20, 2021
1 parent f5a9a93 commit 37b6f2a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ interface TopkElement {

#### Methods

* `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.
* `iterator() -> Iterator<TopkElement>`: get the top-k values as an iterator that yields objects.

Expand All @@ -366,11 +366,15 @@ const { TopK } = require('bloom-filters')
// create a new TopK with k = 10, an error rate of 0.001 and an accuracy of 0.99
const topk = new TopK(10, 0.001, 0.99)

// push some occurrences in the multiset
// push occurrences one-at-a-time in the multiset
topk.add('alice')
topk.add('bob')
topk.add('alice')

// or, equally, push multiple occurrences at-once in the multiset
// topk.add('alice', 2)
// topk.add('bob', 1)

// print the top k values
for(let item of topk.values()) {
console.log(`Item "${item.value}" is in position ${item.rank} with an estimated frequency of ${item.frequency}`)
Expand Down
3 changes: 3 additions & 0 deletions src/sketch/topk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ export default class TopK extends BaseFilter {
* @param element - Element to add
*/
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)

Expand Down

0 comments on commit 37b6f2a

Please sign in to comment.