273 lines
9.9 KiB
C#
273 lines
9.9 KiB
C#
#region Apache License 2.0
|
|
/*
|
|
Nuclex .NET Framework
|
|
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
#endregion // Apache License 2.0
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Nuclex.Support.Collections {
|
|
|
|
partial class MultiDictionary<TKey, TValue> {
|
|
|
|
#region IEnumerable implementation
|
|
|
|
/// <summary>Returns a new object enumerator for the Dictionary</summary>
|
|
/// <returns>The new object enumerator</returns>
|
|
IEnumerator IEnumerable.GetEnumerator() {
|
|
return GetEnumerator();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDictionary implementation
|
|
|
|
/// <summary>Adds an item into the dictionary</summary>
|
|
/// <param name="key">Key under which the item will be added</param>
|
|
/// <param name="value">Item that will be added</param>
|
|
void IDictionary.Add(object key, object value) {
|
|
Add((TKey)key, (TValue)value);
|
|
}
|
|
|
|
/// <summary>Determines whether the specified key exists in the dictionary</summary>
|
|
/// <param name="key">Key that will be checked for</param>
|
|
/// <returns>True if an item with the specified key exists in the dictionary</returns>
|
|
bool IDictionary.Contains(object key) {
|
|
return this.objectDictionary.Contains(key);
|
|
}
|
|
|
|
/// <summary>Whether the size of the dictionary is fixed</summary>
|
|
bool IDictionary.IsFixedSize {
|
|
get { return this.objectDictionary.IsFixedSize; }
|
|
}
|
|
|
|
/// <summary>Returns a collection of all keys in the dictionary</summary>
|
|
ICollection IDictionary.Keys {
|
|
get { return this.objectDictionary.Keys; }
|
|
}
|
|
|
|
/// <summary>Returns a collection of all values stored in the dictionary</summary>
|
|
ICollection IDictionary.Values {
|
|
get {
|
|
if(this.valueCollection == null) {
|
|
this.valueCollection = new ValueCollection(this);
|
|
}
|
|
|
|
return this.valueCollection;
|
|
}
|
|
}
|
|
|
|
/// <summary>Removes an item from the dictionary</summary>
|
|
/// <param name="key">Key of the item that will be removed</param>
|
|
void IDictionary.Remove(object key) {
|
|
RemoveKey((TKey)key);
|
|
}
|
|
|
|
/// <summary>Accesses an item in the dictionary by its key</summary>
|
|
/// <param name="key">Key of the item that will be accessed</param>
|
|
/// <returns>The item with the specified key</returns>
|
|
object IDictionary.this[object key] {
|
|
get { return this.objectDictionary[key]; }
|
|
set { this[(TKey)key] = (ICollection<TValue>)value; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDictionaryEnumerator implementation
|
|
|
|
/// <summary>Returns a new entry enumerator for the dictionary</summary>
|
|
/// <returns>The new entry enumerator</returns>
|
|
IDictionaryEnumerator IDictionary.GetEnumerator() {
|
|
return new Enumerator(this);
|
|
}
|
|
|
|
#endregion // IDictionaryEnumerator implementation
|
|
|
|
#region ICollection<KeyValuePair<TKey, TValue>> implementation
|
|
|
|
/// <summary>Inserts an already prepared element into the dictionary</summary>
|
|
/// <param name="item">Prepared element that will be added to the dictionary</param>
|
|
void ICollection<KeyValuePair<TKey, TValue>>.Add(
|
|
KeyValuePair<TKey, TValue> item
|
|
) {
|
|
Add(item.Key, item.Value);
|
|
}
|
|
|
|
/// <summary>Removes all items from the dictionary</summary>
|
|
/// <param name="itemToRemove">Item that will be removed from the dictionary</param>
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
|
|
KeyValuePair<TKey, TValue> itemToRemove
|
|
) {
|
|
ICollection<TValue> values;
|
|
if(!this.typedDictionary.TryGetValue(itemToRemove.Key, out values)) {
|
|
return false;
|
|
}
|
|
|
|
if(values.Remove(itemToRemove.Value)) {
|
|
if(values.Count == 0) {
|
|
this.typedDictionary.Remove(itemToRemove.Key);
|
|
}
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ICollection implementation
|
|
|
|
/// <summary>Copies the contents of the Dictionary into an array</summary>
|
|
/// <param name="array">Array the Dictionary contents will be copied into</param>
|
|
/// <param name="arrayIndex">
|
|
/// Starting index at which to begin filling the destination array
|
|
/// </param>
|
|
void ICollection.CopyTo(Array array, int arrayIndex) {
|
|
foreach(KeyValuePair<TKey, ICollection<TValue>> item in this.typedDictionary) {
|
|
foreach(TValue value in item.Value) {
|
|
array.SetValue(new KeyValuePair<TKey, TValue>(item.Key, value), arrayIndex);
|
|
++arrayIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Whether the Dictionary is synchronized for multi-threaded usage</summary>
|
|
bool ICollection.IsSynchronized {
|
|
get { return this.objectDictionary.IsSynchronized; }
|
|
}
|
|
|
|
/// <summary>Synchronization root on which the Dictionary locks</summary>
|
|
object ICollection.SyncRoot {
|
|
get { return this.objectDictionary.SyncRoot; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDictionary<TKey, ICollection<TValue>> implementation
|
|
|
|
/// <summary>Adds a series of values to a dictionary</summary>
|
|
/// <param name="key">Key under which the values will be added</param>
|
|
/// <param name="values">Values that will be added to the dictionary</param>
|
|
void IDictionary<TKey, ICollection<TValue>>.Add(TKey key, ICollection<TValue> values) {
|
|
ICollection<TValue> currentValues;
|
|
if(!this.typedDictionary.TryGetValue(key, out currentValues)) {
|
|
currentValues = new ValueList(this);
|
|
}
|
|
|
|
foreach(TValue value in values) {
|
|
currentValues.Add(value);
|
|
}
|
|
}
|
|
|
|
/// <summary>Removes all values with the specified key</summary>
|
|
/// <param name="key">Key whose associated entries will be removed</param>
|
|
/// <returns>True if at least one entry has been removed from the dictionary</returns>
|
|
bool IDictionary<TKey, ICollection<TValue>>.Remove(TKey key) {
|
|
return (RemoveKey(key) > 0);
|
|
}
|
|
|
|
/// <summary>Returns a collection of value collections</summary>
|
|
ICollection<ICollection<TValue>> IDictionary<TKey, ICollection<TValue>>.Values {
|
|
get { return this.typedDictionary.Values; }
|
|
}
|
|
|
|
#endregion // IDictionary<TKey, ICollection<TValue>> implementation
|
|
|
|
#region ICollection<KeyValuePair<TKey, ICollection<TValue>>> implementation
|
|
|
|
/// <summary>Adds a series of values to a dictionary</summary>
|
|
/// <param name="item">Entry containing the values that will be added</param>
|
|
void ICollection<KeyValuePair<TKey, ICollection<TValue>>>.Add(
|
|
KeyValuePair<TKey, ICollection<TValue>> item
|
|
) {
|
|
ICollection<TValue> currentValues;
|
|
if(!this.typedDictionary.TryGetValue(item.Key, out currentValues)) {
|
|
currentValues = new ValueList(this);
|
|
}
|
|
|
|
foreach(TValue value in item.Value) {
|
|
currentValues.Add(value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the dictionary contains the specified key/value pair
|
|
/// </summary>
|
|
/// <param name="item">Key/value pair for which the dictionary will be checked</param>
|
|
/// <returns>True if the dictionary contains the specified key/value pair</returns>
|
|
bool ICollection<KeyValuePair<TKey, ICollection<TValue>>>.Contains(
|
|
KeyValuePair<TKey, ICollection<TValue>> item
|
|
) {
|
|
return this.typedDictionary.Contains(item);
|
|
}
|
|
|
|
/// <summary>Copies the contents of the dictionary into an array</summary>
|
|
/// <param name="array"></param>
|
|
/// <param name="arrayIndex"></param>
|
|
void ICollection<KeyValuePair<TKey, ICollection<TValue>>>.CopyTo(
|
|
KeyValuePair<TKey, ICollection<TValue>>[] array, int arrayIndex
|
|
) {
|
|
this.typedDictionary.CopyTo(array, arrayIndex);
|
|
}
|
|
|
|
/// <summary>Removes the specified key/value pair from the dictionary</summary>
|
|
/// <param name="item">Key/value pair that will be removed</param>
|
|
/// <returns>True if the key/value pair was contained in the dictionary</returns>
|
|
bool ICollection<KeyValuePair<TKey, ICollection<TValue>>>.Remove(
|
|
KeyValuePair<TKey, ICollection<TValue>> item
|
|
) {
|
|
return this.typedDictionary.Remove(item);
|
|
}
|
|
|
|
/// <summary>Returns an enumerator for the dictionary</summary>
|
|
/// <returns>An enumerator for the key/value pairs in the dictionary</returns>
|
|
IEnumerator<KeyValuePair<TKey, ICollection<TValue>>> IEnumerable<
|
|
KeyValuePair<TKey, ICollection<TValue>>
|
|
>.GetEnumerator() {
|
|
return this.typedDictionary.GetEnumerator();
|
|
}
|
|
|
|
/// <summary>Number of unique keys in the dictionary</summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// This Count property returns a different value from the main interface of
|
|
/// the multi dictionary to stay consistent with the implemented interfaces.
|
|
/// </para>
|
|
/// <para>
|
|
/// If you cast a multi dictionary to a collection of collections, the count
|
|
/// property of the outer collection should, of course, be the number of inner
|
|
/// collections it contains (and not the sum of the items contained in all of
|
|
/// the inner collections).
|
|
/// </para>
|
|
/// <para>
|
|
/// If you use the count property in the main interface of the multi dictionary,
|
|
/// the value collections are hidden (it behaves as if the key was in the
|
|
/// dictionary multiple times), so now the sum of all key-value pairs should
|
|
/// be returned.
|
|
/// </para>
|
|
/// </remarks>
|
|
int ICollection<KeyValuePair<TKey, ICollection<TValue>>>.Count {
|
|
get { return this.typedDictionary.Count; }
|
|
}
|
|
|
|
#endregion // ICollection<KeyValuePair<TKey, ICollection<TValue>>> implementation
|
|
|
|
}
|
|
|
|
} // namespace Nuclex.Support.Collections
|