using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- ARRAYS --------------------------------------------
///
/// Computes the average of an array
///
/// The array to calculate the average of.
/// The average of the array.
public static double AverageF(this int[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
long sum = 0;
checked
{
for (int i = 0; i < source.Length; i++)
{
sum += source[i];
}
}
return (double)sum / source.Length;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static double AverageF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
long sum = 0;
checked
{
for (int i = 0; i < source.Length; i++)
{
sum += selector(source[i]);
}
}
return (double)sum / source.Length;
}
///
/// Computes the average of an array
///
/// The array to calculate the average of.
/// The average of the array.
public static double AverageF(this long[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
long sum = 0;
checked
{
for (int i = 0; i < source.Length; i++)
{
sum += source[i];
}
}
return (double)sum / source.Length;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static double AverageF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
long sum = 0;
checked
{
for (int i = 0; i < source.Length; i++)
{
sum += selector(source[i]);
}
}
return (double)sum / source.Length;
}
///
/// Computes the average of an array
///
/// The array to calculate the average of.
/// The average of the array.
public static float AverageF(this float[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
double sum = 0;
for (int i = 0; i < source.Length; i++)
{
sum += source[i];
}
return (float)(sum / source.Length);
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static float AverageF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
for (int i = 0; i < source.Length; i++)
{
sum += selector(source[i]);
}
return (float)(sum / source.Length);
}
///
/// Computes the average of an array
///
/// The array to calculate the average of.
/// The average of the array.
public static double AverageF(this double[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
double sum = 0;
for (int i = 0; i < source.Length; i++)
{
sum += source[i];
}
return sum / source.Length;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static double AverageF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
for (int i = 0; i < source.Length; i++)
{
sum += selector(source[i]);
}
return sum / source.Length;
}
///
/// Computes the average of an array
///
/// The array to calculate the average of.
/// The average of the array.
public static decimal AverageF(this decimal[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
decimal sum = 0;
for (int i = 0; i < source.Length; i++)
{
sum += source[i];
}
return sum / source.Length;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static decimal AverageF(this T[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
decimal sum = 0;
for (int i = 0; i < source.Length; i++)
{
sum += selector(source[i]);
}
return sum / source.Length;
}
// -------------------------- Lists --------------------------------------------
///
/// Computes the average of a list.
///
/// The list to calculate the average of.
/// The average of the list.
public static double AverageF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
long sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
}
return (double)sum / source.Count;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static double AverageF(this List source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
long sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
sum += selector(source[i]);
}
}
return (double)sum / source.Count;
}
///
/// Computes the average of a list.
///
/// The list to calculate the average of.
/// The average of the list.
public static double AverageF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
long sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
}
return (double)sum / source.Count;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static double AverageF(this List source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
long sum = 0;
checked
{
for (int i = 0; i < source.Count; i++)
{
sum += selector(source[i]);
}
}
return (double)sum / source.Count;
}
///
/// Computes the average of a list.
///
/// The list to calculate the average of.
/// The average of the list.
public static float AverageF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
return (float)(sum / source.Count);
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static float AverageF(this List source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
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 / source.Count);
}
///
/// Computes the average of a list.
///
/// The list to calculate the average of.
/// The average of the list.
public static double AverageF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
return sum / source.Count;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static double AverageF(this List source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
double sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += selector(source[i]);
}
return sum / source.Count;
}
///
/// Computes the average of a list.
///
/// The list to calculate the average of.
/// The average of the list.
public static decimal AverageF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
decimal sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += source[i];
}
return sum / source.Count;
}
///
/// Computes the average of values obtained by invoking a transform function on
/// each element of the input array.
///
/// The array to calculate the transformed average of.
/// A transform function to apply to each element.
/// The average of the array.
public static decimal AverageF(this List source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
decimal sum = 0;
for (int i = 0; i < source.Count; i++)
{
sum += selector(source[i]);
}
return sum / source.Count;
}
}
}