Skip to content

Commit

Permalink
refactor!: remove Count for Span and ROSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Nov 14, 2024
1 parent 0cab7f7 commit af5a76b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 77 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
[`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
[`System.MemoryExtensions.Split`](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.split?view=net-9.0).

Expand Down
40 changes: 0 additions & 40 deletions X10D.Tests/src/Collections/SpanTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,6 @@ namespace X10D.Tests.Collections;
[TestFixture]
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]
public void Replace_ShouldReplaceAllElements_GivenSpanOfInt32()
{
Expand Down
37 changes: 0 additions & 37 deletions X10D/src/Collections/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,6 @@ namespace X10D.Collections;
/// </summary>
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>
/// Returns a read-only <see cref="ReadOnlySpan{T}" /> wrapper for the current span.
/// </summary>
Expand Down

0 comments on commit af5a76b

Please sign in to comment.