int_set v1.0.0 IntSet View Source

Efficiently store and index a set of non-negative integers.

A set can be constructed using IntSet.new/0:

iex> IntSet.new
#IntSet<[]>

An IntSet obeys the same set semantics as MapSet, and provides constant-time operations for insertion, deletion, and membership checking. Use Enum.member?/2 to check for membership.

iex> IntSet.new(3) |> Enum.member?(3)
true

Sets also implement Collectable, so it can collect values in any context that a list can:

iex> Enum.into([1, 2, 3], IntSet.new())
#IntSet<[1, 2, 3]>

The inspect/1 implementation for IntSet sorts the members, which makes it way easier to write doctests:

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

Link to this section Summary

Functions

Remove a number from the int set

Create an empty int set

Create an int set with some starting value

Add a value to the int set

Create a new set that contains all of the elements of both x and y

Link to this section Types

Link to this section Functions

Remove a number from the int set.

Examples

iex> set = IntSet.new(5)
#IntSet<[5]>
iex> IntSet.delete(set, 5)
#IntSet<[]>

Create an empty int set.

Examples

iex> IntSet.new
#IntSet<[]>

Create an int set with some starting value.

Examples

You can create a set with a single starting value.

iex> IntSet.new(0)
#IntSet<[0]>

You can also provide an enumerable of integers to start with.

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

Lastly, you can initialize the set with a bit string. Binary strings are interpreted as little-endian, with the very first bit of the string representing the number zero.

iex> IntSet.new(<<1 :: 1>>)
#IntSet<[0]>

iex> IntSet.new(<<0b1000_1000>>)
#IntSet<[0, 4]>

iex> IntSet.new(<<0 :: 1>>)
#IntSet<[]>

Add a value to the int set.

Examples

iex> set = IntSet.new()
#IntSet<[]>
iex> IntSet.put(set, 0)
#IntSet<[0]>
Link to this function union(x, y) View Source
union(t(), t()) :: t()

Create a new set that contains all of the elements of both x and y.

Examples

iex> a = IntSet.new(7)
iex> b = IntSet.new(4)
iex> IntSet.union(a, b)
#IntSet<[4, 7]>