示例#1
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);
            }
示例#2
0
            public void ValuetypeequalityComparerViaBuilder()
            {
                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));
                Assert.AreSame(h, C5.EqualityComparer <double> .Default);
            }
示例#3
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);
            }
示例#4
0
            public void LongequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <long> h = C5.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, C5.EqualityComparer <long> .Default);
            }
示例#5
0
            public void ReftypeequalityComparerViaBuilder()
            {
                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));
                Assert.AreSame(h, C5.EqualityComparer <string> .Default);
            }
示例#6
0
            public void DoubleequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <double> h = 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, EqualityComparer <double> .Default);
            }
示例#7
0
            public void DecimalequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <decimal> h = 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, EqualityComparer <decimal> .Default);
            }
示例#8
0
            public void FloatequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <float> h = 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, EqualityComparer <float> .Default);
            }
示例#9
0
            public void UlongequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <ulong> h = 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, EqualityComparer <ulong> .Default);
            }
示例#10
0
            public void UintequalityComparerViaBuilder()
            {
                SCG.IEqualityComparer <uint> h = 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, EqualityComparer <uint> .Default);
            }
示例#11
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);
            }
示例#12
0
        public GlobalLockDEPQ(int capacity)
        {
            this.comparer             = SCG.Comparer <T> .Default;
            this.itemEquelityComparer = SCG.EqualityComparer <T> .Default;
            int lenght = 1;

            while (lenght < capacity)
            {
                lenght <<= 1;                       //lenght is always equal to 2 power by some number.
            }
            heap = new Interval[lenght];
        }
示例#13
0
        /// <summary>
        /// Create an empty ArrayBase object.
        /// </summary>
        /// <param name="capacity">The initial capacity of the internal array container.
        /// Will be rounded upwards to the nearest power of 2 greater than or equal to 8.</param>
        /// <param name="itemequalityComparer">The item equalityComparer to use, primarily for item equality</param>
        /// <param name="memoryType">The type of memory for the enumerator used to iterate the collection</param>
        protected ArrayBase(int capacity, SCG.IEqualityComparer <T> itemequalityComparer, MemoryType memoryType)
            : base(itemequalityComparer, memoryType)
        {
            int newlength = 8;

            while (newlength < capacity)
            {
                newlength *= 2;
            }
            array = new T[newlength];

            _internalEnumerator = new Enumerator(this, memoryType);
        }
示例#14
0
        private IntervalHeap(int capacity, SCG.IComparer <T> comparer, SCG.IEqualityComparer <T> itemequalityComparer)
        {
            this.comparer             = comparer ?? throw new NullReferenceException("Item comparer cannot be null");
            this.itemequalityComparer = itemequalityComparer ?? throw new NullReferenceException("Item equality comparer cannot be null");
            int length = 1;

            while (length < capacity)
            {
                length <<= 1;
            }

            heap = new Interval[length];
        }
示例#15
0
        public void LinkedHashSet_Generic_Constructor_NullIEqualityComparer()
        {
            SCG.IEqualityComparer <T> comparer = null;
            LinkedHashSet <T>         set      = new LinkedHashSet <T>(comparer);

            if (comparer == null)
            {
                Assert.Equal(EqualityComparer <T> .Default, set.EqualityComparer);
            }
            else
            {
                Assert.Equal(comparer, set.EqualityComparer);
            }
        }
示例#16
0
            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);
            }
示例#17
0
        public void LinkedHashSet_Generic_Constructor_int_IEqualityComparer(int capacity)
        {
            SCG.IEqualityComparer <T> comparer = GetIEqualityComparer();
            LinkedHashSet <T>         set      = new LinkedHashSet <T>(capacity, comparer);

            Assert.Equal(0, set.Count);
            if (comparer == null)
            {
                Assert.Equal(EqualityComparer <T> .Default, set.EqualityComparer);
            }
            else
            {
                Assert.Equal(comparer, set.EqualityComparer);
            }
        }
示例#18
0
        private IntervalHeap(int capacity, SCG.IComparer <T> comparer, SCG.IEqualityComparer <T> itemequalityComparer)
        {
            if (comparer == null)
            {
                throw new NullReferenceException("Item comparer cannot be null");
            }
            if (itemequalityComparer == null)
            {
                throw new NullReferenceException("Item equality comparer cannot be null");
            }

            this.comparer             = comparer;
            this.itemequalityComparer = itemequalityComparer;

            Clear(capacity);
        }
示例#19
0
 public RelaxedLotanShavitSkipList(int max)
 {
     comparer             = SCG.Comparer <T> .Default;
     itemEquelityComparer = SCG.EqualityComparer <T> .Default;
     maxLevel             = max;
     size           = 0;
     level          = 1;
     header         = new Node(maxLevel, default(T));
     header.deleted = 2;
     tail           = new Node(0, default(T), true);
     tail.deleted   = 2;
     for (int i = 0; i < maxLevel; i++)
     {
         header.forward[i] = tail;
     }
 }
示例#20
0
 // When colls is a collection of collections, this method checks
 // that all `inner' collections use the exact same equalityComparer.  Note
 // that two equalityComparer objects may be functionally (extensionally)
 // identical, yet be distinct objects.  However, if the equalityComparers
 // were obtained from EqualityComparer<T>.Default, there will be at most one
 // equalityComparer for each type T.
 public static bool EqualityComparerSanity <T, U>(ICollectionValue <U> colls) where U : IExtensible <T>
 {
     SCG.IEqualityComparer <T> equalityComparer = null;
     foreach (IExtensible <T> coll in colls)
     {
         if (equalityComparer == null)
         {
             equalityComparer = coll.EqualityComparer;
         }
         if (equalityComparer != coll.EqualityComparer)
         {
             return(false);
         }
     }
     return(true);
 }
示例#21
0
        // Given a sequence of strings, return only the first member of each
        // anagram class.

        public static SCG.IEnumerable <String> FirstAnagramOnly(SCG.IEnumerable <String> ss)
        {
            SCG.IEqualityComparer <TreeBag <char> > tbh
                = UnsequencedCollectionEqualityComparer <TreeBag <char>, char> .Default;
            HashSet <TreeBag <char> > anagrams = new HashSet <TreeBag <char> >(tbh);

            foreach (String s in ss)
            {
                TreeBag <char> anagram = AnagramClass(s);
                if (!anagrams.Contains(anagram))
                {
                    anagrams.Add(anagram);
                    yield return(s);
                }
            }
        }
示例#22
0
        public GlobalLockSkipList(int maxlevel)
        {
            this.comparer             = SCG.Comparer <T> .Default;
            this.itemEquelityComparer = SCG.EqualityComparer <T> .Default;
            header = null;
            int max = 1;

            while (max < maxlevel)
            {
                max <<= 1;
            }
            maxLevel = max;
            level    = 0;
            header   = new Node(maxLevel, default(T));
            rng      = new Random();
        }
示例#23
0
        public HuntLockDEPQv3(int capacity)
        {
            this.comparer             = SCG.Comparer <T> .Default;
            this.itemEquelityComparer = SCG.EqualityComparer <T> .Default;
            int lenght = 1;

            while (lenght < capacity)
            {
                lenght <<= 1;
            }
            heap  = new Interval[lenght];
            locks = new object[lenght];
            for (int i = 0; i < lenght; i++)
            {
                heap[i]  = new Interval();
                locks[i] = heap[i].intervalLock;
            }
        }
示例#24
0
文件: TestHelper.cs 项目: flyer87/C6
        public static int IndexOf <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;
            }

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

            throw new Exception();
        }
示例#25
0
        public static HashSet <int> MakeRandom(int count,
                                               SCG.IEqualityComparer <int> eqc)
        {
            HashSet <int> res;

            if (eqc == null)
            {
                res = new HashSet <int>();
            }
            else
            {
                res = new HashSet <int>(eqc);
            }
            for (int i = 0; i < count; i++)
            {
                res.Add(rnd.Next(1000000));
            }
            return(res);
        }
示例#26
0
        public HuntLockDEPQv1(int capacity)
        {
            this.comparer             = SCG.Comparer <T> .Default;
            this.itemEquelityComparer = SCG.EqualityComparer <T> .Default;
            size = 0;
            int length = 1;

            while (length < capacity)
            {
                length <<= 1;
            }
            minheap = new Interval[length];
            locks   = new object[length];
            for (int i = 0; i < length; i++)
            {
                Interval interval = new Interval();
                minheap[i] = interval;
                locks[i]   = interval.intervalLock;
            }
        }
示例#27
0
文件: HashTable.cs 项目: T145/archive
        /// <summary>
        /// Create a hash set with external item equalityComparer.
        /// </summary>
        /// <param name="capacity">Initial table size (rounded to power of 2, at least 16)</param>
        /// <param name="fill">Fill threshold (in range 10% to 90%)</param>
        /// <param name="itemequalityComparer">The external item equalitySCG.Comparer</param>
        public HashSet(int capacity, double fill, SCG.IEqualityComparer <T> itemequalityComparer)
            : base(itemequalityComparer)
        {
            _randomhashfactor = (Debug.UseDeterministicHashing) ? 1529784659 : (2 * (uint)Random.Next() + 1) * 1529784659;

            if (fill < 0.1 || fill > 0.9)
            {
                throw new ArgumentException("Fill outside valid range [0.1, 0.9]");
            }
            if (capacity <= 0)
            {
                throw new ArgumentException("Capacity must be non-negative");
            }
            //this.itemequalityComparer = itemequalityComparer;
            origbits = 4;
            while (capacity - 1 >> origbits > 0)
            {
                origbits++;
            }
            clear();
        }
        public ExpectedDirectedCollectionValue(SCG.IEnumerable <T> items, SCG.IEqualityComparer <T> equalityComparer, bool allowsNull, Func <T> chooseFunction = null, EnumerationDirection direction = EnumerationDirection.Forwards)
            : base(items, equalityComparer, allowsNull, chooseFunction)
        {
            #region Code Contracts

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

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

            // All items must be non-null if collection disallows null values
            Requires(allowsNull || ForAll(items, item => item != null), ItemsMustBeNonNull);

            // Argument must be valid enum constant
            Requires(Enum.IsDefined(typeof(EnumerationDirection), direction), EnumMustBeDefined);

            #endregion

            Direction = direction;
        }
示例#29
0
        public HellerSkipListv2(int maxlevel)
        {
            this.comparer             = SCG.Comparer <T> .Default;
            this.itemEquelityComparer = SCG.EqualityComparer <T> .Default;
            header = null;
            int max = 1;

            while (max < maxlevel)
            {
                max <<= 1;
            }
            maxLevel  = max;
            tail      = new Node(maxLevel - 1, default(T));
            tail.tail = true;
            header    = new Node(maxLevel - 1, default(T));
            for (int i = 0; i < maxLevel; i++)
            {
                header.forward[i] = tail;
                tail.forward[i]   = tail;
            }
        }
示例#30
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);
        }