< Summary

Information
Class: MoreStructures.SuffixStructures.Conversions.ConverterHelpers
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/SuffixStructures/Conversions/ConverterHelpers.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 75
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
IterativeCoalesce(...)100%4100%
IterativeExpand(...)100%2100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/SuffixStructures/Conversions/ConverterHelpers.cs

#LineLine coverage
 1using MoreStructures.SuffixTrees;
 2using MoreStructures.SuffixTries;
 3
 4namespace MoreStructures.SuffixStructures.Conversions;
 5
 6/// <summary>
 7/// Provides functionalities useful to multiple implementations of <see cref="IConverter"/>
 8/// </summary>
 9internal static class ConverterHelpers
 10{
 11    /// <summary>
 12    /// Iteratively coalesces all <see cref="SuffixTrieNode.Intermediate"/> nodes, starting from the one provided and
 13    /// going down the <see cref="SuffixTrieNode"/> structure, which have a single child.
 14    /// </summary>
 15    /// <param name="trieEdge">The edge pointing to the node to be coalesced with its descendants.</param>
 16    /// <param name="trieNode">The node to be coalesced with its descendants.</param>
 17    /// <returns>
 18    /// The couple of coalesced <see cref="SuffixTreeEdge"/> and the corresponding <see cref="SuffixTrieNode"/>.
 19    /// </returns>
 20    /// <exception cref="NotSupportedException">
 21    /// If a node with children of a type different than <see cref="SuffixTrieNode.Intermediate"/> is encountered.
 22    /// </exception>
 23    /// <remarks>
 24    /// Example: the trie nodes path N -(1)-> N -(3)-> N -(5)-> N is coalesced into the tree nodes path N -(1,3)-> N.
 25    /// </remarks>
 26    internal static (SuffixTreeEdge coalescedTreeEdge, SuffixTrieNode coalescedTrieNode) IterativeCoalesce(
 27        SuffixTrieEdge trieEdge, SuffixTrieNode trieNode)
 2004828    {
 2004829        var coalescedTreeEdge = new SuffixTreeEdge(trieEdge.Start, trieEdge.Length);
 2004830        var coalescedTrieNode = trieNode;
 31
 2028632        while (coalescedTrieNode.Children.Count == 1)
 24033        {
 24034            if (coalescedTrieNode is not SuffixTrieNode.Intermediate)
 235                throw new NotSupportedException(
 236                    $"{coalescedTrieNode} of type {coalescedTrieNode.GetType().Name} not supported");
 37
 23838            var trieChild = coalescedTrieNode.Children.Single();
 23839            coalescedTreeEdge = new SuffixTreeEdge(
 23840                coalescedTreeEdge.Start, coalescedTreeEdge.Length + trieChild.Key.Length);
 23841            coalescedTrieNode = coalescedTrieNode.Children.Single().Value;
 23842        }
 43
 2004644        return (coalescedTreeEdge, coalescedTrieNode);
 2004645    }
 46
 47    /// <summary>
 48    /// Iteratively expands the provided <see cref="SuffixTreeEdge"/>, into a chain of <see cref="SuffixTrieEdge"/> and
 49    /// <see cref="SuffixTrieNode"/> instances, where each node has a single child and where the last node points to
 50    /// the provided <see cref="SuffixTrieNode"/>.
 51    /// </summary>
 52    /// <param name="treeEdge">The edge pointing to the node to be expandend.</param>
 53    /// <param name="trieNode">The node to be attached to the last expanded node in the chain.</param>
 54    /// <returns>
 55    /// The couple of expanded <see cref="SuffixTrieEdge"/> and the corresponding <see cref="SuffixTrieNode"/>.
 56    /// </returns>
 57    /// <remarks>
 58    /// Example: the tree edge -(1,3)-> N is expanded into the trie nodes path -(1)-> N -(2)-> N -(3)-> N*.
 59    /// Where N* is the provided <see cref="SuffixTrieNode"/>.
 60    /// </remarks>
 61    internal static (SuffixTrieEdge expandedTrieEdge, SuffixTrieNode expandedTrieNode) IterativeExpand(
 62        SuffixTreeEdge treeEdge, SuffixTrieNode trieNode)
 5663    {
 5664        var treeEdgeStart = treeEdge.Start;
 5665        var expandedTrieEdge = new SuffixTrieEdge(treeEdgeStart);
 5666        var expandedTrieNode = trieNode;
 59667        for (var i = treeEdge.Length - 1; i >= 1; i--)
 24268            expandedTrieNode = new SuffixTrieNode.Intermediate(new Dictionary<SuffixTrieEdge, SuffixTrieNode>
 24269            {
 24270                [new(treeEdgeStart + i)] = expandedTrieNode,
 24271            });
 72
 5673        return (expandedTrieEdge, expandedTrieNode);
 5674    }
 75}