pub struct BqCode { /* private fields */ }Expand description
A binary-quantized (BQ) code: one bit per dimension, packed into
u64 words.
Produced by Quantizer::quantize on a
trained BinaryQuantizer. Each bit is 1
when the corresponding f32 component is at or above the trained
per-dimension mean, 0 otherwise. When the dimension is not a
multiple of 64 the trailing word has unused high bits; those bits
are always 0, so they cannot contribute to Hamming distance.
§Examples
use iqdb_quantize::{BinaryQuantizer, Quantizer};
let mut bq = BinaryQuantizer::new();
bq.train(&[&[0.0_f32, 1.0, 2.0][..], &[2.0_f32, 1.0, 0.0][..]]).expect("ok");
let code = bq.quantize(&[0.5_f32, 1.5, 2.5]).expect("ok");
assert_eq!(code.dim(), 3);
// dim 3 fits in a single u64 word.
assert_eq!(code.as_words().len(), 1);Implementations§
Source§impl BqCode
impl BqCode
Sourcepub fn dim(&self) -> usize
pub fn dim(&self) -> usize
Returns the original vector dimension this code was produced from.
This is the number of meaningful bits in the packed words; the trailing word may have unused high bits, which are always zero.
§Examples
use iqdb_quantize::{BinaryQuantizer, Quantizer};
let mut bq = BinaryQuantizer::new();
bq.train(&[&[0.0_f32; 65][..], &[1.0_f32; 65][..]]).expect("ok");
let code = bq.quantize(&[0.5_f32; 65]).expect("ok");
assert_eq!(code.dim(), 65);
// 65 bits requires two u64 words.
assert_eq!(code.as_words().len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the code encodes a zero-dimensional vector.
A BqCode produced by Quantizer::quantize
on a trained BinaryQuantizer is never
empty (empty inputs are rejected at the boundary); this method
exists for API symmetry with BqCode::dim.
§Examples
use iqdb_quantize::{BinaryQuantizer, Quantizer};
let mut bq = BinaryQuantizer::new();
bq.train(&[&[0.0_f32, 1.0][..]]).expect("ok");
let code = bq.quantize(&[0.5_f32, 0.5]).expect("ok");
assert!(!code.is_empty());Sourcepub fn as_words(&self) -> &[u64]
pub fn as_words(&self) -> &[u64]
Borrow the raw packed u64 words.
§Examples
use iqdb_quantize::{BinaryQuantizer, Quantizer};
let mut bq = BinaryQuantizer::new();
bq.train(&[&[0.0_f32; 4][..], &[1.0_f32; 4][..]]).expect("ok");
let code = bq.quantize(&[0.5_f32; 4]).expect("ok");
assert_eq!(code.as_words().len(), 1);