Skip to content

Commit fe95d69

Browse files
committed
added Count; Where; AsEnumerable.
1 parent d9bd5c5 commit fe95d69

File tree

7 files changed

+214
-1
lines changed

7 files changed

+214
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace SpanExtensions
5+
{
6+
public static partial class ReadOnlySpanExtensions
7+
{
8+
/// <summary>
9+
/// Returns the input typed as <see cref="IEnumerable{T}"/>.
10+
/// </summary>
11+
/// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
12+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
13+
/// <returns>The input sequence typed as <see cref="IEnumerable{T}"/>.</returns>
14+
public static IEnumerable<T> AsEnumerable<T>(this ReadOnlySpan<T> source)
15+
{
16+
ReadOnlySpan<T>.Enumerator e = source.GetEnumerator();
17+
18+
while(e.MoveNext())
19+
{
20+
yield return e.Current;
21+
}
22+
}
23+
}
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
3+
namespace SpanExtensions
4+
{
5+
public static partial class ReadOnlySpanExtensions
6+
{
7+
/// <summary>
8+
/// Returns the number of elements in a <see cref="ReadOnlySpan{T}"/>.
9+
/// </summary>
10+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
11+
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
12+
/// <returns>The number of elements in <paramref name="source"/>.</returns>
13+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
14+
public static int Count<T>(this ReadOnlySpan<T> source)
15+
{
16+
return source.Length;
17+
}
18+
19+
/// <summary>
20+
/// Returns a number that represents how many elements in the specified sequence satisfy a condition.
21+
/// </summary>
22+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
23+
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> that contains elements to be tested and counted.</param>
24+
/// <param name="predicate">A function to test each element for a condition.</param>
25+
/// <returns>A number that represents how many elements in <paramref name="source"/> satisfy the condition in the predicate function.</returns>
26+
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
27+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
28+
public static int Count<T>(this ReadOnlySpan<T> source, Predicate<T> predicate)
29+
{
30+
if(predicate is null)
31+
{
32+
throw new ArgumentNullException(nameof(predicate));
33+
}
34+
35+
int count = 0;
36+
37+
foreach(var item in source)
38+
{
39+
checked
40+
{
41+
if(predicate(item))
42+
{
43+
count++;
44+
}
45+
}
46+
}
47+
48+
return count;
49+
}
50+
}
51+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace SpanExtensions
5+
{
6+
public static partial class ReadOnlySpanExtensions
7+
{
8+
/// <summary>
9+
/// Filters a sequence of values based on a predicate.
10+
/// </summary>
11+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
12+
/// <param name="source">The <see cref="ReadOnlySpan{T}"/> to filter.</param>
13+
/// <param name="predicate">A function to test each element for a condition.</param>
14+
/// <returns>A <see cref="ReadOnlySpan{T}"/> that contains elements from the input sequence that satisfy the condition.</returns>
15+
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
16+
public static IEnumerable<T> Where<T>(this ReadOnlySpan<T> source, Predicate<T> predicate)
17+
{
18+
ReadOnlySpan<T>.Enumerator e = source.GetEnumerator();
19+
20+
while(e.MoveNext())
21+
{
22+
T current = e.Current;
23+
24+
if(predicate(current))
25+
{
26+
yield return current;
27+
}
28+
}
29+
}
30+
31+
/// <summary>
32+
/// Filters a sequence of values based on a predicate.
33+
/// </summary>
34+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
35+
/// <param name="source">The <see cref="ReadOnlySpan{T}"/> to filter.</param>
36+
/// <param name="predicate">A function to test each element for a condition.</param>
37+
/// <returns>A <see cref="ReadOnlySpan{T}"/> that contains elements from the input sequence that satisfy the condition.</returns>
38+
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
39+
public static IEnumerable<T> Where<T>(this ReadOnlySpan<T> source, Func<T, int, bool> predicate)
40+
{
41+
for(int i = 0; i < source.Length; i++)
42+
{
43+
T current = source[i];
44+
45+
if(predicate(current, i))
46+
{
47+
yield return current;
48+
}
49+
}
50+
}
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace SpanExtensions
5+
{
6+
public static partial class SpanExtensions
7+
{
8+
/// <summary>
9+
/// Returns the input typed as <see cref="IEnumerable{T}"/>.
10+
/// </summary>
11+
/// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
12+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
13+
/// <returns>The input sequence typed as <see cref="IEnumerable{T}"/>.</returns>
14+
public static IEnumerable<T> AsEnumerable<T>(this Span<T> source)
15+
{
16+
return ReadOnlySpanExtensions.AsEnumerable<T>(source);
17+
}
18+
}
19+
}

src/Extensions/Span/Linq/Count.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
namespace SpanExtensions
4+
{
5+
public static partial class SpanExtensions
6+
{
7+
/// <summary>
8+
/// Returns the number of elements in a <see cref="Span{T}"/>.
9+
/// </summary>
10+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
11+
/// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
12+
/// <returns>The number of elements in <paramref name="source"/>.</returns>
13+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
14+
public static int Count<T>(this Span<T> source)
15+
{
16+
return ReadOnlySpanExtensions.Count<T>(source);
17+
}
18+
19+
/// <summary>
20+
/// Returns a number that represents how many elements in the specified sequence satisfy a condition.
21+
/// </summary>
22+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
23+
/// <param name="source">A <see cref="Span{T}"/> that contains elements to be tested and counted.</param>
24+
/// <param name="predicate">A function to test each element for a condition.</param>
25+
/// <returns>A number that represents how many elements in <paramref name="source"/> satisfy the condition in the predicate function.</returns>
26+
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
27+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
28+
public static int Count<T>(this Span<T> source, Predicate<T> predicate)
29+
{
30+
return ReadOnlySpanExtensions.Count(source, predicate);
31+
}
32+
}
33+
}

src/Extensions/Span/Linq/SingleOrDefault.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static T SingleOrDefault<T>(this Span<T> source, Predicate<T> predicate,
3939
/// <returns>The single element in <paramref name="source"/>.</returns>
4040
public static T? SingleOrDefault<T>(this Span<T> source)
4141
{
42-
return ReadOnlySpanExtensions.SingleOrDefault<T>(source);
42+
return ReadOnlySpanExtensions.SingleOrDefault<T>(source);
4343
}
4444

4545
/// <summary>

src/Extensions/Span/Linq/Where.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace SpanExtensions
5+
{
6+
public static partial class SpanExtensions
7+
{
8+
/// <summary>
9+
/// Filters a sequence of values based on a predicate.
10+
/// </summary>
11+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
12+
/// <param name="source">The <see cref="Span{T}"/> to filter.</param>
13+
/// <param name="predicate">A function to test each element for a condition.</param>
14+
/// <returns>A <see cref="Span{T}"/> that contains elements from the input sequence that satisfy the condition.</returns>
15+
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
16+
public static IEnumerable<T> Where<T>(this Span<T> source, Predicate<T> predicate)
17+
{
18+
return ReadOnlySpanExtensions.Where(source, predicate);
19+
}
20+
21+
/// <summary>
22+
/// Filters a sequence of values based on a predicate.
23+
/// </summary>
24+
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
25+
/// <param name="source">The <see cref="Span{T}"/> to filter.</param>
26+
/// <param name="predicate">A function to test each element for a condition.</param>
27+
/// <returns>A <see cref="Span{T}"/> that contains elements from the input sequence that satisfy the condition.</returns>
28+
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
29+
public static IEnumerable<T> Where<T>(this Span<T> source, Func<T, int, bool> predicate)
30+
{
31+
return ReadOnlySpanExtensions.Where(source, predicate);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)