| | | 1 | | using MoreStructures.RecImmTrees; |
| | | 2 | | using MoreStructures.Utilities; |
| | | 3 | | |
| | | 4 | | namespace MoreStructures.SuffixStructures; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Extension methods for all <see cref="TreePath{TEdge, TNode}"/> used in |
| | | 8 | | /// <see cref="ISuffixStructureNode{TEdge, TNode}"/> structures. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class SuffixStructureTreePathExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Calculate the suffix corresponding to this path on the provided terminator-including text. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="TEdge"> |
| | | 16 | | /// <inheritdoc cref="ISuffixStructureEdge{TEdge, TNode}" path="/typeparam[@name='TEdge']"/> |
| | | 17 | | /// </typeparam> |
| | | 18 | | /// <typeparam name="TNode"> |
| | | 19 | | /// <inheritdoc cref="ISuffixStructureNode{TEdge, TNode}" path="/typeparam[@name='TNode']"/> |
| | | 20 | | /// </typeparam> |
| | | 21 | | /// <param name="path">The path to traverse to build the suffix.</param> |
| | | 22 | | /// <param name="text">The text, including the terminator character.</param> |
| | | 23 | | /// <returns>A <see cref="IValueEnumerable{T}"/> sequence of <see cref="char"/> containing the suffix.</returns> |
| | | 24 | | public static IValueEnumerable<char> SuffixFor<TEdge, TNode>( |
| | | 25 | | this TreePath<TEdge, TNode> path, |
| | | 26 | | TextWithTerminator text) |
| | | 27 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode>, TextWithTerminator.ISelector |
| | | 28 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> => |
| | | 29 | | |
| | 144 | 30 | | path.PathNodes |
| | 936 | 31 | | .SelectMany(node => text[node.Key]) |
| | 144 | 32 | | .AsValue(); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Whether this path identifies a suffix of the provided text. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <typeparam name="TEdge"> |
| | | 38 | | /// <inheritdoc cref="ISuffixStructureEdge{TEdge, TNode}" path="/typeparam[@name='TEdge']"/> |
| | | 39 | | /// </typeparam> |
| | | 40 | | /// <typeparam name="TNode"> |
| | | 41 | | /// <inheritdoc cref="ISuffixStructureNode{TEdge, TNode}" path="/typeparam[@name='TNode']"/> |
| | | 42 | | /// </typeparam> |
| | | 43 | | /// <param name="path">The path, identifying a segment of the provided text.</param> |
| | | 44 | | /// <param name="text">The text, including the terminator character.</param> |
| | | 45 | | /// <returns>True if the segment of text is also a suffix the text.</returns> |
| | | 46 | | public static bool IsSuffixOf<TEdge, TNode> |
| | | 47 | | (this TreePath<TEdge, TNode> path, |
| | | 48 | | TextWithTerminator text) |
| | | 49 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode>, TextWithTerminator.ISelector |
| | | 50 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> => |
| | | 51 | | |
| | 35 | 52 | | text.EndsWith(path.SuffixFor(text)); |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Whether the provided <paramref name="path"/> includes at least once, on any node of the path, the provided |
| | | 56 | | /// <paramref name="index"/>. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <typeparam name="TEdge"> |
| | | 59 | | /// <inheritdoc cref="ISuffixStructureEdge{TEdge, TNode}" path="/typeparam[@name='TEdge']"/> |
| | | 60 | | /// </typeparam> |
| | | 61 | | /// <typeparam name="TNode"> |
| | | 62 | | /// <inheritdoc cref="ISuffixStructureNode{TEdge, TNode}" path="/typeparam[@name='TNode']"/> |
| | | 63 | | /// </typeparam> |
| | | 64 | | /// <param name="path">The path to walk, looking for <paramref name="index"/>.</param> |
| | | 65 | | /// <param name="index">The index of the char of the text, to look for. Must be non-negative.</param> |
| | | 66 | | /// <returns>A boolean.</returns> |
| | | 67 | | public static bool ContainsIndex<TEdge, TNode>( |
| | | 68 | | this TreePath<TEdge, TNode> path, |
| | | 69 | | int index) |
| | | 70 | | where TEdge : ISuffixStructureEdge<TEdge, TNode> |
| | | 71 | | where TNode : ISuffixStructureNode<TEdge, TNode> => |
| | | 72 | | |
| | 577 | 73 | | index >= 0 |
| | 1414 | 74 | | ? path.PathNodes.Any(pathNode => pathNode.Key.ContainsIndex(index)) |
| | 577 | 75 | | : throw new ArgumentOutOfRangeException(nameof(index), "Must be non-negative."); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Whether the provided <paramref name="path"/> has at least a node starting at a index lower or equal than the |
| | | 79 | | /// provided <paramref name="index"/>. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <typeparam name="TEdge"> |
| | | 82 | | /// <inheritdoc cref="ISuffixStructureEdge{TEdge, TNode}" path="/typeparam[@name='TEdge']"/> |
| | | 83 | | /// </typeparam> |
| | | 84 | | /// <typeparam name="TNode"> |
| | | 85 | | /// <inheritdoc cref="ISuffixStructureNode{TEdge, TNode}" path="/typeparam[@name='TNode']"/> |
| | | 86 | | /// </typeparam> |
| | | 87 | | /// <param name="path">The path to walk, looking for <paramref name="index"/>.</param> |
| | | 88 | | /// <param name="index">The index of the char of the text, to look for. Must be non-negative.</param> |
| | | 89 | | /// <returns>A boolean.</returns> |
| | | 90 | | public static bool ContainsIndexesNonBiggerThan<TEdge, TNode>( |
| | | 91 | | this TreePath<TEdge, TNode> path, |
| | | 92 | | int index) |
| | | 93 | | where TEdge : ISuffixStructureEdge<TEdge, TNode> |
| | | 94 | | where TNode : ISuffixStructureNode<TEdge, TNode> => |
| | | 95 | | |
| | 13 | 96 | | index >= 0 |
| | 15 | 97 | | ? path.PathNodes.Any(pathNode => pathNode.Key.ContainsIndexesNonBiggerThan(index)) |
| | 13 | 98 | | : throw new ArgumentOutOfRangeException(nameof(index), "Must be non-negative."); |
| | | 99 | | } |