using System; using System.Collections.Generic; namespace JM.LinqFaster { public static partial class LinqFaster { // -------------------------- ARRAYS -------------------------------------------- /// /// Combined Where and Select for optimal performance. /// /// The input sequence to filter then transform. /// A function to use to filter the sequence. /// A function to transform the filtered elements. /// A sequence of filtered and transformed elements. public static TResult[] WhereSelectF(this T[] source, Func predicate, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } if (selector == null) { throw Error.ArgumentNull("predicate"); } var result = new TResult[source.Length]; int idx = 0; for (int i = 0; i < source.Length; i++) { if (predicate(source[i])) { result[idx] = selector(source[i]); idx++; } } Array.Resize(ref result, idx); return result; } /// /// Combined Where and Select for optimal performance that uses the index in the /// predicate and selector. /// /// The input sequence to filter then transform. /// A function to use to filter the sequence. /// A function to transform the filtered elements. /// A sequence of filtered and transformed elements. public static TResult[] WhereSelectF(this T[] source, Func predicate, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } if (selector == null) { throw Error.ArgumentNull("predicate"); } var result = new TResult[source.Length]; int idx = 0; for (int i = 0; i < source.Length; i++) { if (predicate(source[i], i)) { result[idx] = selector(source[i], idx); idx++; } } Array.Resize(ref result, idx); return result; } // -------------------------- LISTS -------------------------------------------- /// /// Combined Where and Select for optimal performance. /// /// The input sequence to filter then transform. /// A function to use to filter the sequence. /// A function to transform the filtered elements. /// A sequence of filtered and transformed elements. public static List WhereSelectF(this List source, Func predicate, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } if (selector == null) { throw Error.ArgumentNull("predicate"); } var r = new List(); for (int i = 0; i < source.Count; i++) { if (predicate(source[i])) r.Add(selector(source[i])); } return r; } /// /// Combined Where and Select for optimal performance that uses the index in the /// predicate and selector. /// /// The input sequence to filter then transform. /// A function to use to filter the sequence. /// A function to transform the filtered elements. /// A sequence of filtered and transformed elements. public static List WhereSelectF(this List source, Func predicate, Func selector) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } if (selector == null) { throw Error.ArgumentNull("predicate"); } var r = new List(); int idx = 0; for (int i = 0; i < source.Count; i++) { if (predicate(source[i], i)) { r.Add(selector(source[i], idx)); idx++; } } return r; } } }