Created
September 21, 2025 10:38
-
-
Save spikespaz/2fcdc618b92c64330f066e8fb0f0bcf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BiMap { | |
#k_to_v = new Map(); | |
#v_to_k = new Map(); | |
set(key, value) { | |
if (this.#k_to_v.get(key) === value) return this; | |
this.deleteKey(key); | |
this.deleteValue(value); | |
this.#k_to_v.set(key, value); | |
this.#v_to_k.set(value, key); | |
return this; | |
} | |
get(key) { | |
return this.#k_to_v.get(key); | |
} | |
getByValue(value) { | |
return this.#v_to_k.get(value); | |
} | |
hasKey(key) { | |
return this.#k_to_v.has(key); | |
} | |
hasValue(value) { | |
return this.#v_to_k.has(value); | |
} | |
deleteKey(key) { | |
const value = this.#k_to_v.get(key); | |
if (value === undefined) return false; | |
this.#k_to_v.delete(key); | |
this.#v_to_k.delete(value); | |
return true; | |
} | |
deleteValue(value) { | |
const key = this.#v_to_k.get(value); | |
if (key === undefined) return false; | |
this.#v_to_k.delete(value); | |
this.#k_to_v.delete(key); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment