| | 1 | | using MoreStructures.Utilities; |
| | 2 | | using System.Collections; |
| | 3 | |
|
| | 4 | | namespace MoreStructures; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// A text string with a terminator character which has been rotated leftwards or rightwards, of a number of positions |
| | 8 | | /// (0 included). |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="RotatedText"> |
| | 11 | | /// The text, defined as an <see cref="IEnumerable{T}"/> of chars and containing the terminator character once, in any |
| | 12 | | /// position of the text. |
| | 13 | | /// </param> |
| | 14 | | /// <param name="Terminator"> |
| | 15 | | /// A terminator character, present in the text at most once. If not specified |
| | 16 | | /// <see cref="TextWithTerminator.DefaultTerminator"/> is used. |
| | 17 | | /// </param> |
| | 18 | | /// <param name="ValidateInput"> |
| | 19 | | /// Whether the input, and in particular <see cref="RotatedText"/> should be validated, while this object is created. |
| | 20 | | /// Validation takes O(n) time, where n = number of chars in <see cref="RotatedText"/> and can be an heavy operation. |
| | 21 | | /// </param> |
| | 22 | | /// <remarks> |
| | 23 | | /// <para id="usecases"> |
| | 24 | | /// USECASES |
| | 25 | | /// <br/> |
| | 26 | | /// - A terminator-terminated rotated text is required by Burrows-Wheeler Transform operations, such as inversion. |
| | 27 | | /// <br/> |
| | 28 | | /// - This object provides type safety, as it allows to tell apart rotated terminator-terminated strings from |
| | 29 | | /// generic ones. |
| | 30 | | /// <br/> |
| | 31 | | /// - Consistently using <see cref="RotatedTextWithTerminator"/>, rather than <see cref="string"/>, in all library |
| | 32 | | /// functionalities ensures that the invariant of a rotated terminator-terminated string is always respected. |
| | 33 | | /// <br/> |
| | 34 | | /// - Most string-related functionalities provided by <see cref="RotatedTextWithTerminator"/>, such as |
| | 35 | | /// <see cref="Length"/> and <see cref="this[Index]"/>, as well as <see cref="IEnumerable{T}"/> and |
| | 36 | | /// <see cref="IEnumerable"/> support, are delegated to the underlying string. |
| | 37 | | /// </para> |
| | 38 | | /// </remarks> |
| 11863 | 39 | | public record RotatedTextWithTerminator( |
| 11863 | 40 | | IEnumerable<char> RotatedText, |
| 11863 | 41 | | char Terminator = TextWithTerminator.DefaultTerminator, |
| 11867 | 42 | | bool ValidateInput = true) |
| 11863 | 43 | | : IValueEnumerable<char> |
| 11863 | 44 | | { |
| 11863 | 45 | | // Lazy initialized |
| 11865 | 46 | | private int? _length = null; |
| 11863 | 47 | |
|
| 11863 | 48 | | /// <summary> |
| 11863 | 49 | | /// <inheritdoc cref="RotatedTextWithTerminator" path="/param[@name='RotatedText']"/> |
| 11863 | 50 | | /// </summary> |
| 11863 | 51 | | /// <remarks> |
| 11863 | 52 | | /// Wrapped into a <see cref="IValueEnumerable{T}"/> to preserve value equality. |
| 11863 | 53 | | /// </remarks> |
| 11863 | 54 | | /// <value> |
| 11863 | 55 | | /// A sequence of chars. |
| 11863 | 56 | | /// </value> |
| 417284 | 57 | | public IEnumerable<char> RotatedText { get; init; } = RotatedText.AsValue(); |
| 11863 | 58 | |
|
| 11863 | 59 | | /// <summary> |
| 11863 | 60 | | /// <inheritdoc cref="RotatedTextWithTerminator" path="/param[@name='Terminator']"/> |
| 11863 | 61 | | /// </summary> |
| 11863 | 62 | | /// <value> |
| 11863 | 63 | | /// A single char. |
| 11863 | 64 | | /// </value> |
| 11238 | 65 | | public char Terminator { get; init; } = |
| 28084 | 66 | | !ValidateInput || RotatedText.Count(c => c == Terminator) == 1 |
| 11865 | 67 | | ? Terminator |
| 11865 | 68 | | : throw new ArgumentException($"{nameof(Terminator)} should occur in {nameof(RotatedText)} exactly once"); |
| 11863 | 69 | |
|
| 11863 | 70 | | /// <summary> |
| 11863 | 71 | | /// Select a part of <see cref="RotatedText"/> by the provided selector. |
| 11863 | 72 | | /// </summary> |
| 11863 | 73 | | /// <param name="selector">Any selector acting on a <see cref="RotatedTextWithTerminator"/>.</param> |
| 11863 | 74 | | /// <returns>A string containing the selected part.</returns> |
| 11863 | 75 | | public string this[TextWithTerminator.ISelector selector] => |
| 2 | 76 | | selector.OfRotated(this); |
| 11863 | 77 | |
|
| 11863 | 78 | | /// <summary> |
| 11863 | 79 | | /// Select a part of <see cref="RotatedText"/> by the provided range (start index included, end index excluded). |
| 11863 | 80 | | /// </summary> |
| 11863 | 81 | | /// <param name="range">The range applied to the underlying string.</param> |
| 11863 | 82 | | /// <returns>A sequence of chars containing the selected part.</returns> |
| 11863 | 83 | | public IEnumerable<char> this[Range range] => |
| 11 | 84 | | RotatedText is StringValueEnumerable { StringValue: var str } |
| 11 | 85 | | ? str[range] |
| 11 | 86 | | : RotatedText.Take(range); |
| 11863 | 87 | |
|
| 11863 | 88 | | /// <summary> |
| 11863 | 89 | | /// Select a part of <see cref="RotatedText"/> by the provided index (either w.r.t. the start or to the end of the |
| 11863 | 90 | | /// text). |
| 11863 | 91 | | /// </summary> |
| 11863 | 92 | | /// <param name="index">The index applied to the underlying string.</param> |
| 11863 | 93 | | /// <returns>A char containing the selected part.</returns> |
| 11863 | 94 | | public char this[Index index] => |
| 195205 | 95 | | RotatedText is StringValueEnumerable { StringValue: var str } |
| 195205 | 96 | | ? str[index] |
| 195205 | 97 | | : RotatedText.ElementAtO1(index); |
| 11863 | 98 | |
|
| 11863 | 99 | | /// <summary> |
| 11863 | 100 | | /// The total length of <see cref="RotatedText"/>, including the terminator. |
| 11863 | 101 | | /// </summary> |
| 11863 | 102 | | /// <remarks> |
| 11863 | 103 | | /// <inheritdoc cref="TextWithTerminator.Length" path="/remarks"/> |
| 11863 | 104 | | /// </remarks> |
| 11863 | 105 | | /// <value> |
| 11863 | 106 | | /// A positive integer (at least 1). |
| 11863 | 107 | | /// </value> |
| 11863 | 108 | | public int Length |
| 11863 | 109 | | { |
| 11863 | 110 | | get |
| 612471 | 111 | | { |
| 612471 | 112 | | if (_length == null) |
| 10718 | 113 | | { |
| 10718 | 114 | | _length = |
| 10718 | 115 | | RotatedText is StringValueEnumerable { StringValue: var str } |
| 10718 | 116 | | ? str.Length |
| 10718 | 117 | | : RotatedText.CountO1(); |
| 10718 | 118 | | } |
| 612471 | 119 | | return _length.Value; |
| 612471 | 120 | | } |
| 11863 | 121 | | } |
| 11863 | 122 | |
|
| 11863 | 123 | | /// <summary> |
| 11863 | 124 | | /// Whether this text starts with <paramref name="prefix"/>. |
| 11863 | 125 | | /// </summary> |
| 11863 | 126 | | /// <param name="prefix">A terminator-included string.</param> |
| 11863 | 127 | | /// <returns>True if <see cref="RotatedText"/> starts by <paramref name="prefix"/>.</returns> |
| 11863 | 128 | | public bool StartsWith(string prefix) => |
| 2 | 129 | | RotatedText is StringValueEnumerable { StringValue: var str } |
| 2 | 130 | | ? str.StartsWith(prefix) |
| 2 | 131 | | : RotatedText.Take(prefix.Length).SequenceEqual(prefix); |
| 11863 | 132 | |
|
| 11863 | 133 | | /// <summary> |
| 11863 | 134 | | /// Whether this text ends with <paramref name="suffix"/>. |
| 11863 | 135 | | /// </summary> |
| 11863 | 136 | | /// <param name="suffix">A terminator-included string.</param> |
| 11863 | 137 | | /// <returns>True if <see cref="RotatedText"/> ends by <paramref name="suffix"/>.</returns> |
| 11863 | 138 | | public bool EndsWith(string suffix) => |
| 2 | 139 | | RotatedText is StringValueEnumerable { StringValue: var str } |
| 2 | 140 | | ? str.EndsWith(suffix) |
| 2 | 141 | | : RotatedText.TakeLast(suffix.Length).SequenceEqual(suffix); |
| 11863 | 142 | |
|
| 11863 | 143 | | /// <inheritdoc path="//*[not(self::summary)]"/> |
| 11863 | 144 | | /// <summary> |
| 11863 | 145 | | /// Returns an enumerator that iterates through the collection of chars of the underlying <see cref="RotatedText"/> |
| 11863 | 146 | | /// string, including the <see cref="Terminator"/> char. |
| 11863 | 147 | | /// </summary> |
| 11863 | 148 | | public IEnumerator<char> GetEnumerator() => |
| 6398 | 149 | | RotatedText.GetEnumerator(); |
| 11863 | 150 | |
|
| 11863 | 151 | | /// <inheritdoc path="//*[not(self::summary)]"/> |
| 11863 | 152 | | /// <summary> |
| 11863 | 153 | | /// Returns an enumerator that iterates through the collection of chars of the underlying <see cref="RotatedText"/> |
| 11863 | 154 | | /// string, including the <see cref="Terminator"/> char. |
| 11863 | 155 | | /// </summary> |
| 11863 | 156 | | IEnumerator IEnumerable.GetEnumerator() => |
| 1 | 157 | | ((IEnumerable)RotatedText).GetEnumerator(); |
| 11863 | 158 | | } |