Removed overridable change notifications from MultiDictionary - accurately sending these would involve considerable overhead; added unit tests for all main interface methods of the MultiDictionary
git-svn-id: file:///srv/devel/repo-conversion/nusu@261 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
b37c4a757c
commit
df169e376a
|
@ -1,13 +1,478 @@
|
|||
/*
|
||||
struct CommandLine {
|
||||
[Option]
|
||||
bool? Option;
|
||||
[Option]
|
||||
int? Width;
|
||||
[Option]
|
||||
TypeCode Code;
|
||||
[Values]
|
||||
string[] Values;
|
||||
}
|
||||
*/
|
||||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2012 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Nuclex.Support.Collections {
|
||||
|
||||
/// <summary>Dictionary that can contain multiple values under the same key</summary>
|
||||
/// <typeparam name="TKey">Type of keys used within the dictionary</typeparam>
|
||||
/// <typeparam name="TValue">Type of values used within the dictionary</typeparam>
|
||||
public partial class MultiDictionary<TKey, TValue> : IMultiDictionary<TKey, TValue> {
|
||||
|
||||
#region class Enumerator
|
||||
|
||||
/// <summary>Enumerates the values stored in a multi dictionary</summary>
|
||||
private class Enumerator :
|
||||
IEnumerator<KeyValuePair<TKey, TValue>>, IDictionaryEnumerator {
|
||||
|
||||
/// <summary>Initializes a new multi dictionary enumerator</summary>
|
||||
/// <param name="dictionary">Dictionary that will be enumerated</param>
|
||||
public Enumerator(MultiDictionary<TKey, TValue> dictionary) {
|
||||
this.dictionary = dictionary;
|
||||
|
||||
Reset();
|
||||
}
|
||||
|
||||
/// <summary>The current entry the enumerator is pointing at</summary>
|
||||
public KeyValuePair<TKey, TValue> Current {
|
||||
get {
|
||||
if(this.currentValue == null) {
|
||||
throw new InvalidOperationException("Enumerator is not on a valid position");
|
||||
}
|
||||
|
||||
return new KeyValuePair<TKey, TValue>(
|
||||
this.currentCollection.Current.Key, this.currentValue.Current
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Immediately releases all resources owned by the instance</summary>
|
||||
public void Dispose() {
|
||||
if(this.currentValue != null) {
|
||||
this.currentValue.Dispose();
|
||||
this.currentValue = null;
|
||||
}
|
||||
if(this.currentCollection != null) {
|
||||
this.currentCollection.Dispose();
|
||||
this.currentCollection = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Advances the enumerator to the entry</summary>
|
||||
/// <returns>
|
||||
/// True if there was a next entry, false if the end of the set has been reached
|
||||
/// </returns>
|
||||
public bool MoveNext() {
|
||||
if(this.currentCollection == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(; ; ) {
|
||||
|
||||
// Try to move the enumerator in the current key's list to the next item
|
||||
if(this.currentValue != null) {
|
||||
if(this.currentValue.MoveNext()) {
|
||||
return true; // We found the next item
|
||||
} else {
|
||||
this.currentValue.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// Enumerator for the current key's list reached the end, go to the next key
|
||||
if(this.currentCollection.MoveNext()) {
|
||||
this.currentValue = this.currentCollection.Current.Value.GetEnumerator();
|
||||
} else {
|
||||
this.currentValue = null; // Guaranteed to be disposed already
|
||||
this.currentCollection.Dispose();
|
||||
this.currentCollection = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Resets the enumerator to its initial position</summary>
|
||||
public void Reset() {
|
||||
if(this.currentValue != null) {
|
||||
this.currentValue.Dispose();
|
||||
this.currentValue = null;
|
||||
}
|
||||
if(this.currentCollection != null) {
|
||||
this.currentCollection.Dispose();
|
||||
}
|
||||
this.currentCollection = this.dictionary.GetEnumerator();
|
||||
}
|
||||
|
||||
#region IEnumerator implementation
|
||||
|
||||
/// <summary>The item the enumerator is currently pointing at</summary>
|
||||
object IEnumerator.Current {
|
||||
get { return Current; }
|
||||
}
|
||||
|
||||
#endregion // IEnumerator implementation
|
||||
|
||||
#region IDictionaryEnumerator implementation
|
||||
|
||||
/// <summary>The current entry the enumerator is pointing to</summary>
|
||||
DictionaryEntry IDictionaryEnumerator.Entry {
|
||||
get {
|
||||
enforceEnumeratorOnValidPosition();
|
||||
|
||||
return new DictionaryEntry(
|
||||
this.currentCollection.Current.Key, this.currentValue.Current
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an exception if the enumerator is not on a valid position
|
||||
/// </summary>
|
||||
private void enforceEnumeratorOnValidPosition() {
|
||||
if(this.currentValue == null) {
|
||||
throw new InvalidOperationException("Enumerator is not on a valid position");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The current dictionary key</summary>
|
||||
object IDictionaryEnumerator.Key {
|
||||
get {
|
||||
enforceEnumeratorOnValidPosition();
|
||||
return this.currentCollection.Current.Key;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The current dictionary value</summary>
|
||||
object IDictionaryEnumerator.Value {
|
||||
get {
|
||||
enforceEnumeratorOnValidPosition();
|
||||
return this.currentValue.Current;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // IDictionaryEnumerator implementation
|
||||
|
||||
/// <summary>Dictionary over whose entries the enumerator is enumerating</summary>
|
||||
private IDictionary<TKey, ICollection<TValue>> dictionary;
|
||||
/// <summary>Current key the enumerator is at</summary>
|
||||
private IEnumerator<KeyValuePair<TKey, ICollection<TValue>>> currentCollection;
|
||||
/// <summary>Current value in the current key the enumerator is at</summary>
|
||||
private IEnumerator<TValue> currentValue;
|
||||
|
||||
}
|
||||
|
||||
#endregion // class Enumerator
|
||||
|
||||
#region class ValueList
|
||||
|
||||
/// <summary>Stores the list of values for a dictionary key</summary>
|
||||
private class ValueList : Collection<TValue> {
|
||||
|
||||
/// <summary>Initializes a new value list</summary>
|
||||
/// <param name="dictionary">Dictionary the value list belongs to</param>
|
||||
public ValueList(MultiDictionary<TKey, TValue> dictionary) {
|
||||
this.dictionary = dictionary;
|
||||
}
|
||||
|
||||
/// <summary>Called when the value list is being cleared</summary>
|
||||
protected override void ClearItems() {
|
||||
this.dictionary.count -= Count;
|
||||
base.ClearItems();
|
||||
}
|
||||
|
||||
/// <summary>Called when an item is inserted into the value list</summary>
|
||||
/// <param name="index">Index at which the item is being inserted</param>
|
||||
/// <param name="item">Item that is being inserted</param>
|
||||
protected override void InsertItem(int index, TValue item) {
|
||||
base.InsertItem(index, item);
|
||||
++this.dictionary.count;
|
||||
}
|
||||
|
||||
/// <summary>Called when an item is removed from the value list</summary>
|
||||
/// <param name="index">Index at which the item is being removed</param>
|
||||
protected override void RemoveItem(int index) {
|
||||
base.RemoveItem(index);
|
||||
--this.dictionary.count;
|
||||
}
|
||||
|
||||
/// <summary>The dictionary the value list belongs to</summary>
|
||||
private MultiDictionary<TKey, TValue> dictionary;
|
||||
|
||||
}
|
||||
|
||||
#endregion // class ValueList
|
||||
|
||||
/// <summary>Initializes a new multi dictionary</summary>
|
||||
public MultiDictionary() : this(new Dictionary<TKey, ICollection<TValue>>()) { }
|
||||
|
||||
/// <summary>Initializes a new multi dictionary</summary>
|
||||
/// <param name="dictionary">Dictionary the multi dictionary will be based on</param>
|
||||
internal MultiDictionary(IDictionary<TKey, ICollection<TValue>> dictionary) {
|
||||
this.typedDictionary = dictionary;
|
||||
this.objectDictionary = (this.typedDictionary as IDictionary);
|
||||
|
||||
foreach(ICollection<TValue> values in dictionary.Values) {
|
||||
this.count += values.Count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Whether the dictionary is write-protected</summary>
|
||||
public bool IsReadOnly {
|
||||
get { return this.typedDictionary.IsReadOnly; }
|
||||
}
|
||||
|
||||
/// <summary>Determines the number of values stored under the specified key</summary>
|
||||
/// <param name="key">Key whose values will be counted</param>
|
||||
/// <returns>The number of values stored under the specified key</returns>
|
||||
public int CountValues(TKey key) {
|
||||
ICollection<TValue> values;
|
||||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
return values.Count;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified KeyValuePair is contained in the dictionary
|
||||
/// </summary>
|
||||
/// <param name="item">KeyValuePair that will be checked for</param>
|
||||
/// <returns>True if the provided KeyValuePair was contained in the dictionary</returns>
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item) {
|
||||
ICollection<TValue> values;
|
||||
if(this.typedDictionary.TryGetValue(item.Key, out values)) {
|
||||
return values.Contains(item.Value);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Determines whether the Dictionary contains the specified key</summary>
|
||||
/// <param name="key">Key that will be checked for</param>
|
||||
/// <returns>
|
||||
/// True if an entry with the specified key was contained in the Dictionary
|
||||
/// </returns>
|
||||
public bool ContainsKey(TKey key) {
|
||||
return this.typedDictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
/// <summary>Copies the contents of the Dictionary into an array</summary>
|
||||
/// <param name="array">Array the Dictionary will be copied into</param>
|
||||
/// <param name="arrayIndex">
|
||||
/// Starting index at which to begin filling the destination array
|
||||
/// </param>
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
|
||||
foreach(KeyValuePair<TKey, ICollection<TValue>> item in this.typedDictionary) {
|
||||
foreach(TValue value in item.Value) {
|
||||
array[arrayIndex] = new KeyValuePair<TKey, TValue>(item.Key, value);
|
||||
++arrayIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Number of elements contained in the Dictionary</summary>
|
||||
public int Count {
|
||||
get { return this.count; }
|
||||
}
|
||||
|
||||
/// <summary>Creates a new enumerator for the dictionary</summary>
|
||||
/// <returns>The new dictionary enumerator</returns>
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
/// <summary>Collection of all keys contained in the dictionary</summary>
|
||||
public ICollection<TKey> Keys {
|
||||
get { return this.typedDictionary.Keys; }
|
||||
}
|
||||
|
||||
/// <summary>Collection of all values contained in the dictionary</summary>
|
||||
public ICollection<TValue> Values {
|
||||
get {
|
||||
if(this.valueCollection == null) {
|
||||
this.valueCollection = new ValueCollection(this);
|
||||
}
|
||||
|
||||
return this.valueCollection;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to retrieve the item with the specified key from the dictionary
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the item to attempt to retrieve</param>
|
||||
/// <param name="values">
|
||||
/// Output parameter that will receive the values upon successful completion
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// True if the item was found and has been placed in the output parameter
|
||||
/// </returns>
|
||||
public bool TryGetValue(TKey key, out ICollection<TValue> values) {
|
||||
return this.typedDictionary.TryGetValue(key, out values);
|
||||
}
|
||||
|
||||
/// <summary>Accesses an item in the dictionary by its key</summary>
|
||||
/// <param name="key">Key of the item that will be accessed</param>
|
||||
public ICollection<TValue> this[TKey key] {
|
||||
get { return this.typedDictionary[key]; }
|
||||
set {
|
||||
if(value == null) {
|
||||
ICollection<TValue> values;
|
||||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
foreach(TValue removedValue in values) {
|
||||
|
||||
}
|
||||
}
|
||||
this.typedDictionary.Remove(key);
|
||||
}
|
||||
|
||||
ICollection<TValue> currentValues;
|
||||
if(this.typedDictionary.TryGetValue(key, out currentValues)) {
|
||||
ValueList currentValueList = (ValueList)currentValues;
|
||||
|
||||
int index = 0;
|
||||
foreach(TValue addedValue in value) {
|
||||
if(index < currentValueList.Count) {
|
||||
TValue original = currentValueList[index];
|
||||
currentValueList[index] = addedValue;
|
||||
OnReplaced(
|
||||
new KeyValuePair<TKey, TValue>(key, original),
|
||||
new KeyValuePair<TKey, TValue>(key, addedValue)
|
||||
);
|
||||
} else {
|
||||
currentValueList.Add(addedValue);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
|
||||
}
|
||||
++index;
|
||||
}
|
||||
|
||||
int count = currentValueList.Count;
|
||||
while(count > index) {
|
||||
--count;
|
||||
TValue removedValue = currentValueList[count];
|
||||
currentValueList.RemoveAt(count);
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, removedValue));
|
||||
}
|
||||
} else {
|
||||
currentValues = new ValueList(this);
|
||||
this.typedDictionary.Add(key, currentValues);
|
||||
|
||||
foreach(TValue addedValue in value) {
|
||||
currentValues.Add(addedValue);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
|
||||
++this.count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Inserts an item into the dictionary</summary>
|
||||
/// <param name="key">Key under which to add the new item</param>
|
||||
/// <param name="value">Item that will be added to the dictionary</param>
|
||||
public void Add(TKey key, TValue value) {
|
||||
ICollection<TValue> values;
|
||||
if(!this.typedDictionary.TryGetValue(key, out values)) {
|
||||
values = new ValueList(this);
|
||||
this.typedDictionary.Add(key, values);
|
||||
}
|
||||
|
||||
values.Add(value);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the item with the specified key and value from the dictionary
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the item that will be removed</param>
|
||||
/// <param name="value">Value of the item that will be removed</param>
|
||||
/// <returns>
|
||||
/// True if the specified item was contained in the dictionary and was removed
|
||||
/// </returns>
|
||||
/// <exception cref="NotSupportedException">If the dictionary is read-only</exception>
|
||||
public bool Remove(TKey key, TValue value) {
|
||||
ICollection<TValue> values;
|
||||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
values.Remove(value);
|
||||
|
||||
if(values.Count == 0) {
|
||||
this.typedDictionary.Remove(key);
|
||||
}
|
||||
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Removes all items with the specified key from the dictionary</summary>
|
||||
/// <param name="key">Key of the item that will be removed</param>
|
||||
/// <returns>The number of items that have been removed from the dictionary</returns>
|
||||
/// <exception cref="NotSupportedException">If the dictionary is read-only</exception>
|
||||
public int RemoveKey(TKey key) {
|
||||
ICollection<TValue> values;
|
||||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
this.count -= values.Count;
|
||||
this.typedDictionary.Remove(key);
|
||||
|
||||
foreach(TValue value in values) {
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
|
||||
}
|
||||
return values.Count;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Removes all items from the Dictionary</summary>
|
||||
public void Clear() {
|
||||
OnClearing();
|
||||
this.typedDictionary.Clear();
|
||||
this.count = 0;
|
||||
OnCleared();
|
||||
}
|
||||
|
||||
/// <summary>Fires the 'ItemAdded' event</summary>
|
||||
/// <param name="item">Item that has been added to the collection</param>
|
||||
protected virtual void OnAdded(KeyValuePair<TKey, TValue> item) { }
|
||||
|
||||
/// <summary>Fires the 'ItemRemoved' event</summary>
|
||||
/// <param name="item">Item that has been removed from the collection</param>
|
||||
protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) { }
|
||||
|
||||
/// <summary>Fires the 'ItemReplaced' event</summary>
|
||||
/// <param name="oldItem">Item that was replaced in the collection</param>
|
||||
/// <param name="newItem">Item that the original was replaced by</param>
|
||||
protected virtual void OnReplaced(
|
||||
KeyValuePair<TKey, TValue> oldItem, KeyValuePair<TKey, TValue> newItem
|
||||
) { }
|
||||
|
||||
/// <summary>Fires the 'Clearing' event</summary>
|
||||
protected virtual void OnClearing() { }
|
||||
|
||||
/// <summary>Fires the 'Cleared' event</summary>
|
||||
protected virtual void OnCleared() { }
|
||||
|
||||
/// <summary>The wrapped Dictionary under its type-safe interface</summary>
|
||||
private IDictionary<TKey, ICollection<TValue>> typedDictionary;
|
||||
/// <summary>The wrapped Dictionary under its object interface</summary>
|
||||
private IDictionary objectDictionary;
|
||||
/// <summary>The number of items currently in the multi dictionary</summary>
|
||||
private int count;
|
||||
/// <summary>Provides the values stores in the dictionary in sequence</summary>
|
||||
private ValueCollection valueCollection;
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Collections
|
||||
|
|
|
@ -119,8 +119,6 @@ namespace Nuclex.Support.Collections {
|
|||
if(values.Count == 0) {
|
||||
this.typedDictionary.Remove(itemToRemove.Key);
|
||||
}
|
||||
|
||||
OnRemoved(itemToRemove);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -206,6 +206,184 @@ namespace Nuclex.Support.Collections {
|
|||
Assert.IsFalse(dictionary.ContainsKey(20));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the key collection can be retrieved from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void KeyCollectionCanBeRetrieved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
|
||||
ICollection<int> keys = dictionary.Keys;
|
||||
Assert.IsNotNull(keys);
|
||||
Assert.AreEqual(1, keys.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the key collection can be retrieved from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ValueCollectionCanBeRetrieved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
dictionary.Add(20, "twenty");
|
||||
|
||||
ICollection<string> values = dictionary.Values;
|
||||
Assert.IsNotNull(values);
|
||||
Assert.AreEqual(3, values.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that TryGetValue() returns false and doesn't throw if a key
|
||||
/// is not found in the collection
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TryGetValueReturnsFalseOnMissingKey() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
ICollection<string> values;
|
||||
Assert.IsFalse(dictionary.TryGetValue(123, out values));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that keys can be looked up via TryGetValue()</summary>
|
||||
[Test]
|
||||
public void TryGetValueCanLookUpValues() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
ICollection<string> values;
|
||||
Assert.IsTrue(dictionary.TryGetValue(10, out values));
|
||||
Assert.AreEqual(2, values.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that assigning null to a key deletes all the values stored
|
||||
/// under it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void AssigningNullToKeyRemovesAllValues() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
dictionary.Add(20, "twenty");
|
||||
|
||||
Assert.AreEqual(3, dictionary.Count);
|
||||
dictionary[10] = null;
|
||||
Assert.AreEqual(1, dictionary.Count);
|
||||
Assert.IsFalse(dictionary.ContainsKey(10));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that assigning null to a key deletes all the values stored
|
||||
/// under it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ValueListCanBeAssignedToNewKey() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary[3] = new List<string>() { "three", "drei" };
|
||||
|
||||
Assert.AreEqual(2, dictionary.Count);
|
||||
Assert.IsTrue(dictionary.Contains(new KeyValuePair<int, string>(3, "three")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that assigning null to a key deletes all the values stored
|
||||
/// under it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ValueListCanOverwriteExistingKey() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "dix");
|
||||
|
||||
Assert.AreEqual(1, dictionary.Count);
|
||||
|
||||
dictionary[10] = new List<string>() { "ten", "zehn" };
|
||||
|
||||
Assert.AreEqual(2, dictionary.Count);
|
||||
Assert.IsFalse(dictionary.Contains(new KeyValuePair<int, string>(10, "dix")));
|
||||
Assert.IsTrue(dictionary.Contains(new KeyValuePair<int, string>(10, "ten")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that nothing bad happens when a key is removed from the dictionary
|
||||
/// that it doesn't contain
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void NonExistingKeyCanBeRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
Assert.AreEqual(0, dictionary.RemoveKey(123));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the remove method returns the number of values that have
|
||||
/// been removed from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void RemoveReturnsNumberOfValuesRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
Assert.AreEqual(2, dictionary.RemoveKey(10));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the dictionary becomes empty after clearing it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void DictionaryIsEmptyAfterClear() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
dictionary.Add(20, "twenty");
|
||||
Assert.AreEqual(3, dictionary.Count);
|
||||
dictionary.Clear();
|
||||
Assert.AreEqual(0, dictionary.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that non-existing values can be removed from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void NonExistingValueCanBeRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
Assert.IsFalse(dictionary.Remove(123, "test"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that nothing bad happens when the last value under a key is removed
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void LastValueOfKeyCanBeRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(123, "test");
|
||||
dictionary.Remove(123, "test");
|
||||
Assert.AreEqual(0, dictionary.CountValues(123));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the dictionary can be copied into an array
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void DictionaryCanBeCopiedIntoArray() {
|
||||
var expected = new List<KeyValuePair<int, string>>() {
|
||||
new KeyValuePair<int, string>(1, "one"),
|
||||
new KeyValuePair<int, string>(1, "eins"),
|
||||
new KeyValuePair<int, string>(2, "two"),
|
||||
new KeyValuePair<int, string>(2, "zwei")
|
||||
};
|
||||
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
foreach(KeyValuePair<int, string> entry in expected) {
|
||||
dictionary.Add(entry.Key, entry.Value);
|
||||
}
|
||||
|
||||
var actual = new KeyValuePair<int, string>[4];
|
||||
dictionary.CopyTo(actual, 0);
|
||||
|
||||
CollectionAssert.AreEquivalent(expected, actual);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Collections
|
||||
|
|
|
@ -328,43 +328,17 @@ namespace Nuclex.Support.Collections {
|
|||
get { return this.typedDictionary[key]; }
|
||||
set {
|
||||
if(value == null) {
|
||||
this.typedDictionary.Remove(key);
|
||||
}
|
||||
|
||||
ICollection<TValue> currentValues;
|
||||
if(this.typedDictionary.TryGetValue(key, out currentValues)) {
|
||||
ValueList currentValueList = (ValueList)currentValues;
|
||||
|
||||
int index = 0;
|
||||
foreach(TValue addedValue in value) {
|
||||
if(index < currentValueList.Count) {
|
||||
TValue original = currentValueList[index];
|
||||
currentValueList[index] = addedValue;
|
||||
OnReplaced(
|
||||
new KeyValuePair<TKey, TValue>(key, original),
|
||||
new KeyValuePair<TKey, TValue>(key, addedValue)
|
||||
);
|
||||
} else {
|
||||
currentValueList.Add(addedValue);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
|
||||
}
|
||||
++index;
|
||||
}
|
||||
|
||||
int count = currentValueList.Count;
|
||||
while(count > index) {
|
||||
--count;
|
||||
TValue removedValue = currentValueList[count];
|
||||
currentValueList.RemoveAt(count);
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, removedValue));
|
||||
}
|
||||
RemoveKey(key);
|
||||
} else {
|
||||
currentValues = new ValueList(this);
|
||||
this.typedDictionary.Add(key, currentValues);
|
||||
|
||||
ICollection<TValue> currentValues;
|
||||
if(this.typedDictionary.TryGetValue(key, out currentValues)) {
|
||||
currentValues.Clear();
|
||||
} else {
|
||||
currentValues = new ValueList(this);
|
||||
this.typedDictionary.Add(key, currentValues);
|
||||
}
|
||||
foreach(TValue addedValue in value) {
|
||||
currentValues.Add(addedValue);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -381,7 +355,6 @@ namespace Nuclex.Support.Collections {
|
|||
}
|
||||
|
||||
values.Add(value);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -397,12 +370,9 @@ namespace Nuclex.Support.Collections {
|
|||
ICollection<TValue> values;
|
||||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
values.Remove(value);
|
||||
|
||||
if(values.Count == 0) {
|
||||
this.typedDictionary.Remove(key);
|
||||
}
|
||||
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -418,10 +388,6 @@ namespace Nuclex.Support.Collections {
|
|||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
this.count -= values.Count;
|
||||
this.typedDictionary.Remove(key);
|
||||
|
||||
foreach(TValue value in values) {
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
|
||||
}
|
||||
return values.Count;
|
||||
} else {
|
||||
return 0;
|
||||
|
@ -430,33 +396,10 @@ namespace Nuclex.Support.Collections {
|
|||
|
||||
/// <summary>Removes all items from the Dictionary</summary>
|
||||
public void Clear() {
|
||||
OnClearing();
|
||||
this.typedDictionary.Clear();
|
||||
this.count = 0;
|
||||
OnCleared();
|
||||
}
|
||||
|
||||
/// <summary>Fires the 'ItemAdded' event</summary>
|
||||
/// <param name="item">Item that has been added to the collection</param>
|
||||
protected virtual void OnAdded(KeyValuePair<TKey, TValue> item) { }
|
||||
|
||||
/// <summary>Fires the 'ItemRemoved' event</summary>
|
||||
/// <param name="item">Item that has been removed from the collection</param>
|
||||
protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) { }
|
||||
|
||||
/// <summary>Fires the 'ItemReplaced' event</summary>
|
||||
/// <param name="oldItem">Item that was replaced in the collection</param>
|
||||
/// <param name="newItem">Item that the original was replaced by</param>
|
||||
protected virtual void OnReplaced(
|
||||
KeyValuePair<TKey, TValue> oldItem, KeyValuePair<TKey, TValue> newItem
|
||||
) { }
|
||||
|
||||
/// <summary>Fires the 'Clearing' event</summary>
|
||||
protected virtual void OnClearing() { }
|
||||
|
||||
/// <summary>Fires the 'Cleared' event</summary>
|
||||
protected virtual void OnCleared() { }
|
||||
|
||||
/// <summary>The wrapped Dictionary under its type-safe interface</summary>
|
||||
private IDictionary<TKey, ICollection<TValue>> typedDictionary;
|
||||
/// <summary>The wrapped Dictionary under its object interface</summary>
|
||||
|
|
Loading…
Reference in New Issue
Block a user