Example #1
0
        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;
        }
Example #2
0
        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());
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
 public void SetNextState(char ch)
 {
     _current = _current.GetAutomoveWithChar(ch);
 }
Example #7
0
 public void Reset()
 {
     _current = _root;
 }
Example #8
0
 public AhoAutomat()
 {
     _root    = new Bohr(char.MinValue);
     _current = _root;
 }