MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / distance_prepared

Method distance_prepared

nodedb-vector/src/rerank/codecs/binary.rs:86–111  ·  view source on GitHub ↗

Compute Hamming distance from a prepared query to a binary-encoded candidate.

(
        &self,
        prepared: &PreparedQuery,
        encoded: &[u8],
    )

Source from the content-addressed store, hash-verified

84 /// Compute Hamming distance from a prepared query to a binary-encoded
85 /// candidate.
86 fn distance_prepared(
87 &self,
88 prepared: &PreparedQuery,
89 encoded: &[u8],
90 ) -> Result<f32, RerankError> {
91 let q_bits = match prepared {
92 PreparedQuery::Bytes(b) => b,
93 _ => {
94 return Err(RerankError::BadInput(
95 "binary distance: expected PreparedQuery::Bytes".to_string(),
96 ));
97 }
98 };
99
100 let packed_len = binary_packed_bits_len(self.dim);
101 let uqv_ref = UnifiedQuantizedVectorRef::from_bytes(encoded, packed_len).map_err(|e| {
102 RerankError::BadInput(format!(
103 "binary distance: failed to parse encoded bytes: {e}"
104 ))
105 })?;
106
107 let packed = uqv_ref.packed_bits();
108 // Compute Hamming distance directly via the public helper.
109 let dist = crate::quantize::binary::hamming_distance(q_bits, packed) as f32;
110 Ok(dist)
111 }
112
113 fn name(&self) -> CodecName {
114 CodecName::Binary

Calls 4

binary_packed_bits_lenFunction · 0.85
to_stringMethod · 0.80
packed_bitsMethod · 0.80
hamming_distanceFunction · 0.50