From 9d91023e7c7827e640e8edcda3743cb0109f2170 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 14 Nov 2024 19:48:06 -0400 Subject: wiki-data: TupleMapForBabies --- src/util/wiki-data.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/util/wiki-data.js') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 33db526b..4855a7a2 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -413,3 +413,50 @@ export class TupleMap { return value; } } + +export class TupleMapForBabies { + #here = new WeakMap(); + #next = new WeakMap(); + + set(...args) { + const first = args.at(0); + const last = args.at(-1); + const rest = args.slice(1, -1); + + if (empty(rest)) { + this.#here.set(first, last); + } else if (this.#next.has(first)) { + this.#next.get(first).set(...rest, last); + } else { + const tupleMap = new TupleMapForBabies(); + this.#next.set(first, tupleMap); + tupleMap.set(...rest, last); + } + } + + get(...args) { + const first = args.at(0); + const rest = args.slice(1); + + if (empty(rest)) { + return this.#here.get(first); + } else if (this.#next.has(first)) { + return this.#next.get(first).get(...rest); + } else { + return undefined; + } + } + + has(...args) { + const first = args.at(0); + const rest = args.slice(1); + + if (empty(rest)) { + return this.#here.has(first); + } else if (this.#next.has(first)) { + return this.#next.get(first).has(...rest); + } else { + return false; + } + } +} -- cgit 1.3.0-6-gf8a5