Bloomy.BitArray (Bloomy v0.1.0)

View Source

Efficient bit array implementation using Nx tensors.

This module provides a high-performance bit array using Nx tensors, enabling vectorized operations and EXLA acceleration for GPU/CPU computing.

Examples

iex> bit_array = Bloomy.BitArray.new(1000)
iex> bit_array = Bloomy.BitArray.set(bit_array, [10, 20, 30])
iex> Bloomy.BitArray.get(bit_array, 10)
true
iex> Bloomy.BitArray.get(bit_array, 15)
false

Summary

Functions

Check if all bits at given indices are set to 1.

Clear all bits (set to 0).

Count the number of bits set to 1.

Calculate the fill ratio (proportion of bits set to 1).

Check if bits at given indices are set (value is 1).

Perform bitwise AND with another bit array (intersection operation).

Create a new bit array of given size.

Set bits at given indices to 1.

Perform bitwise OR with another bit array (union operation).

Types

t()

@type t() :: %Bloomy.BitArray{
  backend: atom(),
  bits: Nx.Tensor.t(),
  size: non_neg_integer()
}

Functions

all_set?(bit_array, indices)

Check if all bits at given indices are set to 1.

Useful for bloom filter membership tests.

Parameters

  • bit_array - The BitArray struct
  • indices - Nx tensor or list of indices

Returns

Boolean indicating if all specified bits are set.

Examples

iex> ba = Bloomy.BitArray.new(100)
iex> ba = Bloomy.BitArray.set(ba, [10, 20, 30])
iex> Bloomy.BitArray.all_set?(ba, [10, 20, 30])
true
iex> Bloomy.BitArray.all_set?(ba, [10, 20, 40])
false

clear(bit_array)

Clear all bits (set to 0).

Parameters

  • bit_array - The BitArray struct

Returns

New BitArray with all bits set to 0.

count(bit_array)

Count the number of bits set to 1.

Parameters

  • bit_array - The BitArray struct

Returns

Number of bits set to 1.

Examples

iex> ba = Bloomy.BitArray.new(100)
iex> ba = Bloomy.BitArray.set(ba, [10, 20, 30])
iex> Bloomy.BitArray.count(ba)
3

fill_ratio(bit_array)

Calculate the fill ratio (proportion of bits set to 1).

Parameters

  • bit_array - The BitArray struct

Returns

Float between 0.0 and 1.0 representing the fill ratio.

Examples

iex> ba = Bloomy.BitArray.new(100)
iex> ba = Bloomy.BitArray.set(ba, [10, 20, 30])
iex> Bloomy.BitArray.fill_ratio(ba)
0.03

get(bit_array, indices)

Check if bits at given indices are set (value is 1).

Parameters

  • bit_array - The BitArray struct
  • indices - Single index (integer) or list of indices, or Nx tensor of indices

Returns

Boolean (for single index) or list of booleans (for multiple indices).

Examples

iex> ba = Bloomy.BitArray.new(100)
iex> ba = Bloomy.BitArray.set(ba, [10, 20])
iex> Bloomy.BitArray.get(ba, 10)
true
iex> Bloomy.BitArray.get(ba, 11)
false
iex> Bloomy.BitArray.get(ba, [10, 11, 20])
[true, false, true]

intersect(bit_array1, bit_array2)

Perform bitwise AND with another bit array (intersection operation).

Both bit arrays must have the same size.

Parameters

  • bit_array1 - First BitArray
  • bit_array2 - Second BitArray

Returns

New BitArray containing the intersection (AND) of both arrays.

Examples

iex> ba1 = Bloomy.BitArray.new(100) |> Bloomy.BitArray.set([10, 20, 30])
iex> ba2 = Bloomy.BitArray.new(100) |> Bloomy.BitArray.set([20, 30, 40])
iex> ba_intersect = Bloomy.BitArray.intersect(ba1, ba2)
iex> Bloomy.BitArray.count(ba_intersect)
2

new(size, opts \\ [])

Create a new bit array of given size.

All bits are initialized to 0.

Parameters

  • size - Number of bits in the array
  • opts - Optional keyword list with:
    • :backend - Nx backend to use (default: Nx.default_backend())

Returns

A new BitArray struct.

Examples

iex> ba = Bloomy.BitArray.new(100)
iex> ba.size
100

set(bit_array, indices)

Set bits at given indices to 1.

Parameters

  • bit_array - The BitArray struct
  • indices - Single index (integer) or list of indices, or Nx tensor of indices

Returns

Updated BitArray struct with specified bits set.

Examples

iex> ba = Bloomy.BitArray.new(100)
iex> ba = Bloomy.BitArray.set(ba, 42)
iex> Bloomy.BitArray.get(ba, 42)
true

iex> ba = Bloomy.BitArray.new(100)
iex> ba = Bloomy.BitArray.set(ba, [10, 20, 30])
iex> Bloomy.BitArray.get(ba, [10, 20, 30])
[true, true, true]

union(bit_array1, bit_array2)

Perform bitwise OR with another bit array (union operation).

Both bit arrays must have the same size.

Parameters

  • bit_array1 - First BitArray
  • bit_array2 - Second BitArray

Returns

New BitArray containing the union (OR) of both arrays.

Examples

iex> ba1 = Bloomy.BitArray.new(100) |> Bloomy.BitArray.set([10, 20])
iex> ba2 = Bloomy.BitArray.new(100) |> Bloomy.BitArray.set([20, 30])
iex> ba_union = Bloomy.BitArray.union(ba1, ba2)
iex> Bloomy.BitArray.count(ba_union)
3