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