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.

Typeparam

K the type of keys in the forward map

Typeparam

V the type of values in the reverse map

Example

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
console.log(bimap.get(1)) // `Set(["one", "first"])`
console.log(bimap.rget("second")) // `Set([2, 3, 4, 5])`

// remove entries while maintaining invertibility
bimap.delete(6) // `false` because the key never existed
bimap.delete(2) // `true`
console.log(bimap.rget("second")) // `Set([3, 4, 5])`
bimap.rremove("second", 4, 5, 6, 7)
console.log(bimap.rget("second")) // `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]]) }
console.log(bimap_entries) // `[[1, ["one", "first"]], [3, ["second"]], [4, []], [5, []]]`

// clear the entire bidirectional map
bimap.clear()

Type Parameters

  • K
  • V

Implements

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

    • Optional forward_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)

    • Optional reverse_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, ...items) => 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.

Type declaration

    • (key, ...items): 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.

      Parameters

      • key: K
      • Rest ...items: V[]

      Returns void

radd: ((key, ...items) => 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.

Type declaration

    • (key, ...items): 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.

      Parameters

      • key: V
      • Rest ...items: K[]

      Returns void

clear: (() => void)

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

Type declaration

    • (): void
    • clear out both forward and reverse maps completely of all their entries

      Returns void

delete: ((key, keep_key?) => 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)

Type declaration

    • (key, keep_key?): 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)

      Parameters

      • key: K
      • Optional keep_key: boolean

      Returns boolean

rdelete: ((key, keep_key?) => 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)

Type declaration

    • (key, keep_key?): 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)

      Parameters

      • key: V
      • Optional keep_key: boolean

      Returns boolean

remove: ((key, ...items) => 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.

Type declaration

    • (key, ...items): 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.

      Parameters

      • key: K
      • Rest ...items: V[]

      Returns void

rremove: ((key, ...items) => 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.

Type declaration

    • (key, ...items): 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.

      Parameters

      • key: V
      • Rest ...items: K[]

      Returns void

forEach: ((callbackfn, thisArg?) => void)

Type declaration

    • (callbackfn, thisArg?): void
    • Parameters

      • callbackfn: ((value, key, map) => void)
          • (value, key, map): void
          • Parameters

            • value: Set<V>
            • key: K
            • map: Map<K, Set<V>>

            Returns void

      • Optional thisArg: any

      Returns void

rforEach: ((callbackfn, thisArg?) => void)

Type declaration

    • (callbackfn, thisArg?): void
    • Parameters

      • callbackfn: ((value, key, map) => void)
          • (value, key, map): void
          • Parameters

            • value: Set<K>
            • key: V
            • map: Map<V, Set<K>>

            Returns void

      • Optional thisArg: any

      Returns void

get: ((key) => undefined | Set<V>)

Type declaration

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

      • key: K

      Returns undefined | Set<V>

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

Type declaration

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

      • key: V

      Returns undefined | Set<K>

has: ((key) => boolean)

Type declaration

    • (key): boolean
    • Parameters

      • key: K

      Returns boolean

rhas: ((key) => boolean)

Type declaration

    • (key): boolean
    • Parameters

      • key: V

      Returns boolean

set: ((key, value) => this)

Type declaration

    • (key, value): this
    • Parameters

      • key: K
      • value: Iterable<V>

      Returns this

rset: ((key, value) => this)

Type declaration

    • (key, value): this
    • Parameters

      • key: V
      • value: Iterable<K>

      Returns this

entries: (() => IterableIterator<[K, Set<V>]>)

Type declaration

    • (): IterableIterator<[K, Set<V>]>
    • Returns IterableIterator<[K, Set<V>]>

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

Type declaration

    • (): IterableIterator<[V, Set<K>]>
    • Returns IterableIterator<[V, Set<K>]>

keys: (() => IterableIterator<K>)

Type declaration

    • (): IterableIterator<K>
    • Returns IterableIterator<K>

rkeys: (() => IterableIterator<V>)

Type declaration

    • (): IterableIterator<V>
    • Returns IterableIterator<V>

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

Type declaration

    • (): IterableIterator<Set<V>>
    • Returns IterableIterator<Set<V>>

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

Type declaration

    • (): IterableIterator<Set<K>>
    • Returns IterableIterator<Set<K>>

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

Type declaration

    • (): IterableIterator<[K, Set<V>]>
    • Returns IterableIterator<[K, Set<V>]>

[toStringTag]: string

Generated using TypeDoc