master
neuecc 2020-05-11 23:17:33 +09:00
parent 12c507574e
commit 418586fbfb
7 changed files with 915 additions and 894 deletions

View File

@ -19,6 +19,11 @@ namespace Cysharp.Threading.Tasks
UniTask DisposeAsync(); UniTask DisposeAsync();
} }
//public interface IUniTaskAsyncGrouping<out TKey, out TElement> : IUniTaskAsyncEnumerable<TElement>
//{
// TKey Key { get; }
//}
public static class UniTaskAsyncEnumerableExtensions public static class UniTaskAsyncEnumerableExtensions
{ {
public static UniTaskCancelableAsyncEnumerable<T> WithCancellation<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken) public static UniTaskCancelableAsyncEnumerable<T> WithCancellation<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)

File diff suppressed because it is too large Load Diff

View File

@ -154,7 +154,7 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
try try
{ {
lookup = await inner.ToLookupAsync(innerKeySelector, cancellationToken); lookup = await inner.ToLookupAsync(innerKeySelector, comparer, cancellationToken);
enumerator = outer.GetAsyncEnumerator(cancellationToken); enumerator = outer.GetAsyncEnumerator(cancellationToken);
} }
catch (Exception ex) catch (Exception ex)
@ -179,6 +179,7 @@ namespace Cysharp.Threading.Tasks.Linq
} }
else else
{ {
valueEnumerator.Dispose();
valueEnumerator = null; valueEnumerator = null;
} }
} }
@ -342,7 +343,7 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
try try
{ {
lookup = await inner.ToLookupAwaitAsync(innerKeySelector, cancellationToken); lookup = await inner.ToLookupAwaitAsync(innerKeySelector, comparer, cancellationToken);
enumerator = outer.GetAsyncEnumerator(cancellationToken); enumerator = outer.GetAsyncEnumerator(cancellationToken);
} }
catch (Exception ex) catch (Exception ex)
@ -375,6 +376,7 @@ namespace Cysharp.Threading.Tasks.Linq
} }
else else
{ {
valueEnumerator.Dispose();
valueEnumerator = null; valueEnumerator = null;
} }
} }
@ -568,7 +570,7 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
try try
{ {
lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, cancellationToken: cancellationToken); lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, comparer, cancellationToken: cancellationToken);
enumerator = outer.GetAsyncEnumerator(cancellationToken); enumerator = outer.GetAsyncEnumerator(cancellationToken);
} }
catch (Exception ex) catch (Exception ex)
@ -601,6 +603,7 @@ namespace Cysharp.Threading.Tasks.Linq
} }
else else
{ {
valueEnumerator.Dispose();
valueEnumerator = null; valueEnumerator = null;
} }
} }

View File

@ -514,7 +514,7 @@ namespace Cysharp.Threading.Tasks.Linq
} }
} }
class Grouping<TKey, TElement> : IGrouping<TKey, TElement> class Grouping<TKey, TElement> : IGrouping<TKey, TElement> // , IUniTaskAsyncGrouping<TKey, TElement>
{ {
readonly List<TElement> elements; readonly List<TElement> elements;
@ -539,6 +539,11 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
return elements.GetEnumerator(); return elements.GetEnumerator();
} }
public IUniTaskAsyncEnumerator<TElement> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return this.ToUniTaskAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
}
} }
} }
} }

View File

@ -30,125 +30,6 @@ namespace ___Dummy
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IUniTaskAsyncEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IUniTaskAsyncEnumerable<TSource>, TResult> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IUniTaskAsyncEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IUniTaskAsyncEnumerable<TElement>, TResult> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwait<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupByAwait<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupByAwait<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TKey, IUniTaskAsyncEnumerable<TSource>, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
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, IUniTaskAsyncEnumerable<TElement>, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupByAwait<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupByAwait<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TKey, IUniTaskAsyncEnumerable<TSource>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
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, IUniTaskAsyncEnumerable<TElement>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupByAwaitWithCancellation<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupByAwaitWithCancellation<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupByAwaitWithCancellation<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TKey, IUniTaskAsyncEnumerable<TSource>, CancellationToken, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
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, IUniTaskAsyncEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<IAsyncGrouping<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)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupByAwaitWithCancellation<TSource, TKey, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TKey, IUniTaskAsyncEnumerable<TSource>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
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, IUniTaskAsyncEnumerable<TElement>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
{
throw new NotImplementedException();
}
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, IUniTaskAsyncEnumerable<TInner>, TResult> resultSelector) 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, IUniTaskAsyncEnumerable<TInner>, TResult> resultSelector)
{ {

View File

@ -52,6 +52,8 @@ namespace NetCoreSandbox
} }
// AsyncEnumerable.Range(1, 10).GroupBy(x=>x).Select(x=>x.first
//Enumerable.Range(1,10).ToHashSet( //Enumerable.Range(1,10).ToHashSet(

View File

@ -112,5 +112,78 @@ namespace NetCoreTests.Linq
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys); 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);
}
}
} }
} }