dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.63k forks source link

[API Proposal]: Add `SequenceCompareTo` methods to `Enumerable` #91514

Open Neme12 opened 1 year ago

Neme12 commented 1 year ago

Background and motivation

MemoryExtensions has really useful SequenceEqual and SequenceCompareTo methods. For IEnumerable, there is a SequenceEqual method as well, but sadly no SequenceCompareTo. So this is what I'm proposing.

Also, I noticed that the SequenceCompareTo methods on MemoryExtensions have no overloads that take a comparer, while the SequenceEqual methods do. I'm adding this to the proposal as well just because this feels like an omission. EDIT: There is an existing issue for this: #84838

API Proposal

 namespace System;

 public static partial class MemoryExtensions
 {
     public static bool SequenceEqual<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other) where T : IEquatable<T>?;
     public static bool SequenceEqual<T>(this Span<T> span, ReadOnlySpan<T> other) where T : IEquatable<T>?;
     public static bool SequenceEqual<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other, IEqualityComparer<T>? comparer = null);
     public static bool SequenceEqual<T>(this Span<T> span, ReadOnlySpan<T> other, IEqualityComparer<T>? comparer = null);
     public static int SequenceCompareTo<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other) where T : IComparable<T>?;
     public static int SequenceCompareTo<T>(this Span<T> span, ReadOnlySpan<T> other) where T : IComparable<T>?;
 }

 namespace System.Linq;

 public static partial class Enumerable
 {
     public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
     public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource>? comparer);
+    public static int SequenceCompareTo<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
+    public static int SequenceCompareTo<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IComparer<TSource>? comparer);
 }

API Usage

public int CompareTo(Grapheme other) =>
    _runes.SequenceCompareTo(other._runes);

Alternative Designs

No response

Risks

No response

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-memory See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation `MemoryExtensions` has really useful `SequenceEqual` and `SequenceCompareTo` methods. For `IEnumerable`, there is a `SequenceEqual` method as well, but sadly no `SequenceCompareTo`. So this is what I'm proposing. Also, I noticed that the `SequenceCompareTo` methods on `MemoryExtensions` have no overloads that take a comparer, while the `SequenceEqual` methods do. I'm adding this to the proposal as well just because this feels like an omission. ### API Proposal ```diff namespace System; public static partial class MemoryExtensions { public static bool SequenceEqual(this ReadOnlySpan span, ReadOnlySpan other) where T : IEquatable?; public static bool SequenceEqual(this Span span, ReadOnlySpan other) where T : IEquatable?; public static bool SequenceEqual(this ReadOnlySpan span, ReadOnlySpan other, IEqualityComparer? comparer = null); public static bool SequenceEqual(this Span span, ReadOnlySpan other, IEqualityComparer? comparer = null); public static int SequenceCompareTo(this ReadOnlySpan span, ReadOnlySpan other) where T : IComparable?; public static int SequenceCompareTo(this Span span, ReadOnlySpan other) where T : IComparable?; + public static int SequenceCompareTo(this ReadOnlySpan span, ReadOnlySpan other, IComparer? comparer = null); + public static int SequenceCompareTo(this Span span, ReadOnlySpan other, IComparer? comparer = null); } namespace System.Linq; public static partial class Enumerable { public static bool SequenceEqual(this IEnumerable first, IEnumerable second); public static bool SequenceEqual(this IEnumerable first, IEnumerable second, IEqualityComparer? comparer); + public static int SequenceCompareTo(this IEnumerable first, IEnumerable second); + public static int SequenceCompareTo(this IEnumerable first, IEnumerable second, IComparer? comparer); } ``` ### API Usage ```csharp public int CompareTo(Grapheme other) => _runes.SequenceCompareTo(other._runes); ``` ### Alternative Designs _No response_ ### Risks _No response_
Author: Neme12
Assignees: -
Labels: `api-suggestion`, `area-System.Memory`
Milestone: -
Neme12 commented 1 year ago

Oh, there is an existing issue for the second part already: #84838. I'm removing that from this proposal.