예제 #1
0
 public static void Equalities(String msg, SCG.IEqualityComparer <IList <int> > equalityComparer)
 {
     Console.WriteLine("\n{0}:", msg);
     Console.Write("Equals(col1,col2)={0,-5}; ", equalityComparer.Equals(col1, col2));
     Console.Write("Equals(col1,col3)={0,-5}; ", equalityComparer.Equals(col1, col3));
     Console.WriteLine("Equals(col2,col3)={0,-5}", equalityComparer.Equals(col2, col3));
 }
예제 #2
0
        public bool Equals(CollectionEvent <T> otherEvent, SCG.IEqualityComparer <T> itemequalityComparer)
        {
            if (otherEvent == null || Act != otherEvent.Act || !object.ReferenceEquals(Sender, otherEvent.Sender))
            {
                return(false);
            }
            switch (Act)
            {
            case EventTypeEnum.None:
                break;

            case EventTypeEnum.Changed:
                return(true);

            case EventTypeEnum.Cleared:
                if (Args is ClearedRangeEventArgs)
                {
                    ClearedRangeEventArgs a = Args as ClearedRangeEventArgs, o = otherEvent.Args as ClearedRangeEventArgs;
                    if (o == null)
                    {
                        return(false);
                    }
                    return(a.Full == o.Full && a.Start == o.Start && a.Count == o.Count);
                }
                else
                {
                    if (otherEvent.Args is ClearedRangeEventArgs)
                    {
                        return(false);
                    }
                    ClearedEventArgs a = Args as ClearedEventArgs, o = otherEvent.Args as ClearedEventArgs;
                    return(a.Full == o.Full && a.Count == o.Count);
                }

            case EventTypeEnum.Added:
            {
                ItemCountEventArgs <T> a = Args as ItemCountEventArgs <T>, o = otherEvent.Args as ItemCountEventArgs <T>;
                return(itemequalityComparer.Equals(a.Item, o.Item) && a.Count == o.Count);
            }

            case EventTypeEnum.Removed:
            {
                ItemCountEventArgs <T> a = Args as ItemCountEventArgs <T>, o = otherEvent.Args as ItemCountEventArgs <T>;
                return(itemequalityComparer.Equals(a.Item, o.Item) && a.Count == o.Count);
            }

            case EventTypeEnum.Inserted:
            {
                ItemAtEventArgs <T> a = Args as ItemAtEventArgs <T>, o = otherEvent.Args as ItemAtEventArgs <T>;
                return(a.Index == o.Index && itemequalityComparer.Equals(a.Item, o.Item));
            }

            case EventTypeEnum.RemovedAt:
            {
                ItemAtEventArgs <T> a = Args as ItemAtEventArgs <T>, o = otherEvent.Args as ItemAtEventArgs <T>;
                return(a.Index == o.Index && itemequalityComparer.Equals(a.Item, o.Item));
            }
            }
            throw new ApplicationException("Illegal Action: " + Act);
        }
예제 #3
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));
            }
예제 #4
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));
            }
예제 #5
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);
            }
예제 #6
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);
            }
예제 #7
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);
            }
예제 #8
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);
            }
예제 #9
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);
            }
예제 #10
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);
            }
예제 #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 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);
            }
예제 #13
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);
            }
예제 #14
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);
            }
예제 #15
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);
            }
예제 #16
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);
            }
예제 #17
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);
            }
예제 #18
0
파일: TestHelper.cs 프로젝트: flyer87/C6
        public static int LastIndexOf <T>(this T[] array, T item, SCG.IEqualityComparer <T> equalityComparer = null)
        {
            Contract.Requires(array.Contains(item, equalityComparer));

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

            var index = -1;

            for (var i = 0; i < array.Length; i++)
            {
                if (equalityComparer.Equals(array[i], item))
                {
                    index = i;
                }
            }

            if (index >= 0)
            {
                return(index);
            }

            throw new Exception();
        }
예제 #19
0
        public static int CountDuplicates <T>(this SCG.IEnumerable <T> enumerable, T item, SCG.IEqualityComparer <T> equalityComparer = null)
        {
            // Argument must be non-null
            Requires(enumerable != null, ArgumentMustBeNonNull);


            if (equalityComparer == null)
            {
                equalityComparer = SCG.EqualityComparer <T> .Default;
            }
            return(enumerable.Count(x => equalityComparer.Equals(x, item)));
        }
예제 #20
0
        public static bool SequencedEquals <T>(this ISequenced <T> first, ISequenced <T> second, SCG.IEqualityComparer <T> equalityComparer = null)
        {
            Ensures(Result <bool>() == (ReferenceEquals(first, second) || (first != null && second != null && first.SequenceEqual(second, equalityComparer))));

            // Equal if reference equal - this is true for two nulls as well
            if (ReferenceEquals(first, second))
            {
                return(true);
            }

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

            if (first.Count != second.Count)
            {
                return(false);
            }

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

            using (var e1 = first.GetEnumerator())
                using (var e2 = second.GetEnumerator()) {
                    // Enumerators are equally long
                    while (e1.MoveNext() & e2.MoveNext())
                    {
                        if (!equalityComparer.Equals(e1.Current, e2.Current))
                        {
                            return(false);
                        }
                    }
                }

            return(true);
        }
예제 #21
0
        private static void TestComparerSerialization <T>(SCG.IEqualityComparer <T> equalityComparer, string internalTypeName = null)
        {
            var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            var s  = new MemoryStream();

            var dict = new Dictionary <T, T>(equalityComparer);

            Assert.Same(equalityComparer, dict.EqualityComparer);

            bf.Serialize(s, dict);
            s.Position = 0;
            dict       = (Dictionary <T, T>)bf.Deserialize(s);

            if (internalTypeName == null)
            {
                Assert.IsType(equalityComparer.GetType(), dict.EqualityComparer);
            }
            else
            {
                Assert.Equal(internalTypeName, dict.EqualityComparer.GetType().ToString());
            }

            Assert.True(equalityComparer.Equals(dict.EqualityComparer));
        }
예제 #22
0
 public static void Main(String[] args)
 {
     SCG.IEqualityComparer <int> natural = EquatableEqualityComparer <int> .Default;
     SCG.IEqualityComparer <int> c5comp  = EqualityComparer <int> .Default;
     int count = int.Parse(args[0]);
     {
         bool  res = false;
         Timer t   = new Timer();
         for (int i = count; i > 0; i--)
         {
             res = natural.Equals(4, 5);
         }
         Console.WriteLine("Time = {0} ns/comparison\n", t.Check() * 1E9 / count);
     }
     {
         bool  res = false;
         Timer t   = new Timer();
         for (int i = count; i > 0; i--)
         {
             res = c5comp.Equals(4, 5);
         }
         Console.WriteLine("Time = {0} ns/comparison\n", t.Check() * 1E9 / count);
     }
 }
예제 #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>
 /// Test two entries for equality
 /// </summary>
 /// <param name="entry1">First entry</param>
 /// <param name="entry2">Second entry</param>
 /// <returns>True if keys are equal</returns>
 public bool Equals(KeyValuePair <K, V> entry1, KeyValuePair <K, V> entry2)
 {
     return(keyequalityComparer.Equals(entry1.Key, entry2.Key));
 }
예제 #25
0
 /// <summary>
 /// Test two entries for equality
 /// </summary>
 /// <param name="x">First entry</param>
 /// <param name="y">Second entry</param>
 /// <returns>True if entry values are equal</returns>
 public bool Equals(Multiplicity <T> x, Multiplicity <T> y)
 {
     return(_valueEqualityComparer.Equals(x.Value, y.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);
        }