MultiSet
Defined in: packages/data/src/multiset/multi-set.ts:21
A bag: a set that counts how many times each distinct item was added.
Backed by a Map from item to multiplicity, so adding the same item a
million times costs one entry rather than a million. count() is the total
including multiplicities; distinctCount() is the number of Map entries.
Time complexity: O(1) for add, delete, has, multiplicity,
distinctCount and count. Iterating with [Symbol.iterator] yields each
item as many times as it was added, so it is O(count()); distinct() and
entries() are O(distinctCount()).
Items are compared by SameValueZero, native Map semantics.
Type Parameters
Section titled “Type Parameters”T extends DefinedValue
The type of items. Anything except undefined.
Implements
Section titled “Implements”IMultiset<T>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MultiSet<
T>(iterable?):MultiSet<T>
Defined in: packages/data/src/multiset/multi-set.ts:27
Parameters
Section titled “Parameters”iterable?
Section titled “iterable?”Iterable<T, any, any>
Returns
Section titled “Returns”MultiSet<T>
Methods
Section titled “Methods”[iterator]()
Section titled “[iterator]()”[iterator]():
Iterator<T>
Defined in: packages/data/src/multiset/multi-set.ts:34
Each item repeated as many times as it is held.
Returns
Section titled “Returns”Iterator<T>
Implementation of
Section titled “Implementation of”add(
item,times?):void
Defined in: packages/data/src/multiset/multi-set.ts:60
Adds times occurrences of item. O(1).
Parameters
Section titled “Parameters”T
times?
Section titled “times?”number = 1
How many to add. Defaults to 1. Adding 0 is a no-op and
does not make the item present.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If times is negative or not an integer — use
delete to remove.
Implementation of
Section titled “Implementation of”addAll()
Section titled “addAll()”addAll(
items,times?):void
Defined in: packages/data/src/multiset/multi-set.ts:77
Adds times occurrences of each item. O(m) in the number of items.
Parameters
Section titled “Parameters”Iterable<T>
times?
Section titled “times?”number = 1
How many of each item to add. Defaults to 1.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If times is negative or not an integer.
Implementation of
Section titled “Implementation of”clear()
Section titled “clear()”clear():
void
Defined in: packages/data/src/multiset/multi-set.ts:47
Removes all elements from the collection.
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”count()
Section titled “count()”count():
number
Defined in: packages/data/src/multiset/multi-set.ts:43
The total including multiplicities. O(1).
Returns
Section titled “Returns”number
Implementation of
Section titled “Implementation of”delete()
Section titled “delete()”delete(
item,times?):number
Defined in: packages/data/src/multiset/multi-set.ts:98
Removes up to times occurrences of item. O(1).
Parameters
Section titled “Parameters”T
times?
Section titled “times?”number = 1
How many to remove. Defaults to 1. Removing more than are
held removes all of them rather than throwing.
Returns
Section titled “Returns”number
How many were actually removed.
Throws
Section titled “Throws”If times is negative or not an integer.
Implementation of
Section titled “Implementation of”distinct()
Section titled “distinct()”distinct():
IterableIterator<T>
Defined in: packages/data/src/multiset/multi-set.ts:136
Each distinct item once, in first-insertion order.
Returns
Section titled “Returns”IterableIterator<T>
Implementation of
Section titled “Implementation of”distinctCount()
Section titled “distinctCount()”distinctCount():
number
Defined in: packages/data/src/multiset/multi-set.ts:131
The number of distinct items, ignoring multiplicities. O(1).
Returns
Section titled “Returns”number
Implementation of
Section titled “Implementation of”entries()
Section titled “entries()”entries():
IterableIterator<readonly [T,number]>
Defined in: packages/data/src/multiset/multi-set.ts:141
Each distinct item paired with its multiplicity.
Returns
Section titled “Returns”IterableIterator<readonly [T, number]>
Implementation of
Section titled “Implementation of”has(
item):boolean
Defined in: packages/data/src/multiset/multi-set.ts:121
O(1).
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”multiplicity()
Section titled “multiplicity()”multiplicity(
item):number
Defined in: packages/data/src/multiset/multi-set.ts:126
How many times item is held; 0 when absent. O(1).
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”number