pub struct BinaryEmbedding { /* private fields */ }Expand description
A binary vector embedding represented as bit-packed u64 values.
Each u64 stores 64 bits, with the lowest bit index stored in the
least significant position. The dimension specifies the actual number
of bits used (which may not be a multiple of 64).
Implementations§
Source§impl BinaryEmbedding
impl BinaryEmbedding
Sourcepub fn new(data: Vec<u64>, dimension: usize) -> Result<Self, VectorError>
pub fn new(data: Vec<u64>, dimension: usize) -> Result<Self, VectorError>
Create a new binary embedding from bit-packed data.
§Arguments
data- The bit-packed u64 valuesdimension- The actual number of bits used
§Errors
Returns an error if:
dimensionis zerodata.len()is too small for the given dimensiondata.len()is larger than necessary (contains unused u64s)
§Example
use manifoldb_vector::types::BinaryEmbedding;
// 64 bits fit in one u64
let embedding = BinaryEmbedding::new(vec![0xFFFF_FFFF_FFFF_FFFFu64], 64).unwrap();
// 65 bits need two u64s
let embedding = BinaryEmbedding::new(vec![0u64, 1u64], 65).unwrap();Sourcepub fn from_sign_bits(floats: &[f32]) -> Result<Self, VectorError>
pub fn from_sign_bits(floats: &[f32]) -> Result<Self, VectorError>
Create a binary embedding from a dense f32 vector using sign bits.
Each positive (>= 0) value becomes 1, each negative value becomes 0. This is a common binary quantization technique.
§Example
use manifoldb_vector::types::BinaryEmbedding;
let floats = vec![0.5, -0.3, 0.0, 1.2];
let binary = BinaryEmbedding::from_sign_bits(&floats).unwrap();
assert_eq!(binary.dimension(), 4);
assert!(binary.get_bit(0)); // 0.5 >= 0
assert!(!binary.get_bit(1)); // -0.3 < 0
assert!(binary.get_bit(2)); // 0.0 >= 0
assert!(binary.get_bit(3)); // 1.2 >= 0Sourcepub fn zeros(dimension: usize) -> Result<Self, VectorError>
pub fn zeros(dimension: usize) -> Result<Self, VectorError>
Sourcepub fn ones(dimension: usize) -> Result<Self, VectorError>
pub fn ones(dimension: usize) -> Result<Self, VectorError>
Sourcepub fn try_get_bit(&self, index: usize) -> Option<bool>
pub fn try_get_bit(&self, index: usize) -> Option<bool>
Get a specific bit by index.
Returns None if index >= dimension.
§Example
use manifoldb_vector::types::BinaryEmbedding;
let embedding = BinaryEmbedding::new(vec![0b0000_0101u64], 8).unwrap();
assert_eq!(embedding.try_get_bit(0), Some(true));
assert_eq!(embedding.try_get_bit(1), Some(false));
assert_eq!(embedding.try_get_bit(2), Some(true));
assert_eq!(embedding.try_get_bit(100), None); // Out of boundsSourcepub fn get_bit(&self, index: usize) -> bool
pub fn get_bit(&self, index: usize) -> bool
Get a specific bit by index.
§Panics
Panics if index >= dimension. Use try_get_bit
for a non-panicking alternative.
Sourcepub fn try_set_bit(
&mut self,
index: usize,
value: bool,
) -> Result<(), VectorError>
pub fn try_set_bit( &mut self, index: usize, value: bool, ) -> Result<(), VectorError>
Set a specific bit by index.
Returns Err if index >= dimension.
§Example
use manifoldb_vector::types::BinaryEmbedding;
let mut embedding = BinaryEmbedding::zeros(8).unwrap();
assert!(embedding.try_set_bit(0, true).is_ok());
assert!(embedding.get_bit(0));
assert!(embedding.try_set_bit(100, true).is_err()); // Out of boundsSourcepub fn set_bit(&mut self, index: usize, value: bool)
pub fn set_bit(&mut self, index: usize, value: bool)
Set a specific bit by index.
§Panics
Panics if index >= dimension. Use try_set_bit
for a non-panicking alternative.
Sourcepub fn count_ones(&self) -> u32
pub fn count_ones(&self) -> u32
Count the number of 1-bits in this embedding.
Sourcepub fn xor(&self, other: &Self) -> Result<Self, VectorError>
pub fn xor(&self, other: &Self) -> Result<Self, VectorError>
Compute the XOR of this embedding with another.
§Errors
Returns an error if the embeddings have different dimensions.
Sourcepub fn and(&self, other: &Self) -> Result<Self, VectorError>
pub fn and(&self, other: &Self) -> Result<Self, VectorError>
Compute the AND of this embedding with another.
§Errors
Returns an error if the embeddings have different dimensions.
Sourcepub fn or(&self, other: &Self) -> Result<Self, VectorError>
pub fn or(&self, other: &Self) -> Result<Self, VectorError>
Compute the OR of this embedding with another.
§Errors
Returns an error if the embeddings have different dimensions.
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, VectorError>
pub fn to_bytes(&self) -> Result<Vec<u8>, VectorError>
Encode the binary embedding to bytes.
Format:
- 1 byte: version
- 4 bytes: dimension (big-endian u32)
- N * 8 bytes: data (each u64 as big-endian)
§Errors
Returns an error if dimension exceeds u32::MAX.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, VectorError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, VectorError>
Decode a binary embedding from bytes.
§Errors
Returns an error if the bytes are invalid or truncated.
Trait Implementations§
Source§impl Clone for BinaryEmbedding
impl Clone for BinaryEmbedding
Source§fn clone(&self) -> BinaryEmbedding
fn clone(&self) -> BinaryEmbedding
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more