| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- //-----------------------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- namespace System.Runtime.Collections
- {
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Runtime;
- class NullableKeyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
- {
- bool isNullKeyPresent;
- TValue nullKeyValue;
- IDictionary<TKey, TValue> innerDictionary;
- public NullableKeyDictionary()
- : base()
- {
- this.innerDictionary = new Dictionary<TKey, TValue>();
- }
- public int Count
- {
- get { return this.innerDictionary.Count + (this.isNullKeyPresent ? 1 : 0); }
- }
- public bool IsReadOnly
- {
- get { return false; }
- }
- public ICollection<TKey> Keys
- {
- get
- {
- return new NullKeyDictionaryKeyCollection<TKey, TValue>(this);
- }
- }
- public ICollection<TValue> Values
- {
- get { return new NullKeyDictionaryValueCollection<TKey, TValue>(this); }
- }
- public TValue this[TKey key]
- {
- get
- {
- if (key == null)
- {
- if (this.isNullKeyPresent)
- {
- return this.nullKeyValue;
- }
- else
- {
- throw Fx.Exception.AsError(new KeyNotFoundException());
- }
- }
- else
- {
- return this.innerDictionary[key];
- }
- }
- set
- {
- if (key == null)
- {
- this.isNullKeyPresent = true;
- this.nullKeyValue = value;
- }
- else
- {
- this.innerDictionary[key] = value;
- }
- }
- }
- public void Add(TKey key, TValue value)
- {
- if (key == null)
- {
- if (this.isNullKeyPresent)
- {
- throw Fx.Exception.Argument("key", InternalSR.NullKeyAlreadyPresent);
- }
- this.isNullKeyPresent = true;
- this.nullKeyValue = value;
- }
- else
- {
- this.innerDictionary.Add(key, value);
- }
- }
- public bool ContainsKey(TKey key)
- {
- return key == null ? this.isNullKeyPresent : this.innerDictionary.ContainsKey(key);
- }
- public bool Remove(TKey key)
- {
- if (key == null)
- {
- bool result = this.isNullKeyPresent;
- this.isNullKeyPresent = false;
- this.nullKeyValue = default(TValue);
- return result;
- }
- else
- {
- return this.innerDictionary.Remove(key);
- }
- }
- public bool TryGetValue(TKey key, out TValue value)
- {
- if (key == null)
- {
- if (this.isNullKeyPresent)
- {
- value = this.nullKeyValue;
- return true;
- }
- else
- {
- value = default(TValue);
- return false;
- }
- }
- else
- {
- return this.innerDictionary.TryGetValue(key, out value);
- }
- }
- public void Add(KeyValuePair<TKey, TValue> item)
- {
- Add(item.Key, item.Value);
- }
- public void Clear()
- {
- this.isNullKeyPresent = false;
- this.nullKeyValue = default(TValue);
- this.innerDictionary.Clear();
- }
- public bool Contains(KeyValuePair<TKey, TValue> item)
- {
- if (item.Key == null)
- {
- if (this.isNullKeyPresent)
- {
- return item.Value == null ? this.nullKeyValue == null : item.Value.Equals(this.nullKeyValue);
- }
- else
- {
- return false;
- }
- }
- else
- {
- return this.innerDictionary.Contains(item);
- }
- }
- public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
- {
- this.innerDictionary.CopyTo(array, arrayIndex);
- if (this.isNullKeyPresent)
- {
- array[arrayIndex + this.innerDictionary.Count] = new KeyValuePair<TKey, TValue>(default(TKey), this.nullKeyValue);
- }
- }
- public bool Remove(KeyValuePair<TKey, TValue> item)
- {
- if (item.Key == null)
- {
- if (this.Contains(item))
- {
- this.isNullKeyPresent = false;
- this.nullKeyValue = default(TValue);
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return this.innerDictionary.Remove(item);
- }
- }
- public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
- {
- IEnumerator<KeyValuePair<TKey, TValue>> innerEnumerator = this.innerDictionary.GetEnumerator() as IEnumerator<KeyValuePair<TKey, TValue>>;
- while (innerEnumerator.MoveNext())
- {
- yield return innerEnumerator.Current;
- }
- if (this.isNullKeyPresent)
- {
- yield return new KeyValuePair<TKey, TValue>(default(TKey), this.nullKeyValue);
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IEnumerable<KeyValuePair<TKey, TValue>>)this).GetEnumerator();
- }
- class NullKeyDictionaryKeyCollection<TypeKey, TypeValue> : ICollection<TypeKey>
- {
- NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary;
- public NullKeyDictionaryKeyCollection(NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary)
- {
- this.nullKeyDictionary = nullKeyDictionary;
- }
- public int Count
- {
- get
- {
- int count = this.nullKeyDictionary.innerDictionary.Keys.Count;
- if (this.nullKeyDictionary.isNullKeyPresent)
- {
- count++;
- }
- return count;
- }
- }
- public bool IsReadOnly
- {
- get { return true; }
- }
- public void Add(TypeKey item)
- {
- throw Fx.Exception.AsError(new NotSupportedException(InternalSR.KeyCollectionUpdatesNotAllowed));
- }
- public void Clear()
- {
- throw Fx.Exception.AsError(new NotSupportedException(InternalSR.KeyCollectionUpdatesNotAllowed));
- }
- public bool Contains(TypeKey item)
- {
- return item == null ? this.nullKeyDictionary.isNullKeyPresent : this.nullKeyDictionary.innerDictionary.Keys.Contains(item);
- }
- public void CopyTo(TypeKey[] array, int arrayIndex)
- {
- this.nullKeyDictionary.innerDictionary.Keys.CopyTo(array, arrayIndex);
- if (this.nullKeyDictionary.isNullKeyPresent)
- {
- array[arrayIndex + this.nullKeyDictionary.innerDictionary.Keys.Count] = default(TypeKey);
- }
- }
- public bool Remove(TypeKey item)
- {
- throw Fx.Exception.AsError(new NotSupportedException(InternalSR.KeyCollectionUpdatesNotAllowed));
- }
- public IEnumerator<TypeKey> GetEnumerator()
- {
- foreach (TypeKey item in this.nullKeyDictionary.innerDictionary.Keys)
- {
- yield return item;
- }
- if (this.nullKeyDictionary.isNullKeyPresent)
- {
- yield return default(TypeKey);
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IEnumerable<TypeKey>)this).GetEnumerator();
- }
- }
- class NullKeyDictionaryValueCollection<TypeKey, TypeValue> : ICollection<TypeValue>
- {
- NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary;
- public NullKeyDictionaryValueCollection(NullableKeyDictionary<TypeKey, TypeValue> nullKeyDictionary)
- {
- this.nullKeyDictionary = nullKeyDictionary;
- }
- public int Count
- {
- get
- {
- int count = this.nullKeyDictionary.innerDictionary.Values.Count;
- if (this.nullKeyDictionary.isNullKeyPresent)
- {
- count++;
- }
- return count;
- }
- }
- public bool IsReadOnly
- {
- get { return true; }
- }
- public void Add(TypeValue item)
- {
- throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ValueCollectionUpdatesNotAllowed));
- }
- public void Clear()
- {
- throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ValueCollectionUpdatesNotAllowed));
- }
- public bool Contains(TypeValue item)
- {
- return this.nullKeyDictionary.innerDictionary.Values.Contains(item) ||
- (this.nullKeyDictionary.isNullKeyPresent && this.nullKeyDictionary.nullKeyValue.Equals(item));
- }
- public void CopyTo(TypeValue[] array, int arrayIndex)
- {
- this.nullKeyDictionary.innerDictionary.Values.CopyTo(array, arrayIndex);
- if (this.nullKeyDictionary.isNullKeyPresent)
- {
- array[arrayIndex + this.nullKeyDictionary.innerDictionary.Values.Count] = this.nullKeyDictionary.nullKeyValue;
- }
- }
- public bool Remove(TypeValue item)
- {
- throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ValueCollectionUpdatesNotAllowed));
- }
- public IEnumerator<TypeValue> GetEnumerator()
- {
- foreach (TypeValue item in this.nullKeyDictionary.innerDictionary.Values)
- {
- yield return item;
- }
- if (this.nullKeyDictionary.isNullKeyPresent)
- {
- yield return this.nullKeyDictionary.nullKeyValue;
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IEnumerable<TypeValue>)this).GetEnumerator();
- }
- }
- }
- }
|