using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- Arrays --------------------------------------------
///
/// Returns a number that represents how many elements in the specified
/// array satisfy a condition.
///
/// An array that contains elements to be tested and counted.
/// A function to test each element for a condition.
/// A number that represents how many elements in the array satisfy the condition
/// in the predicate function.
public static int CountF(this T[] source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
int count = 0;
for (int i = 0; i < source.Length; i++)
{
checked
{
if (predicate(source[i]))
{
count++;
}
}
}
return count;
}
// ------------------------------ Lists ---------------------
///
/// Returns a number that represents how many elements in the specified
/// list satisfy a condition.
///
/// A list that contains elements to be tested and counted.
/// A function to test each element for a condition.
/// A number that represents how many elements in the list satisfy the condition
/// in the predicate function.
public static int CountF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
int count = 0;
for (int i = 0; i < source.Count; i++)
{
checked
{
if (predicate(source[i]))
{
count++;
}
}
}
return count;
}
}
}