123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- 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 IUniTaskAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- if (count <= 0) throw Error.ArgumentOutOfRange(nameof(count));
- return new Buffer<TSource>(source, count);
- }
- public static IUniTaskAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count, Int32 skip)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- if (count <= 0) throw Error.ArgumentOutOfRange(nameof(count));
- if (skip <= 0) throw Error.ArgumentOutOfRange(nameof(skip));
- return new BufferSkip<TSource>(source, count, skip);
- }
- }
- internal sealed class Buffer<TSource> : IUniTaskAsyncEnumerable<IList<TSource>>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly int count;
- public Buffer(IUniTaskAsyncEnumerable<TSource> source, int count)
- {
- this.source = source;
- this.count = count;
- }
- public IUniTaskAsyncEnumerator<IList<TSource>> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _Buffer(source, count, cancellationToken);
- }
- sealed class _Buffer : MoveNextSource, IUniTaskAsyncEnumerator<IList<TSource>>
- {
- static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly int count;
- CancellationToken cancellationToken;
- IUniTaskAsyncEnumerator<TSource> enumerator;
- UniTask<bool>.Awaiter awaiter;
- bool continueNext;
- bool completed;
- List<TSource> buffer;
- public _Buffer(IUniTaskAsyncEnumerable<TSource> source, int count, CancellationToken cancellationToken)
- {
- this.source = source;
- this.count = count;
- this.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(this, 3);
- }
- public IList<TSource> Current { get; private set; }
- public UniTask<bool> MoveNextAsync()
- {
- cancellationToken.ThrowIfCancellationRequested();
- if (enumerator == null)
- {
- enumerator = source.GetAsyncEnumerator(cancellationToken);
- buffer = new List<TSource>(count);
- }
- completionSource.Reset();
- SourceMoveNext();
- return new UniTask<bool>(this, completionSource.Version);
- }
- void SourceMoveNext()
- {
- if (completed)
- {
- if (buffer != null && buffer.Count > 0)
- {
- var ret = buffer;
- buffer = null;
- Current = ret;
- completionSource.TrySetResult(true);
- return;
- }
- else
- {
- completionSource.TrySetResult(false);
- return;
- }
- }
- try
- {
- LOOP:
- awaiter = enumerator.MoveNextAsync().GetAwaiter();
- if (awaiter.IsCompleted)
- {
- continueNext = true;
- MoveNextCore(this);
- if (continueNext)
- {
- continueNext = false;
- goto LOOP; // avoid recursive
- }
- }
- else
- {
- awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
- }
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- }
- }
- static void MoveNextCore(object state)
- {
- var self = (_Buffer)state;
- if (self.TryGetResult(self.awaiter, out var result))
- {
- if (result)
- {
- self.buffer.Add(self.enumerator.Current);
- if (self.buffer.Count == self.count)
- {
- self.Current = self.buffer;
- self.buffer = new List<TSource>(self.count);
- self.continueNext = false;
- self.completionSource.TrySetResult(true);
- return;
- }
- else
- {
- if (!self.continueNext)
- {
- self.SourceMoveNext();
- }
- }
- }
- else
- {
- self.continueNext = false;
- self.completed = true;
- self.SourceMoveNext();
- }
- }
- else
- {
- self.continueNext = false;
- }
- }
- public UniTask DisposeAsync()
- {
- TaskTracker.RemoveTracking(this);
- if (enumerator != null)
- {
- return enumerator.DisposeAsync();
- }
- return default;
- }
- }
- }
- internal sealed class BufferSkip<TSource> : IUniTaskAsyncEnumerable<IList<TSource>>
- {
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly int count;
- readonly int skip;
- public BufferSkip(IUniTaskAsyncEnumerable<TSource> source, int count, int skip)
- {
- this.source = source;
- this.count = count;
- this.skip = skip;
- }
- public IUniTaskAsyncEnumerator<IList<TSource>> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _BufferSkip(source, count, skip, cancellationToken);
- }
- sealed class _BufferSkip : MoveNextSource, IUniTaskAsyncEnumerator<IList<TSource>>
- {
- static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
- readonly IUniTaskAsyncEnumerable<TSource> source;
- readonly int count;
- readonly int skip;
- CancellationToken cancellationToken;
- IUniTaskAsyncEnumerator<TSource> enumerator;
- UniTask<bool>.Awaiter awaiter;
- bool continueNext;
- bool completed;
- Queue<List<TSource>> buffers;
- int index = 0;
- public _BufferSkip(IUniTaskAsyncEnumerable<TSource> source, int count, int skip, CancellationToken cancellationToken)
- {
- this.source = source;
- this.count = count;
- this.skip = skip;
- this.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(this, 3);
- }
- public IList<TSource> Current { get; private set; }
- public UniTask<bool> MoveNextAsync()
- {
- cancellationToken.ThrowIfCancellationRequested();
- if (enumerator == null)
- {
- enumerator = source.GetAsyncEnumerator(cancellationToken);
- buffers = new Queue<List<TSource>>();
- }
- completionSource.Reset();
- SourceMoveNext();
- return new UniTask<bool>(this, completionSource.Version);
- }
- void SourceMoveNext()
- {
- if (completed)
- {
- if (buffers.Count > 0)
- {
- Current = buffers.Dequeue();
- completionSource.TrySetResult(true);
- return;
- }
- else
- {
- completionSource.TrySetResult(false);
- return;
- }
- }
- try
- {
- LOOP:
- awaiter = enumerator.MoveNextAsync().GetAwaiter();
- if (awaiter.IsCompleted)
- {
- continueNext = true;
- MoveNextCore(this);
- if (continueNext)
- {
- continueNext = false;
- goto LOOP; // avoid recursive
- }
- }
- else
- {
- awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
- }
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- }
- }
- static void MoveNextCore(object state)
- {
- var self = (_BufferSkip)state;
- if (self.TryGetResult(self.awaiter, out var result))
- {
- if (result)
- {
- if (self.index++ % self.skip == 0)
- {
- self.buffers.Enqueue(new List<TSource>(self.count));
- }
- var item = self.enumerator.Current;
- foreach (var buffer in self.buffers)
- {
- buffer.Add(item);
- }
- if (self.buffers.Count > 0 && self.buffers.Peek().Count == self.count)
- {
- self.Current = self.buffers.Dequeue();
- self.continueNext = false;
- self.completionSource.TrySetResult(true);
- return;
- }
- else
- {
- if (!self.continueNext)
- {
- self.SourceMoveNext();
- }
- }
- }
- else
- {
- self.continueNext = false;
- self.completed = true;
- self.SourceMoveNext();
- }
- }
- else
- {
- self.continueNext = false;
- }
- }
- public UniTask DisposeAsync()
- {
- TaskTracker.RemoveTracking(this);
- if (enumerator != null)
- {
- return enumerator.DisposeAsync();
- }
- return default;
- }
- }
- }
- }
|