Bitmap.Binary

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

inspect(bitmap)

Inspects the bitmap and returns the string representation of 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?(bitmap, index)

Returns a boolean representing whether the bit at position index is set or not

set_all(bitmap)

Set all bits in the bitmap and returns a new bitmap

to_string(bitmap)

Returns the string representation of the bitmap

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

toggle_all(bitmap)

Toggles all bits in the bitmap and returns a new bitmap

unset(bitmap, index)

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

unset?(bitmap, index)

Returns a boolean representing whether the bit at position index is unset or not

unset_all(bitmap)

Unsets all bits in the bitmap and returns a new bitmap

Types

t :: binary

argt :: non_neg_integer | [any] | Range.t

index :: non_neg_integer

bit :: 1 | 0

Functions

at(bitmap, index)

Specs:

Returns the bit value at index in the bitmap

Examples

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

Specs:

Inspects the bitmap and returns the string representation of the bitmap

Note: This can be very long for huge bitmaps.

Source
new(argument)

Specs:

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.Binary.new(400)
<<0::size(400)>>
iex> Bitmap.Binary.new([1,2,3,4,5])
<<0::size(5)>>
iex> Bitmap.Binary.new(1..25)
<<0::size(25)>>
Source
set(bitmap, index)

Specs:

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

Index can also have a value :all in which case all bits will be set like in set_all

Examples

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

Specs:

Returns a boolean representing whether the bit at position index is set or not

Examples

iex> bm = Bitmap.Binary.new(5) |> Bitmap.Binary.set(1) |> Bitmap.Binary.set(3)
iex> Bitmap.Binary.set?(bm, 1)
true
iex> Bitmap.Binary.set?(bm, 4)
false
Source
set_all(bitmap)

Specs:

Set all bits in the bitmap and returns a new bitmap

Examples

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

Specs:

Returns the string representation of the bitmap

Note: This can be very long for huge bitmaps.

Source
toggle(bitmap, index)

Specs:

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

Index can also have a value :all in which case all bits will be toggled like in toggle_all

Examples

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

Specs:

Toggles all bits in the bitmap and returns a new bitmap

Examples

iex> bm = Bitmap.Binary.new(10) |> Bitmap.Binary.set(4) |> Bitmap.Binary.set(8)
iex> Bitmap.Binary.toggle_all(bm)
<<247, 1::size(2)>>
Source
unset(bitmap, index)

Specs:

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

Index can also have a value :all in which case all bits will be unset like in unset_all

Examples

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

Specs:

Returns a boolean representing whether the bit at position index is unset or not

Examples

iex> bm = Bitmap.Binary.new(5) |> Bitmap.Binary.set(1) |> Bitmap.Binary.set(3)
iex> Bitmap.Binary.unset?(bm, 1)
false
iex> Bitmap.Binary.unset?(bm, 4)
true
Source
unset_all(bitmap)

Specs:

Unsets all bits in the bitmap and returns a new bitmap

Examples

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