| | 1 | | using MoreStructures.SuffixStructures; |
| | 2 | |
|
| | 3 | | namespace MoreStructures.SuffixTrees; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// The index key of the collection of children of a <see cref="SuffixTreeNode"/>, which identifies a non-empty |
| | 7 | | /// substring in text used as a selector to navigate the <see cref="SuffixTreeNode"/> in text pattern matching. |
| | 8 | | /// </summary> |
| | 9 | | /// <param name="Start"> |
| | 10 | | /// <inheritdoc cref="ISuffixStructureEdge{TEdge, TNode}.Start" path="/summary"/> |
| | 11 | | /// </param> |
| | 12 | | /// <param name="Length"> |
| | 13 | | /// <inheritdoc cref="ISuffixStructureEdge{TEdge, TNode}.Length" path="/summary"/> |
| | 14 | | /// </param> |
| | 15 | | /// <remarks> |
| | 16 | | /// Supports <see cref="IComparable{T}"/>, by <see cref="Start"/> and <see cref="Length"/>, in this order. |
| | 17 | | /// </remarks> |
| 43822 | 18 | | public record SuffixTreeEdge(int Start, int Length) |
| 43822 | 19 | | : ISuffixStructureEdge<SuffixTreeEdge, SuffixTreeNode>, IComparable<SuffixTreeEdge> |
| 43822 | 20 | | { |
| 43822 | 21 | | /// <inheritdoc/> |
| 82548 | 22 | | public int Start { get; init; } = Start >= 0 |
| 43825 | 23 | | ? Start |
| 43825 | 24 | | : throw new ArgumentOutOfRangeException(nameof(Start), "Must be non-negative."); |
| 43822 | 25 | |
|
| 43822 | 26 | | /// <inheritdoc/> |
| 78145 | 27 | | public int Length { get; init; } = Length >= 0 |
| 43823 | 28 | | ? Length |
| 43823 | 29 | | : throw new ArgumentOutOfRangeException(nameof(Length), "Must be non-negative."); |
| 43822 | 30 | |
|
| 43822 | 31 | | /// <inheritdoc path="//*[not(self::summary)]"/> |
| 43822 | 32 | | /// <summary> |
| 43822 | 33 | | /// <inheritdoc/> |
| 43822 | 34 | | /// <br/> |
| 43822 | 35 | | /// Comparison is done by <see cref="Start"/> first, then <see cref="Length"/>: lower is smaller, higher is |
| 43822 | 36 | | /// bigger. |
| 43822 | 37 | | /// </summary> |
| 43822 | 38 | | /// <exception cref="ArgumentException"> |
| 43822 | 39 | | /// Thrown when <paramref name="other"/> is not a <see cref="SuffixTreeEdge"/>. |
| 43822 | 40 | | /// </exception> |
| 43822 | 41 | | public int CompareTo(SuffixTreeEdge? other) |
| 1124 | 42 | | { |
| 1124 | 43 | | if (other == null) |
| 1 | 44 | | throw new ArgumentException($"Invalid comparison: cannot compare to null."); |
| 43822 | 45 | |
|
| 1123 | 46 | | var startComparison = Start - other.Start; |
| 1123 | 47 | | if (startComparison != 0) |
| 1120 | 48 | | return startComparison; |
| 3 | 49 | | return Length - other.Length; |
| 1123 | 50 | | } |
| 43822 | 51 | |
|
| 43822 | 52 | | /// <inheritdoc/> |
| 43822 | 53 | | public virtual string Of(TextWithTerminator text) => |
| 5321 | 54 | | string.Concat(text[Start..(Start + Length)])[0..Length]; |
| 43822 | 55 | |
|
| 43822 | 56 | | /// <inheritdoc/> |
| 43822 | 57 | | public virtual string OfRotated(RotatedTextWithTerminator text) => |
| 6 | 58 | | string.Concat(text[Start..(Start + Length)])[0..Length]; |
| 43822 | 59 | |
|
| 43822 | 60 | | /// <inheritdoc path="//*[not(self::summary)]"/> |
| 43822 | 61 | | /// <summary> |
| 43822 | 62 | | /// <inheritdoc/> |
| 43822 | 63 | | /// <br/> |
| 43822 | 64 | | /// Generates a string in the form "(<see cref="Start"/>, <see cref="Length"/>)". |
| 43822 | 65 | | /// </summary> |
| 947 | 66 | | public override string ToString() => $"({Start},{Length})"; |
| 43822 | 67 | | } |