Skip to content

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.

T extends DefinedValue

The type of items. Anything except undefined.

new MultiSet<T>(iterable?): MultiSet<T>

Defined in: packages/data/src/multiset/multi-set.ts:27

Iterable<T, any, any>

MultiSet<T>

[iterator](): Iterator<T>

Defined in: packages/data/src/multiset/multi-set.ts:34

Each item repeated as many times as it is held.

Iterator<T>

IMultiset.[iterator]


add(item, times?): void

Defined in: packages/data/src/multiset/multi-set.ts:60

Adds times occurrences of item. O(1).

T

number = 1

How many to add. Defaults to 1. Adding 0 is a no-op and does not make the item present.

void

If times is negative or not an integer — use delete to remove.

IMultiset.add


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.

Iterable<T>

number = 1

How many of each item to add. Defaults to 1.

void

If times is negative or not an integer.

IMultiset.addAll


clear(): void

Defined in: packages/data/src/multiset/multi-set.ts:47

Removes all elements from the collection.

void

IMultiset.clear


count(): number

Defined in: packages/data/src/multiset/multi-set.ts:43

The total including multiplicities. O(1).

number

IMultiset.count


delete(item, times?): number

Defined in: packages/data/src/multiset/multi-set.ts:98

Removes up to times occurrences of item. O(1).

T

number = 1

How many to remove. Defaults to 1. Removing more than are held removes all of them rather than throwing.

number

How many were actually removed.

If times is negative or not an integer.

IMultiset.delete


distinct(): IterableIterator<T>

Defined in: packages/data/src/multiset/multi-set.ts:136

Each distinct item once, in first-insertion order.

IterableIterator<T>

IMultiset.distinct


distinctCount(): number

Defined in: packages/data/src/multiset/multi-set.ts:131

The number of distinct items, ignoring multiplicities. O(1).

number

IMultiset.distinctCount


entries(): IterableIterator<readonly [T, number]>

Defined in: packages/data/src/multiset/multi-set.ts:141

Each distinct item paired with its multiplicity.

IterableIterator<readonly [T, number]>

IMultiset.entries


has(item): boolean

Defined in: packages/data/src/multiset/multi-set.ts:121

O(1).

T

boolean

IMultiset.has


multiplicity(item): number

Defined in: packages/data/src/multiset/multi-set.ts:126

How many times item is held; 0 when absent. O(1).

T

number

IMultiset.multiplicity