-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.ts
61 lines (48 loc) · 1.12 KB
/
Counter.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Dict from "./Dict";
import filter from "./filter";
export default class Counter<V> {
private readonly map: Dict<V, number>;
constructor(init?: Iterable<readonly [V, number]>) {
this.map = new Dict(init);
}
static count<T, K>(elems: Iterable<T>, key: (val: T) => K) {
const counter = new Counter<K>();
for (const elem of elems) {
counter.increment(key(elem));
}
return counter;
}
adjust(val: V, diff: number) {
const newCount = this.map.getOrDefault(val, 0) + diff;
this.map.put(val, newCount);
return newCount;
}
entries() {
return this.map.entries();
}
positiveEntries() {
return filter(this.map.entries(), ([_, count]) => count > 0);
}
values() {
return this.map.keys();
}
counts() {
return this.map.values();
}
increment(val: V) {
return this.adjust(val, 1);
}
decrement(val: V) {
return this.adjust(val, -1);
}
get(val: V) {
return this.map.getOrDefault(val, 0);
}
set(val: V, count: number) {
this.map.put(val, count);
return this;
}
[Symbol.iterator]() {
return this.map[Symbol.iterator]();
}
}