using System.Linq;
using System.Diagnostics;
namespace System.Collections.Generic
{
public static class EnumeratorExtensions
{
///
/// Returns true if there are no more than a set number of items in the IEnumerable<T>.
///
///
/// The source list of items.
/// The number of items that must exist.
/// True if there are no more than the number of items, false otherwise.
public static bool AtMost(this IEnumerable source, int number)
{
if (source == null)
throw new ArgumentNullException("source");
bool result;
int count = 0;
using (IEnumerator data = source.GetEnumerator())
{
while (count < number && data.MoveNext())
{
count++;
}
result = !data.MoveNext();
}
return result;
}
///
/// Returns true if there are no more than a set number of items in the IEnumerable<T> that satisfy a given condition.
///
///
/// The source list of items.
/// The number of items that must exist.
/// The condition to apply to the items.
/// True if there are no more than the number of items, false otherwise.
public static bool AtMost(this IEnumerable source, int number, Func condition)
{
if (source == null)
throw new ArgumentNullException("source");
if (condition == null)
throw new ArgumentNullException("condition");
bool result;
int count = 0;
using (IEnumerator data = source.GetEnumerator())
{
while (count < number && data.MoveNext())
{
if (condition(data.Current))
count++;
}
result = !data.MoveNext();
}
return result;
}
}
}