1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

Add LINQ-inspired All/Any for ReadOnly/Span<T>

This commit is contained in:
Oliver Booth 2022-04-20 19:33:36 +01:00
parent af9449935f
commit 1460e6b6c3
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 152 additions and 0 deletions

View File

@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Carrayextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cbooleanextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cbyteextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Ccharextensions/@EntryIndexedValue">True</s:Boolean>
@ -18,6 +19,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Crandomextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Creflectionextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Csingleextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cspanextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cstreamextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cstringbuilderextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cstringextensions/@EntryIndexedValue">True</s:Boolean>

View File

@ -0,0 +1,75 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="ReadOnlySpan{T}" />.
/// </summary>
public static class ReadOnlySpanExtensions
{
/// <summary>
/// Determines whether all elements of a read-only span satisfy a condition.
/// </summary>
/// <param name="source">A <see cref="ReadOnlySpan{T}" /> that contains the elements to apply the predicate to.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>
/// <see langword="true" /> if every element of the source sequence passes the test in the specified predicate, or if the
/// span is empty; otherwise, <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="predicate" /> is <see langword="null" />.</exception>
public static bool All<TSource>(this ReadOnlySpan<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
if (source.IsEmpty)
{
return true;
}
for (var index = 0; index < source.Length; index++)
{
if (!predicate(source[index]))
{
return false;
}
}
return true;
}
/// <summary>
/// Determines whether any element of a read-only span satisfies a condition.
/// </summary>
/// <param name="source">A <see cref="ReadOnlySpan{T}" /> that contains the elements to apply the predicate to.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>
/// <see langword="true" /> if the source span is not empty and at least one of its elements passes the test in the
/// specified predicate; otherwise, <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="predicate" /> is <see langword="null" />.</exception>
public static bool Any<TSource>(this ReadOnlySpan<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
if (source.IsEmpty)
{
return false;
}
for (var index = 0; index < source.Length; index++)
{
if (predicate(source[index]))
{
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,75 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="Span{T}" />.
/// </summary>
public static class SpanExtensions
{
/// <summary>
/// Determines whether all elements of a span satisfy a condition.
/// </summary>
/// <param name="source">A <see cref="Span{T}" /> that contains the elements to apply the predicate to.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>
/// <see langword="true" /> if every element of the source sequence passes the test in the specified predicate, or if the
/// span is empty; otherwise, <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="predicate" /> is <see langword="null" />.</exception>
public static bool All<TSource>(this Span<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
if (source.IsEmpty)
{
return true;
}
for (var index = 0; index < source.Length; index++)
{
if (!predicate(source[index]))
{
return false;
}
}
return true;
}
/// <summary>
/// Determines whether any element of a span satisfies a condition.
/// </summary>
/// <param name="source">A <see cref="Span{T}" /> that contains the elements to apply the predicate to.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>
/// <see langword="true" /> if the source span is not empty and at least one of its elements passes the test in the
/// specified predicate; otherwise, <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="predicate" /> is <see langword="null" />.</exception>
public static bool Any<TSource>(this Span<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
if (source.IsEmpty)
{
return false;
}
for (var index = 0; index < source.Length; index++)
{
if (predicate(source[index]))
{
return true;
}
}
return false;
}
}