SelectMany

master
neuecc 2020-05-11 02:01:49 +09:00
parent 16c527fa89
commit 4d13523df7
3 changed files with 752 additions and 85 deletions

View File

@ -12,79 +12,124 @@ namespace Cysharp.Threading.Tasks.Linq
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(selector, nameof(selector));
return new SelectMany<TSource, TResult>(source, selector);
return new SelectMany<TSource, TResult, TResult>(source, selector, (x, y) => y);
}
public static IUniTaskAsyncEnumerable<TResult> SelectMany<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, IUniTaskAsyncEnumerable<TResult>> selector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(selector, nameof(selector));
return new SelectMany<TSource, TResult, TResult>(source, selector, (x, y) => y);
}
public static IUniTaskAsyncEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, IUniTaskAsyncEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector));
return new SelectMany<TSource, TCollection, TResult>(source, collectionSelector, resultSelector);
}
public static IUniTaskAsyncEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, IUniTaskAsyncEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector));
return new SelectMany<TSource, TCollection, TResult>(source, collectionSelector, resultSelector);
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(selector, nameof(selector));
return new SelectManyAwait<TSource, TResult, TResult>(source, selector, (x, y) => UniTask.FromResult(y));
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(selector, nameof(selector));
return new SelectManyAwait<TSource, TResult, TResult>(source, selector, (x, y) => UniTask.FromResult(y));
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector));
return new SelectManyAwait<TSource, TCollection, TResult>(source, collectionSelector, resultSelector);
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector));
return new SelectManyAwait<TSource, TCollection, TResult>(source, collectionSelector, resultSelector);
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(selector, nameof(selector));
return new SelectManyAwaitWithCancellation<TSource, TResult, TResult>(source, selector, (x, y, c) => UniTask.FromResult(y));
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(selector, nameof(selector));
return new SelectManyAwaitWithCancellation<TSource, TResult, TResult>(source, selector, (x, y, c) => UniTask.FromResult(y));
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector));
return new SelectManyAwaitWithCancellation<TSource, TCollection, TResult>(source, collectionSelector, resultSelector);
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(collectionSelector, nameof(collectionSelector));
return new SelectManyAwaitWithCancellation<TSource, TCollection, TResult>(source, collectionSelector, resultSelector);
}
}
internal sealed class SelectMany<TSource, TResult> : IUniTaskAsyncEnumerable<TResult>
internal sealed class SelectMany<TSource, TCollection, TResult> : IUniTaskAsyncEnumerable<TResult>
{
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly Func<TSource, IUniTaskAsyncEnumerable<TResult>> selector;
readonly Func<TSource, IUniTaskAsyncEnumerable<TCollection>> selector1;
readonly Func<TSource, int, IUniTaskAsyncEnumerable<TCollection>> selector2;
readonly Func<TSource, TCollection, TResult> resultSelector;
public SelectMany(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, IUniTaskAsyncEnumerable<TResult>> selector)
public SelectMany(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, IUniTaskAsyncEnumerable<TCollection>> selector, Func<TSource, TCollection, TResult> resultSelector)
{
this.source = source;
this.selector = selector;
this.selector1 = selector;
this.selector2 = null;
this.resultSelector = resultSelector;
}
public SelectMany(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, IUniTaskAsyncEnumerable<TCollection>> selector, Func<TSource, TCollection, TResult> resultSelector)
{
this.source = source;
this.selector1 = null;
this.selector2 = selector;
this.resultSelector = resultSelector;
}
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new Enumerator(source, selector, cancellationToken);
return new Enumerator(source, selector1, selector2, resultSelector, cancellationToken);
}
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
@ -94,19 +139,26 @@ namespace Cysharp.Threading.Tasks.Linq
static readonly Action<object> selectedEnumeratorDisposeAsyncCoreDelegate = SelectedEnumeratorDisposeAsyncCore;
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly Func<TSource, IUniTaskAsyncEnumerable<TResult>> selector;
readonly Func<TSource, IUniTaskAsyncEnumerable<TCollection>> selector1;
readonly Func<TSource, int, IUniTaskAsyncEnumerable<TCollection>> selector2;
readonly Func<TSource, TCollection, TResult> resultSelector;
CancellationToken cancellationToken;
TSource sourceCurrent;
int sourceIndex;
IUniTaskAsyncEnumerator<TSource> sourceEnumerator;
IUniTaskAsyncEnumerator<TResult> selectedEnumerator;
IUniTaskAsyncEnumerator<TCollection> selectedEnumerator;
UniTask<bool>.Awaiter sourceAwaiter;
UniTask<bool>.Awaiter selectedAwaiter;
UniTask.Awaiter selectedDisposeAsyncAwaiter;
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, IUniTaskAsyncEnumerable<TResult>> selector, CancellationToken cancellationToken)
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, IUniTaskAsyncEnumerable<TCollection>> selector1, Func<TSource, int, IUniTaskAsyncEnumerable<TCollection>> selector2, Func<TSource, TCollection, TResult> resultSelector, CancellationToken cancellationToken)
{
this.source = source;
this.selector = selector;
this.selector1 = selector1;
this.selector2 = selector2;
this.resultSelector = resultSelector;
this.cancellationToken = cancellationToken;
}
@ -182,14 +234,21 @@ namespace Cysharp.Threading.Tasks.Linq
{
var self = (Enumerator)state;
if (self.TryGetResult(self.selectedAwaiter, out var result))
if (self.TryGetResult(self.sourceAwaiter, out var result))
{
if (result)
{
try
{
var current = self.sourceEnumerator.Current;
self.selectedEnumerator = self.selector(current).GetAsyncEnumerator(self.cancellationToken);
self.sourceCurrent = self.sourceEnumerator.Current;
if (self.selector1 != null)
{
self.selectedEnumerator = self.selector1(self.sourceCurrent).GetAsyncEnumerator(self.cancellationToken);
}
else
{
self.selectedEnumerator = self.selector2(self.sourceCurrent, checked(self.sourceIndex++)).GetAsyncEnumerator(self.cancellationToken);
}
}
catch (Exception ex)
{
@ -216,7 +275,7 @@ namespace Cysharp.Threading.Tasks.Linq
{
try
{
self.Current = self.selectedEnumerator.Current;
self.Current = self.resultSelector(self.sourceCurrent, self.selectedEnumerator.Current);
}
catch (Exception ex)
{
@ -276,4 +335,552 @@ namespace Cysharp.Threading.Tasks.Linq
}
}
}
}
internal sealed class SelectManyAwait<TSource, TCollection, TResult> : IUniTaskAsyncEnumerable<TResult>
{
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly Func<TSource, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector1;
readonly Func<TSource, int, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector2;
readonly Func<TSource, TCollection, UniTask<TResult>> resultSelector;
public SelectManyAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector, Func<TSource, TCollection, UniTask<TResult>> resultSelector)
{
this.source = source;
this.selector1 = selector;
this.selector2 = null;
this.resultSelector = resultSelector;
}
public SelectManyAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector, Func<TSource, TCollection, UniTask<TResult>> resultSelector)
{
this.source = source;
this.selector1 = null;
this.selector2 = selector;
this.resultSelector = resultSelector;
}
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new Enumerator(source, selector1, selector2, resultSelector, cancellationToken);
}
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
{
static readonly Action<object> sourceMoveNextCoreDelegate = SourceMoveNextCore;
static readonly Action<object> selectedSourceMoveNextCoreDelegate = SeletedSourceMoveNextCore;
static readonly Action<object> selectedEnumeratorDisposeAsyncCoreDelegate = SelectedEnumeratorDisposeAsyncCore;
static readonly Action<object> selectorAwaitCoreDelegate = SelectorAwaitCore;
static readonly Action<object> resultSelectorAwaitCoreDelegate = ResultSelectorAwaitCore;
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly Func<TSource, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector1;
readonly Func<TSource, int, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector2;
readonly Func<TSource, TCollection, UniTask<TResult>> resultSelector;
CancellationToken cancellationToken;
TSource sourceCurrent;
int sourceIndex;
IUniTaskAsyncEnumerator<TSource> sourceEnumerator;
IUniTaskAsyncEnumerator<TCollection> selectedEnumerator;
UniTask<bool>.Awaiter sourceAwaiter;
UniTask<bool>.Awaiter selectedAwaiter;
UniTask.Awaiter selectedDisposeAsyncAwaiter;
// await additional
UniTask<IUniTaskAsyncEnumerable<TCollection>>.Awaiter collectionSelectorAwaiter;
UniTask<TResult>.Awaiter resultSelectorAwaiter;
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector1, Func<TSource, int, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector2, Func<TSource, TCollection, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
{
this.source = source;
this.selector1 = selector1;
this.selector2 = selector2;
this.resultSelector = resultSelector;
this.cancellationToken = cancellationToken;
}
public TResult Current { get; private set; }
public UniTask<bool> MoveNextAsync()
{
completionSource.Reset();
// iterate selected field
if (selectedEnumerator != null)
{
MoveNextSelected();
}
else
{
// iterate source field
if (sourceEnumerator == null)
{
sourceEnumerator = source.GetAsyncEnumerator(cancellationToken);
}
MoveNextSource();
}
return new UniTask<bool>(this, completionSource.Version);
}
void MoveNextSource()
{
try
{
sourceAwaiter = sourceEnumerator.MoveNextAsync().GetAwaiter();
}
catch (Exception ex)
{
completionSource.TrySetException(ex);
return;
}
if (sourceAwaiter.IsCompleted)
{
SourceMoveNextCore(this);
}
else
{
sourceAwaiter.SourceOnCompleted(sourceMoveNextCoreDelegate, this);
}
}
void MoveNextSelected()
{
try
{
selectedAwaiter = selectedEnumerator.MoveNextAsync().GetAwaiter();
}
catch (Exception ex)
{
completionSource.TrySetException(ex);
return;
}
if (selectedAwaiter.IsCompleted)
{
SeletedSourceMoveNextCore(this);
}
else
{
selectedAwaiter.SourceOnCompleted(selectedSourceMoveNextCoreDelegate, this);
}
}
static void SourceMoveNextCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.sourceAwaiter, out var result))
{
if (result)
{
try
{
self.sourceCurrent = self.sourceEnumerator.Current;
if (self.selector1 != null)
{
self.collectionSelectorAwaiter = self.selector1(self.sourceCurrent).GetAwaiter();
}
else
{
self.collectionSelectorAwaiter = self.selector2(self.sourceCurrent, checked(self.sourceIndex++)).GetAwaiter();
}
if (self.collectionSelectorAwaiter.IsCompleted)
{
SelectorAwaitCore(self);
}
else
{
self.collectionSelectorAwaiter.SourceOnCompleted(selectorAwaitCoreDelegate, self);
}
}
catch (Exception ex)
{
self.completionSource.TrySetException(ex);
return;
}
}
else
{
self.completionSource.TrySetResult(false);
}
}
}
static void SeletedSourceMoveNextCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.selectedAwaiter, out var result))
{
if (result)
{
try
{
self.resultSelectorAwaiter = self.resultSelector(self.sourceCurrent, self.selectedEnumerator.Current).GetAwaiter();
if (self.resultSelectorAwaiter.IsCompleted)
{
ResultSelectorAwaitCore(self);
}
else
{
self.resultSelectorAwaiter.SourceOnCompleted(resultSelectorAwaitCoreDelegate, self);
}
}
catch (Exception ex)
{
self.completionSource.TrySetException(ex);
return;
}
}
else
{
// dispose selected source and try iterate source.
try
{
self.selectedDisposeAsyncAwaiter = self.selectedEnumerator.DisposeAsync().GetAwaiter();
}
catch (Exception ex)
{
self.completionSource.TrySetException(ex);
return;
}
if (self.selectedDisposeAsyncAwaiter.IsCompleted)
{
SelectedEnumeratorDisposeAsyncCore(self);
}
else
{
self.selectedDisposeAsyncAwaiter.SourceOnCompleted(selectedEnumeratorDisposeAsyncCoreDelegate, self);
}
}
}
}
static void SelectedEnumeratorDisposeAsyncCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.selectedDisposeAsyncAwaiter))
{
self.selectedEnumerator = null;
self.selectedAwaiter = default;
self.MoveNextSource(); // iterate next source
}
}
static void SelectorAwaitCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.collectionSelectorAwaiter, out var result))
{
self.selectedEnumerator = result.GetAsyncEnumerator(self.cancellationToken);
self.MoveNextSelected(); // iterated selected source.
}
}
static void ResultSelectorAwaitCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.resultSelectorAwaiter, out var result))
{
self.Current = result;
self.completionSource.TrySetResult(true);
}
}
public async UniTask DisposeAsync()
{
if (selectedEnumerator != null)
{
await selectedEnumerator.DisposeAsync();
}
if (sourceEnumerator != null)
{
await sourceEnumerator.DisposeAsync();
}
}
}
}
internal sealed class SelectManyAwaitWithCancellation<TSource, TCollection, TResult> : IUniTaskAsyncEnumerable<TResult>
{
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector1;
readonly Func<TSource, int, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector2;
readonly Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector;
public SelectManyAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector, Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector)
{
this.source = source;
this.selector1 = selector;
this.selector2 = null;
this.resultSelector = resultSelector;
}
public SelectManyAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector, Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector)
{
this.source = source;
this.selector1 = null;
this.selector2 = selector;
this.resultSelector = resultSelector;
}
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new Enumerator(source, selector1, selector2, resultSelector, cancellationToken);
}
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
{
static readonly Action<object> sourceMoveNextCoreDelegate = SourceMoveNextCore;
static readonly Action<object> selectedSourceMoveNextCoreDelegate = SeletedSourceMoveNextCore;
static readonly Action<object> selectedEnumeratorDisposeAsyncCoreDelegate = SelectedEnumeratorDisposeAsyncCore;
static readonly Action<object> selectorAwaitCoreDelegate = SelectorAwaitCore;
static readonly Action<object> resultSelectorAwaitCoreDelegate = ResultSelectorAwaitCore;
readonly IUniTaskAsyncEnumerable<TSource> source;
readonly Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector1;
readonly Func<TSource, int, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector2;
readonly Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector;
CancellationToken cancellationToken;
TSource sourceCurrent;
int sourceIndex;
IUniTaskAsyncEnumerator<TSource> sourceEnumerator;
IUniTaskAsyncEnumerator<TCollection> selectedEnumerator;
UniTask<bool>.Awaiter sourceAwaiter;
UniTask<bool>.Awaiter selectedAwaiter;
UniTask.Awaiter selectedDisposeAsyncAwaiter;
// await additional
UniTask<IUniTaskAsyncEnumerable<TCollection>>.Awaiter collectionSelectorAwaiter;
UniTask<TResult>.Awaiter resultSelectorAwaiter;
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector1, Func<TSource, int, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> selector2, Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
{
this.source = source;
this.selector1 = selector1;
this.selector2 = selector2;
this.resultSelector = resultSelector;
this.cancellationToken = cancellationToken;
}
public TResult Current { get; private set; }
public UniTask<bool> MoveNextAsync()
{
completionSource.Reset();
// iterate selected field
if (selectedEnumerator != null)
{
MoveNextSelected();
}
else
{
// iterate source field
if (sourceEnumerator == null)
{
sourceEnumerator = source.GetAsyncEnumerator(cancellationToken);
}
MoveNextSource();
}
return new UniTask<bool>(this, completionSource.Version);
}
void MoveNextSource()
{
try
{
sourceAwaiter = sourceEnumerator.MoveNextAsync().GetAwaiter();
}
catch (Exception ex)
{
completionSource.TrySetException(ex);
return;
}
if (sourceAwaiter.IsCompleted)
{
SourceMoveNextCore(this);
}
else
{
sourceAwaiter.SourceOnCompleted(sourceMoveNextCoreDelegate, this);
}
}
void MoveNextSelected()
{
try
{
selectedAwaiter = selectedEnumerator.MoveNextAsync().GetAwaiter();
}
catch (Exception ex)
{
completionSource.TrySetException(ex);
return;
}
if (selectedAwaiter.IsCompleted)
{
SeletedSourceMoveNextCore(this);
}
else
{
selectedAwaiter.SourceOnCompleted(selectedSourceMoveNextCoreDelegate, this);
}
}
static void SourceMoveNextCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.sourceAwaiter, out var result))
{
if (result)
{
try
{
self.sourceCurrent = self.sourceEnumerator.Current;
if (self.selector1 != null)
{
self.collectionSelectorAwaiter = self.selector1(self.sourceCurrent, self.cancellationToken).GetAwaiter();
}
else
{
self.collectionSelectorAwaiter = self.selector2(self.sourceCurrent, checked(self.sourceIndex++), self.cancellationToken).GetAwaiter();
}
if (self.collectionSelectorAwaiter.IsCompleted)
{
SelectorAwaitCore(self);
}
else
{
self.collectionSelectorAwaiter.SourceOnCompleted(selectorAwaitCoreDelegate, self);
}
}
catch (Exception ex)
{
self.completionSource.TrySetException(ex);
return;
}
}
else
{
self.completionSource.TrySetResult(false);
}
}
}
static void SeletedSourceMoveNextCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.selectedAwaiter, out var result))
{
if (result)
{
try
{
self.resultSelectorAwaiter = self.resultSelector(self.sourceCurrent, self.selectedEnumerator.Current, self.cancellationToken).GetAwaiter();
if (self.resultSelectorAwaiter.IsCompleted)
{
ResultSelectorAwaitCore(self);
}
else
{
self.resultSelectorAwaiter.SourceOnCompleted(resultSelectorAwaitCoreDelegate, self);
}
}
catch (Exception ex)
{
self.completionSource.TrySetException(ex);
return;
}
}
else
{
// dispose selected source and try iterate source.
try
{
self.selectedDisposeAsyncAwaiter = self.selectedEnumerator.DisposeAsync().GetAwaiter();
}
catch (Exception ex)
{
self.completionSource.TrySetException(ex);
return;
}
if (self.selectedDisposeAsyncAwaiter.IsCompleted)
{
SelectedEnumeratorDisposeAsyncCore(self);
}
else
{
self.selectedDisposeAsyncAwaiter.SourceOnCompleted(selectedEnumeratorDisposeAsyncCoreDelegate, self);
}
}
}
}
static void SelectedEnumeratorDisposeAsyncCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.selectedDisposeAsyncAwaiter))
{
self.selectedEnumerator = null;
self.selectedAwaiter = default;
self.MoveNextSource(); // iterate next source
}
}
static void SelectorAwaitCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.collectionSelectorAwaiter, out var result))
{
self.selectedEnumerator = result.GetAsyncEnumerator(self.cancellationToken);
self.MoveNextSelected(); // iterated selected source.
}
}
static void ResultSelectorAwaitCore(object state)
{
var self = (Enumerator)state;
if (self.TryGetResult(self.resultSelectorAwaiter, out var result))
{
self.Current = result;
self.completionSource.TrySetResult(true);
}
}
public async UniTask DisposeAsync()
{
if (selectedEnumerator != null)
{
await selectedEnumerator.DisposeAsync();
}
if (sourceEnumerator != null)
{
await sourceEnumerator.DisposeAsync();
}
}
}
}
}

View File

@ -307,65 +307,6 @@ namespace ___Dummy
public static IUniTaskAsyncEnumerable<TResult> SelectMany<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, IUniTaskAsyncEnumerable<TResult>> selector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectMany<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, IUniTaskAsyncEnumerable<TResult>> selector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, IUniTaskAsyncEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, IUniTaskAsyncEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwait<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TResult>>> selector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> SelectManyAwaitWithCancellation<TSource, TCollection, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask<IUniTaskAsyncEnumerable<TCollection>>> collectionSelector, Func<TSource, TCollection, CancellationToken, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> Skip<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)

View File

@ -36,5 +36,124 @@ namespace NetCoreTests.Linq
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
}
[Theory]
[InlineData(0, 9)] // empty + exists
[InlineData(9, 0)] // exists + empty
[InlineData(9, 9)] // exists + exists
public async Task SelectMany(int leftCount, int rightCount)
{
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany(x => UniTaskAsyncEnumerable.Range(99, rightCount)).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount)).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany((i, x) => UniTaskAsyncEnumerable.Range(99 * i, rightCount)).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount)).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany(x => UniTaskAsyncEnumerable.Range(99, rightCount), (x, y) => x * y).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount), (x, y) => x * y).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany((i, x) => UniTaskAsyncEnumerable.Range(99 * i, rightCount), (x, y) => x * y).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount), (x, y) => x * y).ToArray();
xs.Should().BeEquivalentTo(ys);
}
// await
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount))).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount)).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait((i, x) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount))).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount)).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount), (x, y) => x * y).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait((i, x) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount), (x, y) => x * y).ToArray();
xs.Should().BeEquivalentTo(ys);
}
// with cancel
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount))).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount)).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((i, x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount))).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount)).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount), (x, y) => x * y).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((i, x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount), (x, y) => x * y).ToArray();
xs.Should().BeEquivalentTo(ys);
}
}
[Fact]
public async Task SelectManyException()
{
// error + exists
// exists + error
foreach (var item in UniTaskTestException.Throws())
{
var xs = item.SelectMany(x => UniTaskAsyncEnumerable.Range(0, 1)).ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
foreach (var item in UniTaskTestException.Throws())
{
var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectMany(x => item).ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
// await
foreach (var item in UniTaskTestException.Throws())
{
var xs = item.SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
foreach (var item in UniTaskTestException.Throws())
{
var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectManyAwait(x => UniTask.Run(() => item)).ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
// with c
foreach (var item in UniTaskTestException.Throws())
{
var xs = item.SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
foreach (var item in UniTaskTestException.Throws())
{
var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => item)).ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
}
}
}