Example #1
0
        public bool AddChild(WordHint wordHint)
        {
            if (!string.IsNullOrEmpty(m_name) &&
                !wordHint.Text.StartsWith(m_name, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            int newLevel = Level + 1;

            bool lastChild = newLevel >= wordHint.Text.Length;

            string newName = lastChild ? wordHint.Text :
                             wordHint.Text.Substring(0, newLevel);

            TrieCell oldChild = null;

            if (m_children.TryGetValue(newName, out oldChild))
            {
                return(oldChild.AddChild(wordHint));
            }

            TrieCell newChild = new TrieCell(newName, wordHint, newLevel);

            m_children[newName] = newChild;

            if (newLevel < wordHint.Text.Length)
            { // if there are still chars left, add a grandchild recursively.
                newChild.AddChild(wordHint);
            }

            return(true);
        }
Example #2
0
        public Trie(List <string> words)
        {
            m_root = new TrieCell();

            int index = 0;

            foreach (string word in words)
            {
                AddWord(word, index++);
            }
        }
Example #3
0
        public void Search(string text, int max, List <WordHint> results)
        {
            text = Utils.RemovePrefix(text);
            TrieCell current = m_root;

            for (int level = 1; level <= text.Length && current != null; level++)
            {
                string substr = text.Substring(0, level);
                if (!current.Children.TryGetValue(substr, out current))
                {
                    current = null;
                }
            }

            if (current == null)
            {
                return; // passed text doesn't exist
            }

            AddAll(current, max, results);
        }
Example #4
0
        void AddAll(TrieCell cell, int max, List <WordHint> results)
        {
            if (cell.WordHint != null && !cell.WordHint.Exists(results))
            {
                results.Add(cell.WordHint);
            }
            if (results.Count >= max)
            {
                return;
            }

            foreach (var entry in cell.Children)
            {
                TrieCell child = entry.Value;
                AddAll(child, max, results);

                if (results.Count >= max)
                {
                    return;
                }
            }
        }