using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
///
/// Provides faster array and list specific extension methods with
/// the same semantics as the Linq extensions methods.
///
public static partial class LinqFaster
{
// ------------------------------ Arrays --------------------------
///
/// Applies an accumulator function over an array.
///
/// An array to aggregate over.
/// An accumulator function to be invoked on each element
/// The final accumulator value
public static TSource AggregateF(this TSource[] source, Func func)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
TSource result = source[0];
for (int i = 1; i < source.Length; i++)
{
result = func(result, source[i]);
}
return result;
}
///
/// Applies an accumulator function over an array. The specified seed
/// value is used as the initial accumulator value.
///
/// An array to aggregate over.
/// The initial accumulator value.
/// An accumulator function to be invoked on each element
/// The final accumulator value
public static TAccumulate AggregateF(this TSource[] source, TAccumulate seed, Func func)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
TAccumulate result = seed;
foreach (var v in source)
{
result = func(result, v);
}
return result;
}
///
/// Applies an accumulator function over an array. The specified seed
/// value is used as the initial accumulator value, and the specified
/// function is used to select the result value.
///
/// An array to aggregate over.
/// The initial accumulator value.
/// An accumulator function to be invoked on each element
/// A function to transform the final accumulator value into the result value.
/// The transformed final accumulator value
public static TResult AggregateF(this TSource[] source, TAccumulate seed, Func func, Func resultSelector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
if (resultSelector == null)
{
throw Error.ArgumentNull("resultSelector");
}
TAccumulate result = seed;
foreach (var v in source)
{
result = func(result, v);
}
return resultSelector(result);
}
// ------------------------------ Lists --------------------------
///
/// Applies an accumulator function over a List.
///
///
/// A List to aggregate over.
/// An accumulator function to be invoked on each element
/// The final accumulator value
public static TSource AggregateF(this List source, Func func)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
TSource result = source[0];
for (int i = 1; i < source.Count; i++)
{
result = func(result, source[i]);
}
return result;
}
///
/// Applies an accumulator function over a List. The specified seed
/// value is used as the initial accumulator value.
///
/// A List to aggregate over.
/// The initial accumulator value.
/// An accumulator function to be invoked on each element
/// The final accumulator value
public static TAccumulate AggregateF(this List source, TAccumulate seed, Func func)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
TAccumulate result = seed;
for (int i = 0; i < source.Count; i++)
{
result = func(result, source[i]);
}
return result;
}
///
/// Applies an accumulator function over a List. The specified seed
/// value is used as the initial accumulator value, and the specified
/// function is used to select the result value.
///
/// A List to aggregate over.
/// The initial accumulator value.
/// An accumulator function to be invoked on each element
/// A function to transform the final accumulator value into the result value.
/// The transformed final accumulator value
public static TResult AggregateF(this List source, TAccumulate seed, Func func, Func resultSelector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
if (resultSelector == null)
{
throw Error.ArgumentNull("resultSelector");
}
TAccumulate result = seed;
for (int i = 0; i < source.Count; i++)
{
result = func(result, source[i]);
}
return resultSelector(result);
}
}
}