public void AddEnrty(string entry) { if (string.IsNullOrWhiteSpace(entry)) { return; } Bohr currentLeaf = _root; foreach (char ch in entry) { Bohr next = currentLeaf.Sons.GetValueOrDefault(ch); if (next != null) { currentLeaf = next; } else { next = new Bohr(ch); next.Parent = currentLeaf; currentLeaf.Sons[ch] = next; currentLeaf = next; } } currentLeaf.IsComplete = true; }
private string GetStringFromBohr(Bohr bohr) { Bohr current = bohr; var resultBuilder = new StringBuilder(); Bohr parent = current.Parent; while (parent != null) { resultBuilder.Insert(0, current.Character); current = parent; parent = current.Parent; } return(resultBuilder.ToString()); }
public IReadOnlyList <string> GetCurrentEntries() { var result = new List <string>(); if (_current.IsComplete) { result.Add(GetStringFromBohr(_current)); } Bohr current = _current.GetGoodSuffLink(); while (current != _root) { result.Add(GetStringFromBohr(current)); current = current.GetGoodSuffLink(); } return(result); }
public Bohr GetAutomoveWithChar(char character) { Bohr next = NextStateByChar.GetValueOrDefault(character); if (next != null) { return(next); } Bohr forward = Sons.GetValueOrDefault(character); if (forward == null) { forward = Parent == null ? this : GetSuffLink().GetAutomoveWithChar(character); } NextStateByChar[character] = forward; return(forward); }
public Bohr GetGoodSuffLink() { if (GoodSuffLink != null) { return(GoodSuffLink); } Bohr suffLink = GetSuffLink(); if (suffLink.Parent == null) { GoodSuffLink = suffLink; } else { GoodSuffLink = suffLink.IsComplete ? suffLink : suffLink.GetGoodSuffLink(); } return(GoodSuffLink); }
public void SetNextState(char ch) { _current = _current.GetAutomoveWithChar(ch); }
public void Reset() { _current = _root; }
public AhoAutomat() { _root = new Bohr(char.MinValue); _current = _root; }