|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
-
- namespace Svelto.DataStructures
- {
- public struct ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
- {
- public bool isInitialized { get { return _dictionary != null; } }
-
- /// <summary>
- /// Gets the element that has the specified key.
- /// </summary>
- /// <param name="key">The key of the element to get.</param>
- /// <returns>The element that has the specified key.</returns>
- /// <exception cref="ArgumentNullException">If <paramref name="key"/> is <see langword="null"/>.</exception>
- /// <exception cref="KeyNotFoundException">If property is retrieved and <paramref name="key"/> is not found.</exception>
- public TValue this[TKey key]
- {
- get
- {
- return _dictionary[key];
- }
- }
-
- /// <summary>
- /// Gets the number of items in the dictionary.
- /// </summary>
- /// <value>
- /// The number of items in the dictionary.
- /// </value>
- public int Count
- {
- get
- {
- return _dictionary.Count;
- }
- }
-
- /// <summary>
- /// Gets a key collection that contains the keys of the dictionary.
- /// </summary>
- /// <value>
- /// A key collection that contains the keys of the dictionary.
- /// </value>
- public KeyCollection Keys
- {
- get
- {
- return new KeyCollection(_dictionary.Keys);
- }
- }
-
- /// <summary>
- /// Gets a collection that contains the values in the dictionary.
- /// </summary>
- /// <value>
- /// A collection that contains the values in the object that implements <see cref="ReadOnlyDictionary{TKey, TValue}"/>.
- /// </value>
- public ValueCollection Values
- {
- get
- {
- return new ValueCollection(_dictionary.Values);
- }
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ReadOnlyDictionary{TKey, TValue}"/> class
- /// that is a wrapper around the specified dictionary.
- /// </summary>
- /// <param name="dictionary">The dictionary to wrap.</param>
- public ReadOnlyDictionary(Dictionary<TKey, TValue> dictionary)
- {
- if (dictionary == null)
- throw new ArgumentNullException("dictionary");
-
- _dictionary = dictionary;
- }
-
- /// <inheritdoc/>
- /// <summary>
- /// Gets the element that has the specified key.
- /// </summary>
- /// <exception cref="NotSupportedException">If the property is set.</exception>
- TValue IDictionary<TKey, TValue>.this[TKey key]
- {
- get
- {
- return this[key];
- }
-
- set
- {
- throw new NotSupportedException();
- }
- }
-
- /// <inheritdoc/>
- ICollection<TKey> IDictionary<TKey, TValue>.Keys
- {
- get
- {
- return Keys;
- }
- }
-
- /// <inheritdoc/>
- ICollection<TValue> IDictionary<TKey, TValue>.Values
- {
- get
- {
- return Values;
- }
- }
-
- /// <inheritdoc/>
- bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
- {
- get
- {
- return true;
- }
- }
-
- /// <inheritdoc/>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- /// <inheritdoc/>
- void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- bool IDictionary<TKey, TValue>.Remove(TKey key)
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- void ICollection<KeyValuePair<TKey, TValue>>.Clear()
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
- {
- throw new NotImplementedException();
- }
-
- /// <inheritdoc/>
- void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
- {
- throw new NotImplementedException();
- }
-
- /// <inheritdoc/>
- bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
- {
- throw new NotSupportedException();
- }
-
- bool IDictionary<TKey, TValue>.ContainsKey(TKey key)
- {
- return _dictionary.ContainsKey(key);
- }
-
- bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value)
- {
- return _dictionary.TryGetValue(key, out value);
- }
-
- int ICollection<KeyValuePair<TKey, TValue>>.Count
- {
- get { return _dictionary.Count; }
- }
-
- IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
- {
- return _dictionary.GetEnumerator();
- }
-
- /// <summary>
- /// Determines whether the dictionary contains an element that has the specified key.
- /// </summary>
- /// <param name="key">The key to locate in the dictionary.</param>
- /// <returns><see langword="true"/> if the dictionary contains an element that has the specified key; otherwise, <see langword="false"/>.</returns>
- public bool ContainsKey(TKey key)
- {
- return _dictionary.ContainsKey(key);
- }
-
- public DictionaryEnumerator GetEnumerator()
- {
- return new DictionaryEnumerator(_dictionary);
- }
-
- /// <summary>
- /// Retrieves the value that is associated with the specified key.
- /// </summary>
- /// <param name="key">The key whose value will be retrieved.</param>
- /// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.</param>
- /// <returns><see langword="true"/> if the object that implements <see cref="ReadOnlyDictionary{TKey, TValue}"/> contains an element with the specified key; otherwise, <see langword="false"/>.</returns>
- public bool TryGetValue(TKey key, out TValue value)
- {
- return _dictionary.TryGetValue(key, out value);
- }
-
- readonly Dictionary<TKey, TValue> _dictionary;
-
- /// <summary>
- /// Represents a read-only collection of the keys of a <see cref="ReadOnlyDictionary{TKey, TValue}"/> object.
- /// </summary>
- public struct KeyCollection : ICollection<TKey>, ICollection
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="KeyCollection"/> class
- /// as a wrapper around the specified collection of keys.
- /// </summary>
- /// <param name="keys">The collection of keys to wrap.</param>
- /// <exception cref="ArgumentNullException">If <paramref name="keys"/> is <see langword="null"/>.</exception>
- internal KeyCollection(ICollection<TKey> keys)
- {
- if (keys == null)
- throw new ArgumentNullException("keys");
-
- _keys = keys;
- }
-
- /// <inheritdoc/>
- bool ICollection.IsSynchronized
- {
- get
- {
- return false;
- }
- }
-
- /// <inheritdoc/>
- object ICollection.SyncRoot
- {
- get
- {
- throw new NotImplementedException();
- }
- }
-
- /// <inheritdoc/>
- void ICollection.CopyTo(Array array, int index)
- {
- throw new NotImplementedException();
- }
-
- /// <summary>
- /// Gets the number of elements in the collection.
- /// </summary>
- /// <value>
- /// The number of elements in the collection.
- /// </value>
- public int Count
- {
- get
- {
- return _keys.Count;
- }
- }
-
- /// <inheritdoc/>
- bool ICollection<TKey>.IsReadOnly
- {
- get
- {
- return true;
- }
- }
-
- /// <summary>
- /// Copies the elements of the collection to an array, starting at a specific array index.
- /// </summary>
- /// <param name="array">The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing.</param>
- /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
- /// <exception cref="ArgumentNullException">If <paramref name="array"/> is <see langword="null"/>.</exception>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="arrayIndex"/> is less than 0.</exception>
- /// <exception cref="ArgumentException">
- /// If <paramref name="array"/> is multidimensional.
- /// <para>-or-</para>
- /// <para>If the number of elements in the source collection is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</para>
- /// <para>-or-</para>
- /// <para>If the type <typeparamref name="TKey"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</para>
- /// </exception>
- public void CopyTo(TKey[] array, int arrayIndex)
- {
- _keys.CopyTo(array, arrayIndex);
- }
-
- /// <summary>
- /// Returns an enumerator that iterates through the collection.
- /// </summary>
- /// <returns>An enumerator that can be used to iterate through the collection.</returns>
- public IEnumerator<TKey> GetEnumerator()
- {
- return _keys.GetEnumerator();
- }
-
- /// <inheritdoc/>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- /// <inheritdoc/>
- bool ICollection<TKey>.Contains(TKey item)
- {
- return _keys.Contains(item);
- }
-
- /// <inheritdoc/>
- void ICollection<TKey>.Add(TKey item)
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- bool ICollection<TKey>.Remove(TKey item)
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- void ICollection<TKey>.Clear()
- {
- throw new NotSupportedException();
- }
-
- /// <summary>
- /// The wrapped collection of keys.
- /// </summary>
- readonly ICollection<TKey> _keys;
- }
-
- /// <summary>
- /// Represents a read-only collection of the values of a <see cref="ReadOnlyDictionary{TKey, TValue}"/> object.
- /// </summary>
- public struct ValueCollection : ICollection<TValue>, ICollection
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="ValueCollection"/> class
- /// as a wrapper around the specified collection of values.
- /// </summary>
- /// <param name="values">The collection of values to wrap.</param>
- /// <exception cref="ArgumentNullException">If <paramref name="values"/> is <see langword="null"/>.</exception>
- internal ValueCollection(ICollection<TValue> values)
- {
- if (values == null)
- throw new ArgumentNullException("values");
-
- _values = values;
- }
-
- /// <inheritdoc/>
- bool ICollection.IsSynchronized
- {
- get
- {
- return false;
- }
- }
-
- /// <inheritdoc/>
- object ICollection.SyncRoot
- {
- get
- {
- throw new NotImplementedException();
- }
- }
-
- /// <inheritdoc/>
- void ICollection.CopyTo(Array array, int index)
- {
- throw new NotImplementedException();
- }
-
- /// <summary>
- /// Gets the number of elements in the collection.
- /// </summary>
- /// <value>
- /// The number of elements in the collection.
- /// </value>
- public int Count
- {
- get
- {
- return _values.Count;
- }
- }
-
- /// <inheritdoc/>
- bool ICollection<TValue>.IsReadOnly
- {
- get
- {
- return true;
- }
- }
-
- /// <summary>
- /// Copies the elements of the collection to an array, starting at a specific array index.
- /// </summary>
- /// <param name="array">The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing.</param>
- /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
- /// <exception cref="ArgumentNullException">If <paramref name="array"/> is <see langword="null"/>.</exception>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="arrayIndex"/> is less than 0.</exception>
- /// <exception cref="ArgumentException">
- /// If <paramref name="array"/> is multidimensional.
- /// <para>-or-</para>
- /// <para>If the number of elements in the source collection is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</para>
- /// <para>-or-</para>
- /// <para>If the type <typeparamref name="TValue"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</para>
- /// </exception>
- public void CopyTo(TValue[] array, int arrayIndex)
- {
- _values.CopyTo(array, arrayIndex);
- }
-
- /// <summary>
- /// Returns an enumerator that iterates through the collection.
- /// </summary>
- /// <returns>An enumerator that can be used to iterate through the collection.</returns>
- public IEnumerator<TValue> GetEnumerator()
- {
- return _values.GetEnumerator();
- }
-
- /// <inheritdoc/>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- /// <inheritdoc/>
- bool ICollection<TValue>.Contains(TValue item)
- {
- return _values.Contains(item);
- }
-
- /// <inheritdoc/>
- void ICollection<TValue>.Add(TValue item)
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- bool ICollection<TValue>.Remove(TValue item)
- {
- throw new NotSupportedException();
- }
-
- /// <inheritdoc/>
- void ICollection<TValue>.Clear()
- {
- throw new NotSupportedException();
- }
-
- /// <summary>
- /// The wrapped collection of values.
- /// </summary>
- readonly ICollection<TValue> _values;
- }
-
- public struct DictionaryEnumerator:IEnumerator<KeyValuePair<TKey,TValue>>
- {
- /// <inheritdoc/>
- public TKey Key
- {
- get
- {
- return _enumerator.Current.Key;
- }
- }
-
- /// <inheritdoc/>
- public TValue Value
- {
- get
- {
- return _enumerator.Current.Value;
- }
- }
-
- public DictionaryEnumerator(IDictionary<TKey, TValue> dictionary)
- {
- if (dictionary == null)
- throw new ArgumentNullException("dictionary");
-
- _enumerator = dictionary.GetEnumerator();
- }
-
- /// <inheritdoc/>
- public KeyValuePair<TKey, TValue> Current
- {
- get
- {
- return _enumerator.Current;
- }
- }
-
- /// <inheritdoc/>
- public bool MoveNext()
- {
- return _enumerator.MoveNext();
- }
-
- /// <inheritdoc/>
- public void Reset()
- {
- _enumerator.Reset();
- }
-
- object IEnumerator.Current
- {
- get { return _enumerator.Current; }
- }
-
- public void Dispose()
- {
- _enumerator.Dispose();
- }
-
- readonly IEnumerator<KeyValuePair<TKey, TValue>> _enumerator;
- }
- }
- }
|