-
Notifications
You must be signed in to change notification settings - Fork 412
/
Histogram.js
43 lines (36 loc) · 1.62 KB
/
Histogram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* A Set-like class that keeps track of how many times a value has
* been added. Call add() and remove() like you would for a Set, and
* call count() to find out how many times a given value has been added.
* The default iterator yields the values that have been added at least
* once. Use entries() if you want to iterate [value, count] pairs.
*/
class Histogram {
// To initialize, we just create a Map object to delegate to
constructor() { this.map = new Map(); }
// For any given key, the count is the value in the Map, or zero
// if the key does not appear in the Map.
count(key) { return this.map.get(key) || 0; }
// The Set-like method has() returns true if the count is non-zero
has(key) { return this.count(key) > 0; }
// The size of the histogram is just the number of entries in the Map.
get size() { return this.map.size; }
// To add a key, just increment its count in the Map.
add(key) { this.map.set(key, this.count(key) + 1); }
// Deleting a key is a little trickier because we have to delete
// the key from the Map if the count goes back down to zero.
delete(key) {
let count = this.count(key);
if (count === 1) {
this.map.delete(key);
} else if (count > 1) {
this.map.set(key, count - 1);
}
}
// Iterating a Histogram just returns the keys stored in it
[Symbol.iterator]() { return this.map.keys(); }
// These other iterator methods just delegate to the Map object
keys() { return this.map.keys(); }
values() { return this.map.values(); }
entries() { return this.map.entries(); }
}