-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
46 lines (33 loc) · 1.21 KB
/
index.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
import { SetWithMin } from "./SetWithMin.ts";
class NumberContainers {
#number_to_indices = new Map<number, SetWithMin>();
#index_to_number = new Map<number, number>();
constructor() {}
change(index: number, number: number): void {
const old = this.#index_to_number.get(index);
if (this.#index_to_number.has(index) && typeof old === "number") {
const oldindices = this.#number_to_indices.get(old);
if (!oldindices) {
throw Error("old indices not found");
}
const oldset = oldindices;
oldset?.delete(index);
if (oldset.size === 0) {
this.#number_to_indices.delete(old);
}
}
this.#index_to_number.set(index, number);
const indices = this.#number_to_indices.get(number) ?? new SetWithMin();
this.#number_to_indices.set(number, indices);
indices.add(index);
}
find(number: number): number {
const indices = this.#number_to_indices.get(number);
if (this.#number_to_indices.has(number) && indices) {
return indices.min();
} else {
return -1;
}
}
}
export default NumberContainers;