aoc/utils/jsonds.ts

67 lines
1.4 KiB
TypeScript
Raw Normal View History

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;
}
}
export class JSONHashMap<T extends Object> {
private map: Map<string, T>;
constructor() {
this.map = new Map<string, T>();
}
set(key: T, value: T): void {
const keyJson = JSON.stringify(key, Object.keys(key).sort());
this.map.set(keyJson, value);
}
get(key: T): T | undefined {
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);
}
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;
}
}