< Summary

Information
Class: MoreStructures.BurrowsWheelerTransform.Builders.LastFirstFinders.NaiveFinder
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/BurrowsWheelerTransform/Builders/LastFirstFinders/NaiveFinder.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 117
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/BurrowsWheelerTransform/Builders/LastFirstFinders/NaiveFinder.cs

#LineLine coverage
 1using MoreStructures.Utilities;
 2
 3namespace MoreStructures.BurrowsWheelerTransform.Builders.LastFirstFinders;
 4
 5/// <summary>
 6/// A <see cref="ILastFirstFinder"/> implementation which just iterates over <see cref="BWT"/> and its sorted version
 7/// <see cref="SortedBWT"/> every time.
 8/// </summary>
 9/// <remarks>
 10/// Each operation has Time Complexity = O(n) and Space Complexity = O(1), since no additional structure is precomputed
 11/// and/or stored.
 12/// </remarks>
 13public class NaiveFinder : ILastFirstFinder
 14{
 15    /// <inheritdoc/>
 22216    public IComparer<char> CharComparer { get; }
 17
 18    /// <inheritdoc/>
 213119    public RotatedTextWithTerminator BWT { get; }
 20
 21    /// <inheritdoc/>
 56022    public RotatedTextWithTerminator SortedBWT { get; }
 23
 24    /// <summary>
 25    /// Builds an instance of this finder, for the provided <paramref name="lastBWMColumn"/> and
 26    /// <paramref name="firstBWMColumn"/>. Because both columns of the BWM are provided, no sorting happens.
 27    /// </summary>
 28    /// <param name="lastBWMColumn">
 29    /// The last column of the Burrows-Wheeler Matrix. Corresponds to the <see cref="BWT"/>.
 30    /// </param>
 31    /// <param name="firstBWMColumn">
 32    /// The first column of the Burrows-Wheeler Matrix. Corresponds to the <see cref="SortedBWT"/>.
 33    /// </param>
 16934    public NaiveFinder(RotatedTextWithTerminator lastBWMColumn, RotatedTextWithTerminator firstBWMColumn)
 16935    {
 16936        CharComparer = CharOrTerminatorComparer.Build(lastBWMColumn.Terminator);
 16937        BWT = lastBWMColumn;
 16938        SortedBWT = firstBWMColumn;
 16939    }
 40
 41    private static int FindIndexOfNthOccurrenceInText(
 42        RotatedTextWithTerminator text, char charToFind, int occurrenceRank)
 24843    {
 24844        if (occurrenceRank < 0)
 945            throw new ArgumentOutOfRangeException(nameof(occurrenceRank), $"Must be non-negative: {occurrenceRank}.");
 46
 23947        return Enumerable
 23948            .Range(0, text.Length)
 193749            .Where(index => text[index] == charToFind)
 23950            .Cast<int?>()
 23951            .ElementAtOrDefault(occurrenceRank)
 23952            ?? throw new ArgumentOutOfRangeException(
 23953                $"Invalid {nameof(charToFind)} or {nameof(occurrenceRank)}: {charToFind}, {occurrenceRank}");
 23054    }
 55
 56    /// <inheritdoc path="//*[not(self::remarks)]"/>
 57    /// <remarks>
 58    /// This implementation just iterates over <see cref="BWT"/> every time.
 59    /// <br/>
 60    /// Time Complexity = O(n). Space Complexity = O(1).
 61    /// </remarks>
 62    public virtual int FindIndexOfNthOccurrenceInBWT(int indexOfCharInBWT, int occurrenceRank) =>
 3263        FindIndexOfNthOccurrenceInText(BWT, BWT[indexOfCharInBWT], occurrenceRank);
 64
 65    /// <inheritdoc path="//*[not(self::remarks)]"/>
 66    /// <remarks>
 67    /// This implementation just iterates over <see cref="SortedBWT"/> every time.
 68    /// <br/>
 69    /// Time Complexity = O(n). Space Complexity = O(1).
 70    /// </remarks>
 71    public virtual int FindIndexOfNthOccurrenceInSortedBWT(int indexOfCharInBWT, int occurrenceRank) =>
 21972        FindIndexOfNthOccurrenceInText(SortedBWT, BWT[indexOfCharInBWT], occurrenceRank);
 73
 74    private static int FindOccurrenceRankOfCharInText(
 75        RotatedTextWithTerminator text, int indexOfCharInText)
 43376    {
 43377        if (indexOfCharInText < 0 || indexOfCharInText >= text.Length)
 678            throw new ArgumentOutOfRangeException(nameof(indexOfCharInText), $"Invalid value: {indexOfCharInText}");
 79
 42780        return Enumerable
 42781            .Range(0, indexOfCharInText)
 365482            .Count(index => text[index] == text[indexOfCharInText]);
 42783    }
 84
 85    /// <inheritdoc path="//*[not(self::remarks)]"/>
 86    /// <remarks>
 87    /// This implementation just iterates over <see cref="BWT"/> every time.
 88    /// <br/>
 89    /// Time Complexity = O(n). Space Complexity = O(1).
 90    /// </remarks>
 91    public virtual int FindOccurrenceRankOfCharInBWT(int indexOfCharInBWT) =>
 42492        FindOccurrenceRankOfCharInText(BWT, indexOfCharInBWT);
 93
 94    /// <inheritdoc path="//*[not(self::remarks)]"/>
 95    /// <remarks>
 96    /// This implementation just iterates over <see cref="SortedBWT"/> every time.
 97    /// <br/>
 98    /// Time Complexity = O(n). Space Complexity = O(1).
 99    /// </remarks>
 100    public virtual int FindOccurrenceRankOfCharInSortedBWT(int indexOfCharInSortedBWT) =>
 9101        FindOccurrenceRankOfCharInText(SortedBWT, indexOfCharInSortedBWT);
 102
 103    /// <inheritdoc path="//*[not(self::remarks)]"/>
 104    /// <remarks>
 105    /// First executes <see cref="FindOccurrenceRankOfCharInBWT(int)"/>, to find the occurrence rank of the char at
 106    /// index <paramref name="indexOfCharInBWT"/> and then uses the last-to-first property to find the corresponding
 107    /// char in <see cref="SortedBWT"/> by using <see cref="FindIndexOfNthOccurrenceInSortedBWT(int, int)"/>.
 108    /// <br/>
 109    /// Time and Space Complexity depends on the implementation of <see cref="FindOccurrenceRankOfCharInBWT(int)"/> and
 110    /// <see cref="FindIndexOfNthOccurrenceInSortedBWT(int, int)"/>.
 111    /// </remarks>
 112    public virtual (int indexInSortedBWT, int occurrenceRank) LastToFirst(int indexOfCharInBWT)
 673113    {
 673114        var occurrenceRank = FindOccurrenceRankOfCharInBWT(indexOfCharInBWT);
 673115        return (FindIndexOfNthOccurrenceInSortedBWT(indexOfCharInBWT, occurrenceRank), occurrenceRank);
 673116    }
 117}