コード例 #1
0
        public bool Remove(TKey key)
        {
            switch (this.backingType)
            {
            case Microsoft.Build.Collections.HybridDictionaryBackingType.None:
                return(false);

            case Microsoft.Build.Collections.HybridDictionaryBackingType.Single:
                if (!this.comparer.Equals(this.valueSingle.Key, key))
                {
                    return(false);
                }
                this.valueSingle = new KeyValuePair <TKey, TValue>();
                this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.None;
                return(true);

            case Microsoft.Build.Collections.HybridDictionaryBackingType.List:
                return(this.TryRemoveFromList(key));

            case Microsoft.Build.Collections.HybridDictionaryBackingType.Dictionary:
                return(this.valuesDict.Remove(key));
            }
            Microsoft.Build.Shared.ErrorUtilities.ThrowInternalErrorUnreachable();
            return(false);
        }
コード例 #2
0
 public void Clear()
 {
     this.valuesDict  = null;
     this.valuesList  = null;
     this.valueSingle = new KeyValuePair <TKey, TValue>();
     this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.None;
 }
コード例 #3
0
        public void Add(TKey key, TValue value)
        {
            Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(key, "key");
            switch (this.backingType)
            {
            case Microsoft.Build.Collections.HybridDictionaryBackingType.None:
                this.valueSingle = new KeyValuePair <TKey, TValue>(key, value);
                this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.Single;
                return;

            case Microsoft.Build.Collections.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 Microsoft.Build.Collections.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 == Microsoft.Build.Collections.HybridDictionaryBackingType.List)
                {
                    this.valuesList.Add(new KeyValuePair <TKey, TValue>(key, value));
                    return;
                }
                this.valuesDict.Add(key, value);
                return;

            case Microsoft.Build.Collections.HybridDictionaryBackingType.Dictionary:
                this.valuesDict.Add(key, value);
                return;
            }
        }
コード例 #4
0
 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 > Microsoft.Build.Collections.HybridDictionary <TKey, TValue> .MaxListSize)
     {
         this.valuesDict  = new Dictionary <TKey, TValue>(comparer);
         this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.Dictionary;
     }
     else if (capacity > 1)
     {
         this.valuesList  = new List <KeyValuePair <TKey, TValue> >(capacity);
         this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.List;
     }
     else
     {
         this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.None;
     }
 }
コード例 #5
0
 private void GrowForAdd()
 {
     Microsoft.Build.Shared.ErrorUtilities.VerifyThrow(this.backingType != Microsoft.Build.Collections.HybridDictionaryBackingType.Dictionary, "Backing type is already dictionary");
     if (this.backingType == Microsoft.Build.Collections.HybridDictionaryBackingType.List)
     {
         if (this.valuesList.Count >= Microsoft.Build.Collections.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 = Microsoft.Build.Collections.HybridDictionaryBackingType.Dictionary;
         }
     }
     else
     {
         this.valuesList = new List <KeyValuePair <TKey, TValue> >();
         this.valuesList.Add(this.valueSingle);
         this.valueSingle = new KeyValuePair <TKey, TValue>();
         this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.List;
     }
 }
コード例 #6
0
        public TValue this[TKey key]
        {
            get
            {
                switch (this.backingType)
                {
                case Microsoft.Build.Collections.HybridDictionaryBackingType.None:
                    break;

                case Microsoft.Build.Collections.HybridDictionaryBackingType.Single:
                    if (!this.comparer.Equals(this.valueSingle.Key, key))
                    {
                        break;
                    }
                    return(this.valueSingle.Value);

                case Microsoft.Build.Collections.HybridDictionaryBackingType.List:
                {
                    KeyValuePair <TKey, TValue>?nullable = this.FindEntryInList(key);
                    if (!nullable.HasValue)
                    {
                        break;
                    }
                    return(nullable.Value.Value);
                }

                case Microsoft.Build.Collections.HybridDictionaryBackingType.Dictionary:
                    return(this.valuesDict[key]);

                default:
                    Microsoft.Build.Shared.ErrorUtilities.ThrowInternalErrorUnreachable();
                    break;
                }
                throw new KeyNotFoundException("The specified key was not found in the collection.");
            }
            set
            {
                switch (this.backingType)
                {
                case Microsoft.Build.Collections.HybridDictionaryBackingType.None:
                    this.valueSingle = new KeyValuePair <TKey, TValue>(key, value);
                    this.backingType = Microsoft.Build.Collections.HybridDictionaryBackingType.Single;
                    return;

                case Microsoft.Build.Collections.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 Microsoft.Build.Collections.HybridDictionaryBackingType.List:
                    this.TryRemoveFromList(key);
                    this.Add(key, value);
                    return;

                case Microsoft.Build.Collections.HybridDictionaryBackingType.Dictionary:
                    this.valuesDict[key] = value;
                    return;
                }
                Microsoft.Build.Shared.ErrorUtilities.ThrowInternalErrorUnreachable();
            }
        }