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