Ejemplo n.º 1
0
        /// <summary>
        ///     Add word to trie
        /// </summary>
        public void Add(string word, TRecord record)
        {
            if (string.IsNullOrEmpty(word))
            {
                throw new ArgumentException("Word is empty or null.");
            }

            var current = _root;

            for (var i = 0; i < word.Length; ++i)
            {
                if (!current.Children.ContainsKey(word[i]))
                {
                    var newTrieNode = new TrieMapNode <TRecord>(word[i], default(TRecord));
                    newTrieNode.Parent = current;
                    current.Children.Add(word[i], newTrieNode);
                }

                current = current.Children[word[i]];
            }

            if (current.IsTerminal)
            {
                throw new ApplicationException("Word already exists in Trie.");
            }

            ++_count;
            current.IsTerminal = true;
            current.Record     = record;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     IComparer interface implementation
        /// </summary>
        public int CompareTo(TrieMapNode <TRecord> other)
        {
            if (other == null)
            {
                return(-1);
            }

            return(Key.CompareTo(other.Key));
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     Clears this insance.
 /// </summary>
 public void Clear()
 {
     _count = 0;
     _root.Clear();
     _root = new TrieMapNode <TRecord>(' ', default(TRecord), false);
 }
Ejemplo n.º 4
0
 /// <summary>
 ///     CONSTRUCTOR
 /// </summary>
 public TrieMap()
 {
     _count = 0;
     _root  = new TrieMapNode <TRecord>(' ', default(TRecord), false);
 }