Пример #1
0
        internal void Add(TKey key, TElement element)
        {
            //TODO # Lookup.Add() optimization
            // # Before adding a new key, TryGetValue() method is used to obtain grouping if the key already exists
            // # Using TryGetValue() triggers an extra call to GetHashCode() of comparer
            // # Lookup.Add() method requires an optimized adding mechanism
            IGrouping <TKey, TElement> grouping;

            if (!mKeyGroupPairs.TryGetValue(key, out grouping))
            {
                grouping = new Grouping <TKey, TElement>(key);
                mKeyGroupPairs.Add(key, grouping);
            }

            ((Grouping <TKey, TElement>)grouping).Add(element);
        }
        public void NullableKeyDictionaryTest()
        {
            int value = 0;
            IDictionary <string, int> target = new NullableKeyDictionary <string, int>();

            target.Add(null, 13);
            target.Add(new KeyValuePair <string, int>("a", 14));
            Assert.IsTrue(target.ContainsKey(null));
            Assert.IsTrue(target.ContainsKey("a"));
            Assert.IsTrue(target.Contains(new KeyValuePair <string, int>(null, 13)));
            Assert.IsTrue(target.Contains(new KeyValuePair <string, int>("a", 14)));
            Assert.IsTrue(target.Keys.Contains(null));
            Assert.IsTrue(target.Keys.Contains("a"));
            Assert.IsTrue(target.Values.Contains(13));
            Assert.IsTrue(target.Values.Contains(14));
            Assert.AreEqual(target.Count, 2);

            Assert.IsTrue(target.TryGetValue(null, out value));
            Assert.AreEqual(value, 13);
            Assert.IsTrue(target.TryGetValue("a", out value));
            Assert.AreEqual(value, 14);

            foreach (KeyValuePair <string, int> item in target)
            {
                Console.WriteLine(item);
            }

            Assert.IsTrue(target.Remove(null));
            Assert.IsFalse(target.Remove(new KeyValuePair <string, int>(null, 13)));
            Assert.IsTrue(target.Remove("a"));
            Assert.IsFalse(target.Remove(new KeyValuePair <string, int>("a", 14)));

            foreach (KeyValuePair <string, int> item in target)
            {
                Console.WriteLine(item);
            }
        }