Aetheria/Assets/Scripts/ServerShared/NIH/LinqFaster/Count.cs

86 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- Arrays --------------------------------------------
/// <summary>
/// Returns a number that represents how many elements in the specified
/// array satisfy a condition.
/// </summary>
/// <param name="source">An array that contains elements to be tested and counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>A number that represents how many elements in the array satisfy the condition
/// in the predicate function.</returns>
public static int CountF<T>(this T[] source, Func<T, bool> 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 ---------------------
/// <summary>
/// Returns a number that represents how many elements in the specified
/// list satisfy a condition.
/// </summary>
/// <param name="source">A list that contains elements to be tested and counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>A number that represents how many elements in the list satisfy the condition
/// in the predicate function.</returns>
public static int CountF<T>(this List<T> source, Func<T, bool> 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;
}
}
}