-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount.cs
73 lines (64 loc) · 2.65 KB
/
Count.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
namespace OpenLinq
{
public static partial class EnumerableExt
{
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <typeparam name="T">The type of the elements of source.</typeparam>
/// <param name="source">A sequence that contains elements to be counted.</param>
/// <returns>The number of elements in the input sequence.</returns>
/// <exception cref="System.ArgumentNullException">source or predicate is null.</exception>
/// <exception cref="System.OverflowException">The number of elements in source is larger than System.Int32.MaxValue.</exception>
public static int Count<T>(this IEnumerable<T> source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
checked
{
int count = 0;
using (IEnumerator<T> iterator = source.GetEnumerator())
{
while (iterator.MoveNext())
{
count += 1;
}
iterator.Dispose();
return count;
}
}
}
/// <summary>
/// A number that represents how many elements in the sequence satisfy the condition in the predicate function.
/// </summary>
/// <typeparam name="T">The type of the elements of source.</typeparam>
/// <param name="source">A sequence that contains elements to be tested and counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>The number of elements in the input sequence.</returns>
/// <exception cref="System.ArgumentNullException">source or predicate is null.</exception>
/// <exception cref="System.OverflowException">The number of elements in source is larger than System.Int32.MaxValue.</exception>
public static int Count<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException("source");
checked
{
int sum = 0;
using (IEnumerator<T> iterator = source.GetEnumerator())
{
while (iterator.MoveNext())
{
if (predicate(iterator.Current))
{
sum += 1;
}
}
return sum;
}
}
}
}
}