-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API Proposal: MemoryExtensions.SequenceEqual with IComparer<T> #48304
Comments
Tagging subscribers to this area: @GrabYourPitchforks Issue DetailsBackground and MotivationWe currently have MemoryExtensions.SequenceEqual overloads that work with spans, but they constrain public static bool SequenceEqual<T>(this System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> other) where T : System.IEquatable<T> { throw null; }
public static bool SequenceEqual<T>(this System.Span<T> span, System.ReadOnlySpan<T> other) where T : System.IEquatable<T> { throw null; } This means there's no built-in support for using Proposed APIpublic static class MemoryExtensions
{
public static bool SequenceEqual<T>(this System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> other, IEqualityComparer<T>? comparer);
public static bool SequenceEqual<T>(this System.Span<T> span, System.ReadOnlySpan<T> other, IEqualityComparer<T>? comparer);
...
} We could also consider doing the same thing for SequenceCompareTo, taking the existing overloads and adding new ones that remove the constraint and accept a nullable Usage ExamplesFor example, we would change if (first is TSource[] firstArray && second is TSource[] secondArray)
{
return ((ReadOnlySequence<TSource>)firstArray).SequenceEqual(secondArray, comparer);
}
<table>
<tr>
<th align="left">Author:</th>
<td>stephentoub</td>
</tr>
<tr>
<th align="left">Assignees:</th>
<td>-</td>
</tr>
<tr>
<th align="left">Labels:</th>
<td>
`api-ready-for-review`, `area-System.Memory`, `untriaged`
</td>
</tr>
<tr>
<th align="left">Milestone:</th>
<td>6.0.0</td>
</tr>
</table>
</details> |
Would it need a |
My example does pass a comparer... ? |
😅 So it does |
namespace System
{
public static class MemoryExtensions
{
// Existing
// 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 : System.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);
}
} |
Background and Motivation
We currently have MemoryExtensions.SequenceEqual overloads that work with spans, but they constrain
T : IEquatable<T>
:This means there's no built-in support for using
IEqualityComparer<T>
. It also means APIs likeEnumerable.SequenceEqual
that are unconstrained aren't able to delegate to this in general when provided with array inputs.Proposed API
We could also consider doing the same thing for SequenceCompareTo, taking the existing overloads and adding new ones that remove the constraint and accept a nullable
IComparer<T>
.The implementations would delegate to the existing implementations if comparer is null and T is found to be
IEquatable<T>
(maybe just for value types). Otherwise, they'd loop through the spans comparing each element.Usage Examples
For example, we would change
Enumerable.SequenceEqual
to do:The text was updated successfully, but these errors were encountered: