One9.Multiset (oMultiset v0.1.1)

An unordered multiplicitous container type.

Summary

Functions

Return the cardinality of a Multiset.

Return the first Multiset less any copies of elements the second Multiset shares.

Return the first Multiset less any copies of elements the second Multiset shares.

Create a Multiset from any (finite) Enumerable of {t:term/0, t:non_neg_integer/0} tuples.

Create a Multiset from any (finite) Enumerable of values.

Return the intersection of two Multisets.

Create a Multiset from a MapSet, List of elements, or Map of multiplicities.

Determine whether the first Multiset is a (non-strict) subset of the second.

Return the sum of two Multisets.

Convert a Multiset into a List of unique elements.

Return the cardinality of the support of a Multiset.

Example

iex> One9.Multiset.symmetric_difference(
...>   One9.Multiset.new(%{a: 10, b:  2, c: 1}),
...>   One9.Multiset.new(%{a:  3, b: 10, c: 1})
...> )
One9.Multiset.from_counts(%{a: 7, b: 8})

Convert a Multiset into a simple Map of elements to their multiplicities.

Convert a Multiset into a complete List of elements (including repeats).

Return the union of two Multisets.

Types

internal(value)

@opaque internal(value)

t()

@type t() :: t(term())

t(value)

@type t(value) :: %One9.Multiset{counts: internal(value)}

Functions

count(multiset)

@spec count(t()) :: non_neg_integer()

Return the cardinality of a Multiset.

See also support_count/1.

count_element(multiset, element)

@spec count_element(t(), term()) :: non_neg_integer()

delete(multiset, element, count \\ 1)

@spec delete(t(e), term(), :all) :: t(e) when e: term()
@spec delete(t(e), term(), non_neg_integer()) :: t(e) when e: term()

difference(multiset1, multiset2)

@spec difference(t(e), t()) :: t(e) when e: term()

Return the first Multiset less any copies of elements the second Multiset shares.

This difference is "soft" or "clamping". See also difference!/1.

difference!(multiset1, multiset2)

@spec difference!(t(e), t(e)) :: t(e) when e: term()

Return the first Multiset less any copies of elements the second Multiset shares.

Raises if the first Multiset is not a subset of the second. See also difference/1.

from_counts(enumerable)

@spec from_counts(One9.Ms.t0(e)) :: t(e) when e: term()

Create a Multiset from any (finite) Enumerable of {t:term/0, t:non_neg_integer/0} tuples.

Duplicate entries for the same element are accepted, and will be folded in additively.

Usage

iex> One9.Multiset.from_counts(%{a: 2, b: 1, c: 10})
One9.Multiset.new([:a, :a, :b, :c, :c, :c, :c, :c, :c, :c, :c, :c, :c])

iex> One9.Multiset.from_counts([a: 2, b: 1, c: 0, a: 1])
One9.Multiset.new([:a, :a, :a, :b])

from_elements(enumerable)

@spec from_elements(Enumerable.t(e)) :: t(e) when e: term()

Create a Multiset from any (finite) Enumerable of values.

Usage

iex> One9.Multiset.from_elements(MapSet.new([0, 1, 1, 2, 3, 5, 8]))
One9.Multiset.new([0, 1, 2, 3, 5, 8])

iex> One9.Multiset.from_elements([0, 1, 1, 2, 3, 5, 8])
One9.Multiset.new([0, 1, 1, 2, 3, 5, 8])

intersection(multiset1, multiset2)

@spec intersection(t(e | e1), t(e | e2)) :: t(e)
when e: term(), e1: term(), e2: term()

Return the intersection of two Multisets.

member?(multiset, element)

@spec member?(t(), term()) :: boolean()

new(arg \\ MapSet.new([]))

@spec new([e] | One9.Ms.t0(e) | MapSet.t(e)) :: t(e) when e: term()

Create a Multiset from a MapSet, List of elements, or Map of multiplicities.

To construct a Multiset from any other type, use from_counts/1 or from_elements/1 instead.

Usage

iex> One9.Multiset.new(MapSet.new([0, 1, 1, 2, 3, 5, 8]))
One9.Multiset.new([0, 1, 2, 3, 5, 8])

iex> One9.Multiset.new([0, 1, 1, 2, 3, 5, 8])
One9.Multiset.new([0, 1, 1, 2, 3, 5, 8])

iex> One9.Multiset.new(%{a: 2, b: 1, c: 1})
One9.Multiset.new([:a, :a, :b, :c])

iex> One9.Multiset.new(%{a: 2, b: 1, c: 10})
One9.Multiset.new([:a, :a, :b, :c, :c, :c, :c, :c, :c, :c, :c, :c, :c])

iex> One9.Multiset.new(%{a: 2, b: 1, c: 10**100})
# alternate Inspect representation kicks in approximately when
# the cardinality of a Multiset exceeds
# the square of the cardinality of its support
One9.Multiset.from_counts(%{a: 2, b: 1, c: 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000})

put(multiset, element, count \\ 1)

@spec put(t(e1), e2, pos_integer()) :: t(e1 | e2) when e1: term(), e2: term()
@spec put(t(e1), e2, 0) :: t(e1) when e1: term(), e2: term()

slice(multiset)

subset?(multiset1, multiset2)

@spec subset?(t(), t()) :: boolean()

Determine whether the first Multiset is a (non-strict) subset of the second.

sum(multiset1, multiset2)

@spec sum(t(e1), t(e2)) :: t(e1 | e2) when e1: term(), e2: term()

Return the sum of two Multisets.

support(multiset)

@spec support(t(e)) :: [e] when e: term()

Convert a Multiset into a List of unique elements.

support_count(multiset)

@spec support_count(t()) :: non_neg_integer()

Return the cardinality of the support of a Multiset.

See also count/1.

symmetric_difference(multiset1, multiset2)

@spec symmetric_difference(t(e1), t(e2)) :: t(e1 | e2) when e1: term(), e2: term()

Example

iex> One9.Multiset.symmetric_difference(
...>   One9.Multiset.new(%{a: 10, b:  2, c: 1}),
...>   One9.Multiset.new(%{a:  3, b: 10, c: 1})
...> )
One9.Multiset.from_counts(%{a: 7, b: 8})

take(multiset, element, count)

@spec take(t(e), e1, non_neg_integer()) :: {t(e), [e1]} when e: term(), e1: term()

to_counts(multiset)

@spec to_counts(t(e)) :: One9.Ms.t(e) when e: term()

Convert a Multiset into a simple Map of elements to their multiplicities.

to_enumerable(multiset)

@spec to_enumerable(t(e)) :: Enumerable.t(e) when e: term()

to_list(multiset)

@spec to_list(t(e)) :: [e] when e: term()

Convert a Multiset into a complete List of elements (including repeats).

union(multiset1, multiset2)

@spec union(t(e1), t(e2)) :: t(e1 | e2) when e1: term(), e2: term()

Return the union of two Multisets.