Exemplo n.º 1
0
        /// <summary>
        /// Gets or sets the value associated with the specified word in the <see cref="SuffixTreeMap{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 = word.Length - 1; i >= 0; i--)
                {
                    if (curNode.children.ContainsKey(word[i]))
                    {
                        if (i == 0)
                        {
                            var child = curNode.children[word[i]];
                            wordWasAlreadyAdded = child.IsTerminal;
                            child.IsTerminal    = true;
                            child.Value         = value;
                        }
                        else
                        {
                            curNode = curNode.children[word[i]];
                        }
                    }
                    else
                    {
                        if (i == 0)
                        {
                            var newNode = new SuffixTreeMapNode <TValue>(word[i], value, true);
                            curNode.children.Add(word[i], newNode);
                        }
                        else
                        {
                            var newNode = new SuffixTreeMapNode <TValue>(word[i], default(TValue), false);
                            curNode.children.Add(word[i], newNode);
                            curNode = curNode.children[word[i]];
                        }
                    }
                }

                if (!wordWasAlreadyAdded)
                {
                    Count++;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a word and a value to it to the <see cref="SuffixTreeMap{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 = word.Length - 1; i >= 0; i--)
            {
                if (curNode.children.ContainsKey(word[i]))
                {
                    if (i == 0)
                    {
                        var child = curNode.children[word[i]];
                        wordWasAlreadyAdded = child.IsTerminal;
                        child.IsTerminal    = true;
                        child.Value         = value;
                    }
                    else
                    {
                        curNode = curNode.children[word[i]];
                    }
                }
                else
                {
                    if (i == 0)
                    {
                        var newNode = new SuffixTreeMapNode <TValue>(word[i], value, true);
                        curNode.children.Add(word[i], newNode);
                    }
                    else
                    {
                        var newNode = new SuffixTreeMapNode <TValue>(word[i], default(TValue), false);
                        curNode.children.Add(word[i], newNode);
                        curNode = curNode.children[word[i]];
                    }
                }
            }

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