BinaryEmbedding

Struct BinaryEmbedding 

Source
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

Source

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 values
  • dimension - The actual number of bits used
§Errors

Returns an error if:

  • dimension is zero
  • data.len() is too small for the given dimension
  • data.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();
Source

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 >= 0
Source

pub fn zeros(dimension: usize) -> Result<Self, VectorError>

Create a binary embedding with all zeros.

§Errors

Returns an error if dimension is zero.

Source

pub fn ones(dimension: usize) -> Result<Self, VectorError>

Create a binary embedding with all ones.

§Errors

Returns an error if dimension is zero.

Source

pub fn dimension(&self) -> usize

Get the dimension (number of bits) of this embedding.

Source

pub fn data(&self) -> &[u64]

Get the raw bit-packed data.

Source

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 bounds
Source

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.

Source

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 bounds
Source

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.

Source

pub fn count_ones(&self) -> u32

Count the number of 1-bits in this embedding.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> BinaryEmbedding

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BinaryEmbedding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for BinaryEmbedding

Source§

fn eq(&self, other: &BinaryEmbedding) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for BinaryEmbedding

Source§

impl StructuralPartialEq for BinaryEmbedding

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more