NatSet v0.0.1 NatSet
Functions for working with sets of natural numbers (i.e. non-negative integers).
This module uses bitwise operations to represent such sets much more compactly than you would get when using standard MapSets.
The NatSet
is represented internally as a struct, therefore %NatSet{}
can be used whenever
there is a need to match on any NatSet
. Note though the struct fields are private and must not
be accessed directly. Instead, use the functions in this module.
NatSets are stored using integers, in which each bit represents an element of the set. As Elixir/Erlang uses arbitrary-sized integers, the memory used by a NatSet grows dynamically as needed. The exact amount of memory used depends on your architecture — see Erlang data types.
NatSets are Enumerable
and Collectable
.
Summary
Functions
Ensures that the number n
is absent from nat_set
Returns the difference between nat_set1
and nat_set2
Checks if nat_set1
and nat_set2
have no members in common
Checks if two NatSets are equal
Returns the intersection between nat_set1
and nat_set2
Checks if nat_set
contains n
Returns a new (empty) NatSet
Creates a NatSet from an enumerable
Creates a NatSet from an enumerable via the transformation function
Ensures that the number n
is present in nat_set
Returns the cardinality of (i.e. number of elements in) nat_set
Checks if all of nat_set1
’s elements occur in nat_set2
Converts nat_set
to a list of its elements in ascending order
Converts nat_set
into a stream of its elements in ascending order
Returns the union of nat_set1
and nat_set2
Types
Functions
Ensures that the number n
is absent from nat_set
.
Examples
iex> nat_set = NatSet.new([1, 2, 3])
iex> NatSet.delete(nat_set, 4)
#NatSet<[1, 2, 3]>
iex> NatSet.delete(nat_set, 2)
#NatSet<[1, 3]>
Returns the difference between nat_set1
and nat_set2
.
The result is the set of all elements that are in nat_set1
but not in nat_set2
.
Examples
iex> NatSet.difference(NatSet.new([4, 2]), NatSet.new([2, 3]))
#NatSet<[4]>
Checks if nat_set1
and nat_set2
have no members in common.
Examples
iex> NatSet.disjoint?(NatSet.new([1, 2]), NatSet.new([3, 4]))
true
iex> NatSet.disjoint?(NatSet.new([1, 2]), NatSet.new([2, 3]))
false
Checks if two NatSets are equal.
Examples
iex> NatSet.equal?(NatSet.new([1, 2]), NatSet.new([2, 1]))
true
iex> NatSet.equal?(NatSet.new([1, 2]), NatSet.new(0..2))
false
Returns the intersection between nat_set1
and nat_set2
.
The result is the set of all elements that are in both nat_set1
and nat_set2
.
Examples
iex> NatSet.intersection(NatSet.new([3, 4, 2]), NatSet.new([2, 3, 1]))
#NatSet<[2, 3]>
Checks if nat_set
contains n
.
Examples
iex> nat_set = NatSet.new([1, 2, 3])
iex> NatSet.member?(nat_set, 2)
true
iex> NatSet.member?(nat_set, 4)
false
Creates a NatSet from an enumerable.
Examples
iex> NatSet.new([3, 3, 3, 2, 2, 1])
#NatSet<[1, 2, 3]>
Creates a NatSet from an enumerable via the transformation function.
Examples
iex> NatSet.new([1, 2, 1], fn x -> 2 * x end)
#NatSet<[2, 4]>
Ensures that the number n
is present in nat_set
.
Examples
iex> NatSet.put(NatSet.new([1, 2, 3]), 3)
#NatSet<[1, 2, 3]>
iex> NatSet.put(NatSet.new([1, 2, 3]), 4)
#NatSet<[1, 2, 3, 4]>
Specs
size(t) :: non_neg_integer
Returns the cardinality of (i.e. number of elements in) nat_set
.
Examples
iex> NatSet.new([0, 4, 2]) |> NatSet.size
3
Checks if all of nat_set1
’s elements occur in nat_set2
.
Examples
iex> NatSet.subset?(NatSet.new([1, 2]), NatSet.new([1, 2, 3]))
true
iex> NatSet.subset?(NatSet.new([1, 2, 3]), NatSet.new([1, 2]))
false
Converts nat_set
to a list of its elements in ascending order.
Examples
iex> [3, 1, 4, 2] |> NatSet.new |> NatSet.to_list
[1, 2, 3, 4]
Specs
to_stream(t) :: Enumerable.t
Converts nat_set
into a stream of its elements in ascending order.
Examples
iex> [3, 1, 4, 2] |> NatSet.new |> NatSet.to_stream |> Enum.to_list
[1, 2, 3, 4]