using System; using System.Collections.Generic; namespace Spectre.Console { internal sealed class ListPromptTree where T : notnull { private readonly List> _roots; private readonly IEqualityComparer _comparer; public ListPromptTree(IEqualityComparer comparer) { _roots = new List>(); _comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); } public ListPromptItem? Find(T item) { var stack = new Stack>(_roots); while (stack.Count > 0) { var current = stack.Pop(); if (_comparer.Equals(item, current.Data)) { return current; } stack.PushRange(current.Children); } return null; } public void Add(ListPromptItem node) { _roots.Add(node); } public IEnumerable> Traverse() { foreach (var root in _roots) { var stack = new Stack>(); stack.Push(root); while (stack.Count > 0) { var current = stack.Pop(); yield return current; foreach (var child in current.Children.ReverseEnumerable()) { stack.Push(child); } } } } } }