Build an asymmetric distance table for a query vector. Returns `table[sub][centroid]` = distance from query's sub-vector to each centroid. Pre-computing this table makes distance evaluation O(M) per candidate instead of O(D). Charges `m * k * size_of:: ()` bytes to the governor (if set) before allocating the table.
(&self, query: &[f32])
| 156 | /// Charges `m * k * size_of::<f32>()` bytes to the governor (if set) |
| 157 | /// before allocating the table. |
| 158 | pub fn build_distance_table(&self, query: &[f32]) -> Result<Vec<Vec<f32>>, VectorError> { |
| 159 | debug_assert_eq!(query.len(), self.dim); |
| 160 | let total_bytes = self.m * self.k * size_of::<f32>(); |
| 161 | let _g = try_reserve_or_skip(&self.governor, total_bytes)?; |
| 162 | let mut table = Vec::with_capacity(self.m); |
| 163 | for sub in 0..self.m { |
| 164 | let offset = sub * self.sub_dim; |
| 165 | let sub_query = &query[offset..offset + self.sub_dim]; |
| 166 | let mut dists = Vec::with_capacity(self.k); |
| 167 | for centroid in &self.codebooks[sub] { |
| 168 | let d = l2_sub(sub_query, centroid); |
| 169 | dists.push(d); |
| 170 | } |
| 171 | table.push(dists); |
| 172 | } |
| 173 | Ok(table) |
| 174 | } |
| 175 | |
| 176 | /// Compute asymmetric distance using a precomputed distance table. |
| 177 | /// |