예제 #1
0
        public static int GetUnsequencedHashCode <T>(this SCG.ICollection <T> collection, SCG.IEqualityComparer <T> equalityComparer = null)
        {
            if (collection == null)
            {
                return(0); // TODO: Better default value? H1/H2/H3?
            }

            if (equalityComparer == null)
            {
                equalityComparer = SCG.EqualityComparer <T> .Default;
            }

            var hashCode = 0; // TODO: Better intial value?

            // Does not use Linq.Sum() as it throws an exception if it overflows
            foreach (var item in collection)
            {
                var h = (uint)equalityComparer.GetHashCode(item);

                // We need at least three products, as two is too few
                hashCode += (int)((h * H1 + 1) ^ (h * H2) ^ (h * H3 + 2));
            }

            return(hashCode);
        }
예제 #2
0
파일: BasesTest.cs 프로젝트: raj581/Marvin
            public void ReferenceEqualityEqualityComparerTest()
            {
                REHTest rehtest = new REHTest();

                SCG.IEqualityComparer <REHTest> equalityComparer = ReferenceEqualityComparer <REHTest> .Default;
                Assert.AreEqual(37, rehtest.GetHashCode());
                Assert.IsFalse(equalityComparer.GetHashCode(rehtest) == 37);
            }
예제 #3
0
            public void SeqequalityComparerViaBuilder2()
            {
                SCG.IEqualityComparer <LinkedList <int> > h = C5.EqualityComparer <LinkedList <int> > .Default;
                LinkedList <int> s = new LinkedList <int>();

                s.Add(1); s.Add(2); s.Add(3);
                Assert.AreEqual(CHC.sequencedhashcode(1, 2, 3), h.GetHashCode(s));
            }
예제 #4
0
            public void UnseqequalityComparerViaBuilder3()
            {
                SCG.IEqualityComparer <IFoo <int> > h = C5.EqualityComparer <IFoo <int> > .Default;
                IFoo <int> s = new Foo <int>();

                s.Add(1); s.Add(2); s.Add(3);
                Assert.AreEqual(CHC.unsequencedhashcode(1, 2, 3), h.GetHashCode(s));
            }
예제 #5
0
파일: BasesTest.cs 프로젝트: raj581/Marvin
            public void SeqequalityComparerViaBuilder4()
            {
                SCG.IEqualityComparer <IBaz> h = EqualityComparer <IBaz> .Default;
                IBaz s = new Baz();

                s.Add(1); s.Add(2); s.Add(3);
                Assert.AreEqual(CHC.sequencedhashcode(1, 2, 3), h.GetHashCode(s));
            }
예제 #6
0
            public void UnseqequalityComparerViaBuilder4()
            {
                SCG.IEqualityComparer <IBar> h = C5.EqualityComparer <IBar> .Default;
                IBar s = new Bar();

                s.Add(1); s.Add(2); s.Add(3);
                Assert.AreEqual(CHC.unsequencedhashcode(1, 2, 3), h.GetHashCode(s));
            }
예제 #7
0
            public void ValuetypeequalityComparer()
            {
                SCG.IEqualityComparer <double> h = C5.EqualityComparer <double> .Default;
                double s = 3.4;
                double t = 3.4;
                double u = 5.7;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
            }
예제 #8
0
            public void ReftypeequalityComparer()
            {
                SCG.IEqualityComparer <string> h = C5.EqualityComparer <string> .Default;
                string s = "bamse";
                string t = "bamse";
                string u = "bimse";

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
            }
예제 #9
0
파일: BasesTest.cs 프로젝트: raj581/Marvin
            public void UshortequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <ushort> h = EqualityComparer <ushort> .Default;
                ushort s = 3;
                ushort t = 3;
                ushort u = 60000;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, EqualityComparer <ushort> .Default);
            }
예제 #10
0
            public void ShortequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <short> h = C5.EqualityComparer <short> .Default;
                short s = 3;
                short t = 3;
                short u = -5;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <short> .Default);
            }
예제 #11
0
            public void CharequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <char> h = C5.EqualityComparer <char> .Default;
                char s = 'å';
                char t = 'å';
                char u = 'r';

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <char> .Default);
            }
예제 #12
0
            public void SbyteequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <sbyte> h = RazorDBx.C5.EqualityComparer <sbyte> .Default;
                sbyte s = 3;
                sbyte t = 3;
                sbyte u = -5;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, RazorDBx.C5.EqualityComparer <sbyte> .Default);
            }
예제 #13
0
파일: BasesTest.cs 프로젝트: raj581/Marvin
            public void IntequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <int> h = EqualityComparer <int> .Default;
                int s = 3;
                int t = 3;
                int u = -5;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, EqualityComparer <int> .Default);
            }
예제 #14
0
            public void DecimalequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <decimal> h = C5.EqualityComparer <decimal> .Default;
                decimal s = 3.0001M;
                decimal t = 3.0001M;
                decimal u = -500000000000000M;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <decimal> .Default);
            }
예제 #15
0
            public void FloatequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <float> h = C5.EqualityComparer <float> .Default;
                float s = 3.1F;
                float t = 3.1F;
                float u = -5.2F;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <float> .Default);
            }
예제 #16
0
            public void DoubleequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <double> h = C5.EqualityComparer <double> .Default;
                double s = 3.12345;
                double t = 3.12345;
                double u = -5.2;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <double> .Default);
            }
예제 #17
0
            public void UintequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <uint> h = C5.EqualityComparer <uint> .Default;
                uint s = 3;
                uint t = 3;
                uint u = 3000000000;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <uint> .Default);
            }
예제 #18
0
            public void UlongequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <ulong> h = C5.EqualityComparer <ulong> .Default;
                ulong s = 3;
                ulong t = 3;
                ulong u = 500000000000000UL;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <ulong> .Default);
            }
예제 #19
0
파일: BasesTest.cs 프로젝트: raj581/Marvin
            public void LongequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <long> h = EqualityComparer <long> .Default;
                long s = 3;
                long t = 3;
                long u = -500000000000000L;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, EqualityComparer <long> .Default);
            }
예제 #20
0
            public void ByteequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <byte> h = C5.EqualityComparer <byte> .Default;
                byte s = 3;
                byte t = 3;
                byte u = 5;

                Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, C5.EqualityComparer <byte> .Default);
            }
예제 #21
0
파일: BasesTest.cs 프로젝트: raj581/Marvin
            public void UnseqequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <ICollection <int> > h = EqualityComparer <ICollection <int> > .Default;
                ICollection <int> s = new LinkedList <int>();
                ICollection <int> t = new LinkedList <int>();
                ICollection <int> u = new LinkedList <int>();

                s.Add(1); s.Add(2); s.Add(3);
                t.Add(3); t.Add(2); t.Add(1);
                u.Add(3); u.Add(2); u.Add(4);
                Assert.AreEqual(s.GetUnsequencedHashCode(), h.GetHashCode(s));
                Assert.IsTrue(h.Equals(s, t));
                Assert.IsFalse(h.Equals(s, u));
                Assert.AreSame(h, EqualityComparer <ICollection <int> > .Default);
            }
예제 #22
0
        public static int GetSequencedHashCode <T>(this ISequenced <T> items, SCG.IEqualityComparer <T> equalityComparer = null)
        {
            if (items == null)
            {
                return(0); // TODO: Better default value?
            }

            if (equalityComparer == null)
            {
                equalityComparer = SCG.EqualityComparer <T> .Default;
            }

            var hashCode = 0; // TODO: Better intial value?

            foreach (var item in items)
            {
                hashCode = hashCode * HashFactor + equalityComparer.GetHashCode(item);
            }

            return(hashCode);
        }
예제 #23
0
        public static bool ContainsRange <T>(this SCG.IEnumerable <T> first, SCG.IEnumerable <T> second, SCG.IEqualityComparer <T> equalityComparer = null)
        {
            #region Code Contracts

            // Argument must be non-null
            Requires(first != null, ArgumentMustBeNonNull);

            // Argument must be non-null
            Requires(second != null, ArgumentMustBeNonNull);


            // first remains unchanged
            Ensures(first == null || first.IsSameSequenceAs(OldValue(first.ToList())));

            // second remains unchanged
            Ensures(second == null || second.IsSameSequenceAs(OldValue(second.ToList())));

            #endregion

            var firstArray  = first.ToArray();
            var secondArray = second.ToArray();

            if (firstArray.Length < secondArray.Length)
            {
                return(false);
            }

            // Use default comparer if none is supplied
            if (equalityComparer == null)
            {
                equalityComparer = SCG.EqualityComparer <T> .Default;
            }

            // Sort based on hash code
            Comparison <T> hashCodeComparison = (x, y) => equalityComparer.GetHashCode(x).CompareTo(equalityComparer.GetHashCode(y));
            Array.Sort(firstArray, hashCodeComparison);
            Array.Sort(secondArray, hashCodeComparison);

            for (var j = 0; j < secondArray.Length; j++)
            {
                var found         = false;
                var secondElement = secondArray[j];

                for (var i = j; i < firstArray.Length; i++)
                {
                    var firstElement = firstArray[i];

                    var comparison = hashCodeComparison(firstElement, secondElement);

                    // Equal doesn't exist
                    if (comparison > 0)
                    {
                        break;
                    }

                    if (comparison == 0 && equalityComparer.Equals(firstElement, secondElement))
                    {
                        firstArray.Swap(i, j);
                        // TODO: the hash codes are not necessarily ordered after swapping the items

                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(false);
                }

                // Invariant: all items up to and including j are equal pairwise in the two arrays
                Assume(ForAll(0, j + 1, i => equalityComparer.Equals(firstArray[i], secondArray[i])));
            }

            return(true);
        }
예제 #24
0
 /// <summary>
 /// Get the hash code of the entry
 /// </summary>
 /// <param name="entry">The entry</param>
 /// <returns>The hash code of the key</returns>
 public int GetHashCode(KeyValuePair <K, V> entry)
 {
     return(keyequalityComparer.GetHashCode(entry.Key));
 }
예제 #25
0
 /// <summary>
 /// Get the hash code of the entry
 /// </summary>
 /// <param name="obj">The entry</param>
 /// <returns>The hash code of the entry value</returns>
 public int GetHashCode(Multiplicity <T> obj)
 {
     return(_valueEqualityComparer.GetHashCode(obj.Value));
 }
예제 #26
0
        public static bool UnsequenceEqual <T>(this SCG.IEnumerable <T> first, SCG.IEnumerable <T> second, SCG.IEqualityComparer <T> comparer = null)
        {
            #region Code Contracts

            // first remains unchanged
            Ensures(first == null || first.IsSameSequenceAs(OldValue(first.ToList())));

            // second remains unchanged
            Ensures(second == null || second.IsSameSequenceAs(OldValue(second.ToList())));

            #endregion

            if (ReferenceEquals(first, second))
            {
                return(true);
            }

            if (first == null || second == null)
            {
                return(false);
            }

            var firstArray  = first.ToArray();
            var secondArray = second.ToArray();

            if (firstArray.Length != secondArray.Length)
            {
                return(false);
            }

            // Use default comparer if none is supplied
            if (comparer == null)
            {
                comparer = SCG.EqualityComparer <T> .Default;
            }

            // Sort based on hash code
            Comparison <T> hashCodeComparison = (x, y) => comparer.GetHashCode(x).CompareTo(comparer.GetHashCode(y));
            Array.Sort(firstArray, hashCodeComparison);
            Array.Sort(secondArray, hashCodeComparison);

            for (var i = 0; i < firstArray.Length; i++)
            {
                var found        = false;
                var firstElement = firstArray[i];

                for (var j = i; j < secondArray.Length; j++)
                {
                    var secondElement = secondArray[j];

                    if (hashCodeComparison(firstElement, secondElement) != 0)
                    {
                        break;
                    }

                    if (comparer.Equals(firstElement, secondElement))
                    {
                        secondArray.Swap(i, j);

                        // Continue with next element in first array
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(false);
                }
            }

            return(true);
        }