# new TreeMultiMap()
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
Methods
# backwards() → {IterableIterator}
ES6 reverse iterator for all key-value pairs in descending order of the keys.
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
iterator pointing to the element with the smallest key
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.
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
|
# 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 |
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
iterator pointing to the node after the last element
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.
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 |
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 |
Iterator pointing to the found node
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
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 |
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 |
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 |
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 |
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 |
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 |
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.
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
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 |
iterator pointing to the found node
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
iterator pointing to the node with the highest key
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
iterator pointing to the node preceding the node with the lowest key
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 |
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.
number of elements in the container
number
# toString() → {string}
Serializes contents of the map in the form {key1:value1,key2:value2,...}
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 |
iterator pointing to the found node
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.
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'
}