예제 #1
0
        public void MultiDictionaryVsDictionaryInsertBasic1()
        {
            Dictionary <TestKey <String>, int>      dict  = new Dictionary <TestKey <String>, int>();
            MultiDictionary <TestKey <String>, int> mDict = new MultiDictionary <TestKey <String>, int>(new TestKeyComparer <String>());

            TestKey <String> a = new TestKey <String>(1, "a");
            TestKey <String> b = new TestKey <String>(1, "b");

            dict.Add(a, 1);
            try
            {
                dict.Add(b, 2);
                Assert.Fail("ArgumentException not thrown");
            }
            catch (ArgumentException)
            {
                //Ignore and continue
            }
            mDict.Add(a, 1);
            mDict.Add(b, 2);

            Assert.AreEqual(1, dict.Count);
            Assert.AreEqual(1, dict[a]);
            Assert.AreEqual(1, dict[b]);
            Assert.AreEqual(2, mDict.Count);
            Assert.AreEqual(1, mDict[a]);
            Assert.AreEqual(2, mDict[b]);
        }
예제 #2
0
        public void MultiDictionaryVsDictionaryLookupPool2()
        {
            Dictionary <TestKey <int>, int>      dict  = new Dictionary <TestKey <int>, int>(new TestKeyComparer <int>());
            MultiDictionary <TestKey <int>, int> mDict = new MultiDictionary <TestKey <int>, int>(new TestKeyComparer <int>());

            //Build dictionaries with 10000 keys in them
            List <TestKey <int> > keys = new List <TestKey <int> >();

            for (int i = 0; i < 10000; i++)
            {
                TestKey <int> key = new TestKey <int>(i % 100, i);
                keys.Add(key);
                dict.Add(key, i);
                mDict.Add(key, i);
            }

            Stopwatch timer = new Stopwatch();

            //Lookup all keys in multi-dictionary
            timer.Start();
            foreach (TestKey <int> key in keys)
            {
                mDict.ContainsKey(key);
            }
            timer.Stop();

            TimeSpan mDictTime = timer.Elapsed;

            Console.WriteLine("MultiDictionary took " + timer.Elapsed);

            timer.Reset();

            //Lookup all keys in dictionary
            timer.Start();
            foreach (TestKey <int> key in keys)
            {
                dict.ContainsKey(key);
            }
            timer.Stop();

            TimeSpan dictTime = timer.Elapsed;

            Console.WriteLine("Dictionary took " + timer.Elapsed);

            Assert.IsTrue(mDictTime < dictTime);
        }
예제 #3
0
        public void MultiDictionaryVsDictionaryInsertNormal1(int numKeys)
        {
            Dictionary <TestKey <int>, int>      dict  = new Dictionary <TestKey <int>, int>(new TestKeyComparer <int>());
            MultiDictionary <TestKey <int>, int> mDict = new MultiDictionary <TestKey <int>, int>(new TestKeyComparer <int>());

            //Generate some number of keys
            List <TestKey <int> > keys = new List <TestKey <int> >();

            for (int i = 0; i < numKeys; i++)
            {
                TestKey <int> key = new TestKey <int>(i, i);
                keys.Add(key);
            }

            Stopwatch timer = new Stopwatch();

            //Add to dictionary
            timer.Start();
            foreach (TestKey <int> key in keys)
            {
                dict.Add(key, key.Value);
            }
            timer.Stop();

            TimeSpan dictTime = timer.Elapsed;

            Console.WriteLine("Dictionary took " + timer.Elapsed);

            timer.Reset();

            //Add to multi-dictionary
            timer.Start();
            foreach (TestKey <int> key in keys)
            {
                mDict.Add(key, key.Value);
            }
            timer.Stop();

            TimeSpan mDictTime = timer.Elapsed;

            Console.WriteLine("MultiDictionary took " + timer.Elapsed);

            Assert.IsTrue(mDictTime > dictTime);
        }
예제 #4
0
 public int Compare(TestKey <T> x, TestKey <T> y)
 {
     if (x == null)
     {
         if (y == null)
         {
             return(0);
         }
         return(-1);
     }
     else if (y == null)
     {
         return(1);
     }
     else
     {
         return(x.Value.CompareTo(y.Value));
     }
 }
예제 #5
0
        public void MultiDictionaryVsDictionaryInsertBasic2()
        {
            Dictionary <TestKey <String>, int>      dict  = new Dictionary <TestKey <String>, int>(new TestKeyComparer <String>());
            MultiDictionary <TestKey <String>, int> mDict = new MultiDictionary <TestKey <String>, int>(new TestKeyComparer <String>());

            TestKey <String> a = new TestKey <String>(1, "a");
            TestKey <String> b = new TestKey <String>(1, "b");

            dict.Add(a, 1);
            dict.Add(b, 2);
            mDict.Add(a, 1);
            mDict.Add(b, 2);

            Assert.AreEqual(2, dict.Count);
            Assert.AreEqual(1, dict[a]);
            Assert.AreEqual(2, dict[b]);
            Assert.AreEqual(2, mDict.Count);
            Assert.AreEqual(1, mDict[a]);
            Assert.AreEqual(2, mDict[b]);
        }
예제 #6
0
 public int GetHashCode(TestKey <T> obj)
 {
     return(obj.GetHashCode());
 }
예제 #7
0
 public bool Equals(TestKey <T> x, TestKey <T> y)
 {
     return(this.Compare(x, y) == 0);
 }