pub struct BinaryHash {
pub bits: Vec<u64>,
pub dim: usize,
}Expand description
Binary hash for fast approximate nearest neighbor search.
§Research Background
Binary embeddings enable sub-linear search via Hamming distance. Key insight from our research synthesis: binary embeddings are for blocking, not primary retrieval. The sign-rank limitation means they cannot represent all similarity relationships, but they excel at fast candidate filtering.
§Two-Stage Retrieval Pattern
Query → [Binary Hash] → Hamming Filter (fast) → Candidates
↓
[Dense Similarity]
↓
Final Results§Example
use anno::backends::inference::BinaryHash;
// Create hashes from embeddings
let hash1 = BinaryHash::from_embedding(&[0.1, -0.2, 0.3, -0.4, 0.5, -0.6, 0.7, -0.8]);
let hash2 = BinaryHash::from_embedding(&[0.15, -0.25, 0.35, -0.45, 0.55, -0.65, 0.75, -0.85]);
// Similar embeddings → low Hamming distance
assert!(hash1.hamming_distance(&hash2) < 2);Fields§
§bits: Vec<u64>Packed bits (each u64 holds 64 bits)
dim: usizeOriginal dimension (number of bits)
Implementations§
Source§impl BinaryHash
impl BinaryHash
Sourcepub fn from_embedding(embedding: &[f32]) -> Self
pub fn from_embedding(embedding: &[f32]) -> Self
Create from a dense embedding using sign function.
Each positive value → 1, each negative/zero value → 0.
Sourcepub fn from_embedding_f64(embedding: &[f64]) -> Self
pub fn from_embedding_f64(embedding: &[f64]) -> Self
Create from a dense f64 embedding.
Sourcepub fn hamming_distance(&self, other: &Self) -> u32
pub fn hamming_distance(&self, other: &Self) -> u32
Compute Hamming distance (number of differing bits).
Uses POPCNT instruction when available for hardware acceleration.
Sourcepub fn hamming_distance_normalized(&self, other: &Self) -> f64
pub fn hamming_distance_normalized(&self, other: &Self) -> f64
Compute normalized Hamming distance (0.0 to 1.0).
Sourcepub fn approximate_cosine(&self, other: &Self) -> f64
pub fn approximate_cosine(&self, other: &Self) -> f64
Convert Hamming distance to approximate cosine similarity.
Based on the relationship: cos(θ) ≈ 1 - 2 * (hamming_distance / dim) This is an approximation valid for random hyperplane hashing.
Trait Implementations§
Source§impl Clone for BinaryHash
impl Clone for BinaryHash
Source§fn clone(&self) -> BinaryHash
fn clone(&self) -> BinaryHash
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BinaryHash
impl Debug for BinaryHash
Source§impl Hash for BinaryHash
impl Hash for BinaryHash
Source§impl PartialEq for BinaryHash
impl PartialEq for BinaryHash
impl Eq for BinaryHash
impl StructuralPartialEq for BinaryHash
Auto Trait Implementations§
impl Freeze for BinaryHash
impl RefUnwindSafe for BinaryHash
impl Send for BinaryHash
impl Sync for BinaryHash
impl Unpin for BinaryHash
impl UnsafeUnpin for BinaryHash
impl UnwindSafe for BinaryHash
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more