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]> | null

An iterable of key-value pairs for the initial entries.

Properties

[Symbol.toStringTag]

readonly [Symbol.toStringTag]: string

A 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(): void

Clears 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): this

Adds 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: K

The key to set.

value: V

The 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 | undefined

Returns the key associated with the specified value. If no key is associated with the specified value, undefined is returned.

value: V

The 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): boolean

Removes the element with the specified key. If the element does not exist, the instance remains unchanged.

key: K

The 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): boolean

Removes the element with the specified value. If the element does not exist, the instance remains unchanged.

value: V

The 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): boolean

Checks if an element with the specified value exists.

value: V

The 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);

Source