pull/73/head
neuecc 2020-05-11 12:38:32 +09:00
parent 1316328766
commit a5f47d4095
4 changed files with 181 additions and 780 deletions

View File

@ -1,775 +1,153 @@
namespace Cysharp.Threading.Tasks.Linq using Cysharp.Threading.Tasks.Internal;
using System;
using System.Collections.Generic;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{ {
internal sealed class SkipLast public static partial class UniTaskAsyncEnumerable
{ {
public static IUniTaskAsyncEnumerable<TSource> SkipLast<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)
{
Error.ThrowArgumentNullException(source, nameof(source));
// non skip.
if (count <= 0)
{
return source;
}
return new SkipLast<TSource>(source, count);
}
} }
internal sealed class SkipLast<TSource> : IUniTaskAsyncEnumerable<TSource>
{
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly int count;
public SkipLast(IUniTaskAsyncEnumerable<TSource> source, int count)
{
this.source = source;
this.count = count;
}
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new Enumerator(source, count, cancellationToken);
}
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
{
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly int count;
CancellationToken cancellationToken;
IUniTaskAsyncEnumerator<TSource> enumerator;
UniTask<bool>.Awaiter awaiter;
Queue<TSource> queue;
bool continueNext;
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, int count, CancellationToken cancellationToken)
{
this.source = source;
this.count = count;
this.cancellationToken = cancellationToken;
}
public TSource Current { get; private set; }
public UniTask<bool> MoveNextAsync()
{
cancellationToken.ThrowIfCancellationRequested();
if (enumerator == null)
{
enumerator = source.GetAsyncEnumerator(cancellationToken);
queue = new Queue<TSource>();
}
completionSource.Reset();
SourceMoveNext();
return new UniTask<bool>(this, completionSource.Version);
}
void SourceMoveNext()
{
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 = (Enumerator)state;
if (self.TryGetResult(self.awaiter, out var result))
{
if (result)
{
if (self.queue.Count == self.count)
{
self.continueNext = false;
var deq = self.queue.Dequeue();
self.Current = deq;
self.queue.Enqueue(self.enumerator.Current);
self.completionSource.TrySetResult(true);
}
else
{
self.queue.Enqueue(self.enumerator.Current);
if (!self.continueNext)
{
self.SourceMoveNext();
}
}
}
else
{
self.continueNext = false;
self.completionSource.TrySetResult(false);
}
}
}
public UniTask DisposeAsync()
{
if (enumerator != null)
{
return enumerator.DisposeAsync();
}
return default;
}
}
}
} }

View File

@ -308,12 +308,6 @@ namespace ___Dummy
public static IUniTaskAsyncEnumerable<TSource> SkipLast<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> TakeLast<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count) public static IUniTaskAsyncEnumerable<TSource> TakeLast<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)
{ {

View File

@ -39,9 +39,11 @@ namespace NetCoreSandbox
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10).Do(x => Console.WriteLine("DO:" + x)) await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)
//.TakeWhileAwait(x => UniTask.FromResult(x < 5)) .SelectAwait(x => UniTask.Run(() => x))
.Take(5) .SkipLast(6)
) )
{ {
@ -53,6 +55,8 @@ namespace NetCoreSandbox
} }

View File

@ -39,6 +39,31 @@ namespace NetCoreTests.Linq
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs); await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
} }
} }
[Theory]
[InlineData(0, 0)]
[InlineData(0, 1)]
[InlineData(9, 0)]
[InlineData(9, 1)]
[InlineData(9, 5)]
[InlineData(9, 9)]
[InlineData(9, 15)]
public async Task SkipLast(int collection, int skipCount)
{
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipLast(skipCount).ToArrayAsync();
var ys = Enumerable.Range(1, collection).SkipLast(skipCount).ToArray();
xs.Should().BeEquivalentTo(ys);
}
[Fact]
public async Task SkipLastException()
{
foreach (var item in UniTaskTestException.Throws())
{
var xs = item.SkipLast(5).ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
}
[Theory] [Theory]
[InlineData(0, 0)] [InlineData(0, 0)]