< Summary

Information
Class: MoreStructures.SuffixArrays.Builders.NaiveSuffixArrayBuilder
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/SuffixArrays/Builders/NaiveSuffixArrayBuilder.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 60
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Text()100%1100%
.ctor(...)100%1100%
Build()100%1100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/SuffixArrays/Builders/NaiveSuffixArrayBuilder.cs

#LineLine coverage
 1using MoreStructures.Utilities;
 2
 3namespace MoreStructures.SuffixArrays.Builders;
 4
 5/// <summary>
 6/// An algorithm for building the <see cref="SuffixArray"/> directly from a <see cref="TextWithTerminator"/>, listing
 7/// and sorting all suffixes of <see cref="Text"/>.
 8/// </summary>
 9/// <remarks>
 10///     <para id="algo">
 11///     ALGORITHM
 12///     <br/>
 13///     The following steps are performed, lazily.
 14///     <br/>
 15///     - First all suffixes of the input <see cref="TextWithTerminator"/> are generated.
 16///       <br/>
 17///     - Then the suffixes are sorted in ascending order.
 18///       <br/>
 19///     - Finally, the 1st char of each suffix is taken.
 20///     </para>
 21///     <para id="complexity">
 22///     COMPLEXITY
 23///     <br/>
 24///     - There are n suffixes, where n is the length of the input text (including the terminator).
 25///       <br/>
 26///     - Sorting n strings requires n * log(n) comparisons, each comparing at most n chars.
 27///       <br/>
 28///     - Taking the first char of each of the suffixes takes O(1) time, and there are n of them.
 29///       <br/>
 30///     - Therefore, Time Complexity is O(n^2 * log(n)) and Space Complexity is O(n).
 31///     </para>
 32/// </remarks>
 33public class NaiveSuffixArrayBuilder : ISuffixArrayBuilder
 34{
 35    private readonly IComparer<string> Comparer;
 36
 37    /// <summary>
 38    /// The <see cref="TextWithTerminator"/>, to build the <see cref="SuffixArray"/> of.
 39    /// </summary>
 186040    public TextWithTerminator Text { get; }
 41
 42    /// <summary>
 43    /// <inheritdoc cref="NaiveSuffixArrayBuilder" path="/summary"/>
 44    /// </summary>
 45    /// <param name="text"><inheritdoc cref="Text" path="/summary"/></param>
 5846    public NaiveSuffixArrayBuilder(TextWithTerminator text)
 5847    {
 5848        Text = text;
 5849        Comparer = StringIncludingTerminatorComparer.Build(text.Terminator);
 5850    }
 51
 52    /// <summary>
 53    /// <inheritdoc cref="NaiveSuffixArrayBuilder" path="/summary"/>
 54    /// </summary>
 5855    public SuffixArray Build() => new(Enumerable
 5856        .Range(0, Text.Length)
 90157        .Select(i => string.Concat(Text[i..]))
 90158        .OrderBy(s => s, Comparer)
 95959        .Select(s => Text.Length - s.Length));
 60}