Home Reference Source
import {TreeMultiSet} from 'jstreemap/src/public/tree-multiset.js'
public class | source

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.

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
}

Static Member Summary

Static Public Members
public static get

Allows to create programmatically an instance of the same class

Constructor Summary

Public Constructor
public

constructor(iterable: *)

Creates an empty, or a pre-initialized set.

Member Summary

Public Members
public get

String tag of this class

public set

Sets custom comparison function if key values are not of primitive types.

public get

size: Number: *

Number of keys in the set.

Method Summary

Public Methods
public

Forward ES6 iterator for all keys in ascending order.

public

add(key: *)

Adds a key to the set.

public

ES6 reverse iterator for all keys in descending order.

public

Forward iterator to the first element

public

clear()

Removes all key-value pairs.

public

delete(key: *)

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

public

Forward iterator to the element following the last element

public

Forward ES6 iterator for all values in ascending order.

public

erase(iterator: Iterator)

Removes value for the specified iterator.

public

find(key: *): Iterator

Finds an element with key equivalent to the specified one.

public

first(): *

public

forEach(callback: *)

Iterates all values using a callback in ascending order.

public

has(key: *): Boolean

A boolean indicator whether set contains the specified key.

public

Adds key whether it exists or not in the set.

public

Adds key if such key does not exist in the set.

public

Adds key if such key does not exist in the set.

public

Forward ES6 iterator for all keys in ascending order.

public

last(): *

public

lowerBound(key: *): Iterator

Iterator pointing to the first element that is not less than specified key.

public

Reverse iterator to the last element.

public

Reverse iterator pointing to before the first element.

public

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

public

upperBound(key: *): Iterator

Iterator pointing to the first element that is greater than key.

public

values(): JsITerator

Forward ES6 iterator for all keys in ascending order.

Static Public Members

public static get [Symbol.species]: *: * source

Allows to create programmatically an instance of the same class

Return:

*

constructor object for this class.

Example:

let set = new TreeMultiSet();
let constrFunc = Object.getPrototypeOf(set).constructor[Symbol.species];
let set2 = new constrFunc();

Public Constructors

public constructor(iterable: *) source

Creates an empty, or a pre-initialized set.

Params:

NameTypeAttributeDescription
iterable *
  • optional

Another iterable object whose values are added into the newly created set.

Example:

// Create an empty set
let set = new TreeMultiSet();
// Create and initialize set
let set2 = new TreeMultiSet([1, 2, 3]);

Public Members

public get [Symbol.toStringTag]: String: string source

String tag of this class

Return:

String

Example:

Object.prototype.toString.call(new TreeMultiSet()); // "[object TreeMultiSet]"

public set compareFunc source

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

public get size: Number: * source

Number of keys in the set.

Return:

Number

Public Methods

public [Symbol.iterator](): JsIterator source

Forward ES6 iterator for all keys in ascending order. The same as entries() method

Return:

JsIterator

Example:

let set = new TreeMultiSet([1, 2, 3]);
for (let key of set) {
  console.log(`key: ${key}, value: ${value}`);
}

public add(key: *) source

Adds a key to the set. If the key already exists then another entry with the same value is added.

Params:

NameTypeAttributeDescription
key *

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
}

public backward(): JsReverseIterator source

ES6 reverse iterator for all keys in descending order.

Example:

let set = new TreeMultiSet([1, 2, 3]);
for (let key of set.backwards()) {
  console.log(`key: ${key}`);
}

public begin(): Iterator source

Forward iterator to the first element

Return:

Iterator

Example:

let set = new TreeMultiSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
  console.log(`key: ${it.key}`);
}

public clear() source

Removes all key-value pairs.

Example:

let set = new TreeMultiSet([1, 2, 3]);
set.clear();
console.log(set.size); // 0

public delete(key: *) source

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

Params:

NameTypeAttributeDescription
key *

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}

public end(): Iterator source

Forward iterator to the element following the last element

Return:

Iterator

Example:

let set = new TreeMultiSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
  console.log(`key: ${it.key}`);
}

public entries(): JsIterator source

Forward ES6 iterator for all values in ascending order.

Return:

JsIterator

Example:

let set = new TreeMultiSet([1, 2, 3]);
for (let key of set.entries()) {
  console.log(`key: ${key}`);
}

public erase(iterator: Iterator) source

Removes value for the specified iterator.

Params:

NameTypeAttributeDescription
iterator Iterator

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}

public find(key: *): Iterator source

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

Params:

NameTypeAttributeDescription
key *

Return:

Iterator

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
}

public first(): * source

Return:

*

first element of the container, or undefined if container is empty

Example:

let set = new TreeMultiSet([1, 2, 3]);
let first = set.first(); // 1

public forEach(callback: *) source

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.

Params:

NameTypeAttributeDescription
callback *

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}`);
});

public has(key: *): Boolean source

A boolean indicator whether set contains the specified key.

Params:

NameTypeAttributeDescription
key *

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

Return:

Boolean

Example:

let set = new TreeMultiSet([1, 2, 3]);
let b = set.get(3); // true
b = set.get(4); // false

public insertMulti(key: *): InsertionResult source

Adds key whether it exists or not in the set.

Params:

NameTypeAttributeDescription
key *

Return:

InsertionResult

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

public insertOrReplace(key: *): InsertionResult source

Adds key if such key does not exist in the set. Same as insertUnique()

Params:

NameTypeAttributeDescription
key *

Return:

InsertionResult

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

public insertUnique(key: *): InsertionResult source

Adds key if such key does not exist in the set.

Params:

NameTypeAttributeDescription
key *

Return:

InsertionResult

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

public keys(): JsIterator source

Forward ES6 iterator for all keys in ascending order.

Return:

JsIterator

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().backward()) {
  console.log(k); // 3, 2, 1
}

public last(): * source

Return:

*

last element of the container, or undefined if container is empty

Example:

let set = new TreeMultiSet([1, 2, 3]);
let last = set.last(); // 3

public lowerBound(key: *): Iterator source

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

Params:

NameTypeAttributeDescription
key *

Return:

Iterator

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();
}

public rbegin(): ReverseIterator source

Reverse iterator to the last element.

Return:

ReverseIterator

Example:

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

public rend(): ReverseIterator source

Reverse iterator pointing to before the first element.

Return:

ReverseIterator

Example:

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

public toString(): String source

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

Return:

String

public upperBound(key: *): Iterator source

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

Params:

NameTypeAttributeDescription
key *

Return:

Iterator

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();
}

public values(): JsITerator source

Forward ES6 iterator for all keys in ascending order. It is the same as keys() method

Return:

JsITerator

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().backward()) {
  console.log(v); // '3', '2', '1'
}