BidirectionalMap
import { BidirectionalMap } from "jsr:@ende/data-structures/unstable-bidirectional-map";class ยท experimental
class BidirectionalMap<K, V> extends Map<K, V>An extension of Map that allows lookup by both key and value.
Keys and values must be unique. Setting an existing key updates its value. Setting an existing value updates its key.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap([["one", 1]]);
assertEquals(map.get("one"), 1);
assertEquals(map.getReverse(1), "one");
Example: Inserting a value that already exists
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap();
map.set(1, "one");
map.set(2, "one");
assertEquals(map.size, 1);
assertEquals(map.get(1), undefined);
assertEquals(map.getReverse("one"), 2);
Constructors
constructor
new BidirectionalMap(entries?: Iterable<readonly [K, V]> | null)Creates a new instance.
entries?: Iterable<readonly [K, V]> | nullAn iterable of key-value pairs for the initial entries.
Properties
[Symbol.toStringTag]
readonly [Symbol.toStringTag]: stringA String value that is used in the creation of the default string description of an object.
Called by the built-in method Object.prototype.toString.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap();
assertEquals(map.toString(), "[object BidirectionalMap]");
Methods
clear
clear(): voidClears all entries.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap([["one", 1]]);
map.clear();
assertEquals(map.size, 0);
set
set(key: K, value: V): thisAdds a new element with a specified key and value. If an entry with the same key or value already exists, the entry will be updated.
key: KThe key to set.
value: VThe value to associate with the key.
Returns
The instance.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap();
map.set("one", 1);
assertEquals(map.get("one"), 1);
assertEquals(map.getReverse(1), "one");
getReverse
getReverse(value: V): K | undefinedReturns the key associated with the specified value. If no key is
associated with the specified value, undefined is returned.
value: VThe value to search for.
Returns
The key associated with the specified value, or undefined if no
key is found.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap([["one", 1]]);
assertEquals(map.getReverse(1), "one");
delete
delete(key: K): booleanRemoves the element with the specified key. If the element does not exist, the instance remains unchanged.
key: KThe key of the element to remove.
Returns
true if an element in the instance existed and has been removed,
or false if the element does not exist.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap([["one", 1]]);
map.delete("one");
assertEquals(map.size, 0);
deleteReverse
deleteReverse(value: V): booleanRemoves the element with the specified value. If the element does not exist, the instance remains unchanged.
value: VThe value of the element to remove.
Returns
true if an element in the instance existed and has been removed,
or false if the element does not exist.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap([["one", 1]]);
map.deleteReverse(1);
assertEquals(map.get("one"), undefined);
assertEquals(map.getReverse(1), undefined);
assertEquals(map.size, 0);
hasReverse
hasReverse(value: V): booleanChecks if an element with the specified value exists.
value: VThe value to search for.
Returns
true if an element with the specified value exists, otherwise
false.
Example: Usage
import { BidirectionalMap } from "@ende/data-structures/unstable-bidirectional-map";
import { assertEquals } from "@ende/assert";
const map = new BidirectionalMap([["one", 1]]);
assertEquals(map.hasReverse(1), true);