using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- ARRAYS --------------------------------------------
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static int WhereSumF(this int[] source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
int sum = 0;
checked
{
foreach (var v in source)
{
if (predicate(v))
{
sum += v;
}
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static int WhereSumF(this T[] source,Func predicate, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
int sum = 0;
checked
{
foreach (var v in source)
{
if (predicate(v))
{
sum += selector(v);
}
}
}
return sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static long WhereSumF(this long[] source,Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
long sum = 0;
checked
{
foreach (var v in source)
{
if (predicate(v))
{
sum += v;
}
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static long WhereSumF(this T[] source,Func predicate, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
long sum = 0;
checked
{
foreach (var v in source)
{
if (predicate(v))
{
sum += selector(v);
}
}
}
return sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static float WhereSumF(this float[] source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
foreach (var v in source)
{
if (predicate(v))
{
sum += v;
}
}
return (float)sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static float WhereSumF(this T[] source, Func predicate,Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
foreach (var v in source)
{
if (predicate(v))
{
sum += selector(v);
}
}
return (float)sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static double WhereSumF(this double[] source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
foreach (var v in source)
{
if (predicate(v))
{
sum += v;
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static double WhereSumF(this T[] source, Func predicate,Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
foreach (var v in source)
{
if (predicate(v))
{
sum += selector(v);
}
}
return sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static decimal WhereSumF(this decimal[] source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
decimal sum = 0;
foreach (var v in source)
{
if (predicate(v))
{
sum += v;
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static decimal WhereSumF(this T[] source,Func predicate, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
decimal sum = 0;
foreach (var v in source)
{
if (predicate(v))
{
sum += selector(v);
}
}
return sum;
}
// -------------------------- LISTS --------------------------------------------
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static int WhereSumF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
int sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += s;
}
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static int WhereSumF(this List source, Func predicate, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
int sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += selector(s);
}
}
}
return sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static long WhereSumF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
long sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += s;
}
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static long WhereSumF(this List source, Func predicate, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
long sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += selector(s);
}
}
}
return sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static float WhereSumF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += s;
}
}
return (float)sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static float WhereSumF(this List source, Func predicate,Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += selector(s);
}
}
return (float)sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static double WhereSumF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += s;
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static double WhereSumF(this List source,Func predicate, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += selector(s);
}
}
return sum;
}
///
/// Adds the values in the sequence that match the where predicate.
///
/// The sequence to add.
/// A function to filter the sequence with before summing.
/// The sum of the sequence.
public static decimal WhereSumF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
decimal sum = 0;
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += s;
}
}
return sum;
}
///
/// Performs a filter with the where predicate, then sums the transformed values.
///
/// The sequence of values to transform then sum.
/// A function to filter the sequence with before summing.
/// A transformation function.
/// The sum of the transformed elements.
public static decimal WhereSumF(this List source, Func predicate,Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
decimal sum = 0;
for (int i = 0; i < source.Count; i++)
{
var s = source[i];
if (predicate(s))
{
sum += selector(s);
}
}
return sum;
}
}
}