Current section
Files
Jump to
Current section
Files
README.md
# UintSet
[`UintSet`](https://hexdocs.pm/uint_set/UintSet.html) is an alternative set type in Elixir,
designed to hold non-negative integer elements.
`UintSet` emulates the full `MapSet` interface,
except for `MapSet.size` which is replaced by `UintSet.length`.
Many of the `UintSet` doctests and unit tests were adapted from `MapSet`.
`UintSet` illustrates the construction of a functional data structure from scratch,
implementing protocols—`Inspect`, `Enumerable`, `Collectable`—and a stream.
All the content of an `UintSet` is represented by a single integer,
which in Elixir is limited only by available memory.
Each bit in that integer represents one element:
a bit set to `1` at position `n` means the number `n` is present in the set.
```elixir
iex> s = UintSet.new([0, 2, 3])
#UintSet<[0, 2, 3]>
iex> s.bits
13
iex> s.bits |> Integer.to_string(2)
"1101"
```
Using an integer as a bit vector we can use fast bitwise operators
for set operations like intersection and difference.
See the source code of `UintSet.intersection` and `UintSet.difference`.
Documentation with examples: https://hexdocs.pm/uint_set/UintSet.html.
> This package was inspired by the excellent `intset` example from chapter 6 of
> _The Go Programming Language_, by Alan. A. A. Donovan and Brian W. Kernighan.
> The implementation in Elixir is much simpler because we can use a single (big) integer to hold the bit vector.
> The Go example manages an `uint64[]` slice which they need to grow and shrink on demand.
> Also, they need to loop over the slices performing bitwise operations which
> in Elixir we do in a single expression like `bits1 &&& bits2`.