| | 1 | | using MoreStructures.RecImmTrees.Conversions; |
| | 2 | | using MoreStructures.Utilities; |
| | 3 | |
|
| | 4 | | namespace MoreStructures.RecImmTrees; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// An immutable sequence of <typeparamref name="TNode"/>, where each node is child of its predecessor and parent of |
| | 8 | | /// its successor and where node relationships are stored in <typeparamref name="TEdge"/> instances. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="PathNodes">The sequence of nodes respecting the parent-child relationship.</param> |
| | 11 | | /// <typeparam name="TEdge">The type of edges of the specific structure.</typeparam> |
| | 12 | | /// <typeparam name="TNode">The type of nodes of the specific structure.</typeparam> |
| | 13 | | /// <remarks> |
| | 14 | | /// Immutability is guaranteed by using <see cref="ValueReadOnlyDictionary{TKey, TValue}"/>. |
| | 15 | | /// </remarks> |
| 920 | 16 | | public record TreePath<TEdge, TNode>(IEnumerable<KeyValuePair<TEdge, TNode>> PathNodes) |
| 920 | 17 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode> |
| 920 | 18 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> |
| 920 | 19 | | { |
| 920 | 20 | | /// <summary> |
| 920 | 21 | | /// Builds an empty path, i.e. an empty sequence of nodes. |
| 920 | 22 | | /// </summary> |
| 41 | 23 | | public TreePath() : this(Enumerable.Empty<KeyValuePair<TEdge, TNode>>()) |
| 41 | 24 | | { |
| 41 | 25 | | } |
| 920 | 26 | |
|
| 920 | 27 | | /// <summary> |
| 920 | 28 | | /// Builds a path composed of a single node with its incoming edge. |
| 920 | 29 | | /// </summary> |
| 920 | 30 | | /// <param name="edge">The edge leading to the node.</param> |
| 920 | 31 | | /// <param name="node">The node defining the singleton path.</param> |
| 33 | 32 | | public TreePath(TEdge edge, TNode node) : this(Enumerable.Repeat(KeyValuePair.Create(edge, node), 1)) |
| 33 | 33 | | { |
| 33 | 34 | | } |
| 920 | 35 | |
|
| 920 | 36 | | /// <summary> |
| 920 | 37 | | /// Builds a path composed of the provided couples of edges and nodes. |
| 920 | 38 | | /// </summary> |
| 920 | 39 | | /// <param name="pathNodes">An array of couples (edge, node).</param> |
| 920 | 40 | | public TreePath(params (TEdge edge, TNode node)[] pathNodes) |
| 68 | 41 | | : this(pathNodes.Select(pathNode => KeyValuePair.Create(pathNode.edge, pathNode.node))) |
| 20 | 42 | | { |
| 20 | 43 | | } |
| 920 | 44 | |
|
| 920 | 45 | | /// <summary> |
| 920 | 46 | | /// A readonly view of the private collection of path <typeparamref name="TNode"/> instances. |
| 920 | 47 | | /// </summary> |
| 1008 | 48 | | public IEnumerable<KeyValuePair<TEdge, TNode>> PathNodes { get; } = |
| 920 | 49 | | PathNodes.ToValueReadOnlyCollection(); |
| 920 | 50 | |
|
| 2 | 51 | | private static readonly IStringifier<TEdge, TNode> Stringifier = |
| 30 | 52 | | new FullyIterativeStringifier<TEdge, TNode>(r => string.Empty, (e, n) => $"{e}") |
| 2 | 53 | | { |
| 2 | 54 | | PathSeparator = " => ", |
| 2 | 55 | | }; |
| 920 | 56 | |
|
| 920 | 57 | | /// <inheritdoc path="//*[not(self::summary)]"/> |
| 920 | 58 | | /// <summary> |
| 920 | 59 | | /// <inheritdoc/> |
| 920 | 60 | | /// <br/> |
| 920 | 61 | | /// Uses a <see cref="IStringifier{TEdge, TNode}"/> to generate the string. |
| 920 | 62 | | /// </summary> |
| 6 | 63 | | public override string ToString() => Stringifier.Stringify(this); |
| 920 | 64 | | } |