| | 1 | | using MoreStructures.RecImmTrees; |
| | 2 | | using MoreStructures.Utilities; |
| | 3 | |
|
| | 4 | | namespace MoreStructures.CountTrees; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// An implementation of <see cref="IRecImmDictIndexedTreeNode{TEdge, TNode}"/>, wrapping another |
| | 8 | | /// implementation of <see cref="IRecImmDictIndexedTreeNode{TEdge, TNode}"/>, and counting the total |
| | 9 | | /// number of descendands the wrapped node has below (node itself excluded). |
| | 10 | | /// </summary> |
| | 11 | | /// <typeparam name="TEdge"> |
| | 12 | | /// <inheritdoc cref="IRecImmDictIndexedTreeNode{TEdge, TNode}" path="/typeparam[@name='TEdge']"/> |
| | 13 | | /// </typeparam> |
| | 14 | | /// <typeparam name="TNode"> |
| | 15 | | /// <inheritdoc cref="IRecImmDictIndexedTreeNode{TEdge, TNode}" path="/typeparam[@name='TNode']"/> |
| | 16 | | /// </typeparam> |
| | 17 | | /// <param name="WrappedNode">The node being wrapped, and whose descendants are going to be counted.</param> |
| | 18 | | /// <remarks> |
| | 19 | | /// <para id="records-semantics"> |
| | 20 | | /// RECORDS SEMANTICS |
| | 21 | | /// <br/> |
| | 22 | | /// Due to records semantics, and the use of value readonly dictionaries, two |
| | 23 | | /// <see cref="CountTreeNode{TEdge, TNode}"/> instances wrapping the same underlying node, or two equivalent nodes, |
| | 24 | | /// will be equal. |
| | 25 | | /// </para> |
| | 26 | | /// <para id="iterativity"> |
| | 27 | | /// ITERATIVITY |
| | 28 | | /// <br/> |
| | 29 | | /// <see cref="CountTreeNode{TEdge, TNode}"/> structure construction and properties calculation are done lazily. |
| | 30 | | /// <br/> |
| | 31 | | /// <inheritdoc cref="DocFragments" path="/remarks/para[@id='fully-iterative-advantages']"/> |
| | 32 | | /// </para> |
| | 33 | | /// <para id="caching"> |
| | 34 | | /// CACHING |
| | 35 | | /// <br/> |
| | 36 | | /// Once <see cref="Children"/> and <see cref="DescendantsCount"/> properties are calculated, they are cached to |
| | 37 | | /// avoid multiple calculation. This is also one of the reasons why immutability of the wrapped tree is a |
| | 38 | | /// requirement to use <see cref="CountTreeNode{TEdge, TNode}"/>. |
| | 39 | | /// </para> |
| | 40 | | /// <para id="complexity"> |
| | 41 | | /// COMPLEXITY |
| | 42 | | /// <br/> |
| | 43 | | /// Time Complexity = O(n) and Space Complexity = O(n) where n = number of nodes in <see cref="WrappedNode"/> |
| | 44 | | /// structure. Leafs are visited only once, intermediate nodes are visited (at most) twice. |
| | 45 | | /// </para> |
| | 46 | | /// </remarks> |
| | 47 | | /// <example> |
| | 48 | | /// <code> |
| | 49 | | /// var wrapped = new Node(1) |
| | 50 | | /// { |
| | 51 | | /// Children = new Dictionary<Edge, Node> |
| | 52 | | /// { |
| | 53 | | /// [new(1)] = new(2), |
| | 54 | | /// [new(2)] = new(3) |
| | 55 | | /// { |
| | 56 | | /// ... |
| | 57 | | /// }, |
| | 58 | | /// [new(5)] = new(6), |
| | 59 | | /// } |
| | 60 | | /// }; |
| | 61 | | /// |
| | 62 | | /// var wrapping = new CountTreeNode<Edge, Node>(wrapped); |
| | 63 | | /// Assert.AreEqual(3, wrapping.Children.Count); |
| | 64 | | /// </code> |
| | 65 | | /// </example> |
| 60066 | 66 | | public sealed record CountTreeNode<TEdge, TNode>(TNode WrappedNode) |
| 30026 | 67 | | : IRecImmDictIndexedTreeNode<CountTreeEdge<TEdge, TNode>, CountTreeNode<TEdge, TNode>> |
| 30026 | 68 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode> |
| 30026 | 69 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> |
| 30026 | 70 | | { |
| 30026 | 71 | | private IDictionary<CountTreeEdge<TEdge, TNode>, CountTreeNode<TEdge, TNode>>? _children; |
| 30026 | 72 | | private int? _descendantsCount = null; |
| 30026 | 73 | |
|
| 30026 | 74 | | /// <summary> |
| 30026 | 75 | | /// A private readonly object used for synchronization across multiple calls on the same instance. |
| 30026 | 76 | | /// </summary> |
| 30026 | 77 | | /// <remarks> |
| 30026 | 78 | | /// A <see cref="LockValueObject"/> is used not to break value equality of this record. |
| 30026 | 79 | | /// </remarks> |
| 30026 | 80 | | private readonly LockValueObject _lockObject = new(); |
| 30026 | 81 | |
|
| 300180 | 82 | | private record struct StackFrame(CountTreeNode<TEdge, TNode> Node, TNode WrappedNode, bool ChildrenProcessed); |
| 30026 | 83 | |
|
| 30026 | 84 | | private void Compute() |
| 7 | 85 | | { |
| 7 | 86 | | lock (_lockObject) |
| 7 | 87 | | { |
| 7 | 88 | | var stack = new Stack<StackFrame>(); |
| 7 | 89 | | stack.Push(new(this, WrappedNode, false)); |
| 30026 | 90 | |
|
| 50037 | 91 | | while (stack.Count > 0) |
| 50030 | 92 | | ProcessStack(stack); |
| 7 | 93 | | } |
| 7 | 94 | | } |
| 30026 | 95 | |
|
| 30026 | 96 | | private static void ProcessStack(Stack<StackFrame> stack) |
| 50030 | 97 | | { |
| 50030 | 98 | | var (node, wrappedNode, childrenProcessed) = stack.Pop(); |
| 30026 | 99 | |
|
| 50030 | 100 | | if (wrappedNode.IsLeaf()) |
| 10012 | 101 | | { |
| 10012 | 102 | | var children = new Dictionary<CountTreeEdge<TEdge, TNode>, CountTreeNode<TEdge, TNode>> { }; |
| 10012 | 103 | | node._children = children.ToValueReadOnlyDictionary(); |
| 10012 | 104 | | node._descendantsCount = 0; |
| 10012 | 105 | | return; |
| 30026 | 106 | | } |
| 30026 | 107 | |
|
| 40018 | 108 | | if (!childrenProcessed) |
| 20009 | 109 | | { |
| 20009 | 110 | | stack.Push(new(node, wrappedNode, true)); |
| 30026 | 111 | |
|
| 20009 | 112 | | var children = new Dictionary<CountTreeEdge<TEdge, TNode>, CountTreeNode<TEdge, TNode>> { }; |
| 120055 | 113 | | foreach (var (wrappedChildEdge, wrappedChildNode) in wrappedNode.Children) |
| 30014 | 114 | | { |
| 30014 | 115 | | var childEdge = new CountTreeEdge<TEdge, TNode>(wrappedChildEdge); |
| 30014 | 116 | | var childNode = new CountTreeNode<TEdge, TNode>(wrappedChildNode); |
| 30014 | 117 | | children[childEdge] = childNode; |
| 30026 | 118 | |
|
| 30014 | 119 | | stack.Push(new(childNode, wrappedChildNode, false)); |
| 30014 | 120 | | } |
| 30026 | 121 | |
|
| 20009 | 122 | | node._children = children.ToValueReadOnlyDictionary(); |
| 20009 | 123 | | return; |
| 30026 | 124 | | } |
| 30026 | 125 | |
|
| 30026 | 126 | | // At this point _children has been initialized in previous ProcessStack execution for the same node |
| 30026 | 127 | | // and _descendantsCount has been set for all the children of the node (due to the fact that the node has |
| 30026 | 128 | | // been re-pushed to the stack just before all its children). |
| 50023 | 129 | | node._descendantsCount = node._children!.Sum(c => c.Value._descendantsCount!.Value + 1); |
| 50030 | 130 | | } |
| 30026 | 131 | |
|
| 30026 | 132 | | /// <inheritdoc path="//*[not(self::remarks)]"/> |
| 30026 | 133 | | /// <remarks> |
| 30026 | 134 | | /// <inheritdoc cref="CountTreeNode{TEdge, TNode}" path="/remarks/para[@id='info']"/> |
| 30026 | 135 | | /// </remarks> |
| 30026 | 136 | | public IDictionary<CountTreeEdge<TEdge, TNode>, CountTreeNode<TEdge, TNode>> Children |
| 30026 | 137 | | { |
| 30026 | 138 | | get |
| 7 | 139 | | { |
| 7 | 140 | | if (_children == null) |
| 1 | 141 | | Compute(); |
| 30026 | 142 | |
|
| 7 | 143 | | return _children!; // Initialized in Compute |
| 7 | 144 | | } |
| 30026 | 145 | | } |
| 30026 | 146 | |
|
| 30026 | 147 | | /// <summary> |
| 30026 | 148 | | /// The number of descendands below this node (node itself excluded). |
| 30026 | 149 | | /// </summary> |
| 30026 | 150 | | /// <remarks> |
| 30026 | 151 | | /// <inheritdoc cref="CountTreeNode{TEdge, TNode}" path="/remarks/para[@id='info']"/> |
| 30026 | 152 | | /// </remarks> |
| 30026 | 153 | | public int DescendantsCount |
| 30026 | 154 | | { |
| 30026 | 155 | | get |
| 8 | 156 | | { |
| 8 | 157 | | if (_descendantsCount == null) |
| 6 | 158 | | Compute(); |
| 30026 | 159 | |
|
| 8 | 160 | | return _descendantsCount!.Value; // Initialized in Compute |
| 8 | 161 | | } |
| 30026 | 162 | | } |
| 30026 | 163 | | } |