Exemplo n.º 1
0
        public void Add(IEnumerable <TKey> key, TValue value)
        {
            var enumerator = key.GetEnumerator();

            _root.Add(enumerator, value);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Add a new node to the Trie, beginning at the given node.
        /// </summary>
        /// <param name="node">Node to begin addtion at.</param>
        /// <param name="keys">Keys of node to be created</param>
        /// <param name="value">Value of node to be created</param>
        private void Add(Node <TKey, TValue> node, IEnumerable <TKey> keys, TValue value)
        {
            if (null == node)
            {
                throw new ArgumentNullException("node cannot be null.");
            }
            else if (null == keys)
            {
                throw new ArgumentNullException("node cannot be null.");
            }
            else if (null == value)
            {
                throw new ArgumentNullException("value cannot be null.");
            }

            TKey key = keys.FirstOrDefault();

            if (null == key)
            {
                throw new ArgumentNullException("key cannot be null.");
            }

            Node <TKey, TValue> childNode = node.Children.Where(child => child.Key.Equals(key)).FirstOrDefault();

            if (null != childNode)
            {
                this.Add(childNode, keys.Skip(1), value);
            }
            else
            {
                Node <TKey, TValue> newNode;
                int keysCount = keys.Count();
                if (1 == keysCount)
                {
                    // Insert last new node
                    newNode = new Node <TKey, TValue>(key, value);
                    node.Add(newNode);
                    this.count++;
                    this.keys.Add(keys);
                    this.values.Add(value);
                }
                else if (0 == keysCount)
                {
                    // Add to this node
                    if (null != node.Value)
                    {
                        throw new InvalidOperationException("Duplicate Key!");
                    }
                    else
                    {
                        node.Value = value;
                    }
                }
                else
                {
                    newNode = new Node <TKey, TValue>(key);
                    node.Add(newNode);
                    this.Add(newNode, keys.Skip(1), value);
                }
            }
        }