Class

TreeMultiSet

TreeMultiSet()

TreeMultiSet is a container that stores elements following a specific order, and where multiple elements can have equivalent values.

In a TreeMultiSet, the value of an element also identifies it (the value is itself the key). The value of the elements in a multiset cannot be modified once in the container (the elements are always immutable), but they can be inserted or removed from the container.

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. Set - The value of an element is also the key used to identify it. Multiple equivalent keys - Multiple elements in the container can have equivalent keys.

Constructor

# new TreeMultiSet()

View Source tree-multiset.ts, line 36

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

# __t

Internal tree

View Source tree-multiset.ts, line 53

# 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-multiset.ts, line 251

# size

Number of keys in the set.

View Source tree-multiset.ts, line 196

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

View Source tree-multiset.ts, line 603

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.

View Source tree-multiset.ts, line 241

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

View Source tree-multiset.ts, line 670

forward iterator pointing to the first node

SetIterator
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.

View Source tree-multiset.ts, line 93

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

View Source tree-multiset.ts, line 658

# 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-multiset.ts, line 534

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

View Source tree-multiset.ts, line 682

forward iterator pointing to the node after the last element

SetIterator
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.

View Source tree-multiset.ts, line 545

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

View Source tree-multiset.ts, line 748

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

View Source tree-multiset.ts, line 696

iterator pointing to found node

SetIterator
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

View Source tree-multiset.ts, line 839

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 forEach of standard JS maps

View Source tree-multiset.ts, line 559

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

View Source tree-multiset.ts, line 570

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

View Source tree-multiset.ts, line 735

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

View Source tree-multiset.ts, line 722

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

View Source tree-multiset.ts, line 709

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.

View Source tree-multiset.ts, line 587

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

View Source tree-multiset.ts, line 848

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

View Source tree-multiset.ts, line 777

iterator pointing at found node

SetIterator
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

View Source tree-multiset.ts, line 789

iterator pointing to the node with the highest key

SetIterator
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

View Source tree-multiset.ts, line 801

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

SetIterator
Example
let set = new TreeMultiSet();
...
for (let it = set.rbegin(); !it.equals(set.rend()); it.next()) {
  console.log(`key: ${it.key}`);
}

# size() → {number}

Number of keys in the set.

View Source tree-multiset.ts, line 609

count of elements in the container

number

# toString() → {string}

Serializes contents of the set in the form {key1,key2,...}

View Source tree-multiset.ts, line 854

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

View Source tree-multiset.ts, line 830

iterator pointing at the found node

SetIterator
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

View Source tree-multiset.ts, line 626

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'
}