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
Functions
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}]>
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}]>
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
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}]>
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}]>
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
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
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}]>
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}]>
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}]>
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
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
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}]>
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}]
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}]>