Count, LongCount

master
neuecc 2020-05-09 23:33:27 +09:00
parent aa8cb80866
commit e93bcbf564
4 changed files with 175 additions and 802 deletions

View File

@ -18,7 +18,7 @@ namespace Cysharp.Threading.Tasks.Linq
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return Count<TSource>.InvokeAsync(source, cancellationToken);
return Count<TSource>.InvokeAsync(source, predicate, cancellationToken);
}
public static UniTask<Int32> CountAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
@ -26,7 +26,7 @@ namespace Cysharp.Threading.Tasks.Linq
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return Count<TSource>.InvokeAsync(source, cancellationToken);
return Count<TSource>.InvokeAsync(source, predicate, cancellationToken);
}
public static UniTask<Int32> CountAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
@ -34,7 +34,7 @@ namespace Cysharp.Threading.Tasks.Linq
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return Count<TSource>.InvokeAsync(source, cancellationToken);
return Count<TSource>.InvokeAsync(source, predicate, cancellationToken);
}
}

View File

@ -1,775 +1,144 @@
namespace Cysharp.Threading.Tasks.Linq
using Cysharp.Threading.Tasks.Internal;
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{
internal sealed class LongCount
public static partial class UniTaskAsyncEnumerable
{
public static UniTask<long> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return LongCount<TSource>.InvokeAsync(source, cancellationToken);
}
public static UniTask<long> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return LongCount<TSource>.InvokeAsync(source, predicate, cancellationToken);
}
public static UniTask<long> LongCountAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return LongCount<TSource>.InvokeAsync(source, predicate, cancellationToken);
}
public static UniTask<long> LongCountAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return LongCount<TSource>.InvokeAsync(source, predicate, cancellationToken);
}
}
internal static class LongCount<TSource>
{
internal static async UniTask<long> InvokeAsync(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
long count = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
checked { count++; }
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return count;
}
internal static async UniTask<long> InvokeAsync(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
{
long count = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
if (predicate(e.Current))
{
checked { count++; }
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return count;
}
internal static async UniTask<long> InvokeAsync(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
{
long count = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
if (await predicate(e.Current))
{
checked { count++; }
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return count;
}
internal static async UniTask<long> InvokeAsync(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
{
long count = 0;
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
if (await predicate(e.Current, cancellationToken))
{
checked { count++; }
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return count;
}
}
}

View File

@ -152,15 +152,6 @@ namespace ___Dummy
throw new NotImplementedException();
}
public static UniTask<TSource> ElementAtAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 index, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> ElementAtOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 index, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> Except<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second)
{
@ -366,25 +357,6 @@ namespace ___Dummy
}
public static UniTask<Int64> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> LongCountAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> LongCountAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> OfType<TResult>(this IUniTaskAsyncEnumerable<Object> source)

View File

@ -345,5 +345,37 @@ namespace NetCoreTests.Linq
return Comparer<int>.Default.Compare(Value, other.Value);
}
}
[Theory]
[InlineData(0, 10)]
[InlineData(0, 1)]
[InlineData(10, 0)]
[InlineData(1, 11)]
public async Task Count(int start, int count)
{
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync();
var ys = Enumerable.Range(start, count).Count();
xs.Should().Be(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync(x => x % 2 == 0);
var ys = Enumerable.Range(start, count).Count(x => x % 2 == 0);
xs.Should().Be(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync();
var ys = Enumerable.Range(start, count).LongCount();
xs.Should().Be(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync(x => x % 2 == 0);
var ys = Enumerable.Range(start, count).LongCount(x => x % 2 == 0);
xs.Should().Be(ys);
}
}
}
}