One9.Multiset (oMultiset v0.1.0)
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
Functions
@spec count(t()) :: non_neg_integer()
Return the cardinality of a Multiset.
See also support_count/1
.
@spec count_element(t(), term()) :: non_neg_integer()
Return the first Multiset less any copies of elements the second Multiset shares.
This difference is "soft" or "clamping". See also difference!/1
.
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
.
@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])
@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])
Return the intersection of two Multisets.
@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})
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.
@spec support_count(t()) :: non_neg_integer()
Return the cardinality of the support of a Multiset.
See also count/1
.
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})
@spec take(t(e), e1, non_neg_integer()) :: {t(e), [e1]} when e: term(), e1: term()
Convert a Multiset into a simple Map of elements to their multiplicities.
@spec to_enumerable(t(e)) :: Enumerable.t(e) when e: term()
Convert a Multiset into a complete List of elements (including repeats).
Return the union of two Multisets.