reflex/vectordb/bq/
utils.rs

1use bitvec::prelude::*;
2
3/// Quantizes a slice of floats into a packed bitvector (sign-based).
4pub fn quantize_to_binary(vector: &[f32]) -> Vec<u8> {
5    let mut bv = BitVec::<u8, Lsb0>::with_capacity(vector.len());
6    for &val in vector {
7        bv.push(val > 0.0);
8    }
9    bv.into_vec()
10}
11
12/// Computes Hamming distance (number of differing bits) between two packed vectors.
13pub fn hamming_distance(a: &[u8], b: &[u8]) -> u32 {
14    if a.len() != b.len() {
15        return u32::MAX;
16    }
17
18    a.iter()
19        .zip(b.iter())
20        .map(|(&x, &y)| (x ^ y).count_ones())
21        .sum()
22}