Go to repository

Class BiMap<K, V>

A bidirectional map is a map that represents biyective association between keys and values, and such that it can be accessed both by the keys or by the values. The types of both the keys and the values should be comparable by identity (=== comparison).

The order of association is important, so for example a BiMap<string, number> strings can be accessed by value using numbers, and numbers can accessed by key using strings.

The API of BiMaps resembles that of a Map, but with two versions for each operation involving keys or values.

The operations allow:

  • to create one BiMap, with new BiMap (and an optional list of pairs key-value for initialization -- if some values are associated to more than one key, information is lost),
  • to ask the number of associations kept by the map, with size,
  • to delete all associations at once, with clear,
  • to check that a given key or value has been associated, with hasKey and hasValue,
  • to retrieve the value or key associated with a given key or value, with getByKey and getByValue,
  • to set a particular association between a key and a value, with setByKey and setByValue (these operations imply the other one, and also undo previous associations of the same key and value),
  • to unset a particular association between a key and a value, with deleteByKey and deleteByValue (these operations imply each other),
  • to produce iterators for the keys, the values, and the entries, with keys, values, and entries, and
  • to produce a string version of a BiMap, with toString.

Instances of BiMap can be iterated directly through for-of loops, which achieves the same result as iterating through the entries of such BiMap.

The implementation of a BiMap is given by two synchronized maps: a straight one, from keys to values, and a reversed one, from values to keys. These maps satisfy the invariant that every association in one of them has its reversed counterpart in the other one. The auxiliary operations take care of keeping the invariant, by allowing the deletion of a given key or value in the map where they are values, if they appear there, and the association of a new pair key-value, deleting the old associations for both, if they exist.

Type Parameters

  • K

    The type of the keys.

  • V

    The type of the values.

Constructors

Create a new BiMap associating keys to values biyectively.

An optional list of pairs key-value can be used for initialization, but if some values are associated to more than one key (that is, the relationship is not biyective, information is lost -- only the last value is associated, so the list order is relevant.

Examples:

  new BiMap([['A', 1],['B',2]])                 -> { 'A' <-> 1, 'B' <-> 2 }
new BiMap([['A', 1],['B',1]]) -> { 'B' <-> 1 }
new BiMap([['B', 1],['A',1]]) -> { 'A' <-> 1 }
new BiMap([['A', 1],['B',1],['B',2]]) -> { 'B' <-> 2 }
new BiMap([['A', 1],['B',1],['B',2],['C',2]]) -> { 'C' <-> 2 }

Type Parameters

  • K
  • V

Parameters

  • Optional
    map: [K, V][]

    Optional list of associations to contain in the new BiMap.

Returns BiMap<K, V>

Properties

Private
mapKV: Map<K, V>

The map from key to values.

INVARIANT: for any key k associated with a value v in this map, v is a key in _mapVK with value k.

Private
mapVK: Map<V, K>

The map from values to keys.

INVARIANT: for any key v associated with a value k in this map, k is a key in _mapKV with value v.

Manipulation

Delete all associations in this BiMap.

Returns void

Delete the association between the given key and its value, if it exists.

Parameters

  • key: K

    The key to delete its association

Returns void

Delete the association between the given key and its value, if it exists.

Parameters

  • value: V

    The value to delete its association.

Returns void

Associate the given key to the given value in this BiMap. If any of both have previous associations, they are lost.

Parameters

  • key: K

    The key to associate with the value

  • value: V

    The value to associate with the key

Returns void

Associate the given value to the given key in this BiMap. If any of both have previous associations, they are lost.

Parameters

  • value: V

    The value to associate with the key

  • key: K

    The key to associate with the value

Returns void

Printing

Return a string representation of this BiMap.

Returns string

Private

Private

Implement the association of a key-value pair biyectively, deleting the old associations between both the key and the value given.

Parameters

  • key: K
  • value: V

Returns void

Private

Delete the occurrence of the given key as a value in the reversed map by retrieving its value from the straight map, if it is associated.

It may left the BiMap inconsistent, so it MUST NOT be used by itself alone.

Parameters

  • key: K

Returns void

Private

Delete the occurrence of the given value in the straight map by retrieving its key from the reversed map, if it is associated.

It may left the BiMap inconsistent, so it MUST NOT be used by itself alone.

Parameters

  • value: V

Returns void

Querying

get size(): number

Return the number of associations this BiMap has.

Returns number

An integer with the number of associations in the map.

Retrieve an iterator that can be used in a for-of loop.

Returns IterableIterator<[K, V], any, any>

Return an iterator for the entries of this BiMap, from keys to values.

Returns [K, V][]

Retrieve the value associated with the given key in this BiMap or undefined if the key is not associated with any value.

Parameters

  • key: K

    The key to retrieve the associated value

Returns V

true if the key is present, false otherwise.

Retrieve the key associated with the given value in this BiMap or undefined if the value is not associated with any key.

Parameters

  • value: V

    The value to retrieve the associated key

Returns K

Answer if this BiMap has the given key associated with a value.

Parameters

  • key: K

    The key to search

Returns boolean

true if the key is present, false otherwise.

Answer if this BiMap has the given value associated with a key.

Parameters

  • value: V

    The value to search

Returns boolean

Return an iterator for the keys of this BiMap.

Returns K[]

Return an iterator for the values of this BiMap.

Returns V[]