using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- ARRAYS --------------------------------------------
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static int SumF(this int[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
int sum = 0;
checked
{
foreach (var v in source)
{
sum += v;
}
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static int SumF(this T[] source, 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)
{
sum += selector(v);
}
}
return sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static long SumF(this long[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
long sum = 0;
checked
{
foreach (var v in source)
{
sum += v;
}
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static long SumF(this T[] source, 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)
{
sum += selector(v);
}
}
return sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static float SumF(this float[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
foreach (var v in source)
{
sum += v;
}
return (float)sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static float SumF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
foreach (var v in source)
{
sum += selector(v);
}
return (float)sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static double SumF(this double[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
foreach (var v in source)
{
sum += v;
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static double SumF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
foreach (var v in source)
{
sum += selector(v);
}
return sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static decimal SumF(this decimal[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
decimal sum = 0;
foreach (var v in source)
{
sum += v;
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static decimal SumF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
decimal sum = 0;
foreach (var v in source)
{
sum += selector(v);
}
return sum;
}
// -------------------------- LISTS --------------------------------------------
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static int SumF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
int sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static int SumF(this List source, 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++)
{
sum += selector(source[i]);
}
}
return sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static long SumF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
long sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static long SumF(this List source, 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++)
{
sum += selector(source[i]);
}
}
return sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static float SumF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
return (float)sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static float SumF(this List source, 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++)
{
sum += selector(source[i]);
}
return (float)sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static double SumF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static double SumF(this List source, 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++)
{
sum += selector(source[i]);
}
return sum;
}
///
/// Adds a sequence of values.
///
/// The sequence to add.
/// The sum of the sequence.
public static decimal SumF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
decimal sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
return sum;
}
///
/// Adds the transformed sequence of elements.
///
/// The sequence of values to transform then sum.
/// A transformation function.
/// The sum of the transformed elements.
public static decimal SumF(this List source, 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++)
{
sum += selector(source[i]);
}
return sum;
}
}
}