ExBitset (ex_bitset v1.1.1) View Source

ExBitset provides the functionality to create/query bitarray sets.

To define a set constructor you first need to specify all possible set elements. This is done via the defbitset macro:

defmodule Roles
  import ExBitset, only: [defbitset: 1]

  defbitset [:admin, :owner, :writer, :viewer, :guest]
end

Then, create a new set specifying all of the elements in it:

roles = ExBitset.new(Roles, [:admin, :owner])

To then query the set, you can use any of the following functions:

ExBitset.member?(roles, :owner) # => true

roles2 = ExBitset.new(Roles, [:admin, :writer])

ExBitset.union(roles, roles2) |> Enum.to_list() # => [:admin, :owner, :writer]
ExBitset.intersection(roles, roles2) |> Enum.to_list() # => [:admin]
ExBitset.subtract(role, roles2) |> Enum.to_list() # => [:owner]

# OR alternatively using Enum functions
Enum.join(roles, ", ") # => "admin, owner"
Enum.map(roles, &to_string/1) # => ["admin", "owner"]

Link to this section Summary

Link to this section Types

Link to this section Functions

Specs

count(t()) :: non_neg_integer()
Link to this macro

defbitset(set_domain)

View Source (macro)

Specs

domain(t()) :: [item()]

Specs

from_binary(module(), binary()) :: t()

Specs

from_int(module(), non_neg_integer()) :: t()
Link to this function

intersection(ex_bitset1, ex_bitset2)

View Source

Specs

intersection(t(), t()) :: t()
Link to this function

member?(ex_bitset, item)

View Source

Specs

member?(t(), term()) :: boolean()

Specs

new(module(), [item()]) :: t()
Link to this function

subtract(ex_bitset1, ex_bitset2)

View Source

Specs

subtract(t(), t()) :: t()

Specs

to_binary(t()) :: binary()

Specs

to_int(t()) :: non_neg_integer()

Specs

to_list(t()) :: [item()]
Link to this function

union(ex_bitset1, ex_bitset2)

View Source

Specs

union(t(), t()) :: t()