#if FAT using System; using System.Collections.Generic; using System.Linq; using LinqInternal.Core; namespace LinqInternal.Collections { [System.Serializable] [System.Diagnostics.DebuggerNonUserCode] [System.Diagnostics.DebuggerDisplay("Count={Count}")] internal sealed class ExtendedDictionary : IExtendedDictionary, IDictionary, ICollection> { private readonly IReadOnlyCollection _keysReadonly; private readonly IExtendedReadOnlyDictionary _readOnly; private readonly IEqualityComparer _valueComparer; private readonly IReadOnlyCollection _valuesReadonly; private readonly Dictionary _wrapped; public ExtendedDictionary() { _valueComparer = EqualityComparer.Default; _wrapped = new Dictionary(); _readOnly = new ExtendedReadOnlyDictionary(this); _keysReadonly = new Specialized.DelegatedCollection(() => _wrapped.Keys).AsReadOnly; _valuesReadonly = new Specialized.DelegatedCollection(() => _wrapped.Values).AsReadOnly; } public ExtendedDictionary(IEnumerable> prototype) { _valueComparer = EqualityComparer.Default; _wrapped = new Dictionary(); _readOnly = new ExtendedReadOnlyDictionary(this); _keysReadonly = new ExtendedReadOnlyCollection(_wrapped.Keys); _valuesReadonly = new ExtendedReadOnlyCollection(_wrapped.Values); if (prototype == null) { throw new ArgumentNullException("prototype"); } this.AddRange(prototype); } public ExtendedDictionary(IEnumerable> prototype, IEqualityComparer keyComparer) { _valueComparer = EqualityComparer.Default; if (keyComparer == null) { throw new ArgumentNullException("keyComparer"); } _wrapped = new Dictionary(keyComparer); _readOnly = new ExtendedReadOnlyDictionary(this); _keysReadonly = new ExtendedReadOnlyCollection(_wrapped.Keys); _valuesReadonly = new ExtendedReadOnlyCollection(_wrapped.Values); if (prototype == null) { throw new ArgumentNullException("prototype"); } this.AddRange(prototype); } public ExtendedDictionary(IEqualityComparer keyComparer) { _valueComparer = EqualityComparer.Default; if (keyComparer == null) { throw new ArgumentNullException("keyComparer"); } _wrapped = new Dictionary(keyComparer); _readOnly = new ExtendedReadOnlyDictionary(this); _keysReadonly = new ExtendedReadOnlyCollection(_wrapped.Keys); _valuesReadonly = new ExtendedReadOnlyCollection(_wrapped.Values); } public ExtendedDictionary(IEnumerable> prototype, IEqualityComparer keyComparer, IEqualityComparer valueComparer) { if (valueComparer == null) { throw new ArgumentNullException("valueComparer"); } _valueComparer = valueComparer; if (keyComparer == null) { throw new ArgumentNullException("keyComparer"); } _wrapped = new Dictionary(keyComparer); _readOnly = new ExtendedReadOnlyDictionary(this); _keysReadonly = new ExtendedReadOnlyCollection(_wrapped.Keys); _valuesReadonly = new ExtendedReadOnlyCollection(_wrapped.Values); if (prototype == null) { throw new ArgumentNullException("prototype"); } this.AddRange(prototype); } public ExtendedDictionary(IEqualityComparer keyComparer, IEqualityComparer valueComparer) { if (valueComparer == null) { throw new ArgumentNullException("valueComparer"); } _valueComparer = valueComparer; if (keyComparer == null) { throw new ArgumentNullException("keyComparer"); } _wrapped = new Dictionary(keyComparer); _readOnly = new ExtendedReadOnlyDictionary(this); _keysReadonly = new ExtendedReadOnlyCollection(_wrapped.Keys); _valuesReadonly = new ExtendedReadOnlyCollection(_wrapped.Values); } public IReadOnlyDictionary AsReadOnly { get { return _readOnly; } } public int Count { get { return _wrapped.Count; } } bool ICollection>.IsReadOnly { get { return false; } } ICollection IDictionary.Keys { get { return _wrapped.Keys; } } ICollection IDictionary.Values { get { return _wrapped.Values; } } IReadOnlyCollection> IExtendedCollection>.AsReadOnly { get { return _readOnly; } } IEnumerable IReadOnlyDictionary.Keys { get { return _keysReadonly; } } IEnumerable IReadOnlyDictionary.Values { get { return _valuesReadonly; } } public IReadOnlyCollection Keys { get { return _keysReadonly; } } public IReadOnlyCollection Values { get { return _valuesReadonly; } } public TValue this[TKey key] { get { return _wrapped[key]; } set { _wrapped[key] = value; } } public void Add(KeyValuePair item) { _wrapped.Add(item.Key, item.Value); } public void Add(TKey key, TValue value) { _wrapped.Add(key, value); } public void Clear() { _wrapped.Clear(); } public bool Contains(KeyValuePair item) { try { return _valueComparer.Equals(_wrapped[item.Key], item.Value); } catch (KeyNotFoundException) { return false; } } public bool Contains(KeyValuePair item, IEqualityComparer> comparer) { try { if (comparer == null) { throw new ArgumentNullException("comparer"); } return comparer.Equals(new KeyValuePair(item.Key, _wrapped[item.Key]), item); } catch (KeyNotFoundException) { return false; } } public bool ContainsKey(TKey key) { return _wrapped.ContainsKey(key); } public void CopyTo(KeyValuePair[] array) { Extensions.CanCopyTo(Count, array); Extensions.CopyTo(this, array); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { Extensions.CanCopyTo(Count, array, arrayIndex); Extensions.CopyTo(this, array, arrayIndex); } public void CopyTo(KeyValuePair[] array, int arrayIndex, int countLimit) { Extensions.CanCopyTo(array, arrayIndex, countLimit); Extensions.CopyTo(this, array, arrayIndex, countLimit); } public IEnumerator> GetEnumerator() { return _wrapped.GetEnumerator(); } public bool Remove(KeyValuePair item) { TKey key = item.Key; try { if (_valueComparer.Equals(_wrapped[key], item.Value)) { return _wrapped.Remove(key); } else { return false; } } catch (KeyNotFoundException) { return false; } } public bool Remove(TKey key) { return _wrapped.Remove(key); } public bool Remove(KeyValuePair item, IEqualityComparer> comparer) { return this.RemoveWhereEnumerable(input => comparer.Equals(input, item)).Any(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } public bool TryGetValue(TKey key, out TValue value) { return _wrapped.TryGetValue(key, out value); } public void UnionWith(IEnumerable> other) { Extensions.UnionWith(this, other); } } } #endif