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

refactor!: remove Count for Span and ROSpan

This commit is contained in:
Oliver Booth 2024-11-14 18:32:38 +00:00
parent 0cab7f782a
commit af5a76bca8
Signed by: oliverbooth
GPG Key ID: 2A862C3F46178E8E
3 changed files with 4 additions and 77 deletions

View File

@ -22,6 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Removed `DateOnly.Deconstruct` due to conflict with - X10D: Removed `DateOnly.Deconstruct` due to conflict with
[`System.DateOnly.Deconstruct`](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct?view=net-8.0). [`System.DateOnly.Deconstruct`](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.deconstruct?view=net-8.0).
- X10D: Removed `Span<T>.Count` due to conflict with
[`System.MemoryExtensions.Count`](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.count?view=net-8.0).
- X10D: Removed `ReadOnlySpan<T>.Count` due to conflict with
[`System.MemoryExtensions.Count`](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.count?view=net-8.0).
- X10D: Removed `Span<T>.Split` for .NET 9.0 target due to conflicts with - X10D: Removed `Span<T>.Split` for .NET 9.0 target due to conflicts with
[`System.MemoryExtensions.Split`](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.split?view=net-9.0). [`System.MemoryExtensions.Split`](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.split?view=net-9.0).

View File

@ -8,46 +8,6 @@ namespace X10D.Tests.Collections;
[TestFixture] [TestFixture]
internal class SpanTest internal class SpanTest
{ {
[Test]
public void Count_ShouldReturn0_GivenEmptySpan()
{
Span<int> span = Span<int>.Empty;
int count = span.Count(2);
Assert.That(count, Is.Zero);
}
[Test]
public void Count_ShouldReturn0_GivenEmptyReadOnlySpan()
{
ReadOnlySpan<int> span = ReadOnlySpan<int>.Empty;
int count = span.Count(2);
Assert.That(count, Is.Zero);
}
[Test]
public void Count_ShouldReturn8_GivenSpanWith8MatchingElements()
{
Span<int> span = [1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2];
int count = span.Count(2);
Assert.That(count, Is.EqualTo(8));
}
[Test]
public void Count_ShouldReturn8_GivenReadOnlySpanWith8MatchingElements()
{
ReadOnlySpan<int> span = [1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2];
int count = span.Count(2);
Assert.That(count, Is.EqualTo(8));
}
[Test] [Test]
public void Replace_ShouldReplaceAllElements_GivenSpanOfInt32() public void Replace_ShouldReplaceAllElements_GivenSpanOfInt32()
{ {

View File

@ -5,43 +5,6 @@ namespace X10D.Collections;
/// </summary> /// </summary>
public static class SpanExtensions public static class SpanExtensions
{ {
/// <summary>
/// Returns the number of times that a specified element appears in a span of elements of the same type.
/// </summary>
/// <param name="source">The source to search.</param>
/// <param name="element">The element to count.</param>
/// <typeparam name="T">The type of elements in <paramref name="source" />.</typeparam>
/// <returns>The number of times that <paramref name="element" /> appears in <paramref name="source" />.</returns>
public static int Count<T>(this in Span<T> source, T element)
where T : IEquatable<T>
{
return source.AsReadOnly().Count(element);
}
/// <summary>
/// Returns the number of times that a specified element appears in a read-only span of elements of the same type.
/// </summary>
/// <param name="source">The source to search.</param>
/// <param name="element">The element to count.</param>
/// <typeparam name="T">The type of elements in <paramref name="source" />.</typeparam>
/// <returns>The number of times that <paramref name="element" /> appears in <paramref name="source" />.</returns>
public static int Count<T>(this in ReadOnlySpan<T> source, T element)
where T : IEquatable<T>
{
var count = 0;
for (var index = 0; index < source.Length; index++)
{
T item = source[index];
if (item.Equals(element))
{
count++;
}
}
return count;
}
/// <summary> /// <summary>
/// Returns a read-only <see cref="ReadOnlySpan{T}" /> wrapper for the current span. /// Returns a read-only <see cref="ReadOnlySpan{T}" /> wrapper for the current span.
/// </summary> /// </summary>