Skip to content

Commit 3933e24

Browse files
committed
added Count overloads for pre net8.0 versions.
1 parent 6d0ab79 commit 3933e24

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/Extensions/ReadOnlySpan/Linq/Count.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,48 @@ public static int Count<T>(this ReadOnlySpan<T> source, Predicate<T> predicate)
4747

4848
return count;
4949
}
50+
51+
#if !NET8_0_OR_GREATER
52+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
53+
/// <typeparam name="T">The element type of the span.</typeparam>
54+
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
55+
/// <param name="value">The value for which to search.</param>
56+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
57+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
58+
public static int Count<T>(this ReadOnlySpan<T> source, T value) where T : IEquatable<T>
59+
{
60+
int count = 0;
61+
62+
foreach(var item in source)
63+
{
64+
if(item.Equals(value))
65+
{
66+
count++;
67+
}
68+
}
69+
70+
return count;
71+
}
72+
73+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
74+
/// <typeparam name="T">The element type of the span.</typeparam>
75+
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
76+
/// <param name="value">The value for which to search.</param>
77+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
78+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
79+
public static int Count<T>(this ReadOnlySpan<T> source, ReadOnlySpan<T> value) where T : IEquatable<T>
80+
{
81+
int count = 0;
82+
int current = 0;
83+
84+
while((current = source.IndexOf(value)) != -1)
85+
{
86+
source = source.Slice(current + value.Length);
87+
count++;
88+
}
89+
90+
return count;
91+
}
92+
#endif
5093
}
5194
}

src/Extensions/Span/Linq/Count.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,29 @@ public static int Count<T>(this Span<T> source, Predicate<T> predicate)
2929
{
3030
return ReadOnlySpanExtensions.Count(source, predicate);
3131
}
32+
33+
#if !NET8_0_OR_GREATER
34+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
35+
/// <typeparam name="T">The element type of the span.</typeparam>
36+
/// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
37+
/// <param name="value">The value for which to search.</param>
38+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
39+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
40+
public static int Count<T>(this Span<T> source, T value) where T : IEquatable<T>
41+
{
42+
return ReadOnlySpanExtensions.Count(source, value);
43+
}
44+
45+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
46+
/// <typeparam name="T">The element type of the span.</typeparam>
47+
/// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
48+
/// <param name="value">The value for which to search.</param>
49+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
50+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
51+
public static int Count<T>(this Span<T> source, ReadOnlySpan<T> value) where T : IEquatable<T>
52+
{
53+
return ReadOnlySpanExtensions.Count(source, value);
54+
}
55+
#endif
3256
}
3357
}

0 commit comments

Comments
 (0)