Apply a permutation to rows (and key_offsets) using the sorted index order. `indices[i]` = the original row index that should appear at position `i`.
(
rows: &mut [(String, Vec<u8>)],
_key_offsets: &mut [SortKeyOffsets],
indices: Vec<usize>,
)
| 234 | /// |
| 235 | /// `indices[i]` = the original row index that should appear at position `i`. |
| 236 | fn apply_permutation( |
| 237 | rows: &mut [(String, Vec<u8>)], |
| 238 | _key_offsets: &mut [SortKeyOffsets], |
| 239 | indices: Vec<usize>, |
| 240 | ) { |
| 241 | // Build reordered vector then copy back. The cycle-chase approach is |
| 242 | // tricky to get right with Vec<u8> (non-Copy), so we drain and rebuild. |
| 243 | let mut temp: Vec<(String, Vec<u8>)> = std::mem::take(&mut rows.to_vec()) |
| 244 | .into_iter() |
| 245 | .map(|_| (String::new(), Vec::new())) |
| 246 | .collect(); |
| 247 | // Scatter originals into temp by index order. |
| 248 | // Can't use temp directly — we need all originals available. |
| 249 | let originals: Vec<(String, Vec<u8>)> = |
| 250 | rows.iter().map(|(s, b)| (s.clone(), b.clone())).collect(); |
| 251 | for (target_pos, &src_idx) in indices.iter().enumerate() { |
| 252 | temp[target_pos] = originals[src_idx].clone(); |
| 253 | } |
| 254 | for (i, item) in temp.into_iter().enumerate() { |
| 255 | rows[i] = item; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | pub(super) struct RunReader { |
| 260 | pub(super) reader: BufReader<std::fs::File>, |