123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- using Cysharp.Threading.Tasks.Internal;
- using System;
- using System.Threading;
- namespace Cysharp.Threading.Tasks.Linq
- {
- public static partial class UniTaskAsyncEnumerable
- {
- public static IUniTaskAsyncEnumerable<TSource> SkipWhile<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(predicate, nameof(predicate));
- return new SkipWhile<TSource>(source, predicate);
- }
- public static IUniTaskAsyncEnumerable<TSource> SkipWhile<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, Boolean> predicate)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(predicate, nameof(predicate));
- return new SkipWhileInt<TSource>(source, predicate);
- }
- public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(predicate, nameof(predicate));
- return new SkipWhileAwait<TSource>(source, predicate);
- }
- public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask<Boolean>> predicate)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(predicate, nameof(predicate));
- return new SkipWhileIntAwait<TSource>(source, predicate);
- }
- public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(predicate, nameof(predicate));
- return new SkipWhileAwaitWithCancellation<TSource>(source, predicate);
- }
- public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask<Boolean>> predicate)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(predicate, nameof(predicate));
- return new SkipWhileIntAwaitWithCancellation<TSource>(source, predicate);
- }
- }
- internal sealed class SkipWhile<TSource> : IUniTaskAsyncEnumerable<TSource>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly Func<TSource, bool> predicate;
- public SkipWhile(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
- {
- this.source = source;
- this.predicate = predicate;
- }
- public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _SkipWhile(source, predicate, cancellationToken);
- }
- class _SkipWhile : AsyncEnumeratorBase<TSource, TSource>
- {
- Func<TSource, bool> predicate;
- public _SkipWhile(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
- : base(source, cancellationToken)
- {
- this.predicate = predicate;
- }
- protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
- {
- if (sourceHasCurrent)
- {
- if (predicate == null || !predicate(SourceCurrent))
- {
- predicate = null;
- Current = SourceCurrent;
- result = true;
- return true;
- }
- else
- {
- result = default;
- return false;
- }
- }
- result = false;
- return true;
- }
- }
- }
- internal sealed class SkipWhileInt<TSource> : IUniTaskAsyncEnumerable<TSource>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly Func<TSource, int, bool> predicate;
- public SkipWhileInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
- {
- this.source = source;
- this.predicate = predicate;
- }
- public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _SkipWhileInt(source, predicate, cancellationToken);
- }
- class _SkipWhileInt : AsyncEnumeratorBase<TSource, TSource>
- {
- Func<TSource, int, bool> predicate;
- int index;
- public _SkipWhileInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, CancellationToken cancellationToken)
- : base(source, cancellationToken)
- {
- this.predicate = predicate;
- }
- protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
- {
- if (sourceHasCurrent)
- {
- if (predicate == null || !predicate(SourceCurrent, checked(index++)))
- {
- predicate = null;
- Current = SourceCurrent;
- result = true;
- return true;
- }
- else
- {
- result = default;
- return false;
- }
- }
- result = false;
- return true;
- }
- }
- }
- internal sealed class SkipWhileAwait<TSource> : IUniTaskAsyncEnumerable<TSource>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly Func<TSource, UniTask<bool>> predicate;
- public SkipWhileAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate)
- {
- this.source = source;
- this.predicate = predicate;
- }
- public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _SkipWhileAwait(source, predicate, cancellationToken);
- }
- class _SkipWhileAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
- {
- Func<TSource, UniTask<bool>> predicate;
- public _SkipWhileAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate, CancellationToken cancellationToken)
- : base(source, cancellationToken)
- {
- this.predicate = predicate;
- }
- protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
- {
- if (predicate == null)
- {
- return CompletedTasks.False;
- }
- return predicate(sourceCurrent);
- }
- protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
- {
- if (!awaitResult)
- {
- predicate = null;
- Current = SourceCurrent;
- terminateIteration= false;
- return true;
- }
- else
- {
- terminateIteration= false;
- return false;
- }
- }
- }
- }
- internal sealed class SkipWhileIntAwait<TSource> : IUniTaskAsyncEnumerable<TSource>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly Func<TSource, int, UniTask<bool>> predicate;
- public SkipWhileIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate)
- {
- this.source = source;
- this.predicate = predicate;
- }
- public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _SkipWhileIntAwait(source, predicate, cancellationToken);
- }
- class _SkipWhileIntAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
- {
- Func<TSource, int, UniTask<bool>> predicate;
- int index;
- public _SkipWhileIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate, CancellationToken cancellationToken)
- : base(source, cancellationToken)
- {
- this.predicate = predicate;
- }
- protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
- {
- if (predicate == null)
- {
- return CompletedTasks.False;
- }
- return predicate(sourceCurrent, checked(index++));
- }
- protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
- {
- terminateIteration= false;
- if (!awaitResult)
- {
- predicate = null;
- Current = SourceCurrent;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- internal sealed class SkipWhileAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly Func<TSource, CancellationToken, UniTask<bool>> predicate;
- public SkipWhileAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate)
- {
- this.source = source;
- this.predicate = predicate;
- }
- public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _SkipWhileAwaitWithCancellation(source, predicate, cancellationToken);
- }
- class _SkipWhileAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
- {
- Func<TSource, CancellationToken, UniTask<bool>> predicate;
- public _SkipWhileAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
- : base(source, cancellationToken)
- {
- this.predicate = predicate;
- }
- protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
- {
- if (predicate == null)
- {
- return CompletedTasks.False;
- }
- return predicate(sourceCurrent, cancellationToken);
- }
- protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
- {
- terminateIteration= false;
- if (!awaitResult)
- {
- predicate = null;
- Current = SourceCurrent;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- internal sealed class SkipWhileIntAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
- public SkipWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate)
- {
- this.source = source;
- this.predicate = predicate;
- }
- public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _SkipWhileIntAwaitWithCancellation(source, predicate, cancellationToken);
- }
- class _SkipWhileIntAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
- {
- Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
- int index;
- public _SkipWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
- : base(source, cancellationToken)
- {
- this.predicate = predicate;
- }
- protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
- {
- if (predicate == null)
- {
- return CompletedTasks.False;
- }
- return predicate(sourceCurrent, checked(index++), cancellationToken);
- }
- protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
- {
- terminateIteration= false;
- if (!awaitResult)
- {
- predicate = null;
- Current = SourceCurrent;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- }
|