One9.Ms (oMultiset v0.1.0)

Operations on simple non-struct multiplicity maps ("multisets", t:t/1``). "Non-well-formed" multisets include some entries for absent elements (%{"damn" => 0}`), and may be accepted for certain operations for convenience.

Summary

Functions

Return the cardinality of a multiset.

Determine the multiplicity of an element in a multiset.

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

Create a well-formed multiset from any (finite) Enumerable of values.

Determine whether a value is present at all in a multiset.

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

Convert a multiset into a List of unique elements.

Return the cardinality of the support of a multiset.

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

Return the union of two multisets.

Types

t0()

@type t0() :: t0(term())

t0(value)

@type t0(value) :: Enumerable.t({value, non_neg_integer()})

t()

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

t(value)

@type t(value) :: %{optional(value) => pos_integer()}

t_lax()

@type t_lax() :: t_lax(term())

t_lax(value)

@type t_lax(value) :: %{optional(value) => non_neg_integer()}

Functions

at(ms, index)

@spec at(t0(e), non_neg_integer()) :: e when e: term()

count(ms)

@spec count(t_lax()) :: non_neg_integer()

Return the cardinality of a multiset.

Input may be non-well-formed.

See also support_count/1.

count_element(ms, element)

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

Determine the multiplicity of an element in a multiset.

Input may be non-well-formed.

delete(ms, element, count \\ :all)

@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(ms1, ms2)

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

difference!(ms1, ms2)

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

from_counts(counts \\ %{})

@spec from_counts(t0(e)) :: t(e) when e: term()

Create a well-formed 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.Ms.from_counts([a: 2, b: 1, c: 10, c: 0, a: 1])
%{a: 3, b: 1, c: 10}

iex> One9.Ms.from_counts(%{a: 2, b: 1, c: 0})
%{a: 2, b: 1}

from_elements(elements \\ [])

@spec from_elements(t0(e)) :: t(e) when e: term()

Create a well-formed multiset from any (finite) Enumerable of values.

Usage

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

intersection(ms1, ms2)

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

member?(ms, element)

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

Determine whether a value is present at all in a multiset.

Input must be well-formed.

See also count_element/2.

put(ms, 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(ms)

@spec slice(t(e)) ::
  {size :: non_neg_integer(),
   :gb_trees.tree(
     start_index :: non_neg_integer(),
     {e, chunk_size :: non_neg_integer()}
   )}
when e: term()
@spec slice(t(e)) ::
  :gb_trees.tree(
    start_index :: non_neg_integer(),
    {e, chunk_size :: non_neg_integer()}
  )
when e: term()

subset?(ms1, ms2)

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

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

First operand must be well-formed.

sum(ms1, ms2)

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

support(ms)

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

Convert a multiset into a List of unique elements.

Input must be well-formed.

support_count(ms)

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

Return the cardinality of the support of a multiset.

Argument must be well-formed.

See also count/1.

symmetric_difference(ms1, ms2)

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

take(ms, element, count)

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

to_enumerable(ms)

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

to_list(ms)

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

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

Input may be non-well-formed.

union(ms1, ms2)

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

Return the union of two multisets.

Either/both operands may be non-well-formed. Result will be well-formed whenever both operands are well-formed.