using System; using System.Collections.Generic; namespace JM.LinqFaster { //int, long, float,double, decimal public static partial class LinqFaster { // -------------------------- ARRAYS -------------------------------------------- /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static T MaxF(this T[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } Comparer comparer = Comparer.Default; T r = default(T); if (r == null) { r = source[0]; for (int i = 1; i < source.Length; i++) { if (source[i] != null && comparer.Compare(source[i], r) > 0) r = source[i]; } } else { r = source[0]; for (int i = 1; i < source.Length; i++) { if (comparer.Compare(source[i], r) > 0) r = source[i]; } } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static TResult MaxF(this T[] source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (selector == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } Comparer comparer = Comparer.Default; TResult r = default(TResult); if (r == null) { r = selector(source[0]); for (int i = 1; i < source.Length; i++) { var v = selector(source[i]); if (v != null && comparer.Compare(v, r) > 0) r = v; } } else { r = selector(source[0]); for (int i = 1; i < source.Length; i++) { var v = selector(source[i]); if (comparer.Compare(v, r) > 0) r = v; } } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static int MaxF(this int[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } int r = int.MinValue; for (int i = 0; i < source.Length; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static int MaxF(this T[] source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } int r = int.MinValue; for (int i = 0; i < source.Length; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static long MaxF(this long[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } long r = long.MinValue; for (int i = 0; i < source.Length; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static long MaxF(this T[] source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } long r = long.MinValue; for (int i = 0; i < source.Length; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static float MaxF(this float[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } float r = source[0]; int startIndex = 0; for (; startIndex < source.Length; startIndex++) { if (!float.IsNaN(source[startIndex])) { r = source[startIndex]; break; } } for (int i = startIndex; i < source.Length; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static float MaxF(this T[] source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } float r = selector(source[0]); int startIndex = 0; for (; startIndex < source.Length; startIndex++) { var v = selector(source[startIndex]); if (!float.IsNaN(v)) { r = v; break; } } for (int i = startIndex; i < source.Length; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static double MaxF(this double[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } double r = source[0]; int startIndex = 0; for (; startIndex < source.Length; startIndex++) { if (!double.IsNaN(source[startIndex])) { r = source[startIndex]; break; } } for (int i = startIndex; i < source.Length; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static double MaxF(this T[] source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } double r = selector(source[0]); int startIndex = 0; for (; startIndex < source.Length; startIndex++) { var v = selector(source[startIndex]); if (!double.IsNaN(v)) { r = v; break; } } for (int i = startIndex; i < source.Length; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static decimal MaxF(this decimal[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } decimal r = decimal.MinValue; for (int i = 0; i < source.Length; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static decimal MaxF(this T[] source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } decimal r = decimal.MinValue; for (int i = 0; i < source.Length; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } // -------------------------- LISTS -------------------------------------------- /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static T MaxF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } Comparer comparer = Comparer.Default; T r = default(T); if (r == null) { r = source[0]; for (int i = 1; i < source.Count; i++) { if (source[i] != null && comparer.Compare(source[i], r) > 0) r = source[i]; } } else { r = source[0]; for (int i = 1; i < source.Count; i++) { if (comparer.Compare(source[i], r) > 0) r = source[i]; } } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static TResult MaxF(this List source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (selector == null) { throw Error.ArgumentNull("selector"); } if (source.Count == 0) { throw Error.NoElements(); } Comparer comparer = Comparer.Default; TResult r = default(TResult); if (r == null) { r = selector(source[0]); for (int i = 1; i < source.Count; i++) { var v = selector(source[i]); if (v != null && comparer.Compare(v, r) > 0) r = v; } } else { r = selector(source[0]); for (int i = 1; i < source.Count; i++) { var v = selector(source[i]); if (comparer.Compare(v, r) > 0) r = v; } } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static int MaxF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } int r = int.MinValue; for (int i = 0; i < source.Count; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static int MaxF(this List source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } int r = int.MinValue; for (int i = 0; i < source.Count; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static long MaxF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } long r = long.MinValue; for (int i = 0; i < source.Count; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static long MaxF(this List source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("source"); } long r = long.MinValue; for (int i = 0; i < source.Count; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static float MaxF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } float r = source[0]; int startIndex = 0; for (; startIndex < source.Count; startIndex++) { if (!float.IsNaN(source[startIndex])) { r = source[startIndex]; break; } } for (int i = startIndex; i < source.Count; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static float MaxF(this List source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } float r = selector(source[0]); int startIndex = 0; for (; startIndex < source.Count; startIndex++) { var v = selector(source[startIndex]); if (!float.IsNaN(v)) { r = v; break; } } for (int i = startIndex; i < source.Count; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static double MaxF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } double r = source[0]; int startIndex = 0; for (; startIndex < source.Count; startIndex++) { if (!double.IsNaN(source[startIndex])) { r = source[startIndex]; break; } } for (int i = startIndex; i < source.Count; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static double MaxF(this List source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } if (selector == null) { throw Error.ArgumentNull("selector"); } double r = selector(source[0]); int startIndex = 0; for (; startIndex < source.Count; startIndex++) { var v = selector(source[startIndex]); if (!double.IsNaN(v)) { r = v; break; } } for (int i = startIndex; i < source.Count; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } /// /// Returns the maximum value in a sequence of values. /// /// A sequence of values to determine the maximum of. /// The maximum value in the sequence public static decimal MaxF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } decimal r = decimal.MinValue; for (int i = 0; i < source.Count; i++) { if (source[i] > r) r = source[i]; } return r; } /// /// Invokes a transform function on each element of a sequence and returns the maximum value. /// /// A sequence of values to determine the maximum value of. /// A transform function to apply to each element. /// The maximum value in the transform of the sequence. public static decimal MaxF(this List source, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } decimal r = decimal.MinValue; for (int i = 0; i < source.Count; i++) { var v = selector(source[i]); if (v > r) r = v; } return r; } } }