123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using Cysharp.Threading.Tasks.Internal;
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace Cysharp.Threading.Tasks.Linq
- {
- public static partial class UniTaskAsyncEnumerable
- {
- public static UniTask<TSource> AggregateAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- return Aggregate.AggregateAsync(source, accumulator, cancellationToken);
- }
- public static UniTask<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- return Aggregate.AggregateAsync(source, seed, accumulator, cancellationToken);
- }
- public static UniTask<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- Error.ThrowArgumentNullException(accumulator, nameof(resultSelector));
- return Aggregate.AggregateAsync(source, seed, accumulator, resultSelector, cancellationToken);
- }
- public static UniTask<TSource> AggregateAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, UniTask<TSource>> accumulator, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- return Aggregate.AggregateAwaitAsync(source, accumulator, cancellationToken);
- }
- public static UniTask<TAccumulate> AggregateAwaitAsync<TSource, TAccumulate>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- return Aggregate.AggregateAwaitAsync(source, seed, accumulator, cancellationToken);
- }
- public static UniTask<TResult> AggregateAwaitAsync<TSource, TAccumulate, TResult>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, Func<TAccumulate, UniTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- Error.ThrowArgumentNullException(accumulator, nameof(resultSelector));
- return Aggregate.AggregateAwaitAsync(source, seed, accumulator, resultSelector, cancellationToken);
- }
- public static UniTask<TSource> AggregateAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, UniTask<TSource>> accumulator, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- return Aggregate.AggregateAwaitWithCancellationAsync(source, accumulator, cancellationToken);
- }
- public static UniTask<TAccumulate> AggregateAwaitWithCancellationAsync<TSource, TAccumulate>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- return Aggregate.AggregateAwaitWithCancellationAsync(source, seed, accumulator, cancellationToken);
- }
- public static UniTask<TResult> AggregateAwaitWithCancellationAsync<TSource, TAccumulate, TResult>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, UniTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
- Error.ThrowArgumentNullException(accumulator, nameof(resultSelector));
- return Aggregate.AggregateAwaitWithCancellationAsync(source, seed, accumulator, resultSelector, cancellationToken);
- }
- }
- internal static class Aggregate
- {
- internal static async UniTask<TSource> AggregateAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TSource value;
- if (await e.MoveNextAsync())
- {
- value = e.Current;
- }
- else
- {
- throw Error.NoElements();
- }
- while (await e.MoveNextAsync())
- {
- value = accumulator(value, e.Current);
- }
- return value;
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- internal static async UniTask<TAccumulate> AggregateAsync<TSource, TAccumulate>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TAccumulate value = seed;
- while (await e.MoveNextAsync())
- {
- value = accumulator(value, e.Current);
- }
- return value;
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- internal static async UniTask<TResult> AggregateAsync<TSource, TAccumulate, TResult>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TAccumulate value = seed;
- while (await e.MoveNextAsync())
- {
- value = accumulator(value, e.Current);
- }
- return resultSelector(value);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- // with async
- internal static async UniTask<TSource> AggregateAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, UniTask<TSource>> accumulator, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TSource value;
- if (await e.MoveNextAsync())
- {
- value = e.Current;
- }
- else
- {
- throw Error.NoElements();
- }
- while (await e.MoveNextAsync())
- {
- value = await accumulator(value, e.Current);
- }
- return value;
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- internal static async UniTask<TAccumulate> AggregateAwaitAsync<TSource, TAccumulate>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TAccumulate value = seed;
- while (await e.MoveNextAsync())
- {
- value = await accumulator(value, e.Current);
- }
- return value;
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- internal static async UniTask<TResult> AggregateAwaitAsync<TSource, TAccumulate, TResult>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, Func<TAccumulate, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TAccumulate value = seed;
- while (await e.MoveNextAsync())
- {
- value = await accumulator(value, e.Current);
- }
- return await resultSelector(value);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- // with cancellation
- internal static async UniTask<TSource> AggregateAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, UniTask<TSource>> accumulator, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TSource value;
- if (await e.MoveNextAsync())
- {
- value = e.Current;
- }
- else
- {
- throw Error.NoElements();
- }
- while (await e.MoveNextAsync())
- {
- value = await accumulator(value, e.Current, cancellationToken);
- }
- return value;
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- internal static async UniTask<TAccumulate> AggregateAwaitWithCancellationAsync<TSource, TAccumulate>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TAccumulate value = seed;
- while (await e.MoveNextAsync())
- {
- value = await accumulator(value, e.Current, cancellationToken);
- }
- return value;
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- internal static async UniTask<TResult> AggregateAwaitWithCancellationAsync<TSource, TAccumulate, TResult>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- TAccumulate value = seed;
- while (await e.MoveNextAsync())
- {
- value = await accumulator(value, e.Current, cancellationToken);
- }
- return await resultSelector(value, cancellationToken);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- }
- }
|