MCPcopy Create free account
hub / github.com/argumentcomputer/ix / topological_sort

Function topological_sort

crates/ixon/src/sharing.rs:343–388  ·  view source on GitHub ↗

Topological sort of subterms (leaves first, parents last). CRITICAL: Keys are sorted by hash bytes for deterministic output. This ensures Lean and Rust produce the same topological order.

(
  info_map: &HashMap<blake3::Hash, SubtermInfo>,
)

Source from the content-addressed store, hash-verified

341/// Topological sort of subterms (leaves first, parents last).
342/// CRITICAL: Keys are sorted by hash bytes for deterministic output.
343/// This ensures Lean and Rust produce the same topological order.
344pub fn topological_sort(
345 info_map: &FxHashMap<blake3::Hash, SubtermInfo>,
346) -> Vec<blake3::Hash> {
347 #[derive(Clone, Copy, PartialEq, Eq)]
348 enum VisitState {
349 InProgress,
350 Done,
351 }
352
353 let mut state: FxHashMap<blake3::Hash, VisitState> = FxHashMap::default();
354 let mut result: Vec<blake3::Hash> = Vec::new();
355
356 fn visit(
357 hash: blake3::Hash,
358 info_map: &FxHashMap<blake3::Hash, SubtermInfo>,
359 state: &mut FxHashMap<blake3::Hash, VisitState>,
360 result: &mut Vec<blake3::Hash>,
361 ) {
362 match state.get(&hash) {
363 Some(VisitState::Done) => return,
364 Some(VisitState::InProgress) => return, // Cycle (shouldn't happen)
365 _ => {},
366 }
367
368 state.insert(hash, VisitState::InProgress);
369
370 if let Some(info) = info_map.get(&hash) {
371 for child in &info.children {
372 visit(*child, info_map, state, result);
373 }
374 }
375
376 state.insert(hash, VisitState::Done);
377 result.push(hash);
378 }
379
380 // Sort keys deterministically by hash bytes (lexicographic comparison)
381 let mut sorted_keys: Vec<blake3::Hash> = info_map.keys().cloned().collect();
382 sorted_keys.sort_by_key(|h| *h.as_bytes());
383
384 for hash in sorted_keys {
385 visit(hash, info_map, &mut state, &mut result);
386 }
387
388 result
389}
390
391/// Compute effective sizes for all subterms in topological order.

Callers 1

analyze_blockFunction · 0.85

Calls 2

as_bytesMethod · 0.80
visitFunction · 0.70

Tested by

no test coverage detected