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 }
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
.
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
.
Delete the association between the given key and its value, if it exists.
The key to delete its association
Delete the association between the given key and its value, if it exists.
The value to delete its association.
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.
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.
Return the number of associations this BiMap has.
An integer with the number of associations in the map.
Answer if this BiMap has the given key associated with a value.
The key to search
true if the key is present, false otherwise.
Answer if this BiMap has the given value associated with a key.
The value to search
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:
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),Instances of BiMap can be iterated directly through for-of loops, which achieves the same result as iterating through the entries of such BiMap.
Implementation details
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.