@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Class InvertibleMap<K, V>

    an invertible map maintains a bidirectional one-to-many mapping between keys (of kind K) and collection of values (of kind Set<V>).
    the reverse mapping is also a one-to-many between keys (of kind V) and collection of values (of kind Set<K>).
    the dual map model of this class allows for quick lookups and mutations both directions.
    this data structure highly resembles a directed graph's edges.

    import { assertEquals } from "jsr:@std/assert"

    const bimap = new InvertibleMap<number, string>()

    // add values to the forward map
    bimap.add(1, "one", "first")
    bimap.add(2, "two", "second")

    // add values to the reverse map
    bimap.radd("second", 3, 4, 5)

    // perform lookups in both directions
    assertEquals(bimap.get(1), new Set(["one", "first"]))
    assertEquals(bimap.rget("second"), new Set([2, 3, 4, 5]))

    // remove entries while maintaining invertibility
    bimap.delete(6) // `false` because the key never existed
    bimap.delete(2) // `true`
    assertEquals(bimap.rget("second"), new Set([3, 4, 5]))
    bimap.rremove("second", 4, 5, 6, 7)
    assertEquals(bimap.rget("second"), new Set([3]))

    // iterate over the forward map
    const bimap_entries: [key: number, values: string[]][] = []
    for (const [k, v] of bimap) { bimap_entries.push([k, [...v]]) }
    assertEquals(bimap_entries, [
    [1, ["one", "first"]],
    [3, ["second"]],
    [4, []],
    [5, []],
    ])

    // clear the entire bidirectional map
    bimap.clear()
    assertEquals([...bimap.entries()], [])

    Type Parameters

    • K

      the type of keys in the forward map

    • V

      the type of values in the reverse map

    Implements

    Index

    Constructors

    • create an empty invertible map.
      optionally provide an initial forward_map to populate the forward mapping, and then automatically deriving the reverse mapping from it.
      or provide an initial reverse_map to populate the reverse mapping, and then automatically deriving the froward mapping from it.
      if both forward_map and reverse_map are provided, then it will be up to YOU to make sure that they are actual inverses of each other.

      Type Parameters

      • K
      • V

      Parameters

      • Optionalforward_map: Map<K, Set<V>>

        initiallize by populating with an optional initial forward map (the reverse map will be automatically computed if reverse_map === undefined)

      • Optionalreverse_map: Map<V, Set<K>>

        initiallize by populating with an optional initial reverse map (the forward map will be automatically computed if forward_map === undefined)

      Returns InvertibleMap<K, V>

    Properties

    fmap: Map<K, Set<V>>

    forward mapping. not intended for direct access, since manually mutating it will ruin the invertibility with the reverse map if you're not careful.

    rmap: Map<V, Set<K>>

    reverse mapping. not intended for direct access, since manually mutating it will ruin the invertibility with the forward map if you're not careful.

    size: number

    size of the forward map

    rsize: number

    size of the reverse map

    add: (key: K, ...items: V[]) => void

    at a specific key in the forward map, add the list of items, and then also assign key to the list of items in the reverse map to maintain invertibility.

    radd: (key: V, ...items: K[]) => void

    at a specific key in the reverse map, add the list of items, and then also assign key to the list of items in the forward map to maintain invertibility.

    clear: () => void

    clear out both forward and reverse maps completely of all their entries

    delete: (key: K, keep_key?: boolean) => boolean

    delete a key in the forward map, and also remove its mentions from the reverse map's entries.
    if keep_key is optionally set to true, we will only clear out the set of items held by the forward map at the key, and keep the key itself intact (along with the original (now mutated and clear) Set<V> which the key refers to)

    rdelete: (key: V, keep_key?: boolean) => boolean

    delete a key in the reverse map, and also remove its mentions from the forward map's entries.
    if keep_key is optionally set to true, we will only clear out the set of items held by the reverse map at the key, and keep the key itself intact (along with the original (now mutated and clear) Set<V> which the key refers to)

    remove: (key: K, ...items: V[]) => void

    at a specific key in the forward map, remove/delete the list of items, and then also remove key from the list of items in the reverse map to maintain invertibility.

    rremove: (key: V, ...items: K[]) => void

    at a specific key in the reverse map, remove/delete the list of items, and then also remove key from the list of items in the forward map to maintain invertibility.

    forEach: (
        callbackfn: (value: Set<V>, key: K, map: Map<K, Set<V>>) => void,
        thisArg?: any,
    ) => void

    Executes a provided function once per each key/value pair in the Map, in insertion order.

    rforEach: (
        callbackfn: (value: Set<K>, key: V, map: Map<V, Set<K>>) => void,
        thisArg?: any,
    ) => void
    get: (key: K) => undefined | Set<V>

    Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.

    Type declaration

      • (key: K): undefined | Set<V>
      • Parameters

        • key: K

        Returns undefined | Set<V>

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

    rget: (key: V) => undefined | Set<K>
    has: (key: K) => boolean

    Type declaration

      • (key: K): boolean
      • Parameters

        • key: K

        Returns boolean

        boolean indicating whether an element with the specified key exists or not.

    rhas: (key: V) => boolean
    set: (key: K, value: Iterable<V>) => this

    Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.

    rset: (key: V, value: Iterable<K>) => this
    entries: () => MapIterator<[K, Set<V>]>

    Returns an iterable of key, value pairs for every entry in the map.

    rentries: () => MapIterator<[V, Set<K>]>

    Type declaration

      • (): MapIterator<[V, Set<K>]>
      • Returns an iterable of key, value pairs for every entry in the map.

        Returns MapIterator<[V, Set<K>]>

    keys: () => MapIterator<K>

    Returns an iterable of keys in the map

    rkeys: () => MapIterator<V>

    Type declaration

      • (): MapIterator<V>
      • Returns an iterable of keys in the map

        Returns MapIterator<V>

    values: () => MapIterator<Set<V>>

    Returns an iterable of values in the map

    rvalues: () => MapIterator<Set<K>>

    Type declaration

      • (): MapIterator<Set<K>>
      • Returns an iterable of values in the map

        Returns MapIterator<Set<K>>

    "[iterator]": () => MapIterator<[K, Set<V>]>

    Type declaration

      • (): MapIterator<[K, Set<V>]>
      • Returns an iterable of key, value pairs for every entry in the map.

        Returns MapIterator<[K, Set<V>]>

    "[toStringTag]": string