1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:28:48 +00:00

Add [ReadOnly]Span<T>.Count(Predicate<T>)

This commit is contained in:
Oliver Booth 2022-05-12 13:52:04 +01:00
parent 1428618ca9
commit 23e07b65b8
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
5 changed files with 128 additions and 0 deletions

View File

@ -15,7 +15,9 @@
- X10D: Added `IEnumerable<T>.DisposeAll()`
- X10D: Added `IEnumerable<T>.DisposeAllAsync()`
- X10D: Added `char.IsEmoji`
- X10D: Added `ReadOnlySpan<T>.Count(Predicate<T>)`
- X10D: Added `Rune.IsEmoji`
- X10D: Added `Span<T>.Count(Predicate<T>)`
- X10D: Added `string.IsEmoji`
- X10D: Added `Vector2.WithX()`
- X10D: Added `Vector2.WithY()`

View File

@ -55,4 +55,28 @@ public class ReadOnlySpanTests
return span.Any(null!);
});
}
[TestMethod]
public void Count_ShouldReturn0_GivenEmptySpan()
{
var span = new ReadOnlySpan<int>();
Assert.AreEqual(0, span.Count(i => i % 2 == 0));
}
[TestMethod]
public void Count_ShouldReturn5_ForEvenNumbers_GivenNumbers1To10()
{
var span = new ReadOnlySpan<int>(new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
Assert.AreEqual(5, span.Count(i => i % 2 == 0));
}
[TestMethod]
public void Count_ShouldThrow_GivenNullPredicate()
{
Assert.ThrowsException<ArgumentNullException>(() =>
{
var span = new ReadOnlySpan<int>();
return span.Count(null!);
});
}
}

View File

@ -55,4 +55,28 @@ public class SpanTests
return span.Any(null!);
});
}
[TestMethod]
public void Count_ShouldReturn0_GivenEmptySpan()
{
var span = new Span<int>();
Assert.AreEqual(0, span.Count(i => i % 2 == 0));
}
[TestMethod]
public void Count_ShouldReturn5_ForEvenNumbers_GivenNumbers1To10()
{
var span = new Span<int>(new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
Assert.AreEqual(5, span.Count(i => i % 2 == 0));
}
[TestMethod]
public void Count_ShouldThrow_GivenNullPredicate()
{
Assert.ThrowsException<ArgumentNullException>(() =>
{
var span = new Span<int>();
return span.Count(null!);
});
}
}

View File

@ -84,4 +84,43 @@ public static class ReadOnlySpanExtensions
return false;
}
/// <summary>
/// Returns a number that represents how many elements in the specified sequence satisfy a condition.
/// </summary>
/// <param name="source">A <see cref="ReadOnlySpan{T}" /> that contains elements to be tested and counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <typeparam name="TSource">The type of the elements in <paramref name="source" />.</typeparam>
/// <returns>
/// A number that represents how many elements in the sequence satisfy the condition in the predicate function.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="predicate" /> is <see langword="null" />.</exception>
public static int Count<TSource>(this ReadOnlySpan<TSource> source, Predicate<TSource> predicate)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(predicate);
#else
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
#endif
if (source.IsEmpty)
{
return 0;
}
var count = 0;
foreach (TSource item in source)
{
if (predicate(item))
{
count++;
}
}
return count;
}
}

View File

@ -84,4 +84,43 @@ public static class SpanExtensions
return false;
}
/// <summary>
/// Returns a number that represents how many elements in the specified sequence satisfy a condition.
/// </summary>
/// <param name="source">A <see cref="Span{T}" /> that contains elements to be tested and counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <typeparam name="TSource">The type of the elements in <paramref name="source" />.</typeparam>
/// <returns>
/// A number that represents how many elements in the sequence satisfy the condition in the predicate function.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="predicate" /> is <see langword="null" />.</exception>
public static int Count<TSource>(this Span<TSource> source, Predicate<TSource> predicate)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(predicate);
#else
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
#endif
if (source.IsEmpty)
{
return 0;
}
var count = 0;
foreach (TSource item in source)
{
if (predicate(item))
{
count++;
}
}
return count;
}
}