示例#1
0
        /// <summary>
        /// Returns the minimum value in the enumerable sequence by using the specified comparer to compare values.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="comparerFactory">The definition of a comparer to compare elements.</param>
        public static TSource Min <TSource>(this IEnumerable <TSource> source, Func <ComparerBuilderFor <TSource>, IFullComparer <TSource> > comparerFactory)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <TSource>());

            return(source.Min(comparer));
        }
示例#2
0
        public void NullComparerHash_EqualsDefaultMurmer3Hash()
        {
            var comparer   = ComparerBuilder.For <int>().Null();
            var objectHash = comparer.GetHashCode(0);

            Assert.Equal(Murmur3Hash.Create().HashCode, objectHash);
        }
示例#3
0
        /// <summary>
        /// Sorts part of a pair of arrays. This array contains the keys; the first parameter to this method contains the corresponding items.
        /// </summary>
        /// <typeparam name="TKey">The type of the keys.</typeparam>
        /// <typeparam name="TValue">The type of the items.</typeparam>
        /// <param name="this">The array containing the keys.</param>
        /// <param name="items">The array containing the items.</param>
        /// <param name="index">The offset at which to start sorting.</param>
        /// <param name="length">The number of elements to sort.</param>
        /// <param name="comparerFactory">The definition of a comparer to compare elements.</param>
        public static void Sort <TKey, TValue>(this TKey[] @this, TValue[] items, int index, int length, Func <ComparerBuilderFor <TKey>, IFullComparer <TKey> > comparerFactory)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <TKey>());

            Array.Sort(@this, items, index, length, comparer);
        }
示例#4
0
        /// <summary>
        /// Sorts part of an array.
        /// </summary>
        /// <typeparam name="T">The type of the elements.</typeparam>
        /// <param name="this">The array to sort.</param>
        /// <param name="index">The offset at which to start sorting.</param>
        /// <param name="count">The number of elements to sort.</param>
        /// <param name="comparerFactory">The definition of a comparer to compare elements.</param>
        public static void Sort <T>(this T[] @this, int index, int count, Func <ComparerBuilderFor <T>, IFullComparer <T> > comparerFactory)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <T>());

            Array.Sort(@this, index, count, comparer);
        }
示例#5
0
        public void DefaultEqualityComparer_ForValueType_IEqualityComparer_UnexpectedTypes()
        {
            // When accessing the default equality comparer via the nongeneric IEqualityComparer interface...
            IEqualityComparer frameworkComparer = EqualityComparer <int> .Default;
            IEqualityComparer nitoComparer      = ComparerBuilder.For <int>().Default();

            // The framework version returns true if any reference-equal instances are passed in
            //   https://github.com/dotnet/corefx/blob/53a33cf2662ac8c9a45d13067012d80cf0ba6956/src/Common/src/CoreLib/System/Collections/Generic/EqualityComparer.cs#L29
            var obj = new object();

            Assert.True(frameworkComparer.Equals(obj, obj));

            // But throws if that same object is passed to GetHashCode
            Assert.ThrowsAny <ArgumentException>(() => frameworkComparer.GetHashCode(obj));

            // It also throws if different instances are passed in that are not the expected type
            Assert.ThrowsAny <ArgumentException>(() => frameworkComparer.Equals(obj, new object()));

            // This is particularly interesting with value types; this will throw due to boxing.
            const double value = 13.0;

            Assert.ThrowsAny <ArgumentException>(() => frameworkComparer.Equals(value, value));

            // Nito Comparers throw in all scenarios for consistency
            Assert.ThrowsAny <ArgumentException>(() => nitoComparer.Equals(obj, obj));
            Assert.ThrowsAny <ArgumentException>(() => nitoComparer.GetHashCode(obj));
            Assert.ThrowsAny <ArgumentException>(() => nitoComparer.Equals(obj, new object()));
            Assert.ThrowsAny <ArgumentException>(() => nitoComparer.Equals(value, value));
        }
示例#6
0
        public void NullIsSmallerThanEmpty_NonGeneric()
        {
            var comparer = ComparerBuilder.For <int>().Default().Sequence() as System.Collections.IComparer;

            Assert.True(comparer.Compare(null, Enumerable.Empty <int>()) < 0);
            Assert.True(comparer.Compare(Enumerable.Empty <int>(), null) > 0);
        }
示例#7
0
        /// <summary>
        /// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
        /// </summary>
        /// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
        /// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
        /// <param name="source">A sequence of values to order.</param>
        /// <param name="keySelector">A function to extract a key from an element.</param>
        /// <param name="comparerFactory">The definition of a comparer to compare keys.</param>
        public static IOrderedEnumerable <T> ThenByDescending <T, TKey>(this IOrderedEnumerable <T> source, Func <T, TKey> keySelector, Func <ComparerBuilderFor <TKey>, IFullComparer <TKey> > comparerFactory)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <TKey>());

            return(source.ThenByDescending(keySelector, comparer));
        }
示例#8
0
        /// <summary>
        /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <typeparam name="TKey">Key type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
        /// <param name="comparerFactory">The definition of a comparer to compare keys.</param>
        public static IList <TSource> MinBy <TSource, TKey>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <ComparerBuilderFor <TKey>, IFullComparer <TKey> > comparerFactory)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <TKey>());

            return(source.MinBy(keySelector, comparer));
        }
示例#9
0
        public DeltaHashProvider(IDeltaCache deltaCache,
                                 IDeltaIndexService deltaIndexService,
                                 ILogger logger,
                                 int capacity = 10_000)
        {
            _deltaCache = deltaCache;
            _logger     = logger;
            _deltaHashUpdatesSubject = new ReplaySubject <Cid>(0);
            var comparer = ComparerBuilder.For <Timestamp>().OrderBy(u => u, descending: true);

            _capacity = capacity;
            _hashesByTimeDescending = new SortedList <Timestamp, Cid>(comparer)
            {
                Capacity = _capacity
            };

            var latestDeltaIndex = deltaIndexService.LatestDeltaIndex();

            if (deltaIndexService.LatestDeltaIndex() != null)
            {
                var foundDelta = _deltaCache.TryGetOrAddConfirmedDelta(latestDeltaIndex.Cid, out var delta);
                _hashesByTimeDescending.Add(delta.TimeStamp, latestDeltaIndex.Cid);
                return;
            }

            _hashesByTimeDescending.Add(Timestamp.FromDateTime(DateTime.UnixEpoch), _deltaCache.GenesisHash);
        }
示例#10
0
 public void ImplementsGetHashCode_NonGeneric()
 {
     var comparer = ComparerBuilder.For<int?>().Default() as System.Collections.IEqualityComparer;
     var bclDefault = EqualityComparer<int?>.Default;
     Assert.Equal(comparer.GetHashCode(13), comparer.GetHashCode(13));
     Assert.Equal(bclDefault.GetHashCode(7) == bclDefault.GetHashCode(13), comparer.GetHashCode(7) == comparer.GetHashCode(13));
 }
示例#11
0
 public void ImplementsGetHashCode()
 {
     var comparer = ComparerBuilder.For<int?>().Default();
     var bclDefault = EqualityComparer<int?>.Default;
     Assert.Equal(comparer.GetHashCode(13), comparer.GetHashCode(13));
     Assert.Equal(bclDefault.GetHashCode(7) == bclDefault.GetHashCode(13), comparer.GetHashCode(7) == comparer.GetHashCode(13));
 }
示例#12
0
        public void ReversesComparerWithNull()
        {
            var list = Enumerable.Repeat <int?>(null, 1).Concat(Enumerable.Range(0, 5).Cast <int?>()).ToList();

            list.Sort(ComparerBuilder.For <int?>().Default().Reverse());
            Assert.Equal(new int?[] { 4, 3, 2, 1, 0, null }, list);
        }
示例#13
0
        /// <summary>
        /// Invokes a transform function on each element of a sequence and returns the minimum value according to the specified comparer.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the objects derived from the elements in the source sequence to determine the minimum of.</typeparam>
        /// <param name="source">An observable sequence to determine the mimimum element of.</param>
        /// <param name="selector">A transform function to apply to each element.</param>
        /// <param name="comparerFactory">The definition of a comparer to compare elements.</param>
        public static IObservable <TResult> Min <TSource, TResult>(this IObservable <TSource> source, Func <TSource, TResult> selector, Func <ComparerBuilderFor <TResult>, IFullComparer <TResult> > comparerFactory)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <TResult>());

            return(source.Min(selector, comparer));
        }
示例#14
0
        public void ReversesComparer()
        {
            var list = Enumerable.Range(0, 5).ToList();

            list.Sort(ComparerBuilder.For <int>().Default().Reverse());
            Assert.Equal(new[] { 4, 3, 2, 1, 0 }, list);
        }
示例#15
0
        public void PassesGetHashCodeThrough()
        {
            var comparer    = ComparerBuilder.For <int?>().Default().Reverse();
            var bclComparer = EqualityComparer <int?> .Default;

            Assert.Equal(bclComparer.GetHashCode(7) == bclComparer.GetHashCode(13), comparer.GetHashCode(7) == comparer.GetHashCode(13));
        }
示例#16
0
        /// <summary>
        /// Sorts the list.
        /// </summary>
        /// <typeparam name="T">The type of the elements.</typeparam>
        /// <param name="this">The list to sort.</param>
        /// <param name="comparerFactory">The definition of a comparer to compare elements.</param>
        public static void Sort <T>(this List <T> @this, Func <ComparerBuilderFor <T>, IFullComparer <T> > comparerFactory)
        {
            _ = @this ?? throw new ArgumentNullException(nameof(@this));
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(ComparerBuilder.For <T>());

            @this.Sort(comparer);
        }
示例#17
0
        public void ComparesUnequalElementsAsEqual()
        {
            var comparer = ComparerBuilder.For <int>().Null();

            Assert.Equal(0, comparer.Compare(13, 17));
            Assert.True(comparer.Equals(19, 21));
            Assert.Equal(comparer.GetHashCode(13), comparer.GetHashCode(17));
        }
示例#18
0
 public void NullSequenceIsLessThanValuesAndEmptySequence()
 {
     var none = new int[0];
     var five = new[] { 5 };
     var list = new[] { five, none, null }.ToList();
     list.Sort(ComparerBuilder.For<int[]>().Default());
     Assert.Equal(list, new[] { null, none, five });
 }
示例#19
0
 public void NullSequenceIsEqualToNullSequence()
 {
     var comparer = ComparerBuilder.For<int[]>().Default();
     Assert.True(comparer.Compare(null, null) == 0);
     Assert.True(comparer.Equals(null, null));
     Assert.Equal(comparer.GetHashCode((object)null), comparer.GetHashCode((object)null));
     Assert.Equal(comparer.GetHashCode((int[])null), comparer.GetHashCode((int[])null));
 }
示例#20
0
 public void IsEquivalentToComparerDefault()
 {
     var list1 = new[] { 3, 5, 4, 2, 6 }.ToList();
     var list2 = new[] { 3, 5, 4, 2, 6 }.ToList();
     list1.Sort();
     list2.Sort(ComparerBuilder.For<int>().Default());
     Assert.Equal(list1, list2);
 }
示例#21
0
        public void ThenByIsOnlyAppliedAsTieBreaker()
        {
            IComparer <Person> thenByComparer = new FailComparer <Person>();
            var comparer = ComparerBuilder.For <Person>().Default().ThenBy(thenByComparer);

            Assert.True(comparer.Compare(AbeAbrams, CaseyJohnson) < 0);
            Assert.True(comparer.Compare(CaseyJohnson, AbeAbrams) > 0);
        }
示例#22
0
        public void OrderByWithNullThenByHandlesNull()
        {
            var list = new List <Person> {
                AbeAbrams, JackAbrams, null, WilliamAbrams, CaseyJohnson
            };

            list.Sort(ComparerBuilder.For <Person>().OrderBy(p => p == null, specialNullHandling: true).ThenBy(p => p.FirstName));
            Assert.Equal(new[] { AbeAbrams, CaseyJohnson, JackAbrams, WilliamAbrams, null }, list);
        }
示例#23
0
        public void OrderByWithNullPassesNullThrough()
        {
            var list = new List <Person> {
                null, WilliamAbrams
            };

            list.Sort(ComparerBuilder.For <Person>().OrderBy(p => p == null, specialNullHandling: true));
            Assert.Equal(new[] { WilliamAbrams, null }, list);
        }
示例#24
0
        public void OrderBySortsByKey()
        {
            var list = new List <Person> {
                AbeAbrams, JackAbrams, WilliamAbrams, CaseyJohnson
            };

            list.Sort(ComparerBuilder.For <Person>().OrderBy(p => p.FirstName));
            Assert.Equal(new[] { AbeAbrams, CaseyJohnson, JackAbrams, WilliamAbrams }, list);
        }
示例#25
0
        public void ThenByDescendingSortsByKey()
        {
            var list = new List <Person> {
                AbeAbrams, WilliamAbrams, CaseyJohnson, JackAbrams
            };

            list.Sort(ComparerBuilder.For <Person>().OrderBy(p => p.LastName).ThenBy(p => p.FirstName, descending: true));
            Assert.Equal(new[] { WilliamAbrams, JackAbrams, AbeAbrams, CaseyJohnson }, list);
        }
示例#26
0
        public void ThenByUsesKeyComparer()
        {
            var list = new List <Person> {
                AbeAbrams, WilliamAbrams, CaseyJohnson, JackAbrams
            };

            list.Sort(ComparerBuilder.For <Person>().OrderBy(p => p.LastName).ThenBy(p => p.FirstName, StringComparer.InvariantCulture.Reverse()));
            Assert.Equal(new[] { WilliamAbrams, JackAbrams, AbeAbrams, CaseyJohnson }, list);
        }
示例#27
0
        public void OrderByDescendingUsesKeyComparer()
        {
            var list = new List <Person> {
                AbeAbrams, JackAbrams, WilliamAbrams, CaseyJohnson
            };

            list.Sort(ComparerBuilder.For <Person>().OrderBy(p => p.FirstName, StringComparer.InvariantCulture.Reverse(), descending: true));
            Assert.Equal(new[] { AbeAbrams, CaseyJohnson, JackAbrams, WilliamAbrams }, list);
        }
示例#28
0
        public void ComparesNullElementsAsEqual()
        {
            var comparer = ComparerBuilder.For <int?>().Null();

            Assert.Equal(0, comparer.Compare(null, null));
            Assert.True(comparer.Equals(null, null));
            Assert.Equal(comparer.GetHashCode((object)null), comparer.GetHashCode((object)null));
            Assert.Equal(comparer.GetHashCode((int?)null), comparer.GetHashCode((int?)null));
        }
示例#29
0
        public void OrderByImplementsGetHashCode()
        {
            var comparer   = ComparerBuilder.For <Person>().OrderBy(p => p.LastName);
            var bclDefault = EqualityComparer <string> .Default;

            Assert.Equal(comparer.GetHashCode(AbeAbrams), comparer.GetHashCode(JackAbrams));
            Assert.Equal(bclDefault.GetHashCode(AbeAbrams.LastName) == bclDefault.GetHashCode(CaseyJohnson.LastName),
                         comparer.GetHashCode(AbeAbrams) == comparer.GetHashCode(CaseyJohnson));
        }
示例#30
0
        public void ForFuncT_ReturnsBuilderForT_WithoutInvokingFunc()
        {
            var result = ComparerBuilder.For(() =>
            {
                throw new Exception();
                return(3);
            });

            Assert.IsType <ComparerBuilderFor <int> >(result);
        }