Innovenergy_trunk/csharp/Lib/SrcGen/Trees/TreeStruct.cs

34 lines
1022 B
C#

using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.SrcGen.Trees;
public readonly struct TreeStruct<T> where T : notnull
{
[Obsolete] public TreeStruct() => throw new Exception("Forbidden");
public TreeStruct(T node, Func<T, IEnumerable<T>> getChildren)
{
Node = node;
_GetChildren = getChildren;
}
public IEnumerable<TreeStruct<T>> Children
{
get
{
var getChildren = _GetChildren;
return _GetChildren(Node).Select(c => new TreeStruct<T>(c, getChildren));
}
}
public T Node { get; }
private readonly Func<T, IEnumerable<T>> _GetChildren;
public IEnumerable<T> TraverseDepthFirstPostOrder() => TreeTraversal.TraverseDepthFirstPostOrder(Node, _GetChildren);
public IEnumerable<T> TraverseDepthFirstPreOrder() => TreeTraversal.TraverseDepthFirstPreOrder(Node, _GetChildren);
public IEnumerable<T> TraverseBreadthFirst() => TreeTraversal.TraverseBreadthFirst(Node, _GetChildren);
}