Simple Bitmap v1.2.0 SimpleBitmap View Source

Simple Bitmap to use the big integer to store (and extend) the bitmap.

Example:

iex> b = SimpleBitmap.new() %SimpleBitmap{data: 0} iex> b = SimpleBitmap.set(b, 5) %SimpleBitmap{data: 32} iex> b = SimpleBitmap.set(b, 10) %SimpleBitmap{data: 1056} iex> b = SimpleBitmap.set(b, 38) %SimpleBitmap{data: 274877908000} iex> b = SimpleBitmap.set(b, 179) %SimpleBitmap{data: 766247770432944429179173513575154591809369835969709088} iex> SimpleBitmap.msb(b) 179 iex> SimpleBitmap.msb(b, 10) [179, 38, 10, 5, 0, 0, 0, 0, 0, 0]

Link to this section Summary

Functions

Load a bitmap from a file

Get the index of the most significant bit

Get a list of most significant bits

Initialize a bitmap

Population count for bitmap

Population count for bitmap, using algorithm from https://en.wikipedia.org/wiki/Hamming_weight. However, it is not as performant as popcount()

Save the bitmap into file iex> b = SimpleBitmap.new(1024) %SimpleBitmap{data: 1024} iex> b = SimpleBitmap.set(b, 9) %SimpleBitmap{data: 1536} iex> SimpleBitmap.save(b, "/tmp/simple_bitmap") :ok

Set the bit to 1 for given index

Check if a bit is set to 1 for given index

Set the bit to 0 for given index

Link to this section Types

Link to this type

t() View Source
t() :: %SimpleBitmap{data: term()}

Link to this section Functions

Load a bitmap from a file

iex> b = SimpleBitmap.new(1024) %SimpleBitmap{data: 1024} iex> b = SimpleBitmap.set(b, 9) %SimpleBitmap{data: 1536} iex> b = SimpleBitmap.set(b, 225) %SimpleBitmap{

data: 53919893334301279589334030174039261347274288845081144962207220499968

} iex> SimpleBitmap.save(b, "/tmp/simple_bitmap") iex> SimpleBitmap.load("/tmp/simple_bitmap") %SimpleBitmap{

data: 53919893334301279589334030174039261347274288845081144962207220499968

}

Get the index of the most significant bit.

iex> b = SimpleBitmap.new() iex> b = SimpleBitmap.set(b, 301) iex> b = SimpleBitmap.set(b, 4) iex> b = SimpleBitmap.set(b, 252) iex> SimpleBitmap.msb(b) 301

Get a list of most significant bits.

iex> b = SimpleBitmap.new() iex> b = SimpleBitmap.set(b, 1) iex> b = SimpleBitmap.set(b, 4) iex> b = SimpleBitmap.set(b, 9) iex> b = SimpleBitmap.set(b, 33) iex> b = SimpleBitmap.set(b, 1753421) iex> b = SimpleBitmap.set(b, 9326887) iex> b = SimpleBitmap.unset(b, 32) iex> b = SimpleBitmap.unset(b, 9) iex> SimpleBitmap.msb(b, 10) [9326887, 1753421, 33, 4, 1, 0, 0, 0, 0, 0] iex> SimpleBitmap.msb(b, 10, 3) [4, 1, 0, 0, 0, 0, 0, 0, 0, 0]

Initialize a bitmap.

iex> SimpleBitmap.new() %SimpleBitmap{data: 0} iex> SimpleBitmap.new(1024) %SimpleBitmap{data: 1024}

Link to this function

popcount(bitmap) View Source
popcount(t()) :: non_neg_integer()

Population count for bitmap.

Example:

iex> b = SimpleBitmap.new() iex> b = SimpleBitmap.set(b, 1) iex> b = SimpleBitmap.set(b, 4) iex> b = SimpleBitmap.set(b, 9) iex> b = SimpleBitmap.set(b, 33) iex> SimpleBitmap.popcount(b) 4

Link to this function

popcount1(bitmap) View Source
popcount1(t()) :: non_neg_integer()

Population count for bitmap, using algorithm from https://en.wikipedia.org/wiki/Hamming_weight. However, it is not as performant as popcount().

//This is better when most bits in x are 0
//This is algorithm works the same for all data sizes.
//This algorithm uses 3 arithmetic operations and 1 comparison/branch per "1" bit in x.
int popcount64d(uint64_t x)
{
    int count;
    for (count=0; x; count++)
        x &= x - 1;
    return count;
}

Example:

iex> b = SimpleBitmap.new() iex> b = SimpleBitmap.set(b, 1) iex> b = SimpleBitmap.set(b, 4) iex> b = SimpleBitmap.set(b, 9) iex> b = SimpleBitmap.set(b, 33) iex> SimpleBitmap.popcount(b) 4

Link to this function

save(bitmap, filename) View Source
save(t(), binary()) :: :ok

Save the bitmap into file iex> b = SimpleBitmap.new(1024) %SimpleBitmap{data: 1024} iex> b = SimpleBitmap.set(b, 9) %SimpleBitmap{data: 1536} iex> SimpleBitmap.save(b, "/tmp/simple_bitmap") :ok

Link to this function

set(bitmap, index) View Source
set(t(), non_neg_integer()) :: t()

Set the bit to 1 for given index.

iex> b = SimpleBitmap.new() iex> SimpleBitmap.set(b, 3) %SimpleBitmap{data: 8}

Check if a bit is set to 1 for given index.

iex> b = SimpleBitmap.new() iex> b = SimpleBitmap.set(b, 15) iex> b = SimpleBitmap.set(b, 16) iex> SimpleBitmap.set?(b, 15) true

Link to this function

unset(bitmap, index) View Source
unset(t(), non_neg_integer()) :: t()

Set the bit to 0 for given index.

iex> b = SimpleBitmap.new() iex> b = SimpleBitmap.set(b, 2) iex> b = SimpleBitmap.set(b, 8) iex> SimpleBitmap.unset(b, 8) %SimpleBitmap{data: 4}