| | | 1 | | using MoreStructures.Utilities; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 13 | | public class NaiveFinder : ILastFirstFinder |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc/> |
| | 222 | 16 | | public IComparer<char> CharComparer { get; } |
| | | 17 | | |
| | | 18 | | /// <inheritdoc/> |
| | 2131 | 19 | | public RotatedTextWithTerminator BWT { get; } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc/> |
| | 560 | 22 | | 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> |
| | 169 | 34 | | public NaiveFinder(RotatedTextWithTerminator lastBWMColumn, RotatedTextWithTerminator firstBWMColumn) |
| | 169 | 35 | | { |
| | 169 | 36 | | CharComparer = CharOrTerminatorComparer.Build(lastBWMColumn.Terminator); |
| | 169 | 37 | | BWT = lastBWMColumn; |
| | 169 | 38 | | SortedBWT = firstBWMColumn; |
| | 169 | 39 | | } |
| | | 40 | | |
| | | 41 | | private static int FindIndexOfNthOccurrenceInText( |
| | | 42 | | RotatedTextWithTerminator text, char charToFind, int occurrenceRank) |
| | 248 | 43 | | { |
| | 248 | 44 | | if (occurrenceRank < 0) |
| | 9 | 45 | | throw new ArgumentOutOfRangeException(nameof(occurrenceRank), $"Must be non-negative: {occurrenceRank}."); |
| | | 46 | | |
| | 239 | 47 | | return Enumerable |
| | 239 | 48 | | .Range(0, text.Length) |
| | 1937 | 49 | | .Where(index => text[index] == charToFind) |
| | 239 | 50 | | .Cast<int?>() |
| | 239 | 51 | | .ElementAtOrDefault(occurrenceRank) |
| | 239 | 52 | | ?? throw new ArgumentOutOfRangeException( |
| | 239 | 53 | | $"Invalid {nameof(charToFind)} or {nameof(occurrenceRank)}: {charToFind}, {occurrenceRank}"); |
| | 230 | 54 | | } |
| | | 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) => |
| | 32 | 63 | | 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) => |
| | 219 | 72 | | FindIndexOfNthOccurrenceInText(SortedBWT, BWT[indexOfCharInBWT], occurrenceRank); |
| | | 73 | | |
| | | 74 | | private static int FindOccurrenceRankOfCharInText( |
| | | 75 | | RotatedTextWithTerminator text, int indexOfCharInText) |
| | 433 | 76 | | { |
| | 433 | 77 | | if (indexOfCharInText < 0 || indexOfCharInText >= text.Length) |
| | 6 | 78 | | throw new ArgumentOutOfRangeException(nameof(indexOfCharInText), $"Invalid value: {indexOfCharInText}"); |
| | | 79 | | |
| | 427 | 80 | | return Enumerable |
| | 427 | 81 | | .Range(0, indexOfCharInText) |
| | 3654 | 82 | | .Count(index => text[index] == text[indexOfCharInText]); |
| | 427 | 83 | | } |
| | | 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) => |
| | 424 | 92 | | 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) => |
| | 9 | 101 | | 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) |
| | 673 | 113 | | { |
| | 673 | 114 | | var occurrenceRank = FindOccurrenceRankOfCharInBWT(indexOfCharInBWT); |
| | 673 | 115 | | return (FindIndexOfNthOccurrenceInSortedBWT(indexOfCharInBWT, occurrenceRank), occurrenceRank); |
| | 673 | 116 | | } |
| | | 117 | | } |