Skip to content

Commit

Permalink
Merge pull request #86 from gzatravkin/gzatravkin/fix_garbage
Browse files Browse the repository at this point in the history
Fix garbage generation
  • Loading branch information
mganss authored Jul 20, 2024
2 parents 4b4906e + afb9977 commit 88b2d1f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion AhoCorasick/AhoCorasick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void BuildFail(Trie node = null)

var word = node.Word;
for (int i = 1; i < word.Length && node.Fail == null; i++)
node.Fail = Trie.ExploreFailLink(word.Substring(i));
node.Fail = Trie.ExploreFailLink(word, i, word.Length);

foreach (var subNode in node.Next.Values)
BuildFail(subNode);
Expand Down
19 changes: 12 additions & 7 deletions AhoCorasick/Trie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,23 @@ public virtual Trie Add(string word)
}

/// <summary>
/// Finds the failure node for a specified suffix.
/// Finds the failure node for a specified suffix within the given range of indices.
/// </summary>
/// <param name="word">The suffix.</param>
/// <returns>The failure node or null.</returns>
public virtual Trie ExploreFailLink(string word)
/// <param name="word">The string containing the suffix.</param>
/// <param name="startIndex">The start index of the suffix within the string.</param>
/// <param name="endIndex">The end index (exclusive) of the suffix within the string.</param>
/// <returns>The failure node or null if no failure node is found.</returns>

public virtual Trie ExploreFailLink(string word, int startIndex, int endIndex)
{
var node = this;

foreach (var c in word)
for (int i = startIndex; i < endIndex; i++)
{
node.Next.TryGetValue(c, out node);
if (node == null) return null;
if (!node.Next.TryGetValue(word[i], out node))
{
return null;
}
}

return node;
Expand Down

0 comments on commit 88b2d1f

Please sign in to comment.