public bool Remove(TKey key) { switch (this.backingType) { case HybridDictionaryBackingType.None: return(false); case HybridDictionaryBackingType.Single: if (!this.comparer.Equals(this.valueSingle.Key, key)) { return(false); } this.valueSingle = new KeyValuePair <TKey, TValue>(); this.backingType = HybridDictionaryBackingType.None; return(true); case HybridDictionaryBackingType.List: return(this.TryRemoveFromList(key)); case HybridDictionaryBackingType.Dictionary: return(this.valuesDict.Remove(key)); } ErrorUtilities.ThrowInternalErrorUnreachable(); return(false); }
public void Clear() { this.valuesDict = null; this.valuesList = null; this.valueSingle = new KeyValuePair <TKey, TValue>(); this.backingType = HybridDictionaryBackingType.None; }
public void Add(TKey key, TValue value) { ErrorUtilities.VerifyThrowArgumentNull(key, "key"); switch (this.backingType) { case HybridDictionaryBackingType.None: this.valueSingle = new KeyValuePair <TKey, TValue>(key, value); this.backingType = HybridDictionaryBackingType.Single; return; case HybridDictionaryBackingType.Single: if (this.comparer.Equals(this.valueSingle.Key, key)) { throw new ArgumentException("A value with the same key is already in the collection."); } this.GrowForAdd(); this.valuesList.Add(new KeyValuePair <TKey, TValue>(key, value)); return; case HybridDictionaryBackingType.List: if (this.ContainsKey(key)) { throw new ArgumentException("A value with the same key is already in the collection."); } this.GrowForAdd(); if (this.backingType == HybridDictionaryBackingType.List) { this.valuesList.Add(new KeyValuePair <TKey, TValue>(key, value)); return; } this.valuesDict.Add(key, value); return; case HybridDictionaryBackingType.Dictionary: this.valuesDict.Add(key, value); return; } }
public HybridDictionary(int capacity, IEqualityComparer <TKey> comparer) { this.syncRoot = new object(); this.comparer = comparer; if (this.comparer == null) { this.comparer = EqualityComparer <TKey> .Default; } if (capacity > HybridDictionary <TKey, TValue> .MaxListSize) { this.valuesDict = new Dictionary <TKey, TValue>(comparer); this.backingType = HybridDictionaryBackingType.Dictionary; } else if (capacity > 1) { this.valuesList = new List <KeyValuePair <TKey, TValue> >(capacity); this.backingType = HybridDictionaryBackingType.List; } else { this.backingType = HybridDictionaryBackingType.None; } }
private void GrowForAdd() { ErrorUtilities.VerifyThrow(this.backingType != HybridDictionaryBackingType.Dictionary, "Backing type is already dictionary"); if (this.backingType == HybridDictionaryBackingType.List) { if (this.valuesList.Count >= HybridDictionary <TKey, TValue> .MaxListSize) { this.valuesDict = new Dictionary <TKey, TValue>(this.valuesList.Count * 2, this.comparer); foreach (KeyValuePair <TKey, TValue> pair in this.valuesList) { this.valuesDict.Add(pair.Key, pair.Value); } this.valuesList = null; this.backingType = HybridDictionaryBackingType.Dictionary; } } else { this.valuesList = new List <KeyValuePair <TKey, TValue> >(); this.valuesList.Add(this.valueSingle); this.valueSingle = new KeyValuePair <TKey, TValue>(); this.backingType = HybridDictionaryBackingType.List; } }
public TValue this[TKey key] { get { switch (this.backingType) { case HybridDictionaryBackingType.None: break; case HybridDictionaryBackingType.Single: if (!this.comparer.Equals(this.valueSingle.Key, key)) { break; } return(this.valueSingle.Value); case HybridDictionaryBackingType.List: { KeyValuePair <TKey, TValue>?nullable = this.FindEntryInList(key); if (!nullable.HasValue) { break; } return(nullable.Value.Value); } case HybridDictionaryBackingType.Dictionary: return(this.valuesDict[key]); default: ErrorUtilities.ThrowInternalErrorUnreachable(); break; } throw new KeyNotFoundException("The specified key was not found in the collection."); } set { switch (this.backingType) { case HybridDictionaryBackingType.None: this.valueSingle = new KeyValuePair <TKey, TValue>(key, value); this.backingType = HybridDictionaryBackingType.Single; return; case HybridDictionaryBackingType.Single: if (!this.comparer.Equals(this.valueSingle.Key, key)) { this.Add(key, value); return; } this.valueSingle = new KeyValuePair <TKey, TValue>(key, value); return; case HybridDictionaryBackingType.List: this.TryRemoveFromList(key); this.Add(key, value); return; case HybridDictionaryBackingType.Dictionary: this.valuesDict[key] = value; return; } ErrorUtilities.ThrowInternalErrorUnreachable(); } }