Click here to Skip to main content
15,885,244 members
Articles / Programming Languages / C#

Looking up items in HashTable/Dictionary objects that have multiple keys

Rate me:
Please Sign up or sign in to vote.
4.20/5 (8 votes)
1 May 2008CPOL4 min read 87.6K   585   23  
Dictionary objects take a single key as a look up key. This class simplifies using a Dictionary when you have multiple keys, such as two strings and an int, etc.
using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;
using Utility;
using System.Diagnostics;

namespace Testing
{
    [TestFixture]
    public class TestTestClassKey
    {
        /// <summary>
        /// Define test class to use key for
        /// </summary>
        public class TestClass
        {
            public string Column1 = null;
            public string Column2 = null;

            public TestClass(string Column1, string Column2)
            {
                this.Column1 = Column1;
                this.Column2 = Column2;
            }
        }

        //define key to use for test class
        public class TestClassKey : ClassKey<TestClass>
        {
            //Init with object
            public TestClassKey(TestClass ClassReference) : base(ClassReference) { }

            //return list of column values we need to use as a key
            public override object[] GetKeyValues()
            {
                return new object[] { 
                    ClassReference.Column1, 
                    ClassReference.Column2
                };
            }

        }


        public void TestClassKeyCompare(string Column1Orig, string Column2Orig, string Column1New, string Column2New)
        {
            TestClass model1 = new TestClass(Column1Orig, Column2Orig);
            TestClass model2 = new TestClass(Column1Orig, Column2Orig);

            Assert.AreEqual(new TestClassKey(model1), new TestClassKey(model2));
            Assert.IsTrue(new TestClassKey(model1) == new TestClassKey(model2));

            //change side of one and make sure not equal
            model1.Column1 = Column1New;
            model2.Column2 = Column2New;

            Assert.AreNotEqual(new TestClassKey(model1), new TestClassKey(model2));
            Assert.IsTrue(new TestClassKey(model1) != new TestClassKey(model2));
        }

        [Test]
        public void TestClassKeyDifferent()
        {
            TestClassKeyCompare(
                "one",
                "two",

                "two",
                "on"
            );
        }

        [Test]
        public void TestClassKeyXOROrderProblem()
        {
            TestClassKeyCompare(
                "one",
                "two",

                "two",
                "one"
            );
        }

        [Test]
        public void TestClassKeyNullProblem()
        {
            TestClassKeyCompare(
                null,
                "two",

                "two",
                "one"
            );
        }

        public class PerfResults { public long initMS; public long lookupMS; }

        public PerfResults GetTestPerf()
        {
            Dictionary<TestClassKey, TestClass> testLookups = new Dictionary<TestClassKey, TestClass>();
            Stopwatch stopWatch = new Stopwatch();
            PerfResults results = new PerfResults();

            //test initialization
            stopWatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                TestClass testClass = new TestClass("Column1-" + i, "Column2-" + i);
                testLookups.Add(new TestClassKey(testClass), testClass);
            }
            stopWatch.Stop();

            results.initMS = stopWatch.ElapsedMilliseconds;

            //test getting
            stopWatch = new Stopwatch();
            stopWatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                TestClass testClass = new TestClass("Column1-" + i, "Column2-" + i);
                if (!testLookups.ContainsKey(new TestClassKey(testClass)))
                    throw new ArgumentException("Can't find object we know is there");
            }
            stopWatch.Stop();

            results.lookupMS = stopWatch.ElapsedMilliseconds;

            return results;
        }

        [Test]
        public void TestPerf()
        {
            int testRuns = 10;
            PerfResults totalResults = new PerfResults();

            for (int i = 0; i < testRuns; i++)
            {
                PerfResults thisResult = GetTestPerf();
                totalResults.initMS += thisResult.initMS;
                totalResults.lookupMS += thisResult.lookupMS;
            }

            Debug.WriteLine("Initialization: " + (totalResults.initMS / testRuns).ToString("N0") + "ms");
            Debug.WriteLine("Lookups: " + (totalResults.lookupMS / testRuns).ToString("N0") + "ms");
        }

        //define class that uses directly instead of through third class
        public class TestClass2 : ClassKey<TestClass2>
        {
            public string Column1 = null;
            public string Column2 = null;

            public TestClass2(string Column1, string Column2)
                : this()
            {
                this.Column1 = Column1;
                this.Column2 = Column2;
            }

            //Init with object
            public TestClass2()
                : base()
            {
                this.ClassReference = this;
            }

            //return list of column values we need to use as a key
            public override object[] GetKeyValues()
            {
                return new object[] { 
                    ClassReference.Column1, 
                    ClassReference.Column2
                };
            }

        }

        [Test]
        public void TestEqual()
        {
            TestClass2 a = new TestClass2("one", "two" );
            TestClass2 b = new TestClass2("one", "two");
            TestClass2 c = new TestClass2("two", "one");
            TestClass2 d = null;
            TestClass2 e = null;

            Assert.IsTrue(a == b);
            Assert.IsFalse(a == c);
            Assert.IsFalse(a == d);
            Assert.IsTrue(d == e);
        }

        [Test]
        public void TestNotEqual()
        {
            TestClass2 a = new TestClass2("one", "two");
            TestClass2 b = new TestClass2("one", "two");
            TestClass2 c = new TestClass2("two", "one");
            TestClass2 d = null;
            TestClass2 e = null;

            Assert.IsFalse(a != b);
            Assert.IsTrue(a != c);
            Assert.IsTrue(a != d);
            Assert.IsFalse(d != e);
        }

        [Test]
        public void TestGetHashCode()
        {
            TestClass2 a = new TestClass2("one", "two");
            TestClass2 b = new TestClass2("one", "two");
            TestClass2 c = new TestClass2("two", "one");

            Dictionary<TestClass2, string> testItems = new Dictionary<TestClass2, string>();
            testItems.Add(a, "a");
            //can't add b since same as a
            testItems.Add(c, "c");

            Assert.AreEqual("a", testItems[a]);
            Assert.AreEqual("c", testItems[c]);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Comments and Discussions