/Utilities/Collections/DictionaryBase.cs
C# | 1069 lines | 607 code | 95 blank | 367 comment | 30 complexity | 7d1528dd5795ed37e22d307d202e4f0f MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text;
- using NUnit.Framework;
-
- namespace Delta.Utilities.Collections
- {
- /// <summary>
- /// DictionaryBase is a base class for generics like the .NET 1.1 one.
- /// Implements IDictionary<T> and IDictionary.
- /// <para />
- /// This site contains examples and a great explanation of dictionaries:
- /// http://www.dotnetperls.com/dictionary-keys
- /// </summary>
- /// <typeparam name="TKey">Key type</typeparam>
- /// <typeparam name="TValue">Value type</typeparam>
- [DebuggerDisplay("{DebuggerToString()}")]
- public abstract class DictionaryBase<TKey, TValue>
- : CollectionBase<KeyValuePair<TKey, TValue>>,
- IDictionary<TKey, TValue>, IDictionary
- {
- #region KeysCollection Class
- /// <summary>
- /// A private class that implements ICollection<TKey> and ICollection
- /// for the Keys collection. The collection is read-only.
- /// </summary>
- /// <returns>Collection base</returns>
- private sealed class KeysCollection : CollectionBase<TKey>
- {
- #region Count (Public)
- /// <summary>
- /// Count
- /// </summary>
- /// <returns>Int</returns>
- public override int Count
- {
- get
- {
- return myDictionary.Count;
- } // get
- }
- #endregion
-
- #region Private
-
- #region myDictionary (Private)
- /// <summary>
- /// My dictionary
- /// </summary>
- private readonly DictionaryBase<TKey, TValue> myDictionary;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Constructor.
- /// </summary>
- /// <param name="myDictionary">The dictionary this is associated with.
- /// </param>
- public KeysCollection(DictionaryBase<TKey, TValue> myDictionary)
- {
- this.myDictionary = myDictionary;
- }
- #endregion
-
- #region GetEnumerator (Public)
- /// <summary>
- /// Get enumerator
- /// </summary>
- /// <returns>IEnumerator</returns>
- public override IEnumerator<TKey> GetEnumerator()
- {
- foreach (KeyValuePair<TKey, TValue> pair in myDictionary)
- {
- yield return pair.Key;
- }
- }
- #endregion
-
- #region Contains (Public)
- /// <summary>
- /// Contains
- /// </summary>
- /// <param name="key">Key</param>
- /// <returns>True if the key was found, false otherwise</returns>
- public override bool Contains(TKey key)
- {
- return myDictionary.ContainsKey(key);
- }
- #endregion
-
- #region Clear (Public)
- /// <summary>
- /// Clear
- /// </summary>
- public override void Clear()
- {
- throw new Exception("It is not allowed to clear the keys!");
- }
- #endregion
-
- #region Remove (Public)
- /// <summary>
- /// Remove. Do not call, this method is not implemented!
- /// </summary>
- /// <param name="item">Item</param>
- /// <returns>True if the item was removed.</returns>
- public override bool Remove(TKey item)
- {
- throw new Exception("It is not allowed to remove an item.");
- }
- #endregion
- }
- #endregion
-
- #region ValuesCollection Class
- /// <summary>
- /// A private class that implements ICollection<TValue> and
- /// ICollection for the Values collection. The collection is read-only.
- /// </summary>
- /// <returns>Collection base</returns>
- private sealed class ValuesCollection : CollectionBase<TValue>
- {
- #region Count (Public)
- /// <summary>
- /// Count
- /// </summary>
- /// <returns>Int</returns>
- public override int Count
- {
- get
- {
- return myDictionary.Count;
- } // get
- }
- #endregion
-
- #region Private
-
- #region myDictionary (Private)
- /// <summary>
- /// My dictionary
- /// </summary>
- private readonly DictionaryBase<TKey, TValue> myDictionary;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create values collection
- /// </summary>
- /// <param name="myDictionary">My dictionary</param>
- public ValuesCollection(DictionaryBase<TKey, TValue> myDictionary)
- {
- this.myDictionary = myDictionary;
- }
- #endregion
-
- #region GetEnumerator (Public)
- /// <summary>
- /// Get enumerator
- /// </summary>
- /// <returns>IEnumerator</returns>
- public override IEnumerator<TValue> GetEnumerator()
- {
- foreach (KeyValuePair<TKey, TValue> pair in myDictionary)
- {
- yield return pair.Value;
- }
- }
- #endregion
-
- #region Clear (Public)
- /// <summary>
- /// Clear
- /// </summary>
- public override void Clear()
- {
- throw new Exception("It is not allowed to clear the values!");
- }
- #endregion
-
- #region Remove (Public)
- /// <summary>
- /// Remove. Do not call, this method is not implemented!
- /// </summary>
- /// <param name="item">Item</param>
- /// <returns>True if removing the item succeeded.</returns>
- public override bool Remove(TValue item)
- {
- throw new Exception("It is not allowed to remove an item.");
- }
- #endregion
- }
- #endregion
-
- #region DictionaryEnumeratorWrapper Class
- /// <summary>
- /// A class that wraps a IDictionaryEnumerator around an IEnumerator that
- /// enumerates KeyValuePairs. This is useful in implementing IDictionary,
- /// because IEnumerator can be implemented with an iterator, but
- /// IDictionaryEnumerator cannot.
- /// </summary>
- /// <returns>IDictionary enumerator</returns>
- private class DictionaryEnumeratorWrapper : IDictionaryEnumerator
- {
- #region Entry (Public)
- /// <summary>
- /// Entry
- /// </summary>
- /// <returns>Dictionary entry</returns>
- public DictionaryEntry Entry
- {
- get
- {
- KeyValuePair<TKey, TValue> pair = enumerator.Current;
- DictionaryEntry entry = new DictionaryEntry();
- if (pair.Key != null)
- {
- entry.Key = pair.Key;
- }
- entry.Value = pair.Value;
-
- return entry;
- }
- }
- #endregion
-
- #region Key (Public)
- /// <summary>
- /// Key
- /// </summary>
- /// <returns>Object</returns>
- public object Key
- {
- get
- {
- KeyValuePair<TKey, TValue> pair = enumerator.Current;
-
- return pair.Key;
- }
- }
- #endregion
-
- #region Value (Public)
- /// <summary>
- /// Value
- /// </summary>
- /// <returns>Object</returns>
- public object Value
- {
- get
- {
- KeyValuePair<TKey, TValue> pair = enumerator.Current;
- return pair.Value;
- }
- }
- #endregion
-
- #region Current (Public)
- /// <summary>
- /// Current
- /// </summary>
- /// <returns>Object</returns>
- public object Current
- {
- get
- {
- return Entry;
- }
- }
- #endregion
-
- #region Private
-
- #region enumerator (Private)
- /// <summary>
- /// Enumerator
- /// </summary>
- private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Constructor.
- /// </summary>
- /// <param name="enumerator">
- /// The enumerator of KeyValuePairs that is being wrapped.
- /// </param>
- public DictionaryEnumeratorWrapper(
- IEnumerator<KeyValuePair<TKey, TValue>> enumerator)
- {
- this.enumerator = enumerator;
- }
- #endregion
-
- #region IEnumerator Members
- /// <summary>
- /// Move next
- /// </summary>
- /// <returns>True if there is a next item for the enumerator.</returns>
- public bool MoveNext()
- {
- return enumerator.MoveNext();
- }
-
- /// <summary>
- /// Reset. Do not call, this method is not implemented!
- /// </summary>
- public void Reset()
- {
- throw new NotSupportedException(
- "Reset is not supported on this enumerator.");
- }
- #endregion
- }
- #endregion
-
- #region Item (Public)
- /// <summary>
- /// The indexer of the dictionary. This is used to store keys and values
- /// and retrieve values from the dictionary. The setter accessor must be
- /// overridden in the derived class.
- /// </summary>
- /// <param name="key">Key to find in the dictionary.</param>
- /// <returns>The value associated with the key.</returns>
- /// <exception cref="KeyNotFoundException">Thrown from the get accessor
- /// if the key was not found in the dictionary.</exception>
- public virtual TValue this[TKey key]
- {
- get
- {
- TValue value;
- if (TryGetValue(key, out value))
- {
- return value;
- }
- else
- {
- throw new KeyNotFoundException(
- "The key was not found in the collection.");
- }
- }
-
- set
- {
- throw new NotSupportedException(
- "The set accessor of the indexer must be overridden.");
- }
- }
- #endregion
-
- #region Keys (Public)
- /// <summary>
- /// Returns a collection of the keys in this dictionary.
- /// </summary>
- /// <value>A read-only collection of the keys in this dictionary.</value>
- public virtual ICollection<TKey> Keys
- {
- get
- {
- return new KeysCollection(this);
- } // get
- }
- #endregion
-
- #region Values (Public)
- /// <summary>
- /// Returns a collection of the values in this dictionary. The ordering of
- /// values in this collection is the same as that in the Keys collection.
- /// </summary>
- /// <value>A read-only collection of the values in this dictionary.</value>
- public virtual ICollection<TValue> Values
- {
- get
- {
- return new ValuesCollection(this);
- } // get
- }
- #endregion
-
- #region Private
-
- #region IsFixedSize (Private)
- /// <summary>
- /// Returns whether this dictionary is fixed size.
- /// This implemented always returns false.
- /// </summary>
- /// <typeparam name="TKey">TKey</typeparam>
- /// <typeparam name="TValue">TValue</typeparam>
- /// <value>Always returns false.</value>
- bool IDictionary.IsFixedSize
- {
- get
- {
- return false;
- } // get
- }
- #endregion
-
- #region IsReadOnly (Private)
- /// <summary>
- /// Returns if this dictionary is read-only.
- /// This implementation always returns false.
- /// </summary>
- /// <typeparam name="TKey">TKey</typeparam>
- /// <typeparam name="TValue">TValue</typeparam>
- /// <value>Always returns false.</value>
- bool IDictionary.IsReadOnly
- {
- get
- {
- return false;
- } // get
- }
- #endregion
-
- #region Keys (Private)
- /// <summary>
- /// Returns a collection of all the keys in the dictionary. The values in
- /// this collection will be enumerated in the same order as the
- /// (overridden) GetEnumerator method.
- /// </summary>
- /// <typeparam name="TKey">TKey</typeparam>
- /// <typeparam name="TValue">TValue</typeparam>
- /// <value>The collection of keys.</value>
- ICollection IDictionary.Keys
- {
- get
- {
- return new KeysCollection(this);
- } // get
- }
- #endregion
-
- #region Values (Private)
- /// <summary>
- /// Returns a collection of all the values in the dictionary. The values
- /// in this collection will be enumerated in the same order as the
- /// (overridden) GetEnumerator method.
- /// </summary>
- /// <typeparam name="TKey">TKey</typeparam>
- /// <typeparam name="TValue">TValue</typeparam>
- /// <value>The collection of values.</value>
- ICollection IDictionary.Values
- {
- get
- {
- return new ValuesCollection(this);
- } // get
- }
- #endregion
-
- #region Item (Private)
- /// <summary>
- /// Gets or sets the value associated with a given key. When getting a
- /// value, if this key is not found in the collection, then null is
- /// returned. When setting a value, the value replaces any existing value
- /// in the dictionary. If either the key or value are not of the correct
- /// type for this dictionary, an ArgumentException is thrown.
- /// </summary>
- /// <value>The value associated with the key, or null if the key was not
- /// present.</value>
- /// <exception cref="ArgumentException">Key could not be converted to TKey,
- /// or value could not be converted to TValue.</exception>
- object IDictionary.this[object key]
- {
- get
- {
- if (key is TKey ||
- key == null)
- {
- TKey theKey = (TKey)key;
- TValue theValue;
-
- // The IDictionary (non-generic) indexer returns null for not found,
- // instead of throwing an exception like the generic IDictionary
- // indexer.
- if (TryGetValue(theKey, out theValue))
- {
- return theValue;
- }
- else
- {
- return null;
- }
- }
- else
- {
- return null;
- }
- }
- set
- {
- CheckGenericType<TKey>("key", key);
- CheckGenericType<TValue>("value", value);
- this[(TKey)key] = (TValue)value;
- }
- }
- #endregion
-
- #endregion
-
- #region ICollection<KeyValuePair<TKey,TValue>> Members
- /// <summary>
- /// Adds a key-value pair to the collection. This implementation calls the
- /// Add method with the Key and Value from the item.
- /// </summary>
- /// <param name="item">
- /// A KeyValuePair contains the Key and Value to add.
- /// </param>
- public override void Add(KeyValuePair<TKey, TValue> item)
- {
- Add(item.Key, item.Value);
- }
-
- /// <summary>
- /// Clears the dictionary.
- /// This method must be overridden in the derived class.
- /// </summary>
- public abstract override void Clear();
-
- /// <summary>
- /// Determines if a dictionary contains a given KeyValuePair. This
- /// implementation checks to see if the dictionary contains the given key,
- /// and if the value associated with the key is equal to (via
- /// object.Equals) the value.
- /// </summary>
- /// <param name="item">
- /// A KeyValuePair containing the Key and Value to check for.
- /// </param>
- /// <returns>True if the item was found, false otherwise.</returns>
- public override bool Contains(KeyValuePair<TKey, TValue> item)
- {
- if (ContainsKey(item.Key))
- {
- return (Equals(this[item.Key], item.Value));
- }
- else
- {
- return false;
- }
- }
-
- /// <summary>
- /// Determines if a dictionary contains a given KeyValuePair, and if so,
- /// removes it. This implementation checks to see if the dictionary
- /// contains the given key, and if the value associated with the key is
- /// equal to (via object.Equals) the value. If so, the key-value pair is
- /// removed.
- /// </summary>
- /// <param name="item">
- /// A KeyValuePair containing the Key and Value to check for.
- /// </param>
- /// <returns>
- /// True if the item was found and removed. False otherwise.
- /// </returns>
- public override bool Remove(KeyValuePair<TKey, TValue> item)
- {
- if (((ICollection<KeyValuePair<TKey, TValue>>)this).Contains(item))
- {
- return Remove(item.Key);
- }
- else
- {
- return false;
- }
- }
- #endregion
-
- #region IDictionary Members
- /// <summary>
- /// Adds a key-value pair to the collection. If key or value are not of
- /// the expected types, an ArgumentException is thrown. If both key and
- /// value are of the expected types, the (overridden) Add method is called
- /// with the key and value to add.
- /// </summary>
- /// <param name="key">Key to add</param>
- /// <param name="value">Value to add</param>
- /// <exception cref="ArgumentException">Key or value are not of the
- /// expected type for this dictionary.</exception>
- void IDictionary.Add(object key, object value)
- {
- CheckGenericType<TKey>("key", key);
- CheckGenericType<TValue>("value", value);
- Add((TKey)key, (TValue)value);
- }
-
- /// <summary>
- /// Clears this dictionary. Calls the (overridden) Clear method.
- /// </summary>
- void IDictionary.Clear()
- {
- Clear();
- }
-
- /// <summary>
- /// Determines if this dictionary contains a key equal to
- /// <paramref name="key"/>. The dictionary is not changed. Calls the
- /// (overridden) ContainsKey method. If key is not of the correct TKey for
- /// the dictionary, false is returned.
- /// </summary>
- /// <param name="key">The key to search for.</param>
- /// <returns>True if the dictionary contains key. False if the dictionary
- /// does not contain key.</returns>
- bool IDictionary.Contains(object key)
- {
- if (key is TKey ||
- key == null)
- {
- return ContainsKey((TKey)key);
- }
- else
- {
- return false;
- }
- }
-
- /// <summary>
- /// Returns an enumerator that enumerates all entries in the dictionary.
- /// Each entry is returned as a DictionaryEntry. The entries are enumerated
- /// in the same orders as the (overridden) GetEnumerator method.
- /// </summary>
- /// <returns>An enumerator for enumerating all the elements in the
- /// OrderedDictionary.</returns>
- IDictionaryEnumerator IDictionary.GetEnumerator()
- {
- // You can't implement this directly with an iterator, because iterators
- // automatically implement IEnumerator, not IDictionaryEnumerator.
- // We use the helper class DictionaryEnumeratorWrapper.
- return new DictionaryEnumeratorWrapper(GetEnumerator());
- }
-
- /// <summary>
- /// Removes the key (and associated value) from the collection that is
- /// equal to the passed in key. If no key in the dictionary is equal to
- /// the passed key, the dictionary is unchanged. Calls the (overridden)
- /// Remove method. If key is not of the correct TKey for the dictionary,
- /// the dictionary is unchanged.
- /// </summary>
- /// <param name="key">The key to remove.</param>
- /// <exception cref="ArgumentException">key could not be converted to TKey.
- /// </exception>
- void IDictionary.Remove(object key)
- {
- if (key is TKey ||
- key == null)
- {
- Remove((TKey)key);
- }
- }
- #endregion
-
- #region IDictionary<TKey,TValue> Members
- /// <summary>
- /// Adds a new key-value pair to the dictionary.
- /// </summary>
- /// <remarks>The default implementation of this method
- /// checks to see if the key already exists using
- /// ContainsKey, then calls the indexer setter if the key doesn't
- /// already exist. </remarks>
- /// <param name="key">Key to add.</param>
- /// <param name="value">Value to associated with the key.</param>
- /// <exception cref="ArgumentException">key is already present in the
- /// dictionary</exception>
- public virtual void Add(TKey key, TValue value)
- {
- if (ContainsKey(key))
- {
- throw new ArgumentException(
- "The key was already present in the dictionary.", "key");
- }
- else
- {
- this[key] = value;
- }
- }
-
- /// <summary>
- /// Determines whether a given key is found in the dictionary.
- /// </summary>
- /// <remarks>The default implementation simply calls TryGetValue and
- /// returns what it returns.</remarks>
- /// <param name="key">Key to look for in the dictionary.</param>
- /// <returns>True if the key is present in the dictionary.</returns>
- public virtual bool ContainsKey(TKey key)
- {
- TValue dummy;
- return TryGetValue(key, out dummy);
- }
-
- /// <summary>
- /// Removes a key from the dictionary.
- /// This method must be overridden in the derived class.
- /// </summary>
- /// <param name="key">Key to remove from the dictionary.</param>
- /// <returns>True if the key was found, false otherwise.</returns>
- public abstract bool Remove(TKey key);
-
- /// <summary>
- /// Determines if this dictionary contains a key equal to
- /// <paramref name="key"/>. If so, the value associated with that key is
- /// returned through the value parameter. This method must be overridden
- /// by the derived class.
- /// </summary>
- /// <param name="key">The key to search for.</param>
- /// <param name="value">Returns the value associated with key, if true was
- /// returned.</param>
- /// <returns>True if the dictionary contains key. False if the dictionary
- /// does not contain key.</returns>
- public abstract bool TryGetValue(TKey key, out TValue value);
- #endregion
-
- #region IEnumerable Members
- /// <summary>
- /// Returns an enumerator that enumerates all entries in the dictionary.
- /// Each entry is returned as a DictionaryEntry. The entries are enumerated
- /// in the same orders as the (overridden) GetEnumerator method.
- /// </summary>
- /// <returns>An enumerator for enumerating all the elements in the
- /// OrderedDictionary.</returns>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IDictionary)this).GetEnumerator();
- }
- #endregion
-
- #region ToString (Public)
- /// <summary>
- /// Shows the string representation of the dictionary. The string
- /// representation contains a list of the mappings in the dictionary.
- /// </summary>
- /// <returns>The string representation of the dictionary.</returns>
- public override string ToString()
- {
- bool firstItem = true;
-
- StringBuilder builder = new StringBuilder();
-
- builder.Append("{");
-
- // Call ToString on each item and put it in.
- foreach (KeyValuePair<TKey, TValue> pair in this)
- {
- if (!firstItem)
- {
- builder.Append(", ");
- }
-
- if (pair.Key == null)
- {
- builder.Append("null");
- }
- else
- {
- builder.Append(pair.Key.ToString());
- }
-
- builder.Append("->");
-
- if (pair.Value == null)
- {
- builder.Append("null");
- }
- else
- {
- builder.Append(pair.Value.ToString());
- }
-
- firstItem = false;
- }
-
- builder.Append("}");
- return builder.ToString();
- }
- #endregion
-
- #region Methods (Private)
-
- #region DebuggerToString
- /// <summary>
- /// Display the contents of the dictionary in the debugger. This is
- /// intentionally private, it is called only from the debugger due to the
- /// presence of the DebuggerDisplay attribute. It is similar format to
- /// ToString(), but is limited to 250-300 characters or so, so as not to
- /// overload the debugger.
- /// </summary>
- /// <returns>The string representation of the items in the collection,
- /// similar in format to ToString().</returns>
- internal new string DebuggerToString()
- {
- const int MAXLENGTH = 250;
-
- bool firstItem = true;
-
- StringBuilder builder = new StringBuilder();
-
- builder.Append("{");
-
- // Call ToString on each item and put it in.
- foreach (KeyValuePair<TKey, TValue> pair in this)
- {
- if (builder.Length >= MAXLENGTH)
- {
- builder.Append(", ...");
- break;
- }
-
- if (!firstItem)
- {
- builder.Append(", ");
- }
-
- if (pair.Key == null)
- {
- builder.Append("null");
- }
- else
- {
- builder.Append(pair.Key.ToString());
- }
-
- builder.Append("->");
-
- if (pair.Value == null)
- {
- builder.Append("null");
- }
- else
- {
- builder.Append(pair.Value.ToString());
- }
-
- firstItem = false;
- }
-
- builder.Append("}");
- return builder.ToString();
- }
- #endregion
-
- #region CheckGenericType
- /// <summary>
- /// Check that the given parameter is of the expected generic type.
- /// Throw an ArgumentException if it isn't.
- /// </summary>
- /// <typeparam name="ExpectedType">Expected type of the parameter
- /// </typeparam>
- /// <param name="name">parameter name</param>
- /// <param name="value">parameter value</param>
- private void CheckGenericType<ExpectedType>(string name, object value)
- {
- if (!(value is ExpectedType))
- {
- throw new ArgumentException(
- "The value " + value + " isn't of type " + typeof(ExpectedType) +
- " and can't be used in this generic collection.", name);
- }
- }
- #endregion
-
- #endregion
- }
-
- /// <summary>
- /// Test dictionary base helper, needs to be an extra class because
- /// DictionaryBase is a generic class.
- /// </summary>
- internal class DictionaryBaseTests
- {
- #region Helpers
-
- #region TestDictionary
- /// <summary>
- /// Test dictionary
- /// </summary>
- private class TestDictionary<TKey, TValue> : DictionaryBase<TKey, TValue>
- {
- #region Count (Public)
- /// <summary>
- /// Count
- /// </summary>
- /// <returns>Int</returns>
- public override int Count
- {
- get
- {
- return keys.Count;
- } // get
- }
- #endregion
-
- #region Item (Public)
- /// <summary>
- /// This
- /// </summary>
- /// <param name="key">Key</param>
- /// <returns>TValue</returns>
- public override TValue this[TKey key]
- {
- set
- {
- int index = keys.IndexOf(key);
- if (index < 0)
- {
- keys.Add(key);
- values.Add(value);
- }
- else
- {
- values[index] = value;
- }
- }
- }
- #endregion
-
- #region Private
-
- #region keys (Private)
- /// <summary>
- /// Keys
- /// </summary>
- private readonly List<TKey> keys;
- #endregion
-
- #region values (Private)
- /// <summary>
- /// Values
- /// </summary>
- private readonly List<TValue> values;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create test dictionary
- /// </summary>
- /// <param name="keys">Keys</param>
- /// <param name="values">Values</param>
- public TestDictionary(TKey[] keys, TValue[] values)
- {
- this.keys = new List<TKey>(keys);
- this.values = new List<TValue>(values);
- }
- #endregion
-
- #region GetEnumerator (Public)
- /// <summary>
- /// Get enumerator
- /// </summary>
- /// <returns>IEnumerator</returns>
- public override IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
- {
- for (int i = 0; i < keys.Count; ++i)
- {
- yield return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
- }
- }
- #endregion
-
- #region TryGetValue (Public)
- /// <summary>
- /// Try get value
- /// </summary>
- /// <param name="key">Key</param>
- /// <param name="value">Value</param>
- /// <returns>
- /// True if getting the value with the key succeeded, false otherwise
- /// (the returned value will be the default value, mostly 0 or null).
- /// </returns>
- public override bool TryGetValue(TKey key, out TValue value)
- {
- int index = keys.IndexOf(key);
- if (index < 0)
- {
- value = default(TValue);
- return false;
- }
- else
- {
- value = values[index];
- return true;
- }
- }
- #endregion
-
- #region Remove (Public)
- /// <summary>
- /// Remove value at given key.
- /// </summary>
- /// <param name="key">Key</param>
- /// <returns>
- /// True if removing the key and value succeeded, false otherwise.
- /// </returns>
- public override bool Remove(TKey key)
- {
- int index = keys.IndexOf(key);
- if (index < 0)
- {
- return false;
- }
- else
- {
- keys.RemoveAt(index);
- values.RemoveAt(index);
- return true;
- }
- }
- #endregion
-
- #region Clear (Public)
- /// <summary>
- /// Clear
- /// </summary>
- public override void Clear()
- {
- keys.Clear();
- values.Clear();
- }
- #endregion
- }
- #endregion
-
- #endregion
-
- #region TestDictionaryBase (Static)
- /// <summary>
- /// Test dictionary base. Note: Too slow for a dynamic unit test.
- /// </summary>
- [Test]
- public static void TestDictionaryBase()
- {
- string[] testKeys = {
- "Whats", "up", "inda", "House"
- };
- int[] testValues = {
- 10, 2, 51, 20
- };
-
- TestDictionary<string, int> testDict =
- new TestDictionary<string, int>(testKeys, testValues);
-
- int allValues = 0;
- int allDictValues = 0;
- for (int num = 0; num < testValues.Length; num++)
- {
- allValues += testValues[num];
- }
- foreach (int someValue in testDict.Values)
- {
- allDictValues += someValue;
- }
- Assert.Equal(allValues, allDictValues);
-
- Assert.Equal(10, testDict["Whats"]);
- Assert.Equal(2, testDict["up"]);
- Assert.Equal(51, testDict["inda"]);
- Assert.Equal(20, testDict["House"]);
- }
- #endregion
- }
- }