Mnemonist
BiMap
A BiMap is a an invertible map where, like with a Map, it’s easy to get value from key but also, and this is the main use of the BiMap, trace back from value to key.
This means, however, that the BiMap needs to ensure that some constraints are respected to remain a bijection.
Here are the three conflicting cases you need to keep in mind:
- If the
{A,B}relation exists in theBiMapand you add the{A,C}relation, you will delete the former one (value conflict). - If the
{A,B}relation exists in theBiMapand you add the{C,B}one, you will delete the former one (key conflict). - If the
{A,B}and{C,D}relation exists in theBiMapand you add the{A,D}one, you will delete both former ones (both key & value conflict).
const BiMap = require('mnemonist/bi-map');
Constructor
const map = new BiMap();
Static #.from
Alternatively, one can build a BiMap from an arbitrary JavaScript iterable likewise:
const map = BiMap.from({one: 'hello', two: 'world'});
Members
Methods
Mutation
Read
Iteration
#.inverse
You can access the inverse map through this member.
const map = new BiMap();
map.set('one', 'hello');
map.inverse.get('hello');
>>> 'one'
#.size
Total number of items stored by the map.
const map = new BiMap();
map.set('one', 'hello');
map.size
>>> 1
#.set
Creates a relation in the bimap between key and value.
O(1) amortized
const map = new BiMap();
map.set(key, item);
#.delete
O(1) amortized
Removes the relation associated with the given key.
const map = new BiMap();
map.set('one', 'hello');
map.delete('one');
map.size
>>> 0
#.clear
Completely clears the bimap of its relations.
const map = new BiMap();
map.set('one', 'hello');
map.set('two', 'world');
map.clear();
map.size
>>> 0
#.has
Returns whether the map has the given key.
O(1) amortized
const map = new BiMap();
map.set('one', 'hello');
map.has('one');
>>> true
#.get
Returns the value stored at the given key.
O(1) amortized
const map = new BiMap();
map.set('one', 'hello');
map.get('one');
>>> 'hello'
#.forEach
Iterates over each of the entries of the bimap.
const map = new BiMap();
map.set('one', 'hello');
map.set('two', 'world');
map.forEach((value, key) => {
console.log(key, value);
});
>>> 'one', 'hello'
>>> 'two', 'world'
#.keys
Returns an iterator over the keys of the bimap.
const map = new BiMap();
map.set('one', 'hello');
map.set('two', 'world');
const iterator = map.keys();
iterator.next().value
>>> 'one'
#.values
Returns an iterator over the values of the bimap.
const map = new BiMap();
map.set('one', 'hello');
map.set('two', 'world');
const iterator = map.values();
iterator.next().value
>>> 'hello'
#.entries
Returns an iterator over the entries of the bimap.
const map = new BiMap();
map.set('one', 'hello');
map.set('two', 'world');
const iterator = map.entries();
iterator.next().value
>>> ['one', 'hello']
Iterable
Alternatively, you can iterate over a list’s entries using ES2015 for...of protocol:
const map = new BiMap();
map.set('one', 'hello');
map.set('two', 'world');
for (const entry of map) {
console.log(entry);
}