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

n :: non_neg_integer
t

Functions

delete(nat_set, n)

Specs

delete(t, n) :: t

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]>
difference(nat_set1, nat_set2)

Specs

difference(t, t) :: t

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]>
disjoint?(nat_set1, nat_set2)

Specs

disjoint?(t, t) :: boolean

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
equal?(nat_set1, nat_set2)

Specs

equal?(t, t) :: boolean

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
intersection(nat_set1, nat_set2)

Specs

intersection(t, t) :: t

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]>
member?(nat_set, n)

Specs

member?(t, n) :: boolean

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
new()

Specs

new :: t

Returns a new (empty) NatSet.

Examples

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

Specs

new(Enum.t) :: t

Creates a NatSet from an enumerable.

Examples

iex> NatSet.new([3, 3, 3, 2, 2, 1])
#NatSet<[1, 2, 3]>
new(enumerable, transform)

Specs

new(Enum.t, (n -> n)) :: t

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]>
put(nat_set, n)

Specs

put(t, n) :: t

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]>
size(nat_set)

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
subset?(nat_set1, nat_set2)

Specs

subset?(t, t) :: boolean

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
to_list(nat_set)

Specs

to_list(t) :: [n]

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]
to_stream(nat_set)

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]
union(nat_set1, nat_set2)

Specs

union(t, t) :: t

Returns the union of nat_set1 and nat_set2.

This is the set of elements that occur in nat_set1 or nat_set2.

Examples

iex> NatSet.union(NatSet.new([1, 2]), NatSet.new([2, 3]))
#NatSet<[1, 2, 3]>