Beispiel #1
0
        public static IEnumerable <M> GetNLargest <M>(this IComparer <M> g, IEnumerable <M> source, int n)
        {
            if (n < 0)
            {
                return(g.GetNSmallest(source, -n));
            }
            if (n == 0)
            {
                return(Enumerable.Empty <M>());
            }
            var sourceList = source.ToList();

            n = Math.Min(sourceList.Count, n);
            var resSet     = sourceList.Take(n).ToList();
            var currentMin = g.Min(resSet);

            foreach (var t in sourceList.Skip(n))
            {
                if (g.Compare(t, currentMin) <= 0)
                {
                    continue;
                }
                resSet.Remove(currentMin);
                resSet.Add(t);
                currentMin = g.Min(resSet);
            }
            return(resSet.OrderByDescending(i => i, g));
        }
        /// <summary>
        /// Clamps <paramref name="value"/> to a given inclusive range between <paramref name="min"/> and <paramref name="max"/>.
        /// </summary>
        /// <param name="_this">The <see cref="IComparer"/> used for comparisons.</param>
        /// <param name="value"></param>
        /// <param name="min">The lower border of the range. Is considered lower than <paramref name="_this"/> if null.</param>
        /// <param name="max">The upper border of the range. Is considered higher than <paramref name="_this"/> if null.</param>
        /// <returns>The clamped version of <paramref name="value"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="_this"/> is null.</exception>
        /// <example>
        /// <code>
        /// var result = new MyComparer().Clamp(value, min, max);
        /// </code>
        /// </example>
        public static Object Clamp(this IComparer _this, Object value, Object min, Object max)
        {
            Throw.If.Object.IsNull(_this, nameof(_this));
            Throw.If.Object.IsNull(value, nameof(value));

            if (min != null && max != null && _this.IsGreaterThan(min, max))
            {
                return(_this.Clamp(value, max, min));
            }

            Object result = value;

            if (min != null)
            {
                result = _this.Max(value, min);
            }

            if (max != null)
            {
                result = _this.Min(result, max);
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Clamps <paramref name="value"/> to a given inclusive range between <paramref name="min"/> and <paramref name="max"/>.
        /// </summary>
        /// <typeparam name="T">The type of the objects.</typeparam>
        /// <param name="_this">The <see cref="IComparer{T}"/> used for comparisons.</param>
        /// <param name="value"></param>
        /// <param name="min">The lower border of the range. Is considered lower than <paramref name="_this"/> if null.</param>
        /// <param name="max">The upper border of the range. Is considered higher than <paramref name="_this"/> if null.</param>
        /// <returns>The clamped version of <paramref name="value"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="_this"/> is null.</exception>
        /// <example>
        /// <code>
        /// var result = new MyComparer().Clamp(value, min, max);
        /// </code>
        /// </example>
        public static T Clamp <T>(this IComparer <T> _this, T value, T min, T max)
        {
            Throw.If.Object.IsNull(_this, nameof(_this));
            Throw.If.Object.IsNull(value, nameof(value));

            if (min != null && max != null && _this.IsGreaterThan(min, max))
            {
                return(_this.Clamp(value, max, min));
            }

            T result = value;

            if (min != null)
            {
                result = _this.Max(value, min);
            }

            if (max != null)
            {
                result = _this.Min(result, max);
            }

            return(result);
        }
Beispiel #4
0
 /// <summary>
 /// Returns least item in sequence according to comparer, preferring earlier elements.
 /// </summary>
 public static Maybe <A> MinByMaybe <A>(this IEnumerable <A> xs, IComparer <A> comparer) =>
 xs.Aggregate(None <A>(), (m, x2) => m.Select(x1 => comparer.Min(x1, x2)).Or(Some(x2)));
Beispiel #5
0
 public static M Min <M>(this IComparer <M> g, params M[] source)
 {
     return(g.Min(source.ToList()));
 }