summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/index.ts2
-rw-r--r--utils/jsonds.ts66
-rw-r--r--utils/point2d.ts9
3 files changed, 77 insertions, 0 deletions
diff --git a/utils/index.ts b/utils/index.ts
new file mode 100644
index 0000000..b2e38c1
--- /dev/null
+++ b/utils/index.ts
@@ -0,0 +1,2 @@
+export * from "./jsonds";
+export * from "./point2d";
diff --git a/utils/jsonds.ts b/utils/jsonds.ts
new file mode 100644
index 0000000..c717232
--- /dev/null
+++ b/utils/jsonds.ts
@@ -0,0 +1,66 @@
+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;
+ }
+}
diff --git a/utils/point2d.ts b/utils/point2d.ts
new file mode 100644
index 0000000..3d07569
--- /dev/null
+++ b/utils/point2d.ts
@@ -0,0 +1,9 @@
+export type Vec2 = {
+ x: number;
+ y: number;
+};
+
+export const l2Norm = (p1: Vec2, p2: Vec2) =>
+ Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
+
+export const isDiagAdj = (p1: Vec2, p2: Vec2) => l2Norm(p1, p2) <= Math.sqrt(2);