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

Function apply_permutation

nodedb/src/data/executor/handlers/document/sort.rs:236–257  ·  view source on GitHub ↗

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>,
)

Source from the content-addressed store, hash-verified

234///
235/// `indices[i]` = the original row index that should appear at position `i`.
236fn 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
259pub(super) struct RunReader {
260 pub(super) reader: BufReader<std::fs::File>,

Callers 1

sort_rowsFunction · 0.70

Calls 5

takeFunction · 0.85
collectMethod · 0.80
to_vecMethod · 0.45
iterMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected