Bitmap

Contains functions to create and work with a bitmap

Bitmaps are also known as bit arrays, bit sets and is a fast space efficient data structure for lookups

The module has been designed to be pipe-friendly, so pipe ‘em up

Source

Summary

at(bitmap, index)

Returns the bit value at index in the bitmap

new(argument)

Creates and returns a bitmap of size corresponding to the argument passed

set(bitmap, index)

Sets the bit at index in the bitmap and returns the new bitmap

set_all(bitmap)

Set all bits in the bitmap and returns a new bitmap

toggle(bitmap, index)

Toggles the bit at index in the bitmap and returns the new bitmap

unset(bitmap, index)

Unsets the bit at index in the bitmap and returns the new bitmap

unset_all(bitmap)

Unsets all bits in the bitmap and returns a new bitmap

Functions

at(bitmap, index)

Returns the bit value at index in the bitmap

Examples

iex> bm = Bitmap.new(5)
iex> Bitmap.at(bm, 2)
0
iex> bm = Bitmap.set(bm, 2)
iex> Bitmap.at(bm, 2)
1
Source
new(argument)

Creates and returns a bitmap of size corresponding to the argument passed.

If argument is

  • integer, size of bitmap is equal to the argument
  • range, size of bitmap is equal to the length of argument
  • list, size of bitmap is equal to the length of argument

Note: All bits are set to 0 by default

Examples

iex> Bitmap.new(400)
<<0::size(400)>>
iex> Bitmap.new([1,2,3,4,5])
<<0::size(5)>>
iex> Bitmap.new(1..25)
<<0::size(25)>>
Source
set(bitmap, index)

Sets the bit at index in the bitmap and returns the new bitmap

Examples

iex> Bitmap.set(Bitmap.new(5), 3)
<<2::size(5)>>
iex> Bitmap.set(Bitmap.new(1..10), 2)
<<32, 0::size(2)>>
Source
set_all(bitmap)

Set all bits in the bitmap and returns a new bitmap

Examples

iex> Bitmap.set_all(Bitmap.new(10))
<<255, 3::size(2)>>
iex> Bitmap.set_all(Bitmap.new(100))
<<255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 15::size(4)>>
Source
toggle(bitmap, index)

Toggles the bit at index in the bitmap and returns the new bitmap.

i.e. it sets the bit to 1 if it was 0 or sets the bit to 0 if it was 1

Examples

iex> bm = Bitmap.new(10) |> Bitmap.set(4) |> Bitmap.set(8)
iex> Bitmap.toggle(bm, 3)
<<24, 2::size(2)>>
iex> Bitmap.toggle(bm, 6)
<<10, 2::size(2)>>
Source
unset(bitmap, index)

Unsets the bit at index in the bitmap and returns the new bitmap

Examples

iex> bm = Bitmap.new(10) |> Bitmap.set(4) |> Bitmap.set(8)
iex> Bitmap.unset(bm, 4)
<<0, 2::size(2)>>
iex> Bitmap.unset(bm, 8)
<<8, 0::size(2)>>
Source
unset_all(bitmap)

Unsets all bits in the bitmap and returns a new bitmap

Examples

iex> bm = Bitmap.new(10) |> Bitmap.set(4) |> Bitmap.set(8)
iex> Bitmap.unset_all(bm)
<<0, 0::size(2)>>
Source