Multiset v0.0.4 Multiset

Functions for working with multisets, i.e. sets allowing multiple instances of values.

The number of instances of a value in a multiset is called the multiplicity of the value.

The Multiset is represented internally as a struct, therefore %Multiset{} can be used whenever there is a need to match on any Multiset. Note though the struct fields are private and must not be accessed directly. Instead, use the functions in this module.

Summary

Functions

Deletes k (by default 1) instances of value from multiset

Returns a multiset that is multiset1 without the (instances of) values of multiset2

Checks if two multisets are equal

Creates a multiset from a list of pairs of values and their multiplicities

Returns a multiset containing only (instances of) members that multiset1 and multiset2 have in common

Checks if multiset contains at least one instance of value

Returns the multiplicity of value in multiset

Returns a new multiset

Creates a multiset from an enumerable

Creates a multiset from an enumerable via a function that assigns multiplicities

Inserts k (by default 1) instances of value into multiset

Returns the number of (instances of) values in multiset

Checks if multiset1’s values all have a smaller (or equal) multiplicity as the corresponding values in multiset2

Returns the multiset sum of multiset1 and multiset2

Converts multiset to a list of pairs of values and their multiplicities

Returns the multiset union of multiset1 and multiset2

Returns the values in multiset as a MapSet

Types

t
value :: term

Functions

delete(multiset, value, k \\ 1)

Specs

delete(t, value, integer) :: t

Deletes k (by default 1) instances of value from multiset.

Returns a new multiset which is a copy of multiset but with k fewer instance of value.

Examples

iex> multiset = Multiset.new([1, 2, 3, 3])
iex> Multiset.delete(multiset, 3, 2)
#Multiset<[{1, 1}, {2, 1}]>
iex> Multiset.delete(multiset, 3)
#Multiset<[{1, 1}, {2, 1}, {3, 1}]>
difference(multiset1, multiset2)

Specs

difference(t, t) :: t

Returns a multiset that is multiset1 without the (instances of) values of multiset2.

Examples

iex> Multiset.difference(Multiset.new([1, 2, 2, 3, 3]), Multiset.new([1, 1, 2, 4]))
#Multiset<[{2, 1}, {3, 2}]>
equal?(multiset1, multiset2)

Specs

equal?(t, t) :: boolean

Checks if two multisets are equal.

The comparison between values must be done using ===.

Examples

iex> Multiset.equal?(Multiset.new([1, 2]), Multiset.new([2, 1]))
true
iex> Multiset.equal?(Multiset.new([1, 2]), Multiset.new([1, 1, 2]))
false
from_list(pairs)

Specs

from_list([{t, pos_integer}]) :: t

Creates a multiset from a list of pairs of values and their multiplicities.

Examples

iex> Multiset.from_list([{1, 3}, {2, 4}, {3, 0}])
#Multiset<[{1, 3}, {2, 4}]>
intersection(multiset1, multiset2)

Specs

intersection(t, t) :: t

Returns a multiset containing only (instances of) members that multiset1 and multiset2 have in common.

Examples

iex> Multiset.intersection(Multiset.new([1, 2, 2, 2, 3]), Multiset.new([2, 2, 3, 3, 4]))
#Multiset<[{2, 2}, {3, 1}]>
member?(multiset, value)

Specs

member?(t, value) :: boolean

Checks if multiset contains at least one instance of value.

Examples

iex> Multiset.member?(Multiset.new([1, 2, 3]), 2)
true
iex> Multiset.member?(Multiset.new([1, 2, 3]), 4)
false
multiplicity(multiset, value)

Specs

multiplicity(t, value) :: non_neg_integer

Returns the multiplicity of value in multiset.

Examples

iex> Multiset.multiplicity(Multiset.new([1, 2, 3, 1]), 1)
2
iex> Multiset.multiplicity(Multiset.new([1, 2, 3]), 4)
0
new()

Specs

new :: t

Returns a new multiset.

Examples

iex> Multiset.new
#Multiset<[]>
new(enumerable)

Specs

new(Enum.t) :: t

Creates a multiset from an enumerable.

Examples

iex> Multiset.new([:b, :a, 3, :a])
#Multiset<[{3, 1}, {:a, 2}, {:b, 1}]>
iex> Multiset.new([3, 3, 2, 2, 1])
#Multiset<[{1, 1}, {2, 2}, {3, 2}]>
new(enumerable, multiplicities)

Specs

new(Enum.t, (term -> integer)) :: t

Creates a multiset from an enumerable via a function that assigns multiplicities.

If multiplicities returns an integer < 1 for a value, then that value is not added to the multiset.

Examples

iex> Multiset.new([1, 2, 3], fn x -> x - 1 end)
#Multiset<[{2, 1}, {3, 2}]>
put(multiset, value, k \\ 1)

Specs

put(t, value, integer) :: t

Inserts k (by default 1) instances of value into multiset.

Returns a new multiset which is a copy of multiset but with k more instance of value.

Examples

iex> multiset = Multiset.new([1, 2])
iex> Multiset.put(multiset, 3, 2)
#Multiset<[{1, 1}, {2, 1}, {3, 2}]>
iex> Multiset.put(multiset, 1)
#Multiset<[{1, 2}, {2, 1}]>
size(multiset)

Specs

size(t) :: non_neg_integer

Returns the number of (instances of) values in multiset.

Examples

iex> Multiset.size(Multiset.new([1, 2, 2]))
3
subset?(multiset1, multiset2)

Specs

subset?(t, t) :: boolean

Checks if multiset1’s values all have a smaller (or equal) multiplicity as the corresponding values in multiset2.

Examples

iex> Multiset.subset?(Multiset.new([1, 1, 2]), Multiset.new([1, 1, 2, 2, 3]))
true
iex> Multiset.subset?(Multiset.new([1, 1, 2]), Multiset.new([1, 2, 3]))
false
sum(multiset1, multiset2)

Specs

sum(t, t) :: t

Returns the multiset sum of multiset1 and multiset2.

Examples

iex> Multiset.sum(Multiset.new([1, 2, 2]), Multiset.new([2, 3, 3]))
#Multiset<[{1, 1}, {2, 3}, {3, 2}]>
to_list(multiset)

Specs

to_list(t) :: [{t, pos_integer}]

Converts multiset to a list of pairs of values and their multiplicities.

Examples

iex> Multiset.to_list(Multiset.new([1, 2, 3, 1]))
[{1, 2}, {2, 1}, {3, 1}]
union(multiset1, multiset2)

Specs

union(t, t) :: t

Returns the multiset union of multiset1 and multiset2.

Examples

iex> Multiset.union(Multiset.new([1, 2, 2]), Multiset.new([2, 3, 3]))
#Multiset<[{1, 1}, {2, 2}, {3, 2}]>
values(multiset)

Specs

values(t) :: MapSet.t

Returns the values in multiset as a MapSet.

Examples

iex> Multiset.values(Multiset.new([1, 2, 2, 3]))
MapSet.new([1, 2, 3])