Esempio n. 1
0
        /// <summary>
        /// Gets or sets the value associated with the specified word in the <see cref="TrieMap{TValue}"/>.
        /// </summary>
        /// <param name="word">The word of the value to get or set.</param>
        /// <returns>The value associated with the specified word. If the specified word is not found,
        /// the get operation throws a <see cref="KeyNotFoundException"/>, and
        /// the set operation creates a new element with the specified word.</returns>
        public virtual TValue this[string word]
        {
            get
            {
                TValue value;
                if (TryGetValue(word, out value))
                {
                    return(value);
                }
                else
                {
                    throw new KeyNotFoundException();
                }
            }
            set
            {
                var  curNode             = Root;
                bool wordWasAlreadyAdded = false;

                for (int i = 0; i < word.Length; i++)
                {
                    if (curNode.children.ContainsKey(word[i]))
                    {
                        if (i == word.Length - 1)
                        {
                            var child = curNode.children[word[i]];
                            wordWasAlreadyAdded = child.IsTerminal;
                            child.IsTerminal    = true;
                            child.Value         = value;
                        }
                        else
                        {
                            curNode = curNode.children[word[i]];
                        }
                    }
                    else
                    {
                        if (i == word.Length - 1)
                        {
                            var newNode = new TrieMapNode <TValue>(word[i], value, true);
                            curNode.children.Add(word[i], newNode);
                        }
                        else
                        {
                            var newNode = new TrieMapNode <TValue>(word[i], default(TValue), false);
                            curNode.children.Add(word[i], newNode);
                            curNode = curNode.children[word[i]];
                        }
                    }
                }

                if (!wordWasAlreadyAdded)
                {
                    Count++;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a word and a value to it to the <see cref="TrieMap{TValue}"/>.
        /// </summary>
        /// <param name="word">The word to add.</param>
        /// <param name="value">The value to add.</param>
        public void Add(string word, TValue value)
        {
            var  curNode             = Root;
            bool wordWasAlreadyAdded = false;

            for (int i = 0; i < word.Length; i++)
            {
                if (curNode.children.ContainsKey(word[i]))
                {
                    if (i == word.Length - 1)
                    {
                        var child = curNode.children[word[i]];
                        wordWasAlreadyAdded = child.IsTerminal;
                        child.IsTerminal    = true;
                        child.Value         = value;
                    }
                    else
                    {
                        curNode = curNode.children[word[i]];
                    }
                }
                else
                {
                    if (i == word.Length - 1)
                    {
                        var newNode = new TrieMapNode <TValue>(word[i], value, true);
                        curNode.children.Add(word[i], newNode);
                    }
                    else
                    {
                        var newNode = new TrieMapNode <TValue>(word[i], default(TValue), false);
                        curNode.children.Add(word[i], newNode);
                        curNode = curNode.children[word[i]];
                    }
                }
            }

            if (!wordWasAlreadyAdded)
            {
                Count++;
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Creates a new instance of the <see cref="TrieMap{TValue}"/> class.
 /// </summary>
 public TrieMap()
 {
     Root = new TrieMapNode <TValue>(default(char), default(TValue), false);
 }
Esempio n. 4
0
 /// <summary>
 /// Removes all words from the <see cref="TrieMap{TValue}"/>.
 /// </summary>
 public void Clear()
 {
     Root  = new TrieMapNode <TValue>(default(char), default(TValue), false);
     Count = 0;
 }