commit
7fc6c6bd36
|
@ -0,0 +1,123 @@
|
|||
#pragma warning disable 0649
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks.Sources;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public static class UniTaskValueTaskExtensions
|
||||
{
|
||||
public static ValueTask AsValueTask(this UniTask task)
|
||||
{
|
||||
ref var core = ref Unsafe.As<UniTask, UniTaskToValueTask>(ref task);
|
||||
if (core.source == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
return new ValueTask(new UniTaskValueTaskSource(core.source), core.token);
|
||||
}
|
||||
|
||||
public static ValueTask<T> AsValueTask<T>(this UniTask<T> task)
|
||||
{
|
||||
ref var core = ref Unsafe.As<UniTask<T>, UniTaskToValueTask<T>>(ref task);
|
||||
if (core.source == null)
|
||||
{
|
||||
return new ValueTask<T>(core.result);
|
||||
}
|
||||
|
||||
return new ValueTask<T>(new UniTaskValueTaskSource<T>(core.source), core.token);
|
||||
}
|
||||
|
||||
struct UniTaskToValueTask
|
||||
{
|
||||
public IUniTaskSource source;
|
||||
public short token;
|
||||
}
|
||||
|
||||
class UniTaskValueTaskSource : IValueTaskSource
|
||||
{
|
||||
readonly IUniTaskSource source;
|
||||
|
||||
public UniTaskValueTaskSource(IUniTaskSource source)
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public void GetResult(short token)
|
||||
{
|
||||
source.GetResult(token);
|
||||
}
|
||||
|
||||
public ValueTaskSourceStatus GetStatus(short token)
|
||||
{
|
||||
var status = source.GetStatus(token);
|
||||
switch (status)
|
||||
{
|
||||
case UniTaskStatus.Pending:
|
||||
return ValueTaskSourceStatus.Pending;
|
||||
case UniTaskStatus.Succeeded:
|
||||
return ValueTaskSourceStatus.Succeeded;
|
||||
case UniTaskStatus.Faulted:
|
||||
return ValueTaskSourceStatus.Faulted;
|
||||
case UniTaskStatus.Canceled:
|
||||
return ValueTaskSourceStatus.Canceled;
|
||||
default:
|
||||
return (ValueTaskSourceStatus)status;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
||||
{
|
||||
source.OnCompleted(continuation, state, token);
|
||||
}
|
||||
}
|
||||
|
||||
struct UniTaskToValueTask<T>
|
||||
{
|
||||
public IUniTaskSource<T> source;
|
||||
public T result;
|
||||
public short token;
|
||||
}
|
||||
|
||||
class UniTaskValueTaskSource<T> : IValueTaskSource<T>
|
||||
{
|
||||
readonly IUniTaskSource<T> source;
|
||||
|
||||
public UniTaskValueTaskSource(IUniTaskSource<T> source)
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public T GetResult(short token)
|
||||
{
|
||||
return source.GetResult(token);
|
||||
}
|
||||
|
||||
public ValueTaskSourceStatus GetStatus(short token)
|
||||
{
|
||||
var status = source.GetStatus(token);
|
||||
switch (status)
|
||||
{
|
||||
case UniTaskStatus.Pending:
|
||||
return ValueTaskSourceStatus.Pending;
|
||||
case UniTaskStatus.Succeeded:
|
||||
return ValueTaskSourceStatus.Succeeded;
|
||||
case UniTaskStatus.Faulted:
|
||||
return ValueTaskSourceStatus.Faulted;
|
||||
case UniTaskStatus.Canceled:
|
||||
return ValueTaskSourceStatus.Canceled;
|
||||
default:
|
||||
return (ValueTaskSourceStatus)status;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
||||
{
|
||||
source.OnCompleted(continuation, state, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public partial struct UniTask
|
||||
{
|
||||
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||
public static async UniTask Run(Action action, bool configureAwait = true)
|
||||
{
|
||||
if (configureAwait)
|
||||
{
|
||||
var current = SynchronizationContext.Current;
|
||||
await UniTask.SwitchToThreadPool();
|
||||
try
|
||||
{
|
||||
action();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (current != null)
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
action();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||
public static async UniTask Run(Action<object> action, object state, bool configureAwait = true)
|
||||
{
|
||||
if (configureAwait)
|
||||
{
|
||||
var current = SynchronizationContext.Current;
|
||||
await UniTask.SwitchToThreadPool();
|
||||
try
|
||||
{
|
||||
action(state);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (current != null)
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
action(state);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||
public static async UniTask<T> Run<T>(Func<T> func, bool configureAwait = true)
|
||||
{
|
||||
if (configureAwait)
|
||||
{
|
||||
var current = SynchronizationContext.Current;
|
||||
await UniTask.SwitchToThreadPool();
|
||||
try
|
||||
{
|
||||
return func();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (current != null)
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
return func();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||
public static async UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true)
|
||||
{
|
||||
if (configureAwait)
|
||||
{
|
||||
var current = SynchronizationContext.Current;
|
||||
await UniTask.SwitchToThreadPool();
|
||||
try
|
||||
{
|
||||
return func(state);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (current != null)
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
return func(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public partial struct UniTask
|
||||
{
|
||||
public static UniTask.YieldAwaitable Yield()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
public readonly struct YieldAwaitable
|
||||
{
|
||||
public Awaiter GetAwaiter()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
public readonly struct Awaiter : ICriticalNotifyCompletion
|
||||
{
|
||||
static readonly SendOrPostCallback SendOrPostCallbackDelegate = Continuation;
|
||||
static readonly WaitCallback WaitCallbackDelegate = Continuation;
|
||||
|
||||
public bool IsCompleted => false;
|
||||
|
||||
public void GetResult() { }
|
||||
|
||||
public void OnCompleted(Action continuation)
|
||||
{
|
||||
UnsafeOnCompleted(continuation);
|
||||
}
|
||||
|
||||
public void UnsafeOnCompleted(Action continuation)
|
||||
{
|
||||
var syncContext = SynchronizationContext.Current;
|
||||
if (syncContext != null)
|
||||
{
|
||||
syncContext.Post(SendOrPostCallbackDelegate, continuation);
|
||||
}
|
||||
else
|
||||
{
|
||||
ThreadPool.UnsafeQueueUserWorkItem(WaitCallbackDelegate, continuation);
|
||||
}
|
||||
}
|
||||
|
||||
static void Continuation(object state)
|
||||
{
|
||||
((Action)state).Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,25 +7,11 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\UniTask\Assets\Plugins\UniTask\**\*.cs"
|
||||
Exclude="..\UniTask\Assets\Plugins\UniTask\Triggers\*.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\Editor\*.cs;
|
||||
<Compile Include="..\UniTask\Assets\Plugins\UniTask\**\*.cs" Exclude="..\UniTask\Assets\Plugins\UniTask\Triggers\*.cs;
 ..\UniTask\Assets\Plugins\UniTask\Editor\*.cs;
 
 ..\UniTask\Assets\Plugins\UniTask\Internal\UnityEqualityComparer.cs;
 ..\UniTask\Assets\Plugins\UniTask\Internal\DiagnosticsExtensions.cs;
 ..\UniTask\Assets\Plugins\UniTask\Internal\PlayerLoopRunner.cs;
 
 ..\UniTask\Assets\Plugins\UniTask\CancellationTokenSourceExtensions.cs;
 ..\UniTask\Assets\Plugins\UniTask\EnumeratorAsyncExtensions.cs;
 ..\UniTask\Assets\Plugins\UniTask\PlayerLoopHelper.cs;
 ..\UniTask\Assets\Plugins\UniTask\UniTask.Delay.cs;
 ..\UniTask\Assets\Plugins\UniTask\UniTask.Run.cs;
 ..\UniTask\Assets\Plugins\UniTask\UniTask.Bridge.cs;
 ..\UniTask\Assets\Plugins\UniTask\UniTask.WaitUntil.cs;
 ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.cs;
 ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.uGUI.cs;
 ..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.MonoBehaviour.cs;
 " />
|
||||
</ItemGroup>
|
||||
|
||||
..\UniTask\Assets\Plugins\UniTask\Internal\UnityEqualityComparer.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\Internal\DiagnosticsExtensions.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\Internal\PlayerLoopRunner.cs;
|
||||
|
||||
..\UniTask\Assets\Plugins\UniTask\CancellationTokenSourceExtensions.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\EnumeratorAsyncExtensions.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\PlayerLoopHelper.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\UniTask.Delay.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\UniTask.Run.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\UniTask.Bridge.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\UniTask.WaitUntil.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.uGUI.cs;
|
||||
..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.MonoBehaviour.cs;
|
||||
"/>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,46 +1,241 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
|
||||
namespace NetCoreSandbox
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static string FlattenGenArgs(Type type)
|
||||
{
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
var t = string.Join(", ", type.GetGenericArguments().Select(x => FlattenGenArgs(x)));
|
||||
return Regex.Replace(type.Name, "`.+", "") + "<" + t + ">";
|
||||
}
|
||||
//x.ReturnType.GetGenericArguments()
|
||||
else
|
||||
{
|
||||
return type.Name;
|
||||
}
|
||||
}
|
||||
|
||||
static async IAsyncEnumerable<int> FooAsync([EnumeratorCancellation]CancellationToken cancellationToken = default)
|
||||
{
|
||||
yield return 1;
|
||||
await Task.Delay(10, cancellationToken);
|
||||
}
|
||||
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Foo");
|
||||
var v = await outer().AsTask();
|
||||
await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)
|
||||
.SelectAwait(x => UniTask.Run(() => x))
|
||||
.TakeLast(6)
|
||||
|
||||
Console.WriteLine("Bar:" + v);
|
||||
}
|
||||
|
||||
static async UniTask<int> outer()
|
||||
|
||||
)
|
||||
{
|
||||
//await Task.WhenAll();
|
||||
|
||||
//var foo = await Task.WhenAny(Array.Empty<Task<int>>());
|
||||
|
||||
|
||||
await UniTask.WhenAny(new UniTask[0]);
|
||||
|
||||
return 10;
|
||||
//var v = await DoAsync();
|
||||
//return v;
|
||||
Console.WriteLine(item);
|
||||
}
|
||||
|
||||
|
||||
static async UniTask<int> DoAsync()
|
||||
// AsyncEnumerable.Range(1, 10).GroupBy(x=>x).Select(x=>x.first
|
||||
|
||||
|
||||
|
||||
//Enumerable.Range(1,10).ToHashSet(
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Foo()
|
||||
{
|
||||
var tcs = new UniTaskCompletionSource<int>();
|
||||
|
||||
tcs.TrySetResult(100);
|
||||
// AsyncEnumerable.Range(1,10).Do(
|
||||
|
||||
// AsyncEnumerable.t
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine(@"using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
");
|
||||
|
||||
|
||||
|
||||
var chako = typeof(AsyncEnumerable).GetMethods()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x =>
|
||||
{
|
||||
var ret = FlattenGenArgs(x.ReturnType);
|
||||
|
||||
|
||||
var generics = string.Join(", ", x.GetGenericArguments().Select(x => x.Name));
|
||||
|
||||
if (x.GetParameters().Length == 0) return "";
|
||||
|
||||
var self = x.GetParameters().First();
|
||||
if (x.GetCustomAttributes(typeof(ExtensionAttribute), true).Length == 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
var arg1Type = FlattenGenArgs(x.GetParameters().First().ParameterType);
|
||||
|
||||
var others = string.Join(", ", x.GetParameters().Skip(1).Select(y => FlattenGenArgs(y.ParameterType) + " " + y.Name));
|
||||
|
||||
if (!string.IsNullOrEmpty(others))
|
||||
{
|
||||
others = ", " + others;
|
||||
}
|
||||
|
||||
var template = $"public static {ret} {x.Name}<{generics}>(this {arg1Type} {self.Name}{others})";
|
||||
|
||||
|
||||
|
||||
return template.Replace("ValueTask", "UniTask").Replace("IAsyncEnumerable", "IUniTaskAsyncEnumerable").Replace("<>", "");
|
||||
})
|
||||
.Where(x => x != "")
|
||||
.Select(x => x + "\r\n{\r\n throw new NotImplementedException();\r\n}")
|
||||
.ToArray();
|
||||
|
||||
var huga = string.Join("\r\n\r\n", chako);
|
||||
|
||||
|
||||
|
||||
|
||||
foreach (var item in typeof(AsyncEnumerable).GetMethods().Select(x => x.Name).Distinct())
|
||||
{
|
||||
if (item.EndsWith("AwaitAsync") || item.EndsWith("AwaitWithCancellationAsync") || item.EndsWith("WithCancellation"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var item2 = item.Replace("Async", "");
|
||||
item2 = item2.Replace("Await", "");
|
||||
|
||||
var format = @"
|
||||
internal sealed class {0}
|
||||
{{
|
||||
}}
|
||||
";
|
||||
|
||||
sb.Append(string.Format(format, item2));
|
||||
|
||||
}
|
||||
|
||||
sb.Append("}");
|
||||
|
||||
|
||||
Console.WriteLine(sb.ToString());
|
||||
|
||||
|
||||
|
||||
var v = await tcs.Task;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
public static async IAsyncEnumerable<int> AsyncGen()
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
yield return 10;
|
||||
await UniTask.SwitchToThreadPool();
|
||||
yield return 100;
|
||||
}
|
||||
}
|
||||
|
||||
class MyEnumerable : IEnumerable<int>
|
||||
{
|
||||
public IEnumerator<int> GetEnumerator()
|
||||
{
|
||||
return new MyEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
class MyEnumerator : IEnumerator<int>
|
||||
{
|
||||
public int Current => throw new NotImplementedException();
|
||||
|
||||
object IEnumerator.Current => throw new NotImplementedException();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Console.WriteLine("Called Dispose");
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class MyClass<T>
|
||||
{
|
||||
public CustomAsyncEnumerator<T> GetAsyncEnumerator()
|
||||
{
|
||||
//IAsyncEnumerable
|
||||
return new CustomAsyncEnumerator<T>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public struct CustomAsyncEnumerator<T>
|
||||
{
|
||||
int count;
|
||||
|
||||
public T Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
if (count++ == 3)
|
||||
{
|
||||
return UniTask.FromResult(false);
|
||||
//return false;
|
||||
}
|
||||
return UniTask.FromResult(true);
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,11 @@
|
|||
<RootNamespace>NetCoreSandbox</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Interactive.Async" Version="4.1.1" />
|
||||
<PackageReference Include="System.Reactive" Version="4.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UniTask.NetCore\UniTask.NetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -0,0 +1,496 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Aggregate
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, 10)]
|
||||
[InlineData(0, 1)]
|
||||
[InlineData(10, 0)]
|
||||
[InlineData(1, 11)]
|
||||
public async Task Sum(int start, int count)
|
||||
{
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync();
|
||||
var ys = Enumerable.Range(start, count).Sum();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync(x => x * 2);
|
||||
var ys = Enumerable.Range(start, count).Sum(x => x * 2);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitAsync(x => UniTask.Run(() => x));
|
||||
var ys = Enumerable.Range(start, count).Sum(x => x);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitCancellationAsync((x, _) => UniTask.Run(() => x));
|
||||
var ys = Enumerable.Range(start, count).Sum(x => x);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> array1 = new object[][]
|
||||
{
|
||||
new object[]{new int[] { 1, 10, 100 } },
|
||||
new object[]{new int?[] { 1, null, 100 } },
|
||||
new object[]{new float[] { 1, 10, 100 } },
|
||||
new object[]{new float?[] { 1, null, 100 } },
|
||||
new object[]{new double[] { 1, 10, 100 } },
|
||||
new object[]{new double?[] { 1, null, 100 } },
|
||||
new object[]{new decimal[] { 1, 10, 100 } },
|
||||
new object[]{new decimal?[] { 1, null, 100 } },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(array1))]
|
||||
public async Task Average<T>(T arr)
|
||||
{
|
||||
switch (arr)
|
||||
{
|
||||
case int[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
case int?[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
case float[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
case float?[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
case double[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
case double?[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
case decimal[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
case decimal?[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||
var ys = array.Average();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static IEnumerable<object[]> array2 = new object[][]
|
||||
{
|
||||
new object[]{new int[] { } },
|
||||
new object[]{new int[] { 5 } },
|
||||
new object[]{new int[] { 5, 10, 100 } },
|
||||
new object[]{new int[] { 10, 5,100 } },
|
||||
new object[]{new int[] { 100, 10, 5 } },
|
||||
|
||||
new object[]{new int?[] { } },
|
||||
new object[]{new int?[] { 5 } },
|
||||
new object[]{new int?[] { null, null, null } },
|
||||
new object[]{new int?[] { null, 5, 10, 100 } },
|
||||
new object[]{new int?[] { 10, 5,100, null } },
|
||||
new object[]{new int?[] { 100, 10, 5 } },
|
||||
|
||||
new object[]{new X[] { } },
|
||||
new object[]{new X[] { new X(5) } },
|
||||
new object[]{new X[] { new X(5), new X(10), new X(100) } },
|
||||
new object[]{new X[] { new X(10),new X( 5),new X(100) } },
|
||||
new object[]{new X[] { new X(100), new X(10),new X(5) } },
|
||||
|
||||
new object[]{new XX[] { } },
|
||||
new object[]{new XX[] { new XX(new X(5)) } },
|
||||
new object[]{new XX[] { new XX(new X(5)), new XX(new X(10)), new XX(new X(100)) } },
|
||||
new object[]{new XX[] { new XX(new X(10)),new XX(new X( 5)),new XX(new X(100)) } },
|
||||
new object[]{new XX[] { new XX(new X(100)), new XX(new X(10)),new XX(new X(5)) } },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(array2))]
|
||||
public async Task Min<T>(T arr)
|
||||
{
|
||||
switch (arr)
|
||||
{
|
||||
case int[] array:
|
||||
{
|
||||
{
|
||||
if (array.Length == 0)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MinAsync());
|
||||
Assert.Throws<InvalidOperationException>(() => array.Min());
|
||||
}
|
||||
else
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync();
|
||||
var ys = array.Min();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
{
|
||||
if (array.Length == 0)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MinAsync(x => x * 2));
|
||||
Assert.Throws<InvalidOperationException>(() => array.Min(x => x * 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x * 2);
|
||||
var ys = array.Min(x => x * 2);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case int?[] array:
|
||||
{
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync();
|
||||
var ys = array.Min();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x);
|
||||
var ys = array.Min(x => x);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case X[] array:
|
||||
{
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync();
|
||||
var ys = array.Min();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
|
||||
if (array.Length == 0)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MinAsync(x => x.Value));
|
||||
Assert.Throws<InvalidOperationException>(() => array.Min(x => x.Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x.Value);
|
||||
var ys = array.Min(x => x.Value);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case XX[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x.Value);
|
||||
var ys = array.Min(x => x.Value);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(array2))]
|
||||
public async Task Max<T>(T arr)
|
||||
{
|
||||
switch (arr)
|
||||
{
|
||||
case int[] array:
|
||||
{
|
||||
{
|
||||
if (array.Length == 0)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MaxAsync());
|
||||
Assert.Throws<InvalidOperationException>(() => array.Max());
|
||||
}
|
||||
else
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync();
|
||||
var ys = array.Max();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
{
|
||||
if (array.Length == 0)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x * 2));
|
||||
Assert.Throws<InvalidOperationException>(() => array.Max(x => x * 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x * 2);
|
||||
var ys = array.Max(x => x * 2);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case int?[] array:
|
||||
{
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync();
|
||||
var ys = array.Max();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x);
|
||||
var ys = array.Max(x => x);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case X[] array:
|
||||
{
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync();
|
||||
var ys = array.Max();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
|
||||
if (array.Length == 0)
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x.Value));
|
||||
Assert.Throws<InvalidOperationException>(() => array.Max(x => x.Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x.Value);
|
||||
var ys = array.Max(x => x.Value);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case XX[] array:
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x.Value);
|
||||
var ys = array.Max(x => x.Value);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public class XX
|
||||
{
|
||||
public readonly X Value;
|
||||
|
||||
public XX(X value)
|
||||
{
|
||||
this.Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public class X : IComparable<X>
|
||||
{
|
||||
public readonly int Value;
|
||||
|
||||
public X(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int CompareTo([AllowNull] X other)
|
||||
{
|
||||
return Comparer<int>.Default.Compare(Value, other.Value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 10)]
|
||||
[InlineData(0, 1)]
|
||||
[InlineData(10, 0)]
|
||||
[InlineData(1, 11)]
|
||||
public async Task Count(int start, int count)
|
||||
{
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync();
|
||||
var ys = Enumerable.Range(start, count).Count();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync(x => x % 2 == 0);
|
||||
var ys = Enumerable.Range(start, count).Count(x => x % 2 == 0);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync();
|
||||
var ys = Enumerable.Range(start, count).LongCount();
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync(x => x % 2 == 0);
|
||||
var ys = Enumerable.Range(start, count).LongCount(x => x % 2 == 0);
|
||||
xs.Should().Be(ys);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task AggregateTest1()
|
||||
{
|
||||
// 0
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await new int[] { }.ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y));
|
||||
Assert.Throws<InvalidOperationException>(() => new int[] { }.Aggregate((x, y) => x + y));
|
||||
|
||||
// 1
|
||||
{
|
||||
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y);
|
||||
var b = Enumerable.Range(1, 1).Aggregate((x, y) => x + y);
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 2
|
||||
{
|
||||
var a = await Enumerable.Range(1, 2).ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y);
|
||||
var b = Enumerable.Range(1, 2).Aggregate((x, y) => x + y);
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 10
|
||||
{
|
||||
var a = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y);
|
||||
var b = Enumerable.Range(1, 10).Aggregate((x, y) => x + y);
|
||||
a.Should().Be(b);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AggregateTest2()
|
||||
{
|
||||
// 0
|
||||
{
|
||||
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y);
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 1
|
||||
{
|
||||
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y);
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 2
|
||||
{
|
||||
var a = await Enumerable.Range(1, 2).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||
var b = Enumerable.Range(1, 2).Aggregate(1000, (x, y) => x + y);
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 10
|
||||
{
|
||||
var a = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||
var b = Enumerable.Range(1, 10).Aggregate(1000, (x, y) => x + y);
|
||||
a.Should().Be(b);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AggregateTest3()
|
||||
{
|
||||
// 0
|
||||
{
|
||||
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 1
|
||||
{
|
||||
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 2
|
||||
{
|
||||
var a = await Enumerable.Range(1, 2).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
var b = Enumerable.Range(1, 2).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
a.Should().Be(b);
|
||||
}
|
||||
|
||||
// 10
|
||||
{
|
||||
var a = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
var b = Enumerable.Range(1, 10).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||
a.Should().Be(b);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ForEach()
|
||||
{
|
||||
var list = new List<int>();
|
||||
await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().ForEachAsync(x =>
|
||||
{
|
||||
list.Add(x);
|
||||
});
|
||||
|
||||
list.Should().BeEquivalentTo(Enumerable.Range(1, 10));
|
||||
|
||||
var list2 = new List<(int, int)>();
|
||||
await Enumerable.Range(5, 10).ToUniTaskAsyncEnumerable().ForEachAsync((index, x) =>
|
||||
{
|
||||
list2.Add((index, x));
|
||||
});
|
||||
|
||||
var list3 = Enumerable.Range(5, 10).Select((index, x) => (index, x)).ToArray();
|
||||
list2.Should().BeEquivalentTo(list3);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class AllAny
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(1, 1)]
|
||||
[InlineData(1, 2)]
|
||||
[InlineData(1, 3)]
|
||||
[InlineData(0, 10)]
|
||||
[InlineData(0, 11)]
|
||||
public async Task AllTest(int start, int count)
|
||||
{
|
||||
var range = Enumerable.Range(start, count);
|
||||
var x = await range.ToUniTaskAsyncEnumerable().AllAsync(x => x % 2 == 0);
|
||||
var y = range.All(x => x % 2 == 0);
|
||||
|
||||
x.Should().Be(y);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(1, 1)]
|
||||
[InlineData(1, 2)]
|
||||
[InlineData(1, 3)]
|
||||
[InlineData(0, 10)]
|
||||
[InlineData(0, 11)]
|
||||
public async Task AnyTest(int start, int count)
|
||||
{
|
||||
var range = Enumerable.Range(start, count);
|
||||
{
|
||||
var x = await range.ToUniTaskAsyncEnumerable().AnyAsync();
|
||||
var y = range.Any();
|
||||
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await range.ToUniTaskAsyncEnumerable().AnyAsync(x => x % 2 == 0);
|
||||
var y = range.Any(x => x % 2 == 0);
|
||||
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(1, 1)]
|
||||
[InlineData(1, 2)]
|
||||
[InlineData(1, 3)]
|
||||
[InlineData(0, 10)]
|
||||
[InlineData(0, 11)]
|
||||
public async Task ContainsTest(int start, int count)
|
||||
{
|
||||
var range = Enumerable.Range(start, count);
|
||||
foreach (var c in Enumerable.Range(0, 15))
|
||||
{
|
||||
var x = await range.ToUniTaskAsyncEnumerable().ContainsAsync(c);
|
||||
var y = range.Contains(c);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SequenceEqual()
|
||||
{
|
||||
// empty and empty
|
||||
(await new int[0].ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[0].ToUniTaskAsyncEnumerable())).Should().BeTrue();
|
||||
(new int[0].SequenceEqual(new int[0])).Should().BeTrue();
|
||||
|
||||
// empty and exists
|
||||
(await new int[0].ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||
(new int[0].SequenceEqual(new int[] { 1 })).Should().BeFalse();
|
||||
|
||||
// exists and empty
|
||||
(await new int[] { 1 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[0].ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||
(new int[] { 1 }.SequenceEqual(new int[] { })).Should().BeFalse();
|
||||
|
||||
// samelength same value
|
||||
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable())).Should().BeTrue();
|
||||
(new int[] { 1, 2, 3 }.SequenceEqual(new int[] { 1, 2, 3 })).Should().BeTrue();
|
||||
|
||||
// samelength different value(first)
|
||||
(await new int[] { 5, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||
|
||||
// samelength different value(middle)
|
||||
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 5, 3 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||
|
||||
// samelength different value(last)
|
||||
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 5 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||
|
||||
// left is long
|
||||
(await new int[] { 1, 2, 3, 4 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||
(new int[] { 1, 2, 3, 4 }.SequenceEqual(new int[] { 1, 2, 3 })).Should().BeFalse();
|
||||
|
||||
// right is long
|
||||
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3, 4 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||
(new int[] { 1, 2, 3 }.SequenceEqual(new int[] { 1, 2, 3, 4 })).Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Concat
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(0, 1)]
|
||||
[InlineData(0, 2)]
|
||||
[InlineData(0, 10)]
|
||||
public async Task Append(int start, int count)
|
||||
{
|
||||
var xs = await Enumerable.Range(start, count).ToUniTaskAsyncEnumerable().Append(99).ToArrayAsync();
|
||||
var ys = Enumerable.Range(start, count).Append(99).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AppendThrow()
|
||||
{
|
||||
var xs = UniTaskTestException.ThrowImmediate().Append(99).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
|
||||
var ys = UniTaskTestException.ThrowAfter().Append(99).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
|
||||
var zs = UniTaskTestException.ThrowInMoveNext().Append(99).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(0, 1)]
|
||||
[InlineData(0, 2)]
|
||||
[InlineData(0, 10)]
|
||||
public async Task Prepend(int start, int count)
|
||||
{
|
||||
var xs = await Enumerable.Range(start, count).ToUniTaskAsyncEnumerable().Prepend(99).ToArrayAsync();
|
||||
var ys = Enumerable.Range(start, count).Prepend(99).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PrependThrow()
|
||||
{
|
||||
var xs = UniTaskTestException.ThrowImmediate().Prepend(99).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
|
||||
var ys = UniTaskTestException.ThrowAfter().Prepend(99).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
|
||||
var zs = UniTaskTestException.ThrowInMoveNext().Prepend(99).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> array1 = new object[][]
|
||||
{
|
||||
new object[] { (0, 0), (0, 0) }, // empty + empty
|
||||
new object[] { (0, 1), (0, 0) }, // 1 + empty
|
||||
new object[] { (0, 0), (0, 1) }, // empty + 1
|
||||
new object[] { (0, 5), (0, 0) }, // 5 + empty
|
||||
new object[] { (0, 0), (0, 5) }, // empty + 5
|
||||
new object[] { (0, 5), (0, 5) }, // 5 + 5
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(array1))]
|
||||
public async Task ConcatTest((int, int) left, (int, int) right)
|
||||
{
|
||||
var l = Enumerable.Range(left.Item1, left.Item2);
|
||||
var r = Enumerable.Range(right.Item1, right.Item2);
|
||||
|
||||
var xs = await l.ToUniTaskAsyncEnumerable().Concat(r.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||
var ys = l.Concat(r).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConcatThrow()
|
||||
{
|
||||
{
|
||||
var xs = UniTaskTestException.ThrowImmediate().Concat(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
|
||||
var ys = UniTaskTestException.ThrowAfter().Concat(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
|
||||
var zs = UniTaskTestException.ThrowInMoveNext().Concat(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||
}
|
||||
{
|
||||
var xs = UniTaskAsyncEnumerable.Range(1, 10).Concat(UniTaskTestException.ThrowImmediate()).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
|
||||
var ys = UniTaskAsyncEnumerable.Range(1, 10).Concat(UniTaskTestException.ThrowAfter()).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
|
||||
var zs = UniTaskAsyncEnumerable.Range(1, 10).Concat(UniTaskTestException.ThrowInMoveNext()).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DefaultIfEmpty()
|
||||
{
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, 0).DefaultIfEmpty(99).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, 1).DefaultIfEmpty(99).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, 10).DefaultIfEmpty(99).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
// Throw
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.DefaultIfEmpty().ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,196 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Convert
|
||||
{
|
||||
[Fact]
|
||||
public async Task ToAsyncEnumerable()
|
||||
{
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||
|
||||
xs.Length.Should().Be(100);
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||
|
||||
xs.Length.Should().Be(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task ToObservable()
|
||||
{
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, 10).ToObservable().ToArray();
|
||||
xs.Should().BeEquivalentTo(Enumerable.Range(1, 10));
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, 0).ToObservable().ToArray();
|
||||
xs.Should().BeEquivalentTo(Enumerable.Range(1, 0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task ToAsyncEnumerableTask()
|
||||
{
|
||||
var t = Task.FromResult(100);
|
||||
var xs = await t.ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||
|
||||
xs.Length.Should().Be(1);
|
||||
xs[0].Should().Be(100);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToAsyncEnumerableUniTask()
|
||||
{
|
||||
var t = UniTask.FromResult(100);
|
||||
var xs = await t.ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||
|
||||
xs.Length.Should().Be(1);
|
||||
xs[0].Should().Be(100);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToAsyncEnumerableObservable()
|
||||
{
|
||||
{
|
||||
var xs = await Observable.Range(1, 100).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||
var ys = await Observable.Range(1, 100).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await Observable.Range(1, 100, ThreadPoolScheduler.Instance).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||
var ys = await Observable.Range(1, 100, ThreadPoolScheduler.Instance).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await Observable.Empty<int>(ThreadPoolScheduler.Instance).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||
var ys = await Observable.Empty<int>(ThreadPoolScheduler.Instance).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToDictionary()
|
||||
{
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x);
|
||||
var ys = Enumerable.Range(1, 100).ToDictionary(x => x);
|
||||
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x);
|
||||
var ys = Enumerable.Range(1, 0).ToDictionary(x => x);
|
||||
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x, x => x * 2);
|
||||
var ys = Enumerable.Range(1, 100).ToDictionary(x => x, x => x * 2);
|
||||
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x, x => x * 2);
|
||||
var ys = Enumerable.Range(1, 0).ToDictionary(x => x, x => x * 2);
|
||||
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToLookup()
|
||||
{
|
||||
var arr = new[] { 1, 4, 10, 10, 4, 5, 10, 9 };
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().ToLookupAsync(x => x);
|
||||
var ys = arr.ToLookup(x => x);
|
||||
|
||||
xs.Count.Should().Be(ys.Count);
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
foreach (var key in xs.Select(x => x.Key))
|
||||
{
|
||||
xs[key].Should().BeEquivalentTo(ys[key]);
|
||||
}
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToLookupAsync(x => x);
|
||||
var ys = Enumerable.Range(1, 0).ToLookup(x => x);
|
||||
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().ToLookupAsync(x => x, x => x * 2);
|
||||
var ys = arr.ToLookup(x => x, x => x * 2);
|
||||
|
||||
xs.Count.Should().Be(ys.Count);
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
foreach (var key in xs.Select(x => x.Key))
|
||||
{
|
||||
xs[key].Should().BeEquivalentTo(ys[key]);
|
||||
}
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToLookupAsync(x => x, x => x * 2);
|
||||
var ys = Enumerable.Range(1, 0).ToLookup(x => x, x => x * 2);
|
||||
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToList()
|
||||
{
|
||||
{
|
||||
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToListAsync();
|
||||
var ys = Enumerable.Range(1, 100).ToList();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ToListAsync();
|
||||
var ys = Enumerable.Empty<int>().ToList();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToHashSet()
|
||||
{
|
||||
{
|
||||
var xs = await new[] { 1, 20, 4, 5, 20, 4, 6 }.ToUniTaskAsyncEnumerable().ToHashSetAsync();
|
||||
var ys = new[] { 1, 20, 4, 5, 20, 4, 6 }.ToHashSet();
|
||||
|
||||
xs.OrderBy(x => x).Should().BeEquivalentTo(ys.OrderBy(x => x));
|
||||
}
|
||||
{
|
||||
var xs = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ToHashSetAsync();
|
||||
var ys = Enumerable.Empty<int>().ToHashSet();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Factory
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, 10)]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(1, 5)]
|
||||
[InlineData(1, 0)]
|
||||
[InlineData(0, 11)]
|
||||
[InlineData(1, 11)]
|
||||
public async Task RangeTest(int start, int count)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(start, count).ToArrayAsync();
|
||||
var ys = Enumerable.Range(start, count).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("foo", 0)]
|
||||
[InlineData("bar", 1)]
|
||||
[InlineData("baz", 3)]
|
||||
[InlineData("foobar", 10)]
|
||||
[InlineData("foobarbaz", 11)]
|
||||
public async Task RepeatTest(string value, int count)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Repeat(value, count).ToArrayAsync();
|
||||
var ys = Enumerable.Repeat(value, count).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EmptyTest()
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Empty<int>().ToArrayAsync();
|
||||
var ys = Enumerable.Empty<int>().ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(100)]
|
||||
[InlineData((string)null)]
|
||||
[InlineData("foo")]
|
||||
public async Task ReturnTest<T>(T value)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Return(value).ToArrayAsync();
|
||||
|
||||
xs.Length.Should().Be(1);
|
||||
xs[0].Should().Be(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Filtering
|
||||
{
|
||||
[Fact]
|
||||
public async Task Where()
|
||||
{
|
||||
var range = Enumerable.Range(1, 10);
|
||||
var src = range.ToUniTaskAsyncEnumerable();
|
||||
|
||||
{
|
||||
var a = await src.Where(x => x % 2 == 0).ToArrayAsync();
|
||||
var expected = range.Where(x => x % 2 == 0).ToArray();
|
||||
a.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
{
|
||||
var a = await src.Where((x, i) => (x + i) % 2 == 0).ToArrayAsync();
|
||||
var expected = range.Where((x, i) => (x + i) % 2 == 0).ToArray();
|
||||
a.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
{
|
||||
var a = await src.WhereAwait(x => UniTask.Run(() => x % 2 == 0)).ToArrayAsync();
|
||||
var b = await src.WhereAwait(x => UniTask.FromResult(x % 2 == 0)).ToArrayAsync();
|
||||
var expected = range.Where(x => x % 2 == 0).ToArray();
|
||||
a.Should().BeEquivalentTo(expected);
|
||||
b.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
{
|
||||
var a = await src.WhereAwait((x, i) => UniTask.Run(() => (x + i) % 2 == 0)).ToArrayAsync();
|
||||
var b = await src.WhereAwait((x, i) => UniTask.FromResult((x + i) % 2 == 0)).ToArrayAsync();
|
||||
var expected = range.Where((x, i) => (x + i) % 2 == 0).ToArray();
|
||||
a.Should().BeEquivalentTo(expected);
|
||||
b.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task WhereException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
{
|
||||
var xs = item.Where(x => x % 2 == 0).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.Where((x, i) => x % 2 == 0).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.WhereAwait(x => UniTask.FromResult(x % 2 == 0)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.WhereAwait((x, i) => UniTask.FromResult(x % 2 == 0)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OfType()
|
||||
{
|
||||
var data = new object[] { 0, null, 10, 30, null, "foo", 99 };
|
||||
|
||||
var a = await data.ToUniTaskAsyncEnumerable().OfType<int>().ToArrayAsync();
|
||||
var b = data.OfType<int>().ToArray();
|
||||
|
||||
a.Should().BeEquivalentTo(b);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task OfTypeException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Select(x => (object)x).OfType<int>().ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Cast()
|
||||
{
|
||||
var data = new object[] { 0, 10, 30, 99 };
|
||||
|
||||
var a = await data.ToUniTaskAsyncEnumerable().Cast<int>().ToArrayAsync();
|
||||
var b = data.Cast<int>().ToArray();
|
||||
|
||||
a.Should().BeEquivalentTo(b);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task CastException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Select(x => (object)x).Cast<int>().ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,258 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class FirstLast
|
||||
{
|
||||
[Fact]
|
||||
public async Task FirstTest()
|
||||
{
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().FirstAsync());
|
||||
Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().First());
|
||||
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().FirstAsync();
|
||||
var y = new[] { 99 }.First();
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().FirstAsync(x => x % 98 == 0));
|
||||
Assert.Throws<InvalidOperationException>(() => array.First(x => x % 98 == 0));
|
||||
|
||||
var x = await array.ToUniTaskAsyncEnumerable().FirstAsync(x => x % 2 == 0);
|
||||
var y = array.First(x => x % 2 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
|
||||
{
|
||||
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().FirstOrDefaultAsync();
|
||||
var y = Enumerable.Empty<int>().FirstOrDefault();
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().FirstOrDefaultAsync();
|
||||
var y = new[] { 99 }.FirstOrDefault();
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
var x = await array.ToUniTaskAsyncEnumerable().FirstOrDefaultAsync(x => x % 98 == 0);
|
||||
var y = array.FirstOrDefault(x => x % 98 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
var x = await array.ToUniTaskAsyncEnumerable().FirstAsync(x => x % 2 == 0);
|
||||
var y = array.FirstOrDefault(x => x % 2 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LastTest()
|
||||
{
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().LastAsync());
|
||||
Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().Last());
|
||||
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().LastAsync();
|
||||
var y = new[] { 99 }.Last();
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().LastAsync(x => x % 98 == 0));
|
||||
Assert.Throws<InvalidOperationException>(() => array.Last(x => x % 98 == 0));
|
||||
|
||||
var x = await array.ToUniTaskAsyncEnumerable().LastAsync(x => x % 2 == 0);
|
||||
var y = array.Last(x => x % 2 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
|
||||
{
|
||||
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().LastOrDefaultAsync();
|
||||
var y = Enumerable.Empty<int>().LastOrDefault();
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().LastOrDefaultAsync();
|
||||
var y = new[] { 99 }.LastOrDefault();
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
var x = await array.ToUniTaskAsyncEnumerable().LastOrDefaultAsync(x => x % 98 == 0);
|
||||
var y = array.LastOrDefault(x => x % 98 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
var x = await array.ToUniTaskAsyncEnumerable().LastOrDefaultAsync(x => x % 2 == 0);
|
||||
var y = array.LastOrDefault(x => x % 2 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SingleTest()
|
||||
{
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().SingleAsync());
|
||||
Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().Single());
|
||||
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().SingleAsync();
|
||||
var y = new[] { 99 }.Single();
|
||||
x.Should().Be(y);
|
||||
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleAsync());
|
||||
Assert.Throws<InvalidOperationException>(() => array.Single());
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
// not found
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 999 == 0));
|
||||
Assert.Throws<InvalidOperationException>(() => array.Single(x => x % 999 == 0));
|
||||
// found multi
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 2 == 0));
|
||||
Assert.Throws<InvalidOperationException>(() => array.Single(x => x % 2 == 0));
|
||||
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 144 == 0);
|
||||
var y = array.Single(x => x % 144 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 800 == 0);
|
||||
var y = array.Single(x => x % 800 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
{
|
||||
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().SingleOrDefaultAsync();
|
||||
var y = Enumerable.Empty<int>().SingleOrDefault();
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync();
|
||||
var y = new[] { 99 }.SingleOrDefault();
|
||||
x.Should().Be(y);
|
||||
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync());
|
||||
Assert.Throws<InvalidOperationException>(() => array.SingleOrDefault());
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
// not found
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 999 == 0);
|
||||
var y = array.SingleOrDefault(x => x % 999 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
// found multi
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 2 == 0));
|
||||
Assert.Throws<InvalidOperationException>(() => array.SingleOrDefault(x => x % 2 == 0));
|
||||
|
||||
// normal
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 144 == 0);
|
||||
var y = array.SingleOrDefault(x => x % 144 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 800 == 0);
|
||||
var y = array.SingleOrDefault(x => x % 800 == 0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task ElementAtTest()
|
||||
{
|
||||
{
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ElementAtAsync(0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Enumerable.Empty<int>().ElementAt(0));
|
||||
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().ElementAtAsync(0);
|
||||
var y = new[] { 99 }.ElementAt(0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await array.ToUniTaskAsyncEnumerable().ElementAtAsync(10));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => array.ElementAt(10));
|
||||
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().ElementAtAsync(0);
|
||||
var y = array.ElementAt(0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().ElementAtAsync(3);
|
||||
var y = array.ElementAt(3);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().ElementAtAsync(5);
|
||||
var y = array.ElementAt(5);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
{
|
||||
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(0);
|
||||
var y = Enumerable.Empty<int>().ElementAtOrDefault(0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(0);
|
||||
var y = new[] { 99 }.ElementAtOrDefault(0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
{
|
||||
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(10);
|
||||
var y = array.ElementAtOrDefault(10);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(0);
|
||||
var y = array.ElementAtOrDefault(0);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(3);
|
||||
var y = array.ElementAtOrDefault(3);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
{
|
||||
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(5);
|
||||
var y = array.ElementAtOrDefault(5);
|
||||
x.Should().Be(y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,253 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Joins
|
||||
{
|
||||
static int rd;
|
||||
|
||||
static UniTask<T> RandomRun<T>(T value)
|
||||
{
|
||||
if (Interlocked.Increment(ref rd) % 2 == 0)
|
||||
{
|
||||
return UniTask.Run(() => value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return UniTask.FromResult(value);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Join()
|
||||
{
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||
|
||||
var xs = await outer.ToUniTaskAsyncEnumerable().Join(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => (x, y)).ToArrayAsync();
|
||||
var ys = outer.Join(inner, x => x, x => x, (x, y) => (x, y)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task JoinThrow()
|
||||
{
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = outer.ToUniTaskAsyncEnumerable().Join(item, x => x, x => x, (x, y) => x + y).ToArrayAsync();
|
||||
var ys = item.Join(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => x + y).ToArrayAsync();
|
||||
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JoinAwait()
|
||||
{
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||
|
||||
var xs = await outer.ToUniTaskAsyncEnumerable().JoinAwait(inner.ToUniTaskAsyncEnumerable(), x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun((x, y))).ToArrayAsync();
|
||||
var ys = outer.Join(inner, x => x, x => x, (x, y) => (x, y)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task JoinAwaitThrow()
|
||||
{
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = outer.ToUniTaskAsyncEnumerable().JoinAwait(item, x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun(x + y)).ToArrayAsync();
|
||||
var ys = item.Join(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => x + y).ToArrayAsync();
|
||||
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JoinAwaitCt()
|
||||
{
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||
|
||||
var xs = await outer.ToUniTaskAsyncEnumerable().JoinAwaitWithCancellation(inner.ToUniTaskAsyncEnumerable(), (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun((x, y))).ToArrayAsync();
|
||||
var ys = outer.Join(inner, x => x, x => x, (x, y) => (x, y)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task JoinAwaitCtThrow()
|
||||
{
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = outer.ToUniTaskAsyncEnumerable().JoinAwaitWithCancellation(item, (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x + y)).ToArrayAsync();
|
||||
var ys = item.JoinAwaitWithCancellation(inner.ToUniTaskAsyncEnumerable(), (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x + y)).ToArrayAsync();
|
||||
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GroupBy()
|
||||
{
|
||||
var arr = new[] { 1, 4, 10, 10, 4, 5, 10, 9 };
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().GroupBy(x => x).ToArrayAsync();
|
||||
var ys = arr.GroupBy(x => x).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArrayAsync();
|
||||
var ys = arr.GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.OrderBy(x => x.key).SelectMany(x => x.Item2).Should().BeEquivalentTo(ys.OrderBy(x => x.key).SelectMany(x => x.Item2));
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwait(x => RandomRun(x)).ToArrayAsync();
|
||||
var ys = arr.GroupBy(x => x).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwait(x => RandomRun(x), (key, xs) => RandomRun((key, xs.ToArray()))).ToArrayAsync();
|
||||
var ys = arr.GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.OrderBy(x => x.key).SelectMany(x => x.Item2).Should().BeEquivalentTo(ys.OrderBy(x => x.key).SelectMany(x => x.Item2));
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwaitWithCancellation((x, _) => RandomRun(x)).ToArrayAsync();
|
||||
var ys = arr.GroupBy(x => x).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||
}
|
||||
|
||||
{
|
||||
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwaitWithCancellation((x, _) => RandomRun(x), (key, xs, _) => RandomRun((key, xs.ToArray()))).ToArrayAsync();
|
||||
var ys = arr.GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.OrderBy(x => x.key).SelectMany(x => x.Item2).Should().BeEquivalentTo(ys.OrderBy(x => x.key).SelectMany(x => x.Item2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GroupByThrow()
|
||||
{
|
||||
var arr = new[] { 1, 4, 10, 10, 4, 5, 10, 9 };
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.GroupBy(x => x).ToArrayAsync();
|
||||
var ys = item.GroupByAwait(x => RandomRun(x)).ToArrayAsync();
|
||||
var zs = item.GroupByAwaitWithCancellation((x, _) => RandomRun(x)).ToArrayAsync();
|
||||
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GroupJoin()
|
||||
{
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||
|
||||
{
|
||||
var xs = await outer.ToUniTaskAsyncEnumerable().GroupJoin(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArrayAsync();
|
||||
var ys = outer.GroupJoin(inner, x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await outer.ToUniTaskAsyncEnumerable().GroupJoinAwait(inner.ToUniTaskAsyncEnumerable(), x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun((x, string.Join(", ", y)))).ToArrayAsync();
|
||||
var ys = outer.GroupJoin(inner, x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await outer.ToUniTaskAsyncEnumerable().GroupJoinAwaitWithCancellation(inner.ToUniTaskAsyncEnumerable(), (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun((x, string.Join(", ", y)))).ToArrayAsync();
|
||||
var ys = outer.GroupJoin(inner, x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArray();
|
||||
|
||||
xs.Length.Should().Be(ys.Length);
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GroupJoinThrow()
|
||||
{
|
||||
|
||||
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 }.ToUniTaskAsyncEnumerable();
|
||||
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 }.ToUniTaskAsyncEnumerable();
|
||||
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
{
|
||||
var xs = item.GroupJoin(outer, x => x, x => x, (x, y) => x).ToArrayAsync();
|
||||
var ys = inner.GroupJoin(item, x => x, x => x, (x, y) => x).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
}
|
||||
{
|
||||
var xs = item.GroupJoinAwait(outer, x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun(x)).ToArrayAsync();
|
||||
var ys = inner.GroupJoinAwait(item, x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun(x)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
}
|
||||
{
|
||||
var xs = item.GroupJoinAwaitWithCancellation(outer, (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x)).ToArrayAsync();
|
||||
var ys = inner.GroupJoinAwaitWithCancellation(item, (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,285 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Paging
|
||||
{
|
||||
[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 Skip(int collection, int skipCount)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).Skip(skipCount).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).Skip(skipCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SkipException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Skip(5).ToArrayAsync();
|
||||
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]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(0, 1)]
|
||||
[InlineData(9, 0)]
|
||||
[InlineData(9, 1)]
|
||||
[InlineData(9, 5)]
|
||||
[InlineData(9, 9)]
|
||||
[InlineData(9, 15)]
|
||||
public async Task TakeLast(int collection, int takeCount)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeLast(takeCount).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).TakeLast(takeCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TakeLastException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.TakeLast(5).ToArrayAsync();
|
||||
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 Take(int collection, int takeCount)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).Take(takeCount).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).Take(takeCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TakeException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Take(5).ToArrayAsync();
|
||||
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 SkipWhile(int collection, int skipCount)
|
||||
{
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwait(x => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwait((x, i) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SkipWhileException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SkipWhile(x => x < 2).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SkipWhile((x, i) => x < 2).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SkipWhileAwait((x) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SkipWhileAwait((x, i) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SkipWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SkipWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||
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 TakeWhile(int collection, int skipCount)
|
||||
{
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwait(x => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwait((x, i) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TakeWhileException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.TakeWhile(x => x < 5).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.TakeWhile((x, i) => x < 5).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.TakeWhileAwait((x) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.TakeWhileAwait((x, i) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.TakeWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.TakeWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,323 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Projection
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(0, 1)]
|
||||
[InlineData(0, 2)]
|
||||
[InlineData(0, 10)]
|
||||
public async Task Reverse(int start, int count)
|
||||
{
|
||||
var xs = await Enumerable.Range(start, count).ToUniTaskAsyncEnumerable().Reverse().ToArrayAsync();
|
||||
var ys = Enumerable.Range(start, count).Reverse().ToArray();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReverseException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Reverse().ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(1)]
|
||||
[InlineData(9)]
|
||||
public async Task Select(int count)
|
||||
{
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, count).Select(x => x * x).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, count).Select(x => x * x).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
|
||||
var zs = await UniTaskAsyncEnumerable.Range(1, count).SelectAwait((x) => UniTask.Run(() => x * x)).ToArrayAsync();
|
||||
zs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, count).Select((x, i) => x * x * i).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, count).Select((x, i) => x * x * i).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
|
||||
var zs = await UniTaskAsyncEnumerable.Range(1, count).SelectAwait((x, i) => UniTask.Run(() => x * x * i)).ToArrayAsync();
|
||||
zs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task SelectException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Select(x => UniTaskAsyncEnumerable.Range(0, 1)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
|
||||
// await
|
||||
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SelectAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
|
||||
// cancel
|
||||
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.SelectAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
|
||||
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 * x)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany((i, x) => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany(x => UniTaskAsyncEnumerable.Range(99, rightCount * x), (x, y) => x * y).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (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), (x, y) => x * y).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (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 * x))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait((i, x) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (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)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (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 * x))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((i, x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (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)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 9)] // empty + exists
|
||||
[InlineData(9, 0)] // exists + empty
|
||||
[InlineData(9, 9)] // same
|
||||
[InlineData(9, 4)] // leftlong
|
||||
[InlineData(4, 9)] // rightlong
|
||||
public async Task Zip(int leftCount, int rightCount)
|
||||
{
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).Zip(UniTaskAsyncEnumerable.Range(99, rightCount)).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).ZipAwait(UniTaskAsyncEnumerable.Range(99, rightCount), (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).ZipAwaitWithCancellation(UniTaskAsyncEnumerable.Range(99, rightCount), (x, y, _) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||
var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public async Task ZipException()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Zip(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = UniTaskAsyncEnumerable.Range(1, 10).Zip(item).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
|
||||
// a
|
||||
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.ZipAwait(UniTaskAsyncEnumerable.Range(1, 10), (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = UniTaskAsyncEnumerable.Range(1, 10).ZipAwait(item, (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
|
||||
// c
|
||||
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.ZipAwaitWithCancellation(UniTaskAsyncEnumerable.Range(1, 10), (x, y, c) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = UniTaskAsyncEnumerable.Range(1, 10).ZipAwaitWithCancellation(item, (x, y, c) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
// [InlineData(0, 0)]
|
||||
[InlineData(0, 3)]
|
||||
[InlineData(9, 1)]
|
||||
[InlineData(9, 2)]
|
||||
[InlineData(9, 3)]
|
||||
[InlineData(17, 3)]
|
||||
[InlineData(17, 16)]
|
||||
[InlineData(17, 17)]
|
||||
[InlineData(17, 27)]
|
||||
public async Task Buffer(int rangeCount, int bufferCount)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||
var ys = await AsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
// [InlineData(0, 0)]
|
||||
[InlineData(0, 3, 2)]
|
||||
[InlineData(9, 1, 1)]
|
||||
[InlineData(9, 2, 3)]
|
||||
[InlineData(9, 3, 4)]
|
||||
[InlineData(17, 3, 3)]
|
||||
[InlineData(17, 16, 5)]
|
||||
[InlineData(17, 17, 19)]
|
||||
public async Task BufferSkip(int rangeCount, int bufferCount, int skipCount)
|
||||
{
|
||||
var xs = await UniTaskAsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount, skipCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||
var ys = await AsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount, skipCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BufferError()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Buffer(3).ToArrayAsync();
|
||||
var ys = item.Buffer(3, 2).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,193 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class Sets
|
||||
{
|
||||
public static IEnumerable<object[]> array1 = new object[][]
|
||||
{
|
||||
new object[] { new int[] { } }, // empty
|
||||
new object[] { new int[] { 1, 2, 3 } }, // no dup
|
||||
new object[] { new int[] { 1, 2, 3, 3, 4, 5, 2 } }, // dup
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> array2 = new object[][]
|
||||
{
|
||||
new object[] { new int[] { } }, // empty
|
||||
new object[] { new int[] { 1, 2 } },
|
||||
new object[] { new int[] { 1, 2, 4, 5, 9 } }, // dup
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(array1))]
|
||||
public async Task Distinct(int[] array)
|
||||
{
|
||||
var ys = array.Distinct().ToArray();
|
||||
{
|
||||
(await array.ToUniTaskAsyncEnumerable().Distinct().ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
(await array.ToUniTaskAsyncEnumerable().Distinct(x => x).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
(await array.ToUniTaskAsyncEnumerable().DistinctAwait(x => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
(await array.ToUniTaskAsyncEnumerable().DistinctAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DistinctThrow()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
{
|
||||
var xs = item.Distinct().ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.Distinct(x => x).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.DistinctAwait(x => UniTask.Run(() => x)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.DistinctAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(array1))]
|
||||
public async Task DistinctUntilChanged(int[] array)
|
||||
{
|
||||
var ys = await array.ToAsyncEnumerable().DistinctUntilChanged().ToArrayAsync();
|
||||
{
|
||||
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChanged().ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChanged(x => x).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChangedAwait(x => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChangedAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DistinctUntilChangedThrow()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
{
|
||||
var xs = item.DistinctUntilChanged().ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.DistinctUntilChanged(x => x).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.DistinctUntilChangedAwait(x => UniTask.Run(() => x)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
{
|
||||
var xs = item.DistinctUntilChangedAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task Except()
|
||||
{
|
||||
foreach (var a1 in array1.First().Cast<int[]>())
|
||||
{
|
||||
foreach (var a2 in array2.First().Cast<int[]>())
|
||||
{
|
||||
var xs = await a1.ToUniTaskAsyncEnumerable().Except(a2.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||
var ys = a1.Except(a2).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExceptThrow()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Except(UniTaskAsyncEnumerable.Return(10)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = UniTaskAsyncEnumerable.Return(10).Except(item).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Intersect()
|
||||
{
|
||||
foreach (var a1 in array1.First().Cast<int[]>())
|
||||
{
|
||||
foreach (var a2 in array2.First().Cast<int[]>())
|
||||
{
|
||||
var xs = await a1.ToUniTaskAsyncEnumerable().Intersect(a2.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||
var ys = a1.Intersect(a2).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IntersectThrow()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Intersect(UniTaskAsyncEnumerable.Return(10)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = UniTaskAsyncEnumerable.Return(10).Intersect(item).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Union()
|
||||
{
|
||||
foreach (var a1 in array1.First().Cast<int[]>())
|
||||
{
|
||||
foreach (var a2 in array2.First().Cast<int[]>())
|
||||
{
|
||||
var xs = await a1.ToUniTaskAsyncEnumerable().Union(a2.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||
var ys = a1.Union(a2).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UnionThrow()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = item.Union(UniTaskAsyncEnumerable.Return(10)).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
var xs = UniTaskAsyncEnumerable.Return(10).Union(item).ToArrayAsync();
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,221 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class SortCheck
|
||||
{
|
||||
public int Age { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return (Age, FirstName, LastName).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public class Sort
|
||||
{
|
||||
static int rd;
|
||||
|
||||
static UniTask<T> RandomRun<T>(T value)
|
||||
{
|
||||
if (Interlocked.Increment(ref rd) % 2 == 0)
|
||||
{
|
||||
return UniTask.Run(() => value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return UniTask.FromResult(value);
|
||||
}
|
||||
}
|
||||
static UniTask<T> RandomRun<T>(T value, CancellationToken ct)
|
||||
{
|
||||
if (Interlocked.Increment(ref rd) % 2 == 0)
|
||||
{
|
||||
return UniTask.Run(() => value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return UniTask.FromResult(value);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OrderBy()
|
||||
{
|
||||
var array = new[] { 1, 99, 32, 4, 536, 7, 8 };
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x).ToArrayAsync();
|
||||
var ys = array.OrderBy(x => x).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x).ToArrayAsync();
|
||||
var ys = array.OrderByDescending(x => x).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().OrderByAwait(RandomRun).ToArrayAsync();
|
||||
var ys = array.OrderBy(x => x).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(RandomRun).ToArrayAsync();
|
||||
var ys = array.OrderByDescending(x => x).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||
var ys = array.OrderBy(x => x).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
{
|
||||
var xs = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||
var ys = array.OrderByDescending(x => x).ToArray();
|
||||
xs.Should().BeEquivalentTo(ys);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThenBy()
|
||||
{
|
||||
var array = new[]
|
||||
{
|
||||
new SortCheck { Age = 99, FirstName = "ABC", LastName = "DEF" },
|
||||
new SortCheck { Age = 49, FirstName = "ABC", LastName = "DEF" },
|
||||
new SortCheck { Age = 49, FirstName = "ABC", LastName = "ZKH" },
|
||||
new SortCheck { Age = 12, FirstName = "ABC", LastName = "DEF" },
|
||||
new SortCheck { Age = 49, FirstName = "ABC", LastName = "MEF" },
|
||||
new SortCheck { Age = 12, FirstName = "QQQ", LastName = "DEF" },
|
||||
new SortCheck { Age = 19, FirstName = "ZKN", LastName = "DEF" },
|
||||
new SortCheck { Age = 39, FirstName = "APO", LastName = "REF" },
|
||||
new SortCheck { Age = 59, FirstName = "ABC", LastName = "DEF" },
|
||||
new SortCheck { Age = 99, FirstName = "DBC", LastName = "DEF" },
|
||||
new SortCheck { Age = 99, FirstName = "DBC", LastName = "MEF" },
|
||||
};
|
||||
{
|
||||
var a = array.OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||
var b = array.OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||
var c = array.OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||
var d = array.OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||
var e = array.OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||
var f = array.OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||
var g = array.OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||
var h = array.OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||
|
||||
{
|
||||
var a2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||
var b2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||
var c2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||
var d2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||
var e2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||
var f2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||
var g2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||
var h2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||
|
||||
a.Should().BeEquivalentTo(a2);
|
||||
b.Should().BeEquivalentTo(b2);
|
||||
c.Should().BeEquivalentTo(c2);
|
||||
d.Should().BeEquivalentTo(d2);
|
||||
e.Should().BeEquivalentTo(e2);
|
||||
f.Should().BeEquivalentTo(f2);
|
||||
g.Should().BeEquivalentTo(g2);
|
||||
h.Should().BeEquivalentTo(h2);
|
||||
}
|
||||
{
|
||||
var a2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var b2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var c2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var d2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var e2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var f2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var g2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var h2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||
|
||||
a.Should().BeEquivalentTo(a2);
|
||||
b.Should().BeEquivalentTo(b2);
|
||||
c.Should().BeEquivalentTo(c2);
|
||||
d.Should().BeEquivalentTo(d2);
|
||||
e.Should().BeEquivalentTo(e2);
|
||||
f.Should().BeEquivalentTo(f2);
|
||||
g.Should().BeEquivalentTo(g2);
|
||||
h.Should().BeEquivalentTo(h2);
|
||||
}
|
||||
{
|
||||
var a2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var b2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var c2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var d2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var e2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var f2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var g2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
var h2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||
|
||||
a.Should().BeEquivalentTo(a2);
|
||||
b.Should().BeEquivalentTo(b2);
|
||||
c.Should().BeEquivalentTo(c2);
|
||||
d.Should().BeEquivalentTo(d2);
|
||||
e.Should().BeEquivalentTo(e2);
|
||||
f.Should().BeEquivalentTo(f2);
|
||||
g.Should().BeEquivalentTo(g2);
|
||||
h.Should().BeEquivalentTo(h2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task Throws()
|
||||
{
|
||||
foreach (var item in UniTaskTestException.Throws())
|
||||
{
|
||||
{
|
||||
var a = item.OrderBy(x => x).ToArrayAsync();
|
||||
var b = item.OrderByDescending(x => x).ToArrayAsync();
|
||||
var c = item.OrderBy(x => x).ThenBy(x => x).ToArrayAsync();
|
||||
var d = item.OrderBy(x => x).ThenByDescending(x => x).ToArrayAsync();
|
||||
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await a);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await b);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await c);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await d);
|
||||
}
|
||||
{
|
||||
var a = item.OrderByAwait(RandomRun).ToArrayAsync();
|
||||
var b = item.OrderByDescendingAwait(RandomRun).ToArrayAsync();
|
||||
var c = item.OrderByAwait(RandomRun).ThenByAwait(RandomRun).ToArrayAsync();
|
||||
var d = item.OrderByAwait(RandomRun).ThenByDescendingAwait(RandomRun).ToArrayAsync();
|
||||
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await a);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await b);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await c);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await d);
|
||||
}
|
||||
{
|
||||
var a = item.OrderByAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||
var b = item.OrderByDescendingAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||
var c = item.OrderByAwaitWithCancellation(RandomRun).ThenByAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||
var d = item.OrderByAwaitWithCancellation(RandomRun).ThenByDescendingAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await a);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await b);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await c);
|
||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await d);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,126 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class UniTaskTestException : Exception
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<int> ThrowImmediate()
|
||||
{
|
||||
return UniTaskAsyncEnumerable.Throw<int>(new UniTaskTestException());
|
||||
}
|
||||
public static IUniTaskAsyncEnumerable<int> ThrowAfter()
|
||||
{
|
||||
return new ThrowAfter<int>(new UniTaskTestException());
|
||||
}
|
||||
public static IUniTaskAsyncEnumerable<int> ThrowInMoveNext()
|
||||
{
|
||||
return new ThrowIn<int>(new UniTaskTestException());
|
||||
}
|
||||
|
||||
|
||||
public static IEnumerable<IUniTaskAsyncEnumerable<int>> Throws(int count = 3)
|
||||
{
|
||||
yield return ThrowImmediate();
|
||||
yield return ThrowAfter();
|
||||
yield return ThrowInMoveNext();
|
||||
yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowImmediate());
|
||||
yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowAfter());
|
||||
yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowInMoveNext());
|
||||
}
|
||||
}
|
||||
|
||||
internal class ThrowIn<TValue> : IUniTaskAsyncEnumerable<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
|
||||
public ThrowIn(Exception exception)
|
||||
{
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(exception, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Enumerator(Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
this.exception = exception;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TValue Current => default;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(exception).Throw();
|
||||
return new UniTask<bool>(false);
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class ThrowAfter<TValue> : IUniTaskAsyncEnumerable<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
|
||||
public ThrowAfter(Exception exception)
|
||||
{
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(exception, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Enumerator(Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
this.exception = exception;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TValue Current => default;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var tcs = new UniTaskCompletionSource<bool>();
|
||||
|
||||
var awaiter = UniTask.Yield().GetAwaiter();
|
||||
awaiter.UnsafeOnCompleted(() =>
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
tcs.TrySetException(exception);
|
||||
});
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
@ -9,10 +9,20 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="1.0.1" />
|
||||
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
<PackageReference Include="System.Interactive.Async" Version="4.1.1" />
|
||||
<PackageReference Include="System.Linq.Async" Version="4.1.1" />
|
||||
<PackageReference Include="System.Reactive" Version="4.4.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UniTask.NetCore\UniTask.NetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace UniTask.NetCoreTests
|
||||
{
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public interface IUniTaskAsyncEnumerable<out T>
|
||||
{
|
||||
IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public interface IUniTaskAsyncEnumerator<out T> : IUniTaskAsyncDisposable
|
||||
{
|
||||
T Current { get; }
|
||||
UniTask<bool> MoveNextAsync();
|
||||
}
|
||||
|
||||
public interface IUniTaskAsyncDisposable
|
||||
{
|
||||
UniTask DisposeAsync();
|
||||
}
|
||||
|
||||
public interface IUniTaskOrderedAsyncEnumerable<TElement> : IUniTaskAsyncEnumerable<TElement>
|
||||
{
|
||||
IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
|
||||
IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending);
|
||||
IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending);
|
||||
}
|
||||
|
||||
//public interface IUniTaskAsyncGrouping<out TKey, out TElement> : IUniTaskAsyncEnumerable<TElement>
|
||||
//{
|
||||
// TKey Key { get; }
|
||||
//}
|
||||
|
||||
public static class UniTaskAsyncEnumerableExtensions
|
||||
{
|
||||
public static UniTaskCancelableAsyncEnumerable<T> WithCancellation<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||
{
|
||||
return new UniTaskCancelableAsyncEnumerable<T>(source, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public readonly struct UniTaskCancelableAsyncEnumerable<T>
|
||||
{
|
||||
private readonly IUniTaskAsyncEnumerable<T> enumerable;
|
||||
private readonly CancellationToken cancellationToken;
|
||||
|
||||
internal UniTaskCancelableAsyncEnumerable(IUniTaskAsyncEnumerable<T> enumerable, CancellationToken cancellationToken)
|
||||
{
|
||||
this.enumerable = enumerable;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public Enumerator GetAsyncEnumerator()
|
||||
{
|
||||
return new Enumerator(enumerable.GetAsyncEnumerator(cancellationToken));
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public readonly struct Enumerator
|
||||
{
|
||||
private readonly IUniTaskAsyncEnumerator<T> enumerator;
|
||||
|
||||
internal Enumerator(IUniTaskAsyncEnumerator<T> enumerator)
|
||||
{
|
||||
this.enumerator = enumerator;
|
||||
}
|
||||
|
||||
public T Current => enumerator.Current;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
return enumerator.MoveNextAsync();
|
||||
}
|
||||
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b20cf9f02ac585948a4372fa4ee06504
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -20,6 +20,24 @@ namespace Cysharp.Threading.Tasks.Internal
|
|||
throw new ArgumentNullException(paramName);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Exception ArgumentOutOfRange(string paramName)
|
||||
{
|
||||
return new ArgumentOutOfRangeException(paramName);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Exception NoElements()
|
||||
{
|
||||
return new InvalidOperationException("Source sequence doesn't contain any elements.");
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Exception MoreThanOneElement()
|
||||
{
|
||||
return new InvalidOperationException("Source sequence contains more than one element.");
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentException<T>(string message)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4cc94a232b1c1154b8084bdda29c3484
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,318 @@
|
|||
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.InvokeAsync(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.InvokeAsync(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.InvokeAsync(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.InvokeAsync(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.InvokeAsync(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.InvokeAsync(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.InvokeAsync(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.InvokeAsync(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.InvokeAsync(source, seed, accumulator, resultSelector, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class Aggregate
|
||||
{
|
||||
internal static async UniTask<TSource> InvokeAsync<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> InvokeAsync<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> InvokeAsync<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> InvokeAsync<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> InvokeAsync<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> InvokeAsync<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> InvokeAsync<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> InvokeAsync<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> InvokeAsync<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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5dc68c05a4228c643937f6ebd185bcca
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,108 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<Boolean> AllAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return All.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Boolean> AllAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return All.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Boolean> AllAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return All.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class All
|
||||
{
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (!predicate(e.Current))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (!await predicate(e.Current))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (!await predicate(e.Current, cancellationToken))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7271437e0033af2448b600ee248924dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,136 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<Boolean> AnyAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return Any.InvokeAsync(source, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Boolean> AnyAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Any.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Boolean> AnyAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Any.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Boolean> AnyAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Any.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class Any
|
||||
{
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
if (await e.MoveNextAsync())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (predicate(e.Current))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (await predicate(e.Current))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (await predicate(e.Current, cancellationToken))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e2b2e65745263994fbe34f3e0ec8eb12
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,148 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TSource> Append<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource element)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new AppendPrepend<TSource>(source, element, true);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Prepend<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource element)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new AppendPrepend<TSource>(source, element, true);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class AppendPrepend<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly TSource element;
|
||||
readonly bool append; // or prepend
|
||||
|
||||
public AppendPrepend(IUniTaskAsyncEnumerable<TSource> source, TSource element, bool append)
|
||||
{
|
||||
this.source = source;
|
||||
this.element = element;
|
||||
this.append = append;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, element, append, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||
{
|
||||
enum State : byte
|
||||
{
|
||||
None,
|
||||
RequirePrepend,
|
||||
RequireAppend,
|
||||
Completed
|
||||
}
|
||||
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly TSource element;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
State state;
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, TSource element, bool append, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.element = element;
|
||||
this.state = append ? State.RequireAppend : State.RequirePrepend;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (enumerator == null)
|
||||
{
|
||||
if (state == State.RequireAppend)
|
||||
{
|
||||
Current = element;
|
||||
state = State.None;
|
||||
return CompletedTasks.True;
|
||||
}
|
||||
|
||||
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
|
||||
if (state == State.Completed)
|
||||
{
|
||||
return CompletedTasks.False;
|
||||
}
|
||||
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
MoveNextCoreDelegate(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||
}
|
||||
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
static void MoveNextCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
self.Current = self.enumerator.Current;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (self.state == State.RequireAppend)
|
||||
{
|
||||
self.state = State.Completed;
|
||||
self.Current = self.element;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.state = State.Completed;
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3268ec424b8055f45aa2a26d17c80468
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,10 @@
|
|||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TSource> AsUniTaskAsyncEnumerable<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 69866e262589ea643bbc62a1d696077a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,410 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public abstract class MoveNextSource : IUniTaskSource<bool>
|
||||
{
|
||||
protected UniTaskCompletionSourceCore<bool> completionSource;
|
||||
|
||||
public bool GetResult(short token)
|
||||
{
|
||||
return completionSource.GetResult(token);
|
||||
}
|
||||
|
||||
public UniTaskStatus GetStatus(short token)
|
||||
{
|
||||
return completionSource.GetStatus(token);
|
||||
}
|
||||
|
||||
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||
{
|
||||
completionSource.OnCompleted(continuation, state, token);
|
||||
}
|
||||
|
||||
public UniTaskStatus UnsafeGetStatus()
|
||||
{
|
||||
return completionSource.UnsafeGetStatus();
|
||||
}
|
||||
|
||||
void IUniTaskSource.GetResult(short token)
|
||||
{
|
||||
completionSource.GetResult(token);
|
||||
}
|
||||
|
||||
protected bool TryGetResult<T>(UniTask<T>.Awaiter awaiter, out T result)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = awaiter.GetResult();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected bool TryGetResult(UniTask.Awaiter awaiter)
|
||||
{
|
||||
try
|
||||
{
|
||||
awaiter.GetResult();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class AsyncEnumeratorBase<TSource, TResult> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
protected CancellationToken cancellationToken;
|
||||
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter sourceMoveNext;
|
||||
|
||||
public AsyncEnumeratorBase(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
// abstract
|
||||
|
||||
/// <summary>
|
||||
/// If return value is false, continue source.MoveNext.
|
||||
/// </summary>
|
||||
protected abstract bool TryMoveNextCore(bool sourceHasCurrent, out bool result);
|
||||
|
||||
// Util
|
||||
protected TSource SourceCurrent => enumerator.Current;
|
||||
|
||||
// IUniTaskAsyncEnumerator<T>
|
||||
|
||||
public TResult Current { get; protected set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
if (enumerator == null)
|
||||
{
|
||||
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
|
||||
completionSource.Reset();
|
||||
if (!OnFirstIteration())
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
protected virtual bool OnFirstIteration()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void SourceMoveNext()
|
||||
{
|
||||
CONTINUE:
|
||||
sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter();
|
||||
if (sourceMoveNext.IsCompleted)
|
||||
{
|
||||
bool result = false;
|
||||
try
|
||||
{
|
||||
if (!TryMoveNextCore(sourceMoveNext.GetResult(), out result))
|
||||
{
|
||||
goto CONTINUE;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
completionSource.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(result);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this);
|
||||
}
|
||||
}
|
||||
|
||||
static void MoveNextCallBack(object state)
|
||||
{
|
||||
var self = (AsyncEnumeratorBase<TSource, TResult>)state;
|
||||
bool result;
|
||||
try
|
||||
{
|
||||
if (!self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result))
|
||||
{
|
||||
self.SourceMoveNext();
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
self.completionSource.TrySetCanceled(self.cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.completionSource.TrySetResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
// if require additional resource to dispose, override and call base.DisposeAsync.
|
||||
public virtual UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
||||
static readonly Action<object> setCurrentCallbackDelegate = SetCurrentCallBack;
|
||||
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
protected CancellationToken cancellationToken;
|
||||
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter sourceMoveNext;
|
||||
|
||||
UniTask<TAwait>.Awaiter resultAwaiter;
|
||||
|
||||
public AsyncEnumeratorAwaitSelectorBase(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
// abstract
|
||||
|
||||
protected abstract UniTask<TAwait> TransformAsync(TSource sourceCurrent);
|
||||
protected abstract bool TrySetCurrentCore(TAwait awaitResult, out bool terminateIteration);
|
||||
|
||||
// Util
|
||||
protected TSource SourceCurrent { get; private set; }
|
||||
|
||||
protected (bool waitCallback, bool requireNextIteration) ActionCompleted(bool trySetCurrentResult, out bool moveNextResult)
|
||||
{
|
||||
if (trySetCurrentResult)
|
||||
{
|
||||
moveNextResult = true;
|
||||
return (false, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveNextResult = default;
|
||||
return (false, true);
|
||||
}
|
||||
}
|
||||
protected (bool waitCallback, bool requireNextIteration) WaitAwaitCallback(out bool moveNextResult) { moveNextResult = default; return (true, false); }
|
||||
protected (bool waitCallback, bool requireNextIteration) IterateFinished(out bool moveNextResult) { moveNextResult = false; return (false, false); }
|
||||
|
||||
// IUniTaskAsyncEnumerator<T>
|
||||
|
||||
public TResult Current { get; protected set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
if (enumerator == null)
|
||||
{
|
||||
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
|
||||
completionSource.Reset();
|
||||
SourceMoveNext();
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
protected void SourceMoveNext()
|
||||
{
|
||||
CONTINUE:
|
||||
sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter();
|
||||
if (sourceMoveNext.IsCompleted)
|
||||
{
|
||||
bool result = false;
|
||||
try
|
||||
{
|
||||
(bool waitCallback, bool requireNextIteration) = TryMoveNextCore(sourceMoveNext.GetResult(), out result);
|
||||
|
||||
if (waitCallback)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (requireNextIteration)
|
||||
{
|
||||
goto CONTINUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(result);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this);
|
||||
}
|
||||
}
|
||||
|
||||
(bool waitCallback, bool requireNextIteration) TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
SourceCurrent = enumerator.Current;
|
||||
var task = TransformAsync(SourceCurrent);
|
||||
if (UnwarapTask(task, out var taskResult))
|
||||
{
|
||||
var currentResult = TrySetCurrentCore(taskResult, out var terminateIteration);
|
||||
if (terminateIteration)
|
||||
{
|
||||
return IterateFinished(out result);
|
||||
}
|
||||
|
||||
return ActionCompleted(currentResult, out result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return WaitAwaitCallback(out result);
|
||||
}
|
||||
}
|
||||
|
||||
return IterateFinished(out result);
|
||||
}
|
||||
|
||||
protected bool UnwarapTask(UniTask<TAwait> taskResult, out TAwait result)
|
||||
{
|
||||
resultAwaiter = taskResult.GetAwaiter();
|
||||
|
||||
if (resultAwaiter.IsCompleted)
|
||||
{
|
||||
result = resultAwaiter.GetResult();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
resultAwaiter.SourceOnCompleted(setCurrentCallbackDelegate, this);
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void MoveNextCallBack(object state)
|
||||
{
|
||||
var self = (AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait>)state;
|
||||
bool result = false;
|
||||
try
|
||||
{
|
||||
(bool waitCallback, bool requireNextIteration) = self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result);
|
||||
|
||||
if (waitCallback)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (requireNextIteration)
|
||||
{
|
||||
self.SourceMoveNext();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
self.completionSource.TrySetResult(result);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void SetCurrentCallBack(object state)
|
||||
{
|
||||
var self = (AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait>)state;
|
||||
|
||||
bool doneSetCurrent;
|
||||
bool terminateIteration;
|
||||
try
|
||||
{
|
||||
var result = self.resultAwaiter.GetResult();
|
||||
doneSetCurrent = self.TrySetCurrentCore(result, out terminateIteration);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
self.completionSource.TrySetCanceled(self.cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (doneSetCurrent)
|
||||
{
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (terminateIteration)
|
||||
{
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.SourceMoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if require additional resource to dispose, override and call base.DisposeAsync.
|
||||
public virtual UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 01ba1d3b17e13fb4c95740131c7e6e19
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 58499f95012fb3c47bb7bcbc5862e562
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,237 @@
|
|||
<#@ template debug="false" hostspecific="false" language="C#" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#
|
||||
var types = new[]
|
||||
{
|
||||
(typeof(int), "double"),
|
||||
(typeof(long), "double"),
|
||||
(typeof(float),"float"),
|
||||
(typeof(double),"double"),
|
||||
(typeof(decimal),"decimal"),
|
||||
|
||||
(typeof(int?),"double?"),
|
||||
(typeof(long?),"double?"),
|
||||
(typeof(float?),"float?"),
|
||||
(typeof(double?),"double?"),
|
||||
(typeof(decimal?),"decimal?"),
|
||||
};
|
||||
|
||||
Func<Type, bool> IsNullable = x => x.IsGenericType;
|
||||
Func<Type, Type> ElementType = x => IsNullable(x) ? x.GetGenericArguments()[0] : x;
|
||||
Func<Type, string> TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name;
|
||||
Func<Type, string> WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : "";
|
||||
Func<Type, string> CalcResult = x => { var e = ElementType(x); return (e == typeof(int) || e == typeof(long)) ? "(double)sum / count" : (e == typeof(float)) ? "(float)(sum / count)" : "sum / count"; };
|
||||
#>
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
<# foreach(var (t, ret) in types) { #>
|
||||
public static UniTask<<#= ret #>> AverageAsync(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return Average.InvokeAsync(source, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<<#= ret #>> AverageAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Average.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<<#= ret #>> AverageAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Average.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<<#= ret #>> AverageAwaitCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Average.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
<# } #>
|
||||
}
|
||||
|
||||
internal static class Average
|
||||
{
|
||||
<# foreach(var (t, ret) in types) { #>
|
||||
public static async UniTask<<#= ret #>> InvokeAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
<#= TypeName(t) #> sum = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
<# if (IsNullable(t)) { #>
|
||||
var v = e.Current;
|
||||
if (v.HasValue)
|
||||
{
|
||||
checked
|
||||
{
|
||||
sum += v.Value;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
<# } else { #>
|
||||
checked
|
||||
{
|
||||
sum += e.Current;
|
||||
count++;
|
||||
}
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return <#= CalcResult(t) #>;
|
||||
}
|
||||
|
||||
public static async UniTask<<#= ret #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
<#= TypeName(t) #> sum = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
<# if (IsNullable(t)) { #>
|
||||
var v = selector(e.Current);
|
||||
if (v.HasValue)
|
||||
{
|
||||
checked
|
||||
{
|
||||
sum += v.Value;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
<# } else { #>
|
||||
checked
|
||||
{
|
||||
sum += selector(e.Current);
|
||||
count++;
|
||||
}
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return <#= CalcResult(t) #>;
|
||||
}
|
||||
|
||||
public static async UniTask<<#= ret #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
<#= TypeName(t) #> sum = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
<# if (IsNullable(t)) { #>
|
||||
var v = await selector(e.Current);
|
||||
if (v.HasValue)
|
||||
{
|
||||
checked
|
||||
{
|
||||
sum += v.Value;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
<# } else { #>
|
||||
checked
|
||||
{
|
||||
sum += await selector(e.Current);
|
||||
count++;
|
||||
}
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return <#= CalcResult(t) #>;
|
||||
}
|
||||
|
||||
public static async UniTask<<#= ret #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
<#= TypeName(t) #> sum = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
<# if (IsNullable(t)) { #>
|
||||
var v = await selector(e.Current, cancellationToken);
|
||||
if (v.HasValue)
|
||||
{
|
||||
checked
|
||||
{
|
||||
sum += v.Value;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
<# } else { #>
|
||||
checked
|
||||
{
|
||||
sum += await selector(e.Current, cancellationToken);
|
||||
count++;
|
||||
}
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return <#= CalcResult(t) #>;
|
||||
}
|
||||
|
||||
<# } #>
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 84bce45768c171d4490153eb08630a98
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,340 @@
|
|||
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 Enumerator(source, count, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : 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 Enumerator(IUniTaskAsyncEnumerable<TSource> source, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.count = count;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
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 = (Enumerator)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()
|
||||
{
|
||||
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 Enumerator(source, count, skip, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : 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 Enumerator(IUniTaskAsyncEnumerable<TSource> source, int count, int skip, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.count = count;
|
||||
this.skip = skip;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
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 = (Enumerator)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()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 951310243334a3148a7872977cb31c5c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,53 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TResult> Cast<TResult>(this IUniTaskAsyncEnumerable<Object> source)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new Cast<TResult>(source);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Cast<TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<object> source;
|
||||
|
||||
public Cast(IUniTaskAsyncEnumerable<object> source)
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<object, TResult>
|
||||
{
|
||||
public Enumerator(IUniTaskAsyncEnumerable<object> source, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
Current = (TResult)SourceCurrent;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: edebeae8b61352b428abe9ce8f3fc71a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,162 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TSource> Concat<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second)
|
||||
{
|
||||
Error.ThrowArgumentNullException(first, nameof(first));
|
||||
Error.ThrowArgumentNullException(second, nameof(second));
|
||||
|
||||
return new Concat<TSource>(first, second);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Concat<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> first;
|
||||
readonly IUniTaskAsyncEnumerable<TSource> second;
|
||||
|
||||
public Concat(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second)
|
||||
{
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(first, second, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
enum IteratingState
|
||||
{
|
||||
IteratingFirst,
|
||||
IteratingSecond,
|
||||
Complete
|
||||
}
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> first;
|
||||
readonly IUniTaskAsyncEnumerable<TSource> second;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IteratingState iteratingState;
|
||||
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, CancellationToken cancellationToken)
|
||||
{
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
this.cancellationToken = cancellationToken;
|
||||
this.iteratingState = IteratingState.IteratingFirst;
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (iteratingState == IteratingState.Complete) return CompletedTasks.False;
|
||||
|
||||
completionSource.Reset();
|
||||
StartIterate();
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
void StartIterate()
|
||||
{
|
||||
if (enumerator == null)
|
||||
{
|
||||
if (iteratingState == IteratingState.IteratingFirst)
|
||||
{
|
||||
enumerator = first.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
else if (iteratingState == IteratingState.IteratingSecond)
|
||||
{
|
||||
enumerator = second.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
MoveNextCoreDelegate(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||
}
|
||||
}
|
||||
|
||||
static void MoveNextCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
self.Current = self.enumerator.Current;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (self.iteratingState == IteratingState.IteratingFirst)
|
||||
{
|
||||
self.RunSecondAfterDisposeAsync().Forget();
|
||||
return;
|
||||
}
|
||||
|
||||
self.iteratingState = IteratingState.Complete;
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async UniTaskVoid RunSecondAfterDisposeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await enumerator.DisposeAsync();
|
||||
enumerator = null;
|
||||
awaiter = default;
|
||||
iteratingState = IteratingState.IteratingSecond;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
}
|
||||
|
||||
StartIterate();
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7cb9e19c449127a459851a135ce7d527
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,50 @@
|
|||
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<Boolean> ContainsAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return ContainsAsync(source, value, EqualityComparer<TSource>.Default, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Boolean> ContainsAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return Contains.InvokeAsync(source, value, comparer, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class Contains
|
||||
{
|
||||
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (comparer.Equals(value, e.Current))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 36ab06d30f3223048b4f676e05431a7f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,144 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<Int32> CountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return Count.InvokeAsync(source, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Int32> CountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Count.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Int32> CountAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Count.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<Int32> CountAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Count.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class Count
|
||||
{
|
||||
internal static async UniTask<int> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
internal static async UniTask<int> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (predicate(e.Current))
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
internal static async UniTask<int> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (await predicate(e.Current))
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
internal static async UniTask<int> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (await predicate(e.Current, cancellationToken))
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e606d38eed688574bb2ba89d983cc9bb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,140 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new DefaultIfEmpty<TSource>(source, default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource defaultValue)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new DefaultIfEmpty<TSource>(source, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DefaultIfEmpty<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly TSource defaultValue;
|
||||
|
||||
public DefaultIfEmpty(IUniTaskAsyncEnumerable<TSource> source, TSource defaultValue)
|
||||
{
|
||||
this.source = source;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, defaultValue, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||
{
|
||||
enum IteratingState : byte
|
||||
{
|
||||
Empty,
|
||||
Iterating,
|
||||
Completed
|
||||
}
|
||||
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly TSource defaultValue;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IteratingState iteratingState;
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, TSource defaultValue, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.defaultValue = defaultValue;
|
||||
this.cancellationToken = cancellationToken;
|
||||
|
||||
this.iteratingState = IteratingState.Empty;
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (iteratingState == IteratingState.Completed)
|
||||
{
|
||||
return CompletedTasks.False;
|
||||
}
|
||||
|
||||
if (enumerator == null)
|
||||
{
|
||||
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
MoveNextCore(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||
}
|
||||
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
static void MoveNextCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
self.iteratingState = IteratingState.Iterating;
|
||||
self.Current = self.enumerator.Current;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (self.iteratingState == IteratingState.Empty)
|
||||
{
|
||||
self.iteratingState = IteratingState.Completed;
|
||||
|
||||
self.Current = self.defaultValue;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 19e437c039ad7e1478dbce1779ef8660
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,277 @@
|
|||
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<TSource> Distinct<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
|
||||
{
|
||||
return Distinct(source, EqualityComparer<TSource>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Distinct<TSource>(this IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new Distinct<TSource>(source, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
||||
{
|
||||
return Distinct(source, keySelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new Distinct<TSource, TKey>(source, keySelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
|
||||
{
|
||||
return DistinctAwait(source, keySelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new DistinctAwait<TSource, TKey>(source, keySelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
|
||||
{
|
||||
return DistinctAwaitWithCancellation(source, keySelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new DistinctAwaitCancellation<TSource, TKey>(source, keySelector, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Distinct<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly IEqualityComparer<TSource> comparer;
|
||||
|
||||
public Distinct(IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<TSource, TSource>
|
||||
{
|
||||
readonly HashSet<TSource> set;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.set = new HashSet<TSource>(comparer);
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
var v = SourceCurrent;
|
||||
if (set.Add(v))
|
||||
{
|
||||
Current = v;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Distinct<TSource, TKey> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public Distinct(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<TSource, TSource>
|
||||
{
|
||||
readonly HashSet<TKey> set;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.set = new HashSet<TKey>(comparer);
|
||||
this.keySelector = keySelector;
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
var v = SourceCurrent;
|
||||
if (set.Add(keySelector(v)))
|
||||
{
|
||||
Current = v;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DistinctAwait<TSource, TKey> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public DistinctAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, TKey>
|
||||
{
|
||||
readonly HashSet<TKey> set;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.set = new HashSet<TKey>(comparer);
|
||||
this.keySelector = keySelector;
|
||||
}
|
||||
|
||||
protected override UniTask<TKey> TransformAsync(TSource sourceCurrent)
|
||||
{
|
||||
return keySelector(sourceCurrent);
|
||||
}
|
||||
|
||||
protected override bool TrySetCurrentCore(TKey awaitResult, out bool terminateIteration)
|
||||
{
|
||||
if (set.Add(awaitResult))
|
||||
{
|
||||
Current = SourceCurrent;
|
||||
terminateIteration = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
terminateIteration = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DistinctAwaitCancellation<TSource, TKey> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public DistinctAwaitCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, TKey>
|
||||
{
|
||||
readonly HashSet<TKey> set;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.set = new HashSet<TKey>(comparer);
|
||||
this.keySelector = keySelector;
|
||||
}
|
||||
|
||||
protected override UniTask<TKey> TransformAsync(TSource sourceCurrent)
|
||||
{
|
||||
return keySelector(sourceCurrent, cancellationToken);
|
||||
}
|
||||
|
||||
protected override bool TrySetCurrentCore(TKey awaitResult, out bool terminateIteration)
|
||||
{
|
||||
if (set.Add(awaitResult))
|
||||
{
|
||||
Current = SourceCurrent;
|
||||
terminateIteration = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
terminateIteration = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8f09903be66e5d943b243d7c19cb3811
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,297 @@
|
|||
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<TSource> DistinctUntilChanged<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
|
||||
{
|
||||
return DistinctUntilChanged(source, EqualityComparer<TSource>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new DistinctUntilChanged<TSource>(source, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
||||
{
|
||||
return DistinctUntilChanged(source, keySelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new DistinctUntilChanged<TSource, TKey>(source, keySelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctUntilChangedAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
|
||||
{
|
||||
return DistinctUntilChangedAwait(source, keySelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctUntilChangedAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new DistinctUntilChangedAwait<TSource, TKey>(source, keySelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctUntilChangedAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
|
||||
{
|
||||
return DistinctUntilChangedAwaitWithCancellation(source, keySelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> DistinctUntilChangedAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new DistinctUntilChangedAwaitCancellation<TSource, TKey>(source, keySelector, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DistinctUntilChanged<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly IEqualityComparer<TSource> comparer;
|
||||
|
||||
public DistinctUntilChanged(IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<TSource, TSource>
|
||||
{
|
||||
readonly IEqualityComparer<TSource> comparer;
|
||||
TSource prev;
|
||||
bool first;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.comparer = comparer;
|
||||
this.first = true;
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
var v = SourceCurrent;
|
||||
if (first || !comparer.Equals(prev, v))
|
||||
{
|
||||
first = false;
|
||||
Current = prev = v;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DistinctUntilChanged<TSource, TKey> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public DistinctUntilChanged(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<TSource, TSource>
|
||||
{
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
TKey prev;
|
||||
bool first;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.comparer = comparer;
|
||||
this.keySelector = keySelector;
|
||||
this.first = true;
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
var v = SourceCurrent;
|
||||
var key = keySelector(v);
|
||||
if (first || !comparer.Equals(prev, key))
|
||||
{
|
||||
first = false;
|
||||
prev = key;
|
||||
Current = v;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DistinctUntilChangedAwait<TSource, TKey> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public DistinctUntilChangedAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, TKey>
|
||||
{
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
TKey prev;
|
||||
bool first;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.comparer = comparer;
|
||||
this.keySelector = keySelector;
|
||||
this.first = true;
|
||||
}
|
||||
|
||||
protected override UniTask<TKey> TransformAsync(TSource sourceCurrent)
|
||||
{
|
||||
return keySelector(sourceCurrent);
|
||||
}
|
||||
|
||||
protected override bool TrySetCurrentCore(TKey key, out bool terminateIteration)
|
||||
{
|
||||
if (first || !comparer.Equals(prev, key))
|
||||
{
|
||||
first = false;
|
||||
prev = key;
|
||||
Current = SourceCurrent;
|
||||
terminateIteration = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
terminateIteration = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DistinctUntilChangedAwaitCancellation<TSource, TKey> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public DistinctUntilChangedAwaitCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, TKey>
|
||||
{
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
TKey prev;
|
||||
bool first;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
this.comparer = comparer;
|
||||
this.keySelector = keySelector;
|
||||
this.first = true;
|
||||
}
|
||||
|
||||
protected override UniTask<TKey> TransformAsync(TSource sourceCurrent)
|
||||
{
|
||||
return keySelector(sourceCurrent, cancellationToken);
|
||||
}
|
||||
|
||||
protected override bool TrySetCurrentCore(TKey key, out bool terminateIteration)
|
||||
{
|
||||
if (first || !comparer.Equals(prev, key))
|
||||
{
|
||||
first = false;
|
||||
prev = key;
|
||||
Current = SourceCurrent;
|
||||
terminateIteration = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
terminateIteration = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0351f6767df7e644b935d4d599968162
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,256 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TSource> Do<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
return source.Do(onNext, null, null);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Do<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
return source.Do(onNext, onError, null);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Do<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
return source.Do(onNext, null, onCompleted);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Do<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
return new Do<TSource>(source, onNext, onError, onCompleted);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Do<TSource>(this IUniTaskAsyncEnumerable<TSource> source, IObserver<TSource> observer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(observer, nameof(observer));
|
||||
|
||||
return source.Do(observer.OnNext, observer.OnError, observer.OnCompleted); // alloc delegate.
|
||||
}
|
||||
|
||||
// not yet impl.
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Func<Exception, UniTask> onError)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Func<UniTask> onCompleted)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Func<Exception, UniTask> onError, Func<UniTask> onCompleted)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Func<Exception, CancellationToken, UniTask> onError)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Func<CancellationToken, UniTask> onCompleted)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public static IUniTaskAsyncEnumerable<TSource> DoAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Func<Exception, CancellationToken, UniTask> onError, Func<CancellationToken, UniTask> onCompleted)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
}
|
||||
|
||||
internal sealed class Do<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Action<TSource> onNext;
|
||||
readonly Action<Exception> onError;
|
||||
readonly Action onCompleted;
|
||||
|
||||
public Do(IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
|
||||
{
|
||||
this.source = source;
|
||||
this.onNext = onNext;
|
||||
this.onError = onError;
|
||||
this.onCompleted = onCompleted;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, onNext, onError, onCompleted, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Action<TSource> onNext;
|
||||
readonly Action<Exception> onError;
|
||||
readonly Action onCompleted;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.onNext = onNext;
|
||||
this.onError = onError;
|
||||
this.onCompleted = onCompleted;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
bool isCompleted = false;
|
||||
try
|
||||
{
|
||||
if (enumerator == null)
|
||||
{
|
||||
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
isCompleted = awaiter.IsCompleted;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CallTrySetExceptionAfterNotification(ex);
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
if (isCompleted)
|
||||
{
|
||||
MoveNextCore(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||
}
|
||||
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
void CallTrySetExceptionAfterNotification(Exception ex)
|
||||
{
|
||||
if (onError != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
onError(ex);
|
||||
}
|
||||
catch (Exception ex2)
|
||||
{
|
||||
completionSource.TrySetException(ex2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
completionSource.TrySetException(ex);
|
||||
}
|
||||
|
||||
bool TryGetResultWithNotification<T>(UniTask<T>.Awaiter awaiter, out T result)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = awaiter.GetResult();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CallTrySetExceptionAfterNotification(ex);
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void MoveNextCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResultWithNotification(self.awaiter, out var result))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
var v = self.enumerator.Current;
|
||||
|
||||
if (self.onNext != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
self.onNext(v);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.CallTrySetExceptionAfterNotification(ex);
|
||||
}
|
||||
}
|
||||
|
||||
self.Current = v;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (self.onCompleted != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
self.onCompleted();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.CallTrySetExceptionAfterNotification(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dd83c8e12dedf75409b829b93146d130
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,58 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<TSource> ElementAtAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return ElementAt.InvokeAsync(source, index, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> ElementAtOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return ElementAt.InvokeAsync(source, index, cancellationToken, true);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class ElementAt
|
||||
{
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
int i = 0;
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (i++ == index)
|
||||
{
|
||||
return e.Current;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.ArgumentOutOfRange(nameof(index));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c835bd2dd8555234c8919c7b8ef3b69a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,47 @@
|
|||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<T> Empty<T>()
|
||||
{
|
||||
return Cysharp.Threading.Tasks.Linq.Empty<T>.Instance;
|
||||
}
|
||||
}
|
||||
|
||||
internal class Empty<T> : IUniTaskAsyncEnumerable<T>
|
||||
{
|
||||
public static readonly IUniTaskAsyncEnumerable<T> Instance = new Empty<T>();
|
||||
|
||||
Empty()
|
||||
{
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Enumerator.Instance;
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<T>
|
||||
{
|
||||
public static readonly IUniTaskAsyncEnumerator<T> Instance = new Enumerator();
|
||||
|
||||
Enumerator()
|
||||
{
|
||||
}
|
||||
|
||||
public T Current => default;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
return CompletedTasks.False;
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4fa123ad6258abb4184721b719a13810
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,116 @@
|
|||
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<TSource> Except<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second)
|
||||
{
|
||||
Error.ThrowArgumentNullException(first, nameof(first));
|
||||
Error.ThrowArgumentNullException(second, nameof(second));
|
||||
|
||||
return new Except<TSource>(first, second, EqualityComparer<TSource>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Except<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(first, nameof(first));
|
||||
Error.ThrowArgumentNullException(second, nameof(second));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new Except<TSource>(first, second, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Except<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> first;
|
||||
readonly IUniTaskAsyncEnumerable<TSource> second;
|
||||
readonly IEqualityComparer<TSource> comparer;
|
||||
|
||||
public Except(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(first, second, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<TSource, TSource>
|
||||
{
|
||||
static Action<object> HashSetAsyncCoreDelegate = HashSetAsyncCore;
|
||||
|
||||
readonly IEqualityComparer<TSource> comparer;
|
||||
readonly IUniTaskAsyncEnumerable<TSource> second;
|
||||
|
||||
HashSet<TSource> set;
|
||||
UniTask<HashSet<TSource>>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(first, cancellationToken)
|
||||
{
|
||||
this.second = second;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
protected override bool OnFirstIteration()
|
||||
{
|
||||
if (set != null) return false;
|
||||
|
||||
awaiter = second.ToHashSetAsync(cancellationToken).GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
set = awaiter.GetResult();
|
||||
SourceMoveNext();
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(HashSetAsyncCoreDelegate, this);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void HashSetAsyncCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
self.set = result;
|
||||
self.SourceMoveNext();
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
var v = SourceCurrent;
|
||||
if (set.Add(v))
|
||||
{
|
||||
Current = v;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 38c1c4129f59dcb49a5b864eaf4ec63c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,200 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return First.InvokeAsync(source, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return First.InvokeAsync(source, predicate, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> FirstAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return First.InvokeAsync(source, predicate, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> FirstAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return First.InvokeAsync(source, predicate, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return First.InvokeAsync(source, cancellationToken, true);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return First.InvokeAsync(source, predicate, cancellationToken, true);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> FirstOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return First.InvokeAsync(source, predicate, cancellationToken, true);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> FirstOrDefaultAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return First.InvokeAsync(source, predicate, cancellationToken, true);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class First
|
||||
{
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
if (await e.MoveNextAsync())
|
||||
{
|
||||
return e.Current;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var v = e.Current;
|
||||
if (predicate(v))
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var v = e.Current;
|
||||
if (await predicate(v))
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var v = e.Current;
|
||||
if (await predicate(v, cancellationToken))
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 417946e97e9eed84db6f840f57037ca6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,177 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask ForEachAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(action, nameof(action));
|
||||
|
||||
return Cysharp.Threading.Tasks.Linq.ForEach.InvokeAsync(source, action, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask ForEachAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource, Int32> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(action, nameof(action));
|
||||
|
||||
return Cysharp.Threading.Tasks.Linq.ForEach.InvokeAsync(source, action, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask ForEachAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(action, nameof(action));
|
||||
|
||||
return Cysharp.Threading.Tasks.Linq.ForEach.InvokeAwaitAsync(source, action, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask ForEachAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(action, nameof(action));
|
||||
|
||||
return Cysharp.Threading.Tasks.Linq.ForEach.InvokeAwaitAsync(source, action, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask ForEachAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(action, nameof(action));
|
||||
|
||||
return Cysharp.Threading.Tasks.Linq.ForEach.InvokeAwaitWithCancellationAsync(source, action, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask ForEachAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(action, nameof(action));
|
||||
|
||||
return Cysharp.Threading.Tasks.Linq.ForEach.InvokeAwaitWithCancellationAsync(source, action, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class ForEach
|
||||
{
|
||||
public static async UniTask InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
action(e.Current);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Action<TSource, Int32> action, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
int index = 0;
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
action(e.Current, checked(index++));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask InvokeAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> action, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
await action(e.Current);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask InvokeAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask> action, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
int index = 0;
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
await action(e.Current, checked(index++));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask InvokeAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> action, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
await action(e.Current, cancellationToken);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask InvokeAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask> action, CancellationToken cancellationToken)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
int index = 0;
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
await action(e.Current, checked(index++), cancellationToken);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ca8d7f8177ba16140920af405aea3fd4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,911 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
// Ix-Async returns IGrouping but it is competely waste, use standard IGrouping.
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
return new GroupBy<TSource, TKey, TSource>(source, keySelector, x => x, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupBy<TSource, TKey, TSource>(source, keySelector, x => x, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
return new GroupBy<TSource, TKey, TElement>(source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupBy<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
return new GroupBy<TSource, TKey, TSource, TResult>(source, keySelector, x => x, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupBy<TSource, TKey, TSource, TResult>(source, keySelector, x => x, resultSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
return new GroupBy<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupBy<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer);
|
||||
}
|
||||
|
||||
// await
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TSource>> GroupByAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
return new GroupByAwait<TSource, TKey, TSource>(source, keySelector, x => UniTask.FromResult(x), EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TSource>> GroupByAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwait<TSource, TKey, TSource>(source, keySelector, x => UniTask.FromResult(x), comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>> GroupByAwait<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
return new GroupByAwait<TSource, TKey, TElement>(source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>> GroupByAwait<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwait<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwait<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TKey, IEnumerable<TSource>, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
return new GroupByAwait<TSource, TKey, TSource, TResult>(source, keySelector, x => UniTask.FromResult(x), resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwait<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
return new GroupByAwait<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwait<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TKey, IEnumerable<TSource>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwait<TSource, TKey, TSource, TResult>(source, keySelector, x => UniTask.FromResult(x), resultSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwait<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwait<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer);
|
||||
}
|
||||
|
||||
// with ct
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TSource>> GroupByAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TSource>(source, keySelector, (x, _) => UniTask.FromResult(x), EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TSource>> GroupByAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TSource>(source, keySelector, (x, _) => UniTask.FromResult(x), comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>> GroupByAwaitWithCancellation<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TElement>(source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>> GroupByAwaitWithCancellation<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TElement>(source, keySelector, elementSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwaitWithCancellation<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TKey, IEnumerable<TSource>, CancellationToken, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TSource, TResult>(source, keySelector, (x, _) => UniTask.FromResult(x), resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwaitWithCancellation<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwaitWithCancellation<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TKey, IEnumerable<TSource>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TSource, TResult>(source, keySelector, (x, _) => UniTask.FromResult(x), resultSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupByAwaitWithCancellation<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
return new GroupByAwaitWithCancellation<TSource, TKey, TElement, TResult>(source, keySelector, elementSelector, resultSelector, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupBy<TSource, TKey, TElement> : IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
readonly Func<TSource, TElement> elementSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupBy(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<IGrouping<TKey, TElement>> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, elementSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<IGrouping<TKey, TElement>>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
readonly Func<TSource, TElement> elementSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IEnumerator<IGrouping<TKey, TElement>> groupEnumerator;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public IGrouping<TKey, TElement> Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (groupEnumerator == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
var lookup = await source.ToLookupAsync(keySelector, elementSelector, comparer, cancellationToken);
|
||||
groupEnumerator = lookup.GetEnumerator();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (groupEnumerator.MoveNext())
|
||||
{
|
||||
Current = groupEnumerator.Current as IGrouping<TKey, TElement>;
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupBy<TSource, TKey, TElement, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
readonly Func<TSource, TElement> elementSelector;
|
||||
readonly Func<TKey, IEnumerable<TElement>, TResult> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupBy(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, elementSelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, TKey> keySelector;
|
||||
readonly Func<TSource, TElement> elementSelector;
|
||||
readonly Func<TKey, IEnumerable<TElement>, TResult> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IEnumerator<IGrouping<TKey, TElement>> groupEnumerator;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (groupEnumerator == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
var lookup = await source.ToLookupAsync(keySelector, elementSelector, comparer, cancellationToken);
|
||||
groupEnumerator = lookup.GetEnumerator();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (groupEnumerator.MoveNext())
|
||||
{
|
||||
var current = groupEnumerator.Current;
|
||||
Current = resultSelector(current.Key, current);
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupByAwait<TSource, TKey, TElement> : IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, UniTask<TElement>> elementSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupByAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<IGrouping<TKey, TElement>> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, elementSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<IGrouping<TKey, TElement>>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, UniTask<TElement>> elementSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IEnumerator<IGrouping<TKey, TElement>> groupEnumerator;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public IGrouping<TKey, TElement> Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (groupEnumerator == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
var lookup = await source.ToLookupAwaitAsync(keySelector, elementSelector, comparer, cancellationToken);
|
||||
groupEnumerator = lookup.GetEnumerator();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (groupEnumerator.MoveNext())
|
||||
{
|
||||
Current = groupEnumerator.Current as IGrouping<TKey, TElement>;
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupByAwait<TSource, TKey, TElement, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, UniTask<TElement>> elementSelector;
|
||||
readonly Func<TKey, IEnumerable<TElement>, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupByAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, elementSelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
readonly static Action<object> ResultSelectCoreDelegate = ResultSelectCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, UniTask<TElement>> elementSelector;
|
||||
readonly Func<TKey, IEnumerable<TElement>, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IEnumerator<IGrouping<TKey, TElement>> groupEnumerator;
|
||||
UniTask<TResult>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (groupEnumerator == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
var lookup = await source.ToLookupAwaitAsync(keySelector, elementSelector, comparer, cancellationToken);
|
||||
groupEnumerator = lookup.GetEnumerator();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (groupEnumerator.MoveNext())
|
||||
{
|
||||
var current = groupEnumerator.Current;
|
||||
|
||||
awaiter = resultSelector(current.Key, current).GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
ResultSelectCore(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(ResultSelectCoreDelegate, this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void ResultSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
self.Current = result;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupByAwaitWithCancellation<TSource, TKey, TElement> : IUniTaskAsyncEnumerable<IGrouping<TKey, TElement>>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TElement>> elementSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupByAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<IGrouping<TKey, TElement>> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, elementSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<IGrouping<TKey, TElement>>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TElement>> elementSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IEnumerator<IGrouping<TKey, TElement>> groupEnumerator;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public IGrouping<TKey, TElement> Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (groupEnumerator == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
var lookup = await source.ToLookupAwaitWithCancellationAsync(keySelector, elementSelector, comparer, cancellationToken);
|
||||
groupEnumerator = lookup.GetEnumerator();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (groupEnumerator.MoveNext())
|
||||
{
|
||||
Current = groupEnumerator.Current as IGrouping<TKey, TElement>;
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupByAwaitWithCancellation<TSource, TKey, TElement, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TElement>> elementSelector;
|
||||
readonly Func<TKey, IEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupByAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, keySelector, elementSelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
readonly static Action<object> ResultSelectCoreDelegate = ResultSelectCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly Func<TSource, CancellationToken, UniTask<TElement>> elementSelector;
|
||||
readonly Func<TKey, IEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
IEnumerator<IGrouping<TKey, TElement>> groupEnumerator;
|
||||
UniTask<TResult>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, Func<TKey, IEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.keySelector = keySelector;
|
||||
this.elementSelector = elementSelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (groupEnumerator == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
var lookup = await source.ToLookupAwaitWithCancellationAsync(keySelector, elementSelector, comparer, cancellationToken);
|
||||
groupEnumerator = lookup.GetEnumerator();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (groupEnumerator.MoveNext())
|
||||
{
|
||||
var current = groupEnumerator.Current;
|
||||
|
||||
awaiter = resultSelector(current.Key, current, cancellationToken).GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
ResultSelectCore(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(ResultSelectCoreDelegate, this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void ResultSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
self.Current = result;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a2de80df1cc8a1240ab0ee7badd334d0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,606 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
|
||||
return new GroupJoin<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new GroupJoin<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
|
||||
return new GroupJoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new GroupJoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
|
||||
return new GroupJoinWithCancellationAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new GroupJoinWithCancellationAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal sealed class GroupJoin<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, TKey> outerKeySelector;
|
||||
readonly Func<TInner, TKey> innerKeySelector;
|
||||
readonly Func<TOuter, IEnumerable<TInner>, TResult> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupJoin(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, TKey> outerKeySelector;
|
||||
readonly Func<TInner, TKey> innerKeySelector;
|
||||
readonly Func<TOuter, IEnumerable<TInner>, TResult> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
ILookup<TKey, TInner> lookup;
|
||||
IUniTaskAsyncEnumerator<TOuter> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (lookup == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
lookup = await inner.ToLookupAsync(innerKeySelector, comparer, cancellationToken);
|
||||
enumerator = outer.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
MoveNextCore(this);
|
||||
}
|
||||
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)
|
||||
{
|
||||
var outer = self.enumerator.Current;
|
||||
var key = self.outerKeySelector(outer);
|
||||
var values = self.lookup[key];
|
||||
|
||||
self.Current = self.resultSelector(outer, values);
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupJoinAwait<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupJoinAwait(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
readonly static Action<object> ResultSelectCoreDelegate = ResultSelectCore;
|
||||
readonly static Action<object> OuterKeySelectCoreDelegate = OuterKeySelectCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
ILookup<TKey, TInner> lookup;
|
||||
IUniTaskAsyncEnumerator<TOuter> enumerator;
|
||||
TOuter outerValue;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
UniTask<TKey>.Awaiter outerKeyAwaiter;
|
||||
UniTask<TResult>.Awaiter resultAwaiter;
|
||||
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (lookup == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
lookup = await inner.ToLookupAwaitAsync(innerKeySelector, comparer, cancellationToken);
|
||||
enumerator = outer.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
MoveNextCore(this);
|
||||
}
|
||||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
self.outerValue = self.enumerator.Current;
|
||||
self.outerKeyAwaiter = self.outerKeySelector(self.outerValue).GetAwaiter();
|
||||
if (self.outerKeyAwaiter.IsCompleted)
|
||||
{
|
||||
OuterKeySelectCore(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.outerKeyAwaiter.SourceOnCompleted(OuterKeySelectCoreDelegate, self);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.completionSource.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void OuterKeySelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.outerKeyAwaiter, out var result))
|
||||
{
|
||||
try
|
||||
{
|
||||
var values = self.lookup[result];
|
||||
self.resultAwaiter = self.resultSelector(self.outerValue, values).GetAwaiter();
|
||||
if (self.resultAwaiter.IsCompleted)
|
||||
{
|
||||
ResultSelectCore(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, self);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.completionSource.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ResultSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.resultAwaiter, out var result))
|
||||
{
|
||||
self.Current = result;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class GroupJoinWithCancellationAwait<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public GroupJoinWithCancellationAwait(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
readonly static Action<object> ResultSelectCoreDelegate = ResultSelectCore;
|
||||
readonly static Action<object> OuterKeySelectCoreDelegate = OuterKeySelectCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
ILookup<TKey, TInner> lookup;
|
||||
IUniTaskAsyncEnumerator<TOuter> enumerator;
|
||||
TOuter outerValue;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
UniTask<TKey>.Awaiter outerKeyAwaiter;
|
||||
UniTask<TResult>.Awaiter resultAwaiter;
|
||||
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (lookup == null)
|
||||
{
|
||||
CreateLookup().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateLookup()
|
||||
{
|
||||
try
|
||||
{
|
||||
lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, comparer, cancellationToken);
|
||||
enumerator = outer.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
MoveNextCore(this);
|
||||
}
|
||||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
self.outerValue = self.enumerator.Current;
|
||||
self.outerKeyAwaiter = self.outerKeySelector(self.outerValue, self.cancellationToken).GetAwaiter();
|
||||
if (self.outerKeyAwaiter.IsCompleted)
|
||||
{
|
||||
OuterKeySelectCore(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.outerKeyAwaiter.SourceOnCompleted(OuterKeySelectCoreDelegate, self);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.completionSource.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void OuterKeySelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.outerKeyAwaiter, out var result))
|
||||
{
|
||||
try
|
||||
{
|
||||
var values = self.lookup[result];
|
||||
self.resultAwaiter = self.resultSelector(self.outerValue, values, self.cancellationToken).GetAwaiter();
|
||||
if (self.resultAwaiter.IsCompleted)
|
||||
{
|
||||
ResultSelectCore(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, self);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
self.completionSource.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ResultSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.resultAwaiter, out var result))
|
||||
{
|
||||
self.Current = result;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7bf7759d03bf3f64190d3ae83b182c2c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,117 @@
|
|||
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<TSource> Intersect<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second)
|
||||
{
|
||||
Error.ThrowArgumentNullException(first, nameof(first));
|
||||
Error.ThrowArgumentNullException(second, nameof(second));
|
||||
|
||||
return new Intersect<TSource>(first, second, EqualityComparer<TSource>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TSource> Intersect<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(first, nameof(first));
|
||||
Error.ThrowArgumentNullException(second, nameof(second));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new Intersect<TSource>(first, second, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Intersect<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> first;
|
||||
readonly IUniTaskAsyncEnumerable<TSource> second;
|
||||
readonly IEqualityComparer<TSource> comparer;
|
||||
|
||||
public Intersect(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
|
||||
{
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(first, second, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<TSource, TSource>
|
||||
{
|
||||
static Action<object> HashSetAsyncCoreDelegate = HashSetAsyncCore;
|
||||
|
||||
readonly IEqualityComparer<TSource> comparer;
|
||||
readonly IUniTaskAsyncEnumerable<TSource> second;
|
||||
|
||||
HashSet<TSource> set;
|
||||
UniTask<HashSet<TSource>>.Awaiter awaiter;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
|
||||
|
||||
: base(first, cancellationToken)
|
||||
{
|
||||
this.second = second;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
protected override bool OnFirstIteration()
|
||||
{
|
||||
if (set != null) return false;
|
||||
|
||||
awaiter = second.ToHashSetAsync(cancellationToken).GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
set = awaiter.GetResult();
|
||||
SourceMoveNext();
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(HashSetAsyncCoreDelegate, this);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void HashSetAsyncCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
self.set = result;
|
||||
self.SourceMoveNext();
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
var v = SourceCurrent;
|
||||
|
||||
if (set.Remove(v))
|
||||
{
|
||||
Current = v;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 93999a70f5d57134bbe971f3e988c4f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,722 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
|
||||
return new Join<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new Join<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> JoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
|
||||
return new JoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> JoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new JoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
|
||||
return new JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public static IUniTaskAsyncEnumerable<TResult> JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(outer, nameof(outer));
|
||||
Error.ThrowArgumentNullException(inner, nameof(inner));
|
||||
Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
|
||||
Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
|
||||
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Join<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, TKey> outerKeySelector;
|
||||
readonly Func<TInner, TKey> innerKeySelector;
|
||||
readonly Func<TOuter, TInner, TResult> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public Join(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, TKey> outerKeySelector;
|
||||
readonly Func<TInner, TKey> innerKeySelector;
|
||||
readonly Func<TOuter, TInner, TResult> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
ILookup<TKey, TInner> lookup;
|
||||
IUniTaskAsyncEnumerator<TOuter> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
TOuter currentOuterValue;
|
||||
IEnumerator<TInner> valueEnumerator;
|
||||
|
||||
bool continueNext;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (lookup == null)
|
||||
{
|
||||
CreateInnerHashSet().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateInnerHashSet()
|
||||
{
|
||||
try
|
||||
{
|
||||
lookup = await inner.ToLookupAsync(innerKeySelector, comparer, cancellationToken);
|
||||
enumerator = outer.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
LOOP:
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
if (valueEnumerator.MoveNext())
|
||||
{
|
||||
Current = resultSelector(currentOuterValue, valueEnumerator.Current);
|
||||
goto TRY_SET_RESULT_TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
valueEnumerator = null;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
TRY_SET_RESULT_TRUE:
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
|
||||
|
||||
static void MoveNextCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
self.currentOuterValue = self.enumerator.Current;
|
||||
var key = self.outerKeySelector(self.currentOuterValue);
|
||||
self.valueEnumerator = self.lookup[key].GetEnumerator();
|
||||
|
||||
if (self.continueNext)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
self.SourceMoveNext();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
}
|
||||
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class JoinAwait<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, TInner, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public JoinAwait(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
static readonly Action<object> OuterSelectCoreDelegate = OuterSelectCore;
|
||||
static readonly Action<object> ResultSelectCoreDelegate = ResultSelectCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, TInner, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
ILookup<TKey, TInner> lookup;
|
||||
IUniTaskAsyncEnumerator<TOuter> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
TOuter currentOuterValue;
|
||||
IEnumerator<TInner> valueEnumerator;
|
||||
|
||||
UniTask<TResult>.Awaiter resultAwaiter;
|
||||
UniTask<TKey>.Awaiter outerKeyAwaiter;
|
||||
|
||||
bool continueNext;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (lookup == null)
|
||||
{
|
||||
CreateInnerHashSet().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateInnerHashSet()
|
||||
{
|
||||
try
|
||||
{
|
||||
lookup = await inner.ToLookupAwaitAsync(innerKeySelector, comparer, cancellationToken);
|
||||
enumerator = outer.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
LOOP:
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
if (valueEnumerator.MoveNext())
|
||||
{
|
||||
resultAwaiter = resultSelector(currentOuterValue, valueEnumerator.Current).GetAwaiter();
|
||||
if (resultAwaiter.IsCompleted)
|
||||
{
|
||||
ResultSelectCore(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
valueEnumerator = null;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
self.currentOuterValue = self.enumerator.Current;
|
||||
|
||||
self.outerKeyAwaiter = self.outerKeySelector(self.currentOuterValue).GetAwaiter();
|
||||
|
||||
if (self.outerKeyAwaiter.IsCompleted)
|
||||
{
|
||||
OuterSelectCore(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
self.outerKeyAwaiter.SourceOnCompleted(OuterSelectCoreDelegate, self);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void OuterSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.outerKeyAwaiter, out var key))
|
||||
{
|
||||
self.valueEnumerator = self.lookup[key].GetEnumerator();
|
||||
|
||||
if (self.continueNext)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
self.SourceMoveNext();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void ResultSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.resultAwaiter, out var result))
|
||||
{
|
||||
self.Current = result;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
}
|
||||
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public JoinAwaitWithCancellation(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
static readonly Action<object> OuterSelectCoreDelegate = OuterSelectCore;
|
||||
static readonly Action<object> ResultSelectCoreDelegate = ResultSelectCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TOuter> outer;
|
||||
readonly IUniTaskAsyncEnumerable<TInner> inner;
|
||||
readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
|
||||
readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
|
||||
readonly Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector;
|
||||
readonly IEqualityComparer<TKey> comparer;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
ILookup<TKey, TInner> lookup;
|
||||
IUniTaskAsyncEnumerator<TOuter> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
TOuter currentOuterValue;
|
||||
IEnumerator<TInner> valueEnumerator;
|
||||
|
||||
UniTask<TResult>.Awaiter resultAwaiter;
|
||||
UniTask<TKey>.Awaiter outerKeyAwaiter;
|
||||
|
||||
bool continueNext;
|
||||
|
||||
public Enumerator(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||
{
|
||||
this.outer = outer;
|
||||
this.inner = inner;
|
||||
this.outerKeySelector = outerKeySelector;
|
||||
this.innerKeySelector = innerKeySelector;
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
completionSource.Reset();
|
||||
|
||||
if (lookup == null)
|
||||
{
|
||||
CreateInnerHashSet().Forget();
|
||||
}
|
||||
else
|
||||
{
|
||||
SourceMoveNext();
|
||||
}
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateInnerHashSet()
|
||||
{
|
||||
try
|
||||
{
|
||||
lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, comparer, cancellationToken: cancellationToken);
|
||||
enumerator = outer.GetAsyncEnumerator(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
SourceMoveNext();
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
LOOP:
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
if (valueEnumerator.MoveNext())
|
||||
{
|
||||
resultAwaiter = resultSelector(currentOuterValue, valueEnumerator.Current, cancellationToken).GetAwaiter();
|
||||
if (resultAwaiter.IsCompleted)
|
||||
{
|
||||
ResultSelectCore(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
valueEnumerator = null;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
self.currentOuterValue = self.enumerator.Current;
|
||||
|
||||
self.outerKeyAwaiter = self.outerKeySelector(self.currentOuterValue, self.cancellationToken).GetAwaiter();
|
||||
|
||||
if (self.outerKeyAwaiter.IsCompleted)
|
||||
{
|
||||
OuterSelectCore(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
self.outerKeyAwaiter.SourceOnCompleted(OuterSelectCoreDelegate, self);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void OuterSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.outerKeyAwaiter, out var key))
|
||||
{
|
||||
self.valueEnumerator = self.lookup[key].GetEnumerator();
|
||||
|
||||
if (self.continueNext)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
self.SourceMoveNext();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.continueNext = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void ResultSelectCore(object state)
|
||||
{
|
||||
var self = (Enumerator)state;
|
||||
|
||||
if (self.TryGetResult(self.resultAwaiter, out var result))
|
||||
{
|
||||
self.Current = result;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
}
|
||||
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dc4ff8cb6d7c9a64896f2f082124d6b3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,240 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<TSource> LastAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return Last.InvokeAsync(source, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> LastAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Last.InvokeAsync(source, predicate, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> LastAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Last.InvokeAsync(source, predicate, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> LastAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Last.InvokeAsync(source, predicate, cancellationToken, false);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> LastOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return Last.InvokeAsync(source, cancellationToken, true);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> LastOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Last.InvokeAsync(source, predicate, cancellationToken, true);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> LastOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Last.InvokeAsync(source, predicate, cancellationToken, true);
|
||||
}
|
||||
|
||||
public static UniTask<TSource> LastOrDefaultAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return Last.InvokeAsync(source, predicate, cancellationToken, true);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class Last
|
||||
{
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
TSource value = default;
|
||||
if (await e.MoveNextAsync())
|
||||
{
|
||||
value = e.Current;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = e.Current;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
TSource value = default;
|
||||
|
||||
bool found = false;
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var v = e.Current;
|
||||
if (predicate(v))
|
||||
{
|
||||
found = true;
|
||||
value = v;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (found)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
TSource value = default;
|
||||
|
||||
bool found = false;
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var v = e.Current;
|
||||
if (await predicate(v))
|
||||
{
|
||||
found = true;
|
||||
value = v;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (found)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
|
||||
{
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
TSource value = default;
|
||||
|
||||
bool found = false;
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var v = e.Current;
|
||||
if (await predicate(v, cancellationToken))
|
||||
{
|
||||
found = true;
|
||||
value = v;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultIfEmpty)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (found)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Error.NoElements();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a0ccc93be1387fa4a975f06310127c11
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,144 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<long> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return LongCount.InvokeAsync(source, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<long> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return LongCount.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<long> LongCountAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return LongCount.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<long> LongCountAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||
|
||||
return LongCount.InvokeAsync(source, predicate, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class LongCount
|
||||
{
|
||||
internal static async UniTask<long> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
internal static async UniTask<long> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (predicate(e.Current))
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
internal static async UniTask<long> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (await predicate(e.Current))
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
internal static async UniTask<long> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
long count = 0;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
if (await predicate(e.Current, cancellationToken))
|
||||
{
|
||||
checked { count++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 198b39e58ced3ab4f97ccbe0916787d5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,200 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<TSource> MaxAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return Max.InvokeAsync(source, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<TResult> MaxAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Max.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<TResult> MaxAwaitAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Max.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<TResult> MaxAwaitCancellationAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Max.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static partial class Max
|
||||
{
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
TSource value = default;
|
||||
var comparer = Comparer<TSource>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = e.Current;
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = e.Current;
|
||||
if (comparer.Compare(value, x) < 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<TResult> InvokeAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
TResult value = default;
|
||||
var comparer = Comparer<TResult>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = selector(e.Current);
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = selector(e.Current);
|
||||
if (comparer.Compare(value, x) < 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<TResult> InvokeAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
TResult value = default;
|
||||
var comparer = Comparer<TResult>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = await selector(e.Current);
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = await selector(e.Current);
|
||||
if (comparer.Compare(value, x) < 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<TResult> InvokeAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
TResult value = default;
|
||||
var comparer = Comparer<TResult>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = await selector(e.Current, cancellationToken);
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = await selector(e.Current, cancellationToken);
|
||||
if (comparer.Compare(value, x) < 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5c8a118a6b664c441820b8a87d7f6e28
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,200 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static UniTask<TSource> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return Min.InvokeAsync(source, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<TResult> MinAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Min.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<TResult> MinAwaitAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Min.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<TResult> MinAwaitCancellationAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return Min.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal static partial class Min
|
||||
{
|
||||
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
TSource value = default;
|
||||
var comparer = Comparer<TSource>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = e.Current;
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = e.Current;
|
||||
if (comparer.Compare(value, x) > 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<TResult> InvokeAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
TResult value = default;
|
||||
var comparer = Comparer<TResult>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = selector(e.Current);
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = selector(e.Current);
|
||||
if (comparer.Compare(value, x) > 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<TResult> InvokeAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
TResult value = default;
|
||||
var comparer = Comparer<TResult>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = await selector(e.Current);
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = await selector(e.Current);
|
||||
if (comparer.Compare(value, x) > 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<TResult> InvokeAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
TResult value = default;
|
||||
var comparer = Comparer<TResult>.Default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = await selector(e.Current, cancellationToken);
|
||||
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = await selector(e.Current, cancellationToken);
|
||||
if (comparer.Compare(value, x) > 0)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 57ac9da21d3457849a8e45548290a508
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2d6da02d9ab970e4999daf7147d98e36
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,262 @@
|
|||
<#@ template debug="false" hostspecific="false" language="C#" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#
|
||||
var types = new[]
|
||||
{
|
||||
typeof(int),
|
||||
typeof(long),
|
||||
typeof(float),
|
||||
typeof(double),
|
||||
typeof(decimal),
|
||||
|
||||
typeof(int?),
|
||||
typeof(long?),
|
||||
typeof(float?),
|
||||
typeof(double?),
|
||||
typeof(decimal?),
|
||||
};
|
||||
|
||||
Func<Type, bool> IsNullable = x => x.IsGenericType;
|
||||
Func<Type, string> TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name;
|
||||
Func<Type, string> WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : "";
|
||||
#>
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
<# foreach(var (minMax, op) in new[]{("Min",">"), ("Max", "<")}) { #>
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
<# foreach(var t in types) { #>
|
||||
public static UniTask<<#= TypeName(t) #>> <#= minMax #>Async(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return <#= minMax #>.InvokeAsync(source, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<<#= TypeName(t) #>> <#= minMax #>Async<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return <#= minMax #>.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return <#= minMax #>.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
public static UniTask<<#= TypeName(t) #>> <#= minMax #>AwaitCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(source, nameof(selector));
|
||||
|
||||
return <#= minMax #>.InvokeAsync(source, selector, cancellationToken);
|
||||
}
|
||||
|
||||
<# } #>
|
||||
}
|
||||
|
||||
internal static partial class <#= minMax #>
|
||||
{
|
||||
<# foreach(var t in types) { #>
|
||||
public static async UniTask<<#= TypeName(t) #>> InvokeAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken)
|
||||
{
|
||||
<#= TypeName(t) #> value = default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = e.Current;
|
||||
<# if (IsNullable(t)) { #>
|
||||
if(value == null) continue;
|
||||
<# } #>
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
<# if (IsNullable(t)) { #>
|
||||
return default;
|
||||
<# } else { #>
|
||||
throw Error.NoElements();
|
||||
<# } #>
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = e.Current;
|
||||
<# if (IsNullable(t)) { #>
|
||||
if( x == null) continue;
|
||||
<# } #>
|
||||
if (value <#= op #> x)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<<#= TypeName(t) #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
<#= TypeName(t) #> value = default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = selector(e.Current);
|
||||
<# if (IsNullable(t)) { #>
|
||||
if(value == null) continue;
|
||||
<# } #>
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
<# if (IsNullable(t)) { #>
|
||||
return default;
|
||||
<# } else { #>
|
||||
throw Error.NoElements();
|
||||
<# } #>
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = selector(e.Current);
|
||||
<# if (IsNullable(t)) { #>
|
||||
if( x == null) continue;
|
||||
<# } #>
|
||||
if (value <#= op #> x)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<<#= TypeName(t) #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
<#= TypeName(t) #> value = default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = await selector(e.Current);
|
||||
<# if (IsNullable(t)) { #>
|
||||
if(value == null) continue;
|
||||
<# } #>
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
<# if (IsNullable(t)) { #>
|
||||
return default;
|
||||
<# } else { #>
|
||||
throw Error.NoElements();
|
||||
<# } #>
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = await selector(e.Current);
|
||||
<# if (IsNullable(t)) { #>
|
||||
if( x == null) continue;
|
||||
<# } #>
|
||||
if (value <#= op #> x)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async UniTask<<#= TypeName(t) #>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
|
||||
{
|
||||
<#= TypeName(t) #> value = default;
|
||||
|
||||
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||
try
|
||||
{
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
value = await selector(e.Current, cancellationToken);
|
||||
<# if (IsNullable(t)) { #>
|
||||
if(value == null) continue;
|
||||
<# } #>
|
||||
goto NEXT_LOOP;
|
||||
}
|
||||
|
||||
<# if (IsNullable(t)) { #>
|
||||
return default;
|
||||
<# } else { #>
|
||||
throw Error.NoElements();
|
||||
<# } #>
|
||||
NEXT_LOOP:
|
||||
|
||||
while (await e.MoveNextAsync())
|
||||
{
|
||||
var x = await selector(e.Current, cancellationToken);
|
||||
<# if (IsNullable(t)) { #>
|
||||
if( x == null) continue;
|
||||
<# } #>
|
||||
if (value <#= op #> x)
|
||||
{
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
await e.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
<# } #>
|
||||
}
|
||||
|
||||
<# } #>
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 18108e9feb2ec40498df573cfef2ea15
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,56 @@
|
|||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<T> Never<T>()
|
||||
{
|
||||
return Cysharp.Threading.Tasks.Linq.Never<T>.Instance;
|
||||
}
|
||||
}
|
||||
|
||||
internal class Never<T> : IUniTaskAsyncEnumerable<T>
|
||||
{
|
||||
public static readonly IUniTaskAsyncEnumerable<T> Instance = new Never<T>();
|
||||
|
||||
Never()
|
||||
{
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<T>
|
||||
{
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Enumerator(CancellationToken cancellationToken)
|
||||
{
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public T Current => default;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
var tcs = new UniTaskCompletionSource<bool>();
|
||||
|
||||
cancellationToken.Register(state =>
|
||||
{
|
||||
var task = (UniTaskCompletionSource<bool>)state;
|
||||
task.TrySetCanceled(cancellationToken);
|
||||
}, tcs);
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8b307c3d3be71a94da251564bcdefa3d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,61 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TResult> OfType<TResult>(this IUniTaskAsyncEnumerable<Object> source)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new OfType<TResult>(source);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class OfType<TResult> : IUniTaskAsyncEnumerable<TResult>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<object> source;
|
||||
|
||||
public OfType(IUniTaskAsyncEnumerable<object> source)
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(source, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : AsyncEnumeratorBase<object, TResult>
|
||||
{
|
||||
public Enumerator(IUniTaskAsyncEnumerable<object> source, CancellationToken cancellationToken)
|
||||
|
||||
: base(source, cancellationToken)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||
{
|
||||
if (sourceHasCurrent)
|
||||
{
|
||||
if (SourceCurrent is TResult castCurent)
|
||||
{
|
||||
Current = castCurent;
|
||||
result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 111ffe87a7d700442a9ef5af554b252c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,556 @@
|
|||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
#region OrderBy_OrderByDescending
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, Comparer<TKey>.Default, false, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, false, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return new OrderedAsyncEnumerableAwait<TSource, TKey>(source, keySelector, Comparer<TKey>.Default, false, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new OrderedAsyncEnumerableAwait<TSource, TKey>(source, keySelector, comparer, false, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return new OrderedAsyncEnumerableAwaitWithCancellation<TSource, TKey>(source, keySelector, Comparer<TKey>.Default, false, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new OrderedAsyncEnumerableAwaitWithCancellation<TSource, TKey>(source, keySelector, comparer, false, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, Comparer<TKey>.Default, true, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, true, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByDescendingAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return new OrderedAsyncEnumerableAwait<TSource, TKey>(source, keySelector, Comparer<TKey>.Default, true, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByDescendingAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new OrderedAsyncEnumerableAwait<TSource, TKey>(source, keySelector, comparer, true, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return new OrderedAsyncEnumerableAwaitWithCancellation<TSource, TKey>(source, keySelector, Comparer<TKey>.Default, true, null);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return new OrderedAsyncEnumerableAwaitWithCancellation<TSource, TKey>(source, keySelector, comparer, true, null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ThenBy_ThenByDescending
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, Comparer<TKey>.Default, false);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, comparer, false);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByAwait<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, Comparer<TKey>.Default, false);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByAwait<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, comparer, false);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellation<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, Comparer<TKey>.Default, false);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellation<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, comparer, false);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, Comparer<TKey>.Default, true);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, comparer, true);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByDescendingAwait<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, Comparer<TKey>.Default, true);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByDescendingAwait<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, comparer, true);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellation<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, Comparer<TKey>.Default, true);
|
||||
}
|
||||
|
||||
public static IUniTaskOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellation<TSource, TKey>(this IUniTaskOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
|
||||
Error.ThrowArgumentNullException(comparer, nameof(comparer));
|
||||
|
||||
return source.CreateOrderedEnumerable(keySelector, comparer, true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
internal abstract class AsyncEnumerableSorter<TElement>
|
||||
{
|
||||
internal abstract UniTask ComputeKeysAsync(TElement[] elements, int count);
|
||||
|
||||
internal abstract int CompareKeys(int index1, int index2);
|
||||
|
||||
internal async UniTask<int[]> SortAsync(TElement[] elements, int count)
|
||||
{
|
||||
await ComputeKeysAsync(elements, count);
|
||||
|
||||
int[] map = new int[count];
|
||||
for (int i = 0; i < count; i++) map[i] = i;
|
||||
QuickSort(map, 0, count - 1);
|
||||
return map;
|
||||
}
|
||||
|
||||
void QuickSort(int[] map, int left, int right)
|
||||
{
|
||||
do
|
||||
{
|
||||
int i = left;
|
||||
int j = right;
|
||||
int x = map[i + ((j - i) >> 1)];
|
||||
do
|
||||
{
|
||||
while (i < map.Length && CompareKeys(x, map[i]) > 0) i++;
|
||||
while (j >= 0 && CompareKeys(x, map[j]) < 0) j--;
|
||||
if (i > j) break;
|
||||
if (i < j)
|
||||
{
|
||||
int temp = map[i];
|
||||
map[i] = map[j];
|
||||
map[j] = temp;
|
||||
}
|
||||
i++;
|
||||
j--;
|
||||
} while (i <= j);
|
||||
if (j - left <= right - i)
|
||||
{
|
||||
if (left < j) QuickSort(map, left, j);
|
||||
left = i;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i < right) QuickSort(map, i, right);
|
||||
right = j;
|
||||
}
|
||||
} while (left < right);
|
||||
}
|
||||
}
|
||||
|
||||
internal class SyncSelectorAsyncEnumerableSorter<TElement, TKey> : AsyncEnumerableSorter<TElement>
|
||||
{
|
||||
readonly Func<TElement, TKey> keySelector;
|
||||
readonly IComparer<TKey> comparer;
|
||||
readonly bool descending;
|
||||
readonly AsyncEnumerableSorter<TElement> next;
|
||||
TKey[] keys;
|
||||
|
||||
internal SyncSelectorAsyncEnumerableSorter(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending, AsyncEnumerableSorter<TElement> next)
|
||||
{
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
this.descending = descending;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
internal override async UniTask ComputeKeysAsync(TElement[] elements, int count)
|
||||
{
|
||||
keys = new TKey[count];
|
||||
for (int i = 0; i < count; i++) keys[i] = keySelector(elements[i]);
|
||||
if (next != null) await next.ComputeKeysAsync(elements, count);
|
||||
}
|
||||
|
||||
internal override int CompareKeys(int index1, int index2)
|
||||
{
|
||||
int c = comparer.Compare(keys[index1], keys[index2]);
|
||||
if (c == 0)
|
||||
{
|
||||
if (next == null) return index1 - index2;
|
||||
return next.CompareKeys(index1, index2);
|
||||
}
|
||||
return descending ? -c : c;
|
||||
}
|
||||
}
|
||||
|
||||
internal class AsyncSelectorEnumerableSorter<TElement, TKey> : AsyncEnumerableSorter<TElement>
|
||||
{
|
||||
readonly Func<TElement, UniTask<TKey>> keySelector;
|
||||
readonly IComparer<TKey> comparer;
|
||||
readonly bool descending;
|
||||
readonly AsyncEnumerableSorter<TElement> next;
|
||||
TKey[] keys;
|
||||
|
||||
internal AsyncSelectorEnumerableSorter(Func<TElement, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, AsyncEnumerableSorter<TElement> next)
|
||||
{
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
this.descending = descending;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
internal override async UniTask ComputeKeysAsync(TElement[] elements, int count)
|
||||
{
|
||||
keys = new TKey[count];
|
||||
for (int i = 0; i < count; i++) keys[i] = await keySelector(elements[i]);
|
||||
if (next != null) await next.ComputeKeysAsync(elements, count);
|
||||
}
|
||||
|
||||
internal override int CompareKeys(int index1, int index2)
|
||||
{
|
||||
int c = comparer.Compare(keys[index1], keys[index2]);
|
||||
if (c == 0)
|
||||
{
|
||||
if (next == null) return index1 - index2;
|
||||
return next.CompareKeys(index1, index2);
|
||||
}
|
||||
return descending ? -c : c;
|
||||
}
|
||||
}
|
||||
|
||||
internal class AsyncSelectorWithCancellationEnumerableSorter<TElement, TKey> : AsyncEnumerableSorter<TElement>
|
||||
{
|
||||
readonly Func<TElement, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly IComparer<TKey> comparer;
|
||||
readonly bool descending;
|
||||
readonly AsyncEnumerableSorter<TElement> next;
|
||||
CancellationToken cancellationToken;
|
||||
TKey[] keys;
|
||||
|
||||
internal AsyncSelectorWithCancellationEnumerableSorter(Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, AsyncEnumerableSorter<TElement> next, CancellationToken cancellationToken)
|
||||
{
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
this.descending = descending;
|
||||
this.next = next;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
internal override async UniTask ComputeKeysAsync(TElement[] elements, int count)
|
||||
{
|
||||
keys = new TKey[count];
|
||||
for (int i = 0; i < count; i++) keys[i] = await keySelector(elements[i], cancellationToken);
|
||||
if (next != null) await next.ComputeKeysAsync(elements, count);
|
||||
}
|
||||
|
||||
internal override int CompareKeys(int index1, int index2)
|
||||
{
|
||||
int c = comparer.Compare(keys[index1], keys[index2]);
|
||||
if (c == 0)
|
||||
{
|
||||
if (next == null) return index1 - index2;
|
||||
return next.CompareKeys(index1, index2);
|
||||
}
|
||||
return descending ? -c : c;
|
||||
}
|
||||
}
|
||||
|
||||
internal abstract class OrderedAsyncEnumerable<TElement> : IUniTaskOrderedAsyncEnumerable<TElement>
|
||||
{
|
||||
protected readonly IUniTaskAsyncEnumerable<TElement> source;
|
||||
|
||||
public OrderedAsyncEnumerable(IUniTaskAsyncEnumerable<TElement> source)
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending)
|
||||
{
|
||||
return new OrderedAsyncEnumerable<TElement, TKey>(source, keySelector, comparer, descending, this);
|
||||
}
|
||||
|
||||
public IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending)
|
||||
{
|
||||
return new OrderedAsyncEnumerableAwait<TElement, TKey>(source, keySelector, comparer, descending, this);
|
||||
}
|
||||
|
||||
public IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending)
|
||||
{
|
||||
return new OrderedAsyncEnumerableAwaitWithCancellation<TElement, TKey>(source, keySelector, comparer, descending, this);
|
||||
}
|
||||
|
||||
internal abstract AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement> next, CancellationToken cancellationToken);
|
||||
|
||||
public IUniTaskAsyncEnumerator<TElement> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(this, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TElement>
|
||||
{
|
||||
protected readonly OrderedAsyncEnumerable<TElement> parent;
|
||||
CancellationToken cancellationToken;
|
||||
TElement[] buffer;
|
||||
int[] map;
|
||||
int index;
|
||||
|
||||
public Enumerator(OrderedAsyncEnumerable<TElement> parent, CancellationToken cancellationToken)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TElement Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
completionSource.Reset();
|
||||
CreateSortSource().Forget();
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
if (index < buffer.Length)
|
||||
{
|
||||
Current = buffer[map[index++]];
|
||||
return CompletedTasks.True;
|
||||
}
|
||||
else
|
||||
{
|
||||
return CompletedTasks.False;
|
||||
}
|
||||
}
|
||||
|
||||
async UniTaskVoid CreateSortSource()
|
||||
{
|
||||
try
|
||||
{
|
||||
buffer = await parent.source.ToArrayAsync();
|
||||
if (buffer.Length == 0)
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
return;
|
||||
}
|
||||
|
||||
var sorter = parent.GetAsyncEnumerableSorter(null, cancellationToken);
|
||||
map = await sorter.SortAsync(buffer, buffer.Length);
|
||||
sorter = null;
|
||||
|
||||
// set first value
|
||||
Current = buffer[map[index++]];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
return;
|
||||
}
|
||||
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class OrderedAsyncEnumerable<TElement, TKey> : OrderedAsyncEnumerable<TElement>
|
||||
{
|
||||
readonly Func<TElement, TKey> keySelector;
|
||||
readonly IComparer<TKey> comparer;
|
||||
readonly bool descending;
|
||||
readonly OrderedAsyncEnumerable<TElement> parent;
|
||||
|
||||
public OrderedAsyncEnumerable(IUniTaskAsyncEnumerable<TElement> source, Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending, OrderedAsyncEnumerable<TElement> parent)
|
||||
: base(source)
|
||||
{
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
this.descending = descending;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
internal override AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement> next, CancellationToken cancellationToken)
|
||||
{
|
||||
AsyncEnumerableSorter<TElement> sorter = new SyncSelectorAsyncEnumerableSorter<TElement, TKey>(keySelector, comparer, descending, next);
|
||||
if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken);
|
||||
return sorter;
|
||||
}
|
||||
}
|
||||
|
||||
internal class OrderedAsyncEnumerableAwait<TElement, TKey> : OrderedAsyncEnumerable<TElement>
|
||||
{
|
||||
readonly Func<TElement, UniTask<TKey>> keySelector;
|
||||
readonly IComparer<TKey> comparer;
|
||||
readonly bool descending;
|
||||
readonly OrderedAsyncEnumerable<TElement> parent;
|
||||
|
||||
public OrderedAsyncEnumerableAwait(IUniTaskAsyncEnumerable<TElement> source, Func<TElement, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, OrderedAsyncEnumerable<TElement> parent)
|
||||
: base(source)
|
||||
{
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
this.descending = descending;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
internal override AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement> next, CancellationToken cancellationToken)
|
||||
{
|
||||
AsyncEnumerableSorter<TElement> sorter = new AsyncSelectorEnumerableSorter<TElement, TKey>(keySelector, comparer, descending, next);
|
||||
if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken);
|
||||
return sorter;
|
||||
}
|
||||
}
|
||||
|
||||
internal class OrderedAsyncEnumerableAwaitWithCancellation<TElement, TKey> : OrderedAsyncEnumerable<TElement>
|
||||
{
|
||||
readonly Func<TElement, CancellationToken, UniTask<TKey>> keySelector;
|
||||
readonly IComparer<TKey> comparer;
|
||||
readonly bool descending;
|
||||
readonly OrderedAsyncEnumerable<TElement> parent;
|
||||
|
||||
public OrderedAsyncEnumerableAwaitWithCancellation(IUniTaskAsyncEnumerable<TElement> source, Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending, OrderedAsyncEnumerable<TElement> parent)
|
||||
: base(source)
|
||||
{
|
||||
this.keySelector = keySelector;
|
||||
this.comparer = comparer;
|
||||
this.descending = descending;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
internal override AsyncEnumerableSorter<TElement> GetAsyncEnumerableSorter(AsyncEnumerableSorter<TElement> next, CancellationToken cancellationToken)
|
||||
{
|
||||
AsyncEnumerableSorter<TElement> sorter = new AsyncSelectorWithCancellationEnumerableSorter<TElement, TKey>(keySelector, comparer, descending, next, cancellationToken);
|
||||
if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken);
|
||||
return sorter;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 413883ceff8546143bdf200aafa4b8f7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,75 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<int> Range(int start, int count)
|
||||
{
|
||||
if (count < 0) throw Error.ArgumentOutOfRange(nameof(count));
|
||||
|
||||
var end = (long)start + count - 1L;
|
||||
if (end > int.MaxValue) throw Error.ArgumentOutOfRange(nameof(count));
|
||||
|
||||
if (count == 0) UniTaskAsyncEnumerable.Empty<int>();
|
||||
|
||||
return new Cysharp.Threading.Tasks.Linq.Range(start, count);
|
||||
}
|
||||
}
|
||||
|
||||
internal class Range : IUniTaskAsyncEnumerable<int>
|
||||
{
|
||||
readonly int start;
|
||||
readonly int end;
|
||||
|
||||
public Range(int start, int count)
|
||||
{
|
||||
this.start = start;
|
||||
this.end = start + count;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<int> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(start, end, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<int>
|
||||
{
|
||||
readonly int start;
|
||||
readonly int end;
|
||||
int current;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Enumerator(int start, int end, CancellationToken cancellationToken)
|
||||
{
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.cancellationToken = cancellationToken;
|
||||
|
||||
this.current = start - 1;
|
||||
}
|
||||
|
||||
public int Current => current;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
current++;
|
||||
|
||||
if (current != end)
|
||||
{
|
||||
return CompletedTasks.True;
|
||||
}
|
||||
|
||||
return CompletedTasks.False;
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d826418a813498648b10542d0a5fb173
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,68 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TElement> Repeat<TElement>(TElement element, int count)
|
||||
{
|
||||
if (count < 0) throw Error.ArgumentOutOfRange(nameof(count));
|
||||
|
||||
return new Repeat<TElement>(element, count);
|
||||
}
|
||||
}
|
||||
|
||||
internal class Repeat<TElement> : IUniTaskAsyncEnumerable<TElement>
|
||||
{
|
||||
readonly TElement element;
|
||||
readonly int count;
|
||||
|
||||
public Repeat(TElement element, int count)
|
||||
{
|
||||
this.element = element;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TElement> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(element, count, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<TElement>
|
||||
{
|
||||
readonly TElement element;
|
||||
readonly int count;
|
||||
int remaining;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Enumerator(TElement element, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
this.element = element;
|
||||
this.count = count;
|
||||
this.cancellationToken = cancellationToken;
|
||||
|
||||
this.remaining = count;
|
||||
}
|
||||
|
||||
public TElement Current => element;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (remaining-- != 0)
|
||||
{
|
||||
return CompletedTasks.True;
|
||||
}
|
||||
|
||||
return CompletedTasks.False;
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3819a3925165a674d80ee848c8600379
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,63 @@
|
|||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TValue> Return<TValue>(TValue value)
|
||||
{
|
||||
return new Return<TValue>(value);
|
||||
}
|
||||
}
|
||||
|
||||
internal class Return<TValue> : IUniTaskAsyncEnumerable<TValue>
|
||||
{
|
||||
readonly TValue value;
|
||||
|
||||
public Return(TValue value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(value, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<TValue>
|
||||
{
|
||||
readonly TValue value;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
bool called;
|
||||
|
||||
public Enumerator(TValue value, CancellationToken cancellationToken)
|
||||
{
|
||||
this.value = value;
|
||||
this.cancellationToken = cancellationToken;
|
||||
this.called = false;
|
||||
}
|
||||
|
||||
public TValue Current => value;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (!called)
|
||||
{
|
||||
called = true;
|
||||
return CompletedTasks.True;
|
||||
}
|
||||
|
||||
return CompletedTasks.False;
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue