public static T2 WhereSumF <T, T2>(this ReadOnlySpan <T> source, Func <T, bool> predicate, Func <T, T2> selector) { if (source == null) { throw Error.ArgumentNull(nameof(source)); } if (predicate == null) { throw Error.ArgumentNull(nameof(predicate)); } if (selector == null) { throw Error.ArgumentNull(nameof(selector)); } T2 a = default(T2); checked { for (int index = 0; index < source.Length; index++) { if (predicate(source[index])) { a = GenericOperators.Add(a, selector(source[index])); } } } return(a); }
public static T2 SumF <T, T2>(this IReadOnlyList <T> source, Func <T, T2> selector) { switch (source) { case null: throw Error.ArgumentNull(nameof(source)); case T[] sa: return(sa.SumF(selector)); case List <T> sl: return(sl.SumF(selector)); default: { if (selector == null) { throw Error.ArgumentNull(nameof(selector)); } int sourceCount = source.Count; T2 a = default(T2); checked { for (int index = 0; index < sourceCount; index++) { a = GenericOperators.Add(a, selector(source[index])); } } return(a); } } }
public static T2 WhereSumF <T, T2>(this T[] source, Func <T, bool> predicate, Func <T, T2> selector) { if (source == null) { throw Error.ArgumentNull(nameof(source)); } if (predicate == null) { throw Error.ArgumentNull(nameof(predicate)); } if (selector == null) { throw Error.ArgumentNull(nameof(selector)); } T2 a = default(T2); checked { foreach (T b in source) { if (predicate(b)) { a = GenericOperators.Add(a, selector(b)); } } } return(a); }
public static T2 SumF <T, T2>(this List <T> source, Func <T, T2> selector) { if (source == null) { throw Error.ArgumentNull(nameof(source)); } if (selector == null) { throw Error.ArgumentNull(nameof(selector)); } T2 a = default(T2); checked { for (int index = 0; index < source.Count; index++) { a = GenericOperators.Add(a, selector(source[index])); } } return(a); }
/// <summary> /// When the effector is InPlace and RelativeToStart, this /// combines the initial and combined values created in "GetNext". /// /// This exists for optimization purposes. InPlace RelativeToStart /// effectors are common, and GenericOperators.Add is really slow /// by comparison to simply adding the two values. Thus, for extra /// speed, child classes with explicit T2 values can reimplement a /// faster version of this method. /// </summary> /// <param name="init"></param> /// <param name="combined"></param> /// <returns></returns> protected virtual T2 Combine_InPlaceAndRelativeToStart(T2 init, T2 combined) { return(GenericOperators.Add <T2>(init, combined)); }