MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / tree_order

Function tree_order

atomic-cli/src/commands/view/list.rs:285–347  ·  view source on GitHub ↗

Order the visible views depth-first along the parent chains. Returns `(depth, entry)` pairs in render order: roots (views with no parent, or with a parent missing from the listing) at depth 0, each child level one step deeper. Roots and siblings sort alphabetically. Cycles in the parent data are cut by a visited set.

(
    entries: &'a [ViewEntry],
    visible: &HashSet<String>,
)

Source from the content-addressed store, hash-verified

283/// child level one step deeper. Roots and siblings sort alphabetically.
284/// Cycles in the parent data are cut by a visited set.
285pub(crate) fn tree_order<'a>(
286 entries: &'a [ViewEntry],
287 visible: &HashSet<String>,
288) -> Vec<(usize, &'a ViewEntry)> {
289 let by_name: HashMap<&str, &ViewEntry> = entries.iter().map(|e| (e.name.as_str(), e)).collect();
290
291 let mut children: HashMap<&str, Vec<&ViewEntry>> = HashMap::new();
292 let mut roots: Vec<&ViewEntry> = Vec::new();
293 for entry in entries {
294 match entry.parent.as_deref().and_then(|p| by_name.get(p)) {
295 Some(parent) => children
296 .entry(parent.name.as_str())
297 .or_default()
298 .push(entry),
299 None => roots.push(entry),
300 }
301 }
302 for kids in children.values_mut() {
303 kids.sort_by(|a, b| a.name.cmp(&b.name));
304 }
305 roots.sort_by(|a, b| a.name.cmp(&b.name));
306
307 fn visit<'a>(
308 entry: &'a ViewEntry,
309 depth: usize,
310 children: &HashMap<&str, Vec<&'a ViewEntry>>,
311 visited: &mut HashSet<&'a str>,
312 visible: &HashSet<String>,
313 ordered: &mut Vec<(usize, &'a ViewEntry)>,
314 ) {
315 if !visited.insert(entry.name.as_str()) {
316 return; // cut parent cycles
317 }
318 if visible.contains(&entry.name) {
319 ordered.push((depth, entry));
320 }
321 if let Some(kids) = children.get(entry.name.as_str()) {
322 for kid in kids {
323 visit(kid, depth + 1, children, visited, visible, ordered);
324 }
325 }
326 }
327
328 let mut ordered = Vec::new();
329 let mut visited: HashSet<&str> = HashSet::new();
330 for root in &roots {
331 visit(root, 0, &children, &mut visited, visible, &mut ordered);
332 }
333
334 // Defensive: views caught in a parent cycle are never roots, so the
335 // walk above never reaches them. Render any remainder in name order
336 // from depth 0 so nothing silently vanishes from the listing.
337 let mut leftovers: Vec<&ViewEntry> = entries
338 .iter()
339 .filter(|e| !visited.contains(e.name.as_str()))
340 .collect();
341 leftovers.sort_by(|a, b| a.name.cmp(&b.name));
342 for entry in &leftovers {

Calls 8

visitFunction · 0.70
getMethod · 0.65
collectMethod · 0.45
iterMethod · 0.45
as_strMethod · 0.45
pushMethod · 0.45
cmpMethod · 0.45
containsMethod · 0.45