Bloomy.BitArray (Bloomy v0.1.0)
View SourceEfficient 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
@type t() :: %Bloomy.BitArray{ backend: atom(), bits: Nx.Tensor.t(), size: non_neg_integer() }
Functions
Check if all bits at given indices are set to 1.
Useful for bloom filter membership tests.
Parameters
bit_array- The BitArray structindices- 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 all bits (set to 0).
Parameters
bit_array- The BitArray struct
Returns
New BitArray with all bits set to 0.
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
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
Check if bits at given indices are set (value is 1).
Parameters
bit_array- The BitArray structindices- 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]
Perform bitwise AND with another bit array (intersection operation).
Both bit arrays must have the same size.
Parameters
bit_array1- First BitArraybit_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
Create a new bit array of given size.
All bits are initialized to 0.
Parameters
size- Number of bits in the arrayopts- 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 bits at given indices to 1.
Parameters
bit_array- The BitArray structindices- 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]
Perform bitwise OR with another bit array (union operation).
Both bit arrays must have the same size.
Parameters
bit_array1- First BitArraybit_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