Example #1
0
        /// <summary>Get or set the value associated with the supplied key.</summary>
        /// <param name="key">The key of the association.</param>
        /// <returns>Value associated with the specified key.</returns>
        /// <exception cref="ArgumentNullException">When <em>key</em> is <b>null</b>.</exception>
        /// <exception cref="KeyNotFoundException">When getting a value for a non-existant key.</exception>
        /// <remarks>Setting a value for a non-existant key performs an insert operation.</remarks>
        public TValue this[TKey key]
        {
            get
            {
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }

                int index;
                Leaf <TKey, TValue> leaf = Find(key, out index);
                if (index < 0)
                {
                    throw new KeyNotFoundException("The given key was not present in the dictionary.");
                }
                return(leaf.GetValue(index));
            }
            set
            {
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }

                var path = new TreePath <TKey, TValue>(this, key);
                if (path.IsFound)
                {
                    path.LeafValue = value;
                }
                else
                {
                    Insert(path, key, value);
                }
            }
        }
Example #2
0
 internal void Add(Leaf <TKey, TValue> source, int sourceStart, int sourceStop)
 {
     for (int i = sourceStart; i < sourceStop; ++i)
     {
         Add(source.GetKey(i), source.GetValue(i));
     }
 }
Example #3
0
        /// <summary>Get or set the value associated with the supplied key.</summary>
        public object this[object key]
        {
            get
            {
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }

                int index;
                Leaf <TKey, TValue> leaf = Find((TKey)key, out index);
                if (index < 0)
                {
                    return(null);
                }
                return(leaf.GetValue(index));
            }
            set
            {
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }

                var path = new TreePath <TKey, TValue>(this, (TKey)key);
                if (path.IsFound)
                {
                    path.LeafValue = (TValue)value;
                }
                else
                {
                    Insert(path, (TKey)key, (TValue)value);
                }
            }
        }
Example #4
0
        /// <summary>Determine if the collection contains the supplied key and value.</summary>
        /// <param name="pair">Key/value pair to find.</param>
        /// <returns><b>true</b> if the collection contains the specified pair;
        /// otherwise <b>false</b>.</returns>
        bool ICollection <KeyValuePair <TKey, TValue> > .Contains(KeyValuePair <TKey, TValue> pair)
        {
            var path = new TreePath <TKey, TValue>(this, pair.Key);

            if (!path.IsFound)
            {
                return(false);
            }

            Leaf <TKey, TValue> leaf = (Leaf <TKey, TValue>)path.TopNode;

            return(pair.Value.Equals(leaf.GetValue(path.TopNodeIndex)));
        }
Example #5
0
            /// <summary>
            /// Get an enumerator that will loop thru the collection of values.
            /// </summary>
            /// <returns>An enumerator for the collection.</returns>
            public IEnumerator <TValue> GetEnumerator()
            {
                for (Leaf <TKey, TValue> currentLeaf = tree.GetFirstLeaf();;)
                {
                    for (int leafIndex = 0; leafIndex < currentLeaf.KeyCount; ++leafIndex)
                    {
                        yield return(currentLeaf.GetValue(leafIndex));
                    }

                    currentLeaf = currentLeaf.RightLeaf;
                    if (currentLeaf == null)
                    {
                        break;
                    }
                }
            }
Example #6
0
        /// <summary>Get the value associated with the supplied key.</summary>
        /// <param name="key">Target of search.</param>
        /// <param name="value">If the key is found, its value is placed here; otherwise
        /// it will be loaded with the default value for its type.</param>
        /// <returns><b>true</b> if supplied key is found; otherwise <b>false</b>.</returns>
        /// <exception cref="ArgumentNullException">When <em>key</em> is <b>null</b>.</exception>
        public bool TryGetValue(TKey key, out TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            int index;
            Leaf <TKey, TValue> leaf = Find(key, out index);

            if (index >= 0)
            {
                value = leaf.GetValue(index);
                return(true);
            }
            else
            {
                value = default(TValue);
                return(false);
            }
        }