123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- #pragma warning disable CS8601 // Possible null reference assignment.
- #pragma warning disable CS8602 // Dereference of a possibly null reference.
- #pragma warning disable CS8603 // Possible null reference return.
- #pragma warning disable CS8604 // Possible null reference argument.
- #pragma warning disable CS8605 // Unboxing a possibly null value.
- #pragma warning disable CS8618 // Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
- #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
- namespace EasyDevCore.Common.Wrapper
- {
- /// <summary>
- /// Represents a collection of keys and values. You can add multiple values to the same key.
- /// </summary>
- /// <typeparam name="TKey">The type of the key.</typeparam>
- /// <typeparam name="TValue">The type of the value.</typeparam>
- public class MultiDictionary<TKey, TValue> : IDictionary<TKey, TValue>
- {
- #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
- private Dictionary<TKey, List<TValue>> _Dictionary = null;
- #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
- /// <summary>
- /// Initializes a new instance of the <see cref="MultiDictionary<TKey, TValue>"/> class.
- /// </summary>
- public MultiDictionary()
- {
- #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
- _Dictionary = new Dictionary<TKey, List<TValue>>();
- #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
- }
- /// <summary>
- /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </summary>
- /// <param name="key">The object to use as the key of the element to add.</param>
- /// <param name="value">The object to use as the value of the element to add.</param>
- /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
- /// </exception>
- ///
- /// <exception cref="T:System.ArgumentException">
- /// An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </exception>
- ///
- /// <exception cref="T:System.NotSupportedException">
- /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
- /// </exception>
- public void Add(TKey key, TValue value)
- {
- List<TValue> list;
- if (_Dictionary.TryGetValue(key, out list))
- {
- list.Add(value);
- }
- else
- {
- list = new List<TValue>();
- list.Add(value);
- _Dictionary.Add(key, list);
- }
- }
- /// <summary>
- /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key.
- /// </summary>
- /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</param>
- /// <returns>
- /// true if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false.
- /// </returns>
- /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
- /// </exception>
- public bool ContainsKey(TKey key)
- {
- return _Dictionary.ContainsKey(key);
- }
- /// <summary>
- /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </summary>
- /// <param name="key">The key of the element to remove.</param>
- /// <returns>
- /// true if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </returns>
- /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
- /// </exception>
- ///
- /// <exception cref="T:System.NotSupportedException">
- /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
- /// </exception>
- public bool Remove(TKey key)
- {
- return _Dictionary.Remove(key);
- }
- /// <summary>
- /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </summary>
- /// <returns>
- /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </returns>
- public ICollection<TKey> Keys
- {
- get
- {
- return _Dictionary.Keys;
- }
- }
- /// <summary>
- /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </summary>
- /// <returns>
- /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
- /// </returns>
- public ICollection<TValue> Values
- {
- get
- {
- List<TValue> values = new List<TValue>();
- #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
- using (Dictionary<TKey, List<TValue>>.Enumerator enumerator = _Dictionary.GetEnumerator())
- #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
- {
- while (enumerator.MoveNext())
- {
- values.AddRange(enumerator.Current.Value);
- }
- }
- return values;
- }
- }
- bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value)
- {
- throw new NotSupportedException("TryGetValue is not supported");
- }
- /// <summary>
- /// Gets or sets the element with the specified key.
- /// </summary>
- /// <returns>
- /// The element with the specified key.
- /// </returns>
- ///
- /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
- /// </exception>
- ///
- /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">
- /// The property is retrieved and <paramref name="key"/> is not found.
- /// </exception>
- ///
- /// <exception cref="T:System.NotSupportedException">
- /// The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
- /// </exception>
- TValue IDictionary<TKey, TValue>.this[TKey key]
- {
- get
- {
- return _Dictionary[key][0];
- }
- set
- {
- _Dictionary[key][0] = value;
- }
- }
- /// <summary>
- /// Gets or sets the element with the specified key.
- /// </summary>
- /// <returns>
- /// The element with the specified key.
- /// </returns>
- ///
- /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
- /// </exception>
- ///
- /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">
- /// The property is retrieved and <paramref name="key"/> is not found.
- /// </exception>
- ///
- /// <exception cref="T:System.NotSupportedException">
- /// The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
- /// </exception>
- public IList<TValue> this[TKey key]
- {
- get
- {
- return _Dictionary[key];
- }
- }
- /// <summary>
- /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
- /// </summary>
- /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
- /// <exception cref="T:System.NotSupportedException">
- /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
- /// </exception>
- public void Add(KeyValuePair<TKey, TValue> item)
- {
- Add(item.Key, item.Value);
- }
- /// <summary>
- /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
- /// </summary>
- /// <exception cref="T:System.NotSupportedException">
- /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
- /// </exception>
- public void Clear()
- {
- _Dictionary.Clear();
- }
- /// <summary>
- /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
- /// </summary>
- /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
- /// <returns>
- /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
- /// </returns>
- public bool Contains(KeyValuePair<TKey, TValue> item)
- {
- List<TValue> list;
- if (!_Dictionary.TryGetValue(item.Key, out list))
- {
- return false;
- }
- else
- {
- return list.Contains(item.Value);
- }
- }
- /// <summary>
- /// Copies to.
- /// </summary>
- /// <param name="array">The array.</param>
- /// <param name="arrayIndex">Index of the array.</param>
- public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
- {
- if (array == null)
- throw new ArgumentNullException("array");
- if (arrayIndex < 0 || arrayIndex > array.Length)
- throw new ArgumentOutOfRangeException("array index out of range");
- if (array.Length - arrayIndex < this.Count)
- throw new ArgumentException("Array too small");
- Dictionary<TKey, List<TValue>>.Enumerator enumerator = _Dictionary.GetEnumerator();
- while (enumerator.MoveNext())
- {
- KeyValuePair<TKey, List<TValue>> mapPair = enumerator.Current;
- foreach (TValue val in mapPair.Value)
- {
- array[arrayIndex++] = new KeyValuePair<TKey, TValue>(mapPair.Key, val);
- }
- }
- }
- /// <summary>
- /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
- /// </summary>
- /// <returns>
- /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
- /// </returns>
- public int Count
- {
- get
- {
- int count = 0;
- Dictionary<TKey, List<TValue>>.Enumerator enumerator = _Dictionary.GetEnumerator();
- while (enumerator.MoveNext())
- {
- KeyValuePair<TKey, List<TValue>> pair = enumerator.Current;
- count += pair.Value.Count;
- }
- return count;
- }
- }
- /// <summary>
- /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
- /// </summary>
- /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
- /// </returns>
- public bool IsReadOnly
- {
- get { return false; }
- }
- /// <summary>
- /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
- /// </summary>
- /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
- /// <returns>
- /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
- /// </returns>
- /// <exception cref="T:System.NotSupportedException">
- /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
- /// </exception>
- public bool Remove(KeyValuePair<TKey, TValue> item)
- {
- List<TValue> list;
- if (_Dictionary.TryGetValue(item.Key, out list))
- {
- return list.Remove(item.Value);
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// Returns an enumerator that iterates through the collection.
- /// </summary>
- /// <returns>
- /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
- /// </returns>
- public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
- {
- Dictionary<TKey, List<TValue>>.Enumerator enumerateKeys = _Dictionary.GetEnumerator();
- while (enumerateKeys.MoveNext())
- {
- foreach (TValue val in enumerateKeys.Current.Value)
- {
- KeyValuePair<TKey, TValue> pair = new KeyValuePair<TKey, TValue>(
- enumerateKeys.Current.Key, val);
- yield return pair;
- }
- }
- }
- /// <summary>
- /// Returns an enumerator that iterates through a collection.
- /// </summary>
- /// <returns>
- /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
- /// </returns>
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- }
|