# new TreeMultiSet()
Example
let set = new TreeMultiSet();
// add few values
set.add(1);
set.add(2);
set.add(2);
// check whether key exists
let flag = set.has(1); // << true
// print all keys
for (let key of set) {
console.log(`key: ${key}`); // 1, 2, 2
}
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
# add(key) → {void}
Adds a key to the set. If the key already exists then another entry with the same value is added.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
New key to be added |
void
Example
let set = new TreeMultiSet();
set.add(1);
set.add(1);
set.add(2);
// print all keys
for (let key of set) {
console.log(`key: ${key}`); // 1, 1, 2
}
# backwards() → {IterableIterator}
ES6 reverse iterator for all keys in descending order.
reverse iterator for all elements
IterableIterator
Example
let set = new TreeMultiSet([1, 2, 3]);
for (let key of set.backwards()) {
console.log(`key: ${key}`);
}
# begin() → {SetIterator}
Forward iterator to the first element
forward iterator pointing to the first node
Example
let set = new TreeMultiSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
console.log(`key: ${it.key}`);
}
# clear()
Removes all key-value pairs.
Example
let set = new TreeMultiSet([1, 2, 3]);
set.clear();
console.log(set.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 set = new TreeMultiSet([1, 2, 2, 3]);
set.delete(2);
console.log(set.toString()); // {1,2,3}
set.delete(2); / remove the second copy of the key
console.log(set.toString()); // {1,3}
# end() → {SetIterator}
Forward iterator to the element following the last element
forward iterator pointing to the node after the last element
Example
let set = new TreeMultiSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
console.log(`key: ${it.key}`);
}
# entries() → {IterableIterator}
Forward ES6 iterator for all values in ascending order.
forward iterator for all elements
IterableIterator
Example
let set = new TreeMultiSet([1, 2, 3]);
for (let key of set.entries()) {
console.log(`key: ${key}`);
}
# erase(iterator) → {void}
Removes value for the specified iterator.
Parameters:
| Name | Type | Description |
|---|---|---|
iterator |
SetIterator
|
Iterator pointing at the node to be removed |
void
Example
let set = new TreeMultiSet([1,2,3]);
let it = set.find(2);
it.prev();
set.erase(it); // removes a node with key 1
console.log(set.toString()); // {2,3}
# find(key) → {SetIterator}
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 search for |
iterator pointing to found node
Example
let set = new TreeMultiSet([1, 2, 3]);
...
let it = set.find(1);
if (!it.equals(set.end())) {
console.log(`Found key: ${it.key}`); // 1
}
# first() → {K|undefined}
Returns first element of the container, or undefined if container is empty
first element of the container, or undefined if container is empty
K
|
undefined
Example
let set = new TreeMultiSet([1, 2, 3]);
let first = set.first(); // 1
# forEach(callback) → {void}
Iterates all values using a callback in ascending order. Note that ES6 specifies the order of key parameters in the callback differently from for-of loop.
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
*
|
The same type of callback as used in |
void
Example
let set = new TreeMultiSet([1, 2, 3]);
set.forEach(function(value, key, container) {
// value is the same as key
console.log(`key: ${key}, value: ${value}`);
});
# has(key) → {boolean}
A boolean indicator whether set contains 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 container
boolean
Example
let set = new TreeMultiSet([1, 2, 3]);
let b = set.get(3); // true
b = set.get(4); // false
# insertMulti(key) → {InsertionResult}
Adds key whether it exists or not in the set.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Key to be added |
indicates whether a node was added and provides iterator to it.
Example
let set = new TreeMultiSet();
set.insertMulti(1);
set.insertMulti(1); // this step has no effect on the map
let flag = set.has(1); // true
let size = set.size; // 2
# insertOrReplace(key) → {InsertionResult}
Adds key if such key does not exist in the set. Same as insertUnique()
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Key to be added |
indicates whether a node was added and provides iterator to it.
Example
let set = new TreeMultiSet();
set.insertOrReplace(1);
set.insertOrReplace(1); // this step has no effect on the set
let flag = set.has(1); // true
let size = set.size; // 1
# insertUnique(key) → {InsertionResult}
Adds key if such key does not exist in the set.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Key to be added |
indicates whether a node was added and provides iterator to it.
Example
let set = new TreeMultiSet();
set.insertUnique(1);
set.insertUnique(1); // this step has no effect on the set
let flag = set.has(1); // true
let size = set.size; // 1
# keys() → {IterableIterator}
Forward ES6 iterator for all keys in ascending order.
forward iterator for all keys
IterableIterator
Example
// iterate all keys
let set = new TreeMultiSet([1, 2, 3]);
for (let k of set.keys()) {
console.log(k); // 1, 2, 3
}
// iterate all keys in reverse order
let set = new TreeMultiSet([1, 2, 3]);
for (let k of set.keys().backwards()) {
console.log(k); // 3, 2, 1
}
# last() → {K|undefined}
Returns last element of the container, or undefined if container is empty
last element of the container, or undefined if container is empty
K
|
undefined
Example
let set = new TreeMultiSet([1, 2, 3]);
let last = set.last(); // 3
# lowerBound(key) → {SetIterator}
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 at found node
Example
let set = new TreeMultiSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = set.lowerBound(0);
let to = set.upperBound(50);
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
let set = new TreeMultiSet();
... // 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(set.upperBound(50));
let to = new ReverseIterator(set.lowerBound(0));
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
# rbegin() → {SetIterator}
Returns iterator to the first element for reverse iterator
iterator pointing to the node with the highest key
Example
let set = new TreeMultiSet();
...
for (let it = set.rbegin(); !it.equals(set.rend()); it.next()) {
console.log(`key: ${it.key}`);
}
# rend() → {SetIterator}
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 set = new TreeMultiSet();
...
for (let it = set.rbegin(); !it.equals(set.rend()); it.next()) {
console.log(`key: ${it.key}`);
}
# toString() → {string}
Serializes contents of the set in the form {key1,key2,...}
serialized contents of the tree
string
# upperBound(key) → {SetIterator}
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 at the found node
Example
let set = new TreeMultiSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = set.lowerBound(0);
let to = set.upperBound(50);
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
let set = new TreeMultiSet();
... // 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(set.upperBound(50));
let to = new ReverseIterator(set.lowerBound(0));
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
# values() → {IterableIterator}
Forward ES6 iterator for all keys in ascending order. It is the same as keys() method
forward iterator for all values
IterableIterator
Example
// iterate all values
let set = new TreeMultiSet([1, 2, 3]);
for (let v of set.values()) {
console.log(v); // '1', '2', '3'
}
// iterate all values in reverse order
let set = new TreeMultiSet([1, 2, 3]);
for (let v of set.values().backwards()) {
console.log(v); // '3', '2', '1'
}