Class

TreeMultiMap

TreeMultiMap()

TreeMultiMap is an associative container that stores elements formed by a combination of a key value and a mapped value, following a specific order, and where multiple elements can have equivalent keys.

In a TreeMultiMap, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ.

Container properties

Associative - Elements in associative containers are referenced by their key and not by their absolute position in the container. Ordered - The elements in the container follow a strict order at all times. All inserted elements are given a position in this order. Map - Each element associates a key to a mapped value. Keys are meant to identify the elements whose main content is the mapped value. Multiple equivalent keys - Multiple elements in the container can have equivalent keys.

Constructor

# new TreeMultiMap()

View Source tree-multimap.ts, line 43

Example
let map = new TreeMultiMap();
// add few values
map.set(1, 'a');
map.set(2, 'b');
map.set(2, 'c');
// find a value by key
let v = map.get(1); // << 'a'
find all values for a given key
// print all key-value pairs
let from = map.lowerBound(2);
let to = map.upperBound(2);
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

Members

# compareFunc

Sets custom comparison function if key values are not of primitive types. Callback is a 3-way comparison function accepts two key values (lhs, rhs). It is expected to return +1 if the value of rhs is greater than lhs -1 if the value of rhs is less than lhs 0 if values are the same

View Source tree-multimap.ts, line 273

# size

Number of key-value pairs in the map.

View Source tree-multimap.ts, line 218

Methods

# backwards() → {IterableIterator}

ES6 reverse iterator for all key-value pairs in descending order of the keys.

View Source tree-multimap.ts, line 263

reverse iterator for all values

IterableIterator
Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let [key,value] of map.backwards()) {
  console.log(`key: ${key}, value: ${value}`);
}

# begin() → {MapIterator}

Forward iterator to the first element

View Source tree-multimap.ts, line 733

iterator pointing to the element with the smallest key

MapIterator
Example
let m = new TreeMultiMap();
...
for (let it = m.begin(); !it.equals(m.end()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# clear()

Removes all key-value pairs.

View Source tree-multimap.ts, line 102

Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.clear();
console.log(map.size); // 0

# compareFunc(func)

Sets custom comparison function if key values are not of primitive types. Callback is a 3-way comparison function accepts two key values (lhs, rhs). It is expected to return +1 if the value of rhs is greater than lhs -1 if the value of rhs is less than lhs 0 if values are the same

Parameters:
Name Type Description
func compareFunctionType

View Source tree-multimap.ts, line 721

# delete(key) → {void}

Removes key-value pair with the specified key if such entry exists. Does nothing otherwise.

Parameters:
Name Type Description
key K

Key to be removed

View Source tree-multimap.ts, line 588

void
Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.delete(2);
console.log(map.toString()); // {1:A,3:C}

# end() → {MapIterator}

Forward iterator to the element following the last element

View Source tree-multimap.ts, line 745

iterator pointing to the node after the last element

MapIterator
Example
let m = new TreeMultiMap();
...
for (let it = m.begin(); !it.equals(m.end()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# entries() → {IterableIterator}

Forward ES6 iterator for all key-value pairs in ascending order of the keys.

View Source tree-multimap.ts, line 599

forward iterator for all entries

IterableIterator
Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let [key,value] of map.entries()) {
  console.log(`key: ${key}, value: ${value}`);
}

# erase(iterator) → {void}

Removes key-value pair for the specified iterator.

Parameters:
Name Type Description
iterator MapIterator

Node to be removed

View Source tree-multimap.ts, line 828

void
Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let it = map.find(2);
it.prev();
map.erase(it); // removes a node with key 1
console.log(map.toString()); // {2:B,3:C}

# find(key) → {MapIterator}

Finds an element with key equivalent to the specified one. If such key does not exist end() iterator is returned.

Parameters:
Name Type Description
key K

Key to find

View Source tree-multimap.ts, line 759

Iterator pointing to the found node

MapIterator
Example
let m = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
...
let it = m.find(1);
if (!it.equals(m.end())) {
  console.log(`key: ${it.key}, value: ${it.value}`); // 1, 'A'
}

# first() → {Array|undefined}

Returns first key/value pair of the container, or undefined if container is empty

View Source tree-multimap.ts, line 923

first key/value pair of the container, or undefined if container is empty

Array | undefined
Example
let m = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let first = m.first();
if (first) {
  let key = first[0];   // 1
  let value = first[1]; // 'A'
}

# forEach(callback) → {void}

Iterates all key-value pairs using a callback in ascending order of the keys. Note that ES6 specifies the order of key value parameters in the callback differently from for-of loop.

Parameters:
Name Type Description
callback *

The same callback type as for regular JS maps

View Source tree-multimap.ts, line 612

void
Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.forEach(function(value, key, container) {
  console.log(`key: ${key}, value: ${value}`);
});

# get(key) → {V|undefined}

Finds value associated with the specified key. If specified key does not exist then undefined is returned.

Parameters:
Name Type Description
key K

a value of any type that can be compared with a key

View Source tree-multimap.ts, line 623

value associated with the key

V | undefined
Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let v = map.get(3); // 'C'
let v = map.get(4); // returns undefined

# has(key) → {boolean}

A boolean indicator whether map contains a key-value pair with the specified key

Parameters:
Name Type Description
key K

a value of any type that can be compared with a key

View Source tree-multimap.ts, line 633

true when key exists in the conainer

boolean
Example
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let b = map.get(3); // true

# insertMulti(key, value) → {InsertionResult}

Adds key-value pair. If such key already exists in the map then adds another node with the same key and a new value.

Parameters:
Name Type Description
key K

key to be added

value V

value to be associated with the key

View Source tree-multimap.ts, line 815

indicates whether a node was added and provides iterator to it.

Example
let m = new TreeMultiMap();
let res = m.insertMulti(1, 'A');
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.value}`); // prints A
}
res = m.insertMulti(1, 'B') // adds a new node
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.value}`); // prints B
  it.prev();
  console.log(`Previously inserted ${res.iterator.value}`); // prints A
}

# insertOrReplace(key, value) → {InsertionResult}

Adds key-value pair if such key does not exist in the map. Replaces value if such key exists

Parameters:
Name Type Description
key K

Key to be added / updated

value V

Value to be associated with the key

View Source tree-multimap.ts, line 795

indicates whether a node was added and provides iterator to it.

Example
let m = new TreeMultiMap();
let res = m.insertOrReplace(1, 'A');
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.value}`); // prints A
}
res = m.insertOrReplace(1, 'B') // replaces value on the existing node
if (res.wasReplaced) {
  console.log(`Replaced ${res.iterator.key}`); // prints B
}

# insertUnique(key, value) → {InsertionResult}

Adds key-value pair if such key does not exist in the map

Parameters:
Name Type Description
key K

Key to be added

value V

Value to be associated with the key

View Source tree-multimap.ts, line 777

indicates whether a node was added and provides iterator to it.

Example
let m = new TreeMultiMap();
let res = m.insertUnique(1, 'A');
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.value}`); // prints A
}
res = m.insertUnique(1, 'B') // this step has no effect on the map
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.key}`); // not executed
}

# keys() → {IterableIterator}

Forward ES6 iterator for all keys in ascending order of the keys.

View Source tree-multimap.ts, line 650

forward iterator for all keys

IterableIterator
Example
// iterate all keys
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let k of map.keys()) {
  console.log(k); // 1, 2, 3
}
// iterate all keys in reverse order
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let k of map.keys().backwards()) {
  console.log(k); // 3, 2, 1
}

# last() → {Array|undefined}

Returns last key/value pair of the container, or undefined if container is empty

View Source tree-multimap.ts, line 936

last key/value pair of the container, or undefined if container is empty

Array | undefined
Example
let m = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let last = m.last();
if (last) {
  let key = last[0];   // 3
  let value = last[1]; // 'C'
}

# lowerBound(key) → {MapIterator}

Iterator pointing to the first element that is not less than specified key. If no such element is found, see end() iterator is returned.

Parameters:
Name Type Description
key K

Key to search for

View Source tree-multimap.ts, line 857

iterator pointing to the found node

MapIterator
Example
let m = new TreeMultiMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = m.lowerBound(0);
let to = m.upperBound(50);
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

let m = new TreeMultiMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive in reverse order
let from = new ReverseIterator(m.upperBound(50));
let to = new ReverseIterator(m.lowerBound(0));
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

# rbegin() → {MapIterator}

Returns iterator to the first element for reverse iterator

View Source tree-multimap.ts, line 869

iterator pointing to the node with the highest key

MapIterator
Example
let m = new TreeMultiMap();
...
for (let it = m.rbegin(); !it.equals(m.rend()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# rend() → {MapIterator}

Returns end iterator for reverse iteration, e.g. pointing to a position after the last element

View Source tree-multimap.ts, line 881

iterator pointing to the node preceding the node with the lowest key

MapIterator
Example
let m = new TreeMultiMap();
...
for (let it = m.rbegin(); !it.equals(m.rend()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# set(key, value) → {void}

Adds a key-value pair to the map. Multiple key-value pairs with the same key are allowed in TreeMultiMap.

Parameters:
Name Type Description
key K

Key to be added / updated

value V

New value to be associated

View Source tree-multimap.ts, line 666

void
Example
let map = new TreeMultiMap();
map.set(1, 'A');
map.set(1, 'B');
map.set(2, 'C');
for (let k of map.values()) {
  console.log(k); // A, B, C
}

# size() → {number}

Number of key-value pairs in the map.

View Source tree-multimap.ts, line 672

number of elements in the container

number

# toString() → {string}

Serializes contents of the map in the form {key1:value1,key2:value2,...}

View Source tree-multimap.ts, line 942

serialized contents of the tree

string

# upperBound(key) → {MapIterator}

Iterator pointing to the first element that is greater than key. If no such element is found end() iterator is returned.

Parameters:
Name Type Description
key K

Key to search for

View Source tree-multimap.ts, line 910

iterator pointing to the found node

MapIterator
Example
let m = new TreeMultiMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = m.lowerBound(0);
let to = m.upperBound(50);
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

let m = new TreeMultiMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive in reverse order
let from = new ReverseIterator(m.upperBound(50));
let to = new ReverseIterator(m.lowerBound(0));
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

# values() → {IterableIterator}

Forward ES6 iterator for all values in ascending order of the keys.

View Source tree-multimap.ts, line 689

forward iterator for all values

IterableIterator
Example
// iterate all values
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let v of map.values()) {
  console.log(v); // 'A', 'B', 'C'
}
// iterate all values in reverse order
let map = new TreeMultiMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let v of map.values().backwards()) {
  console.log(v); // 'C', 'B', 'A'
}