2023-12-02 16:16:56 -05:00
|
|
|
export class JSONSet<T extends Object> {
|
|
|
|
private items: Set<string>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.items = new Set<string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
add(item: T): void {
|
|
|
|
const itemJson = JSON.stringify(item, Object.keys(item).sort());
|
|
|
|
this.items.add(itemJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
has(item: T): boolean {
|
|
|
|
const itemJson = JSON.stringify(item, Object.keys(item).sort());
|
|
|
|
return this.items.has(itemJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(item: T): boolean {
|
|
|
|
const itemJson = JSON.stringify(item, Object.keys(item).sort());
|
|
|
|
return this.items.delete(itemJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear(): void {
|
|
|
|
this.items.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
get size(): number {
|
|
|
|
return this.items.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 22:42:06 -05:00
|
|
|
export class JSONHashMap<T extends Object, R extends Object> {
|
|
|
|
private map: Map<string, R>;
|
2023-12-02 16:16:56 -05:00
|
|
|
|
|
|
|
constructor() {
|
2023-12-05 22:42:06 -05:00
|
|
|
this.map = new Map<string, R>();
|
2023-12-02 16:16:56 -05:00
|
|
|
}
|
|
|
|
|
2023-12-05 22:42:06 -05:00
|
|
|
set(key: T, value: R): void {
|
2023-12-02 16:16:56 -05:00
|
|
|
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
|
|
|
this.map.set(keyJson, value);
|
|
|
|
}
|
|
|
|
|
2023-12-05 22:42:06 -05:00
|
|
|
get(key: T): R | undefined {
|
2023-12-02 16:16:56 -05:00
|
|
|
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
|
|
|
return this.map.get(keyJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
has(key: T): boolean {
|
|
|
|
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
|
|
|
return this.map.has(keyJson);
|
|
|
|
}
|
|
|
|
|
2023-12-05 22:42:06 -05:00
|
|
|
keys(): T[] {
|
|
|
|
return Array.from(this.map.keys()).map((x) => JSON.parse(x) as T);
|
|
|
|
}
|
|
|
|
|
2023-12-02 16:16:56 -05:00
|
|
|
delete(key: T): boolean {
|
|
|
|
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
|
|
|
return this.map.delete(keyJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear(): void {
|
|
|
|
this.map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
get size(): number {
|
|
|
|
return this.map.size;
|
|
|
|
}
|
|
|
|
}
|