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 OrderBy(this IUniTaskAsyncEnumerable source, Func keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return new OrderedAsyncEnumerable(source, keySelector, Comparer.Default, false, null); } public static IUniTaskOrderedAsyncEnumerable OrderBy(this IUniTaskAsyncEnumerable source, Func keySelector, IComparer comparer) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); Error.ThrowArgumentNullException(comparer, nameof(comparer)); return new OrderedAsyncEnumerable(source, keySelector, comparer, false, null); } public static IUniTaskOrderedAsyncEnumerable OrderByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return new OrderedAsyncEnumerableAwait(source, keySelector, Comparer.Default, false, null); } public static IUniTaskOrderedAsyncEnumerable OrderByAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); Error.ThrowArgumentNullException(comparer, nameof(comparer)); return new OrderedAsyncEnumerableAwait(source, keySelector, comparer, false, null); } public static IUniTaskOrderedAsyncEnumerable OrderByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, Comparer.Default, false, null); } public static IUniTaskOrderedAsyncEnumerable OrderByAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); Error.ThrowArgumentNullException(comparer, nameof(comparer)); return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, comparer, false, null); } public static IUniTaskOrderedAsyncEnumerable OrderByDescending(this IUniTaskAsyncEnumerable source, Func keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return new OrderedAsyncEnumerable(source, keySelector, Comparer.Default, true, null); } public static IUniTaskOrderedAsyncEnumerable OrderByDescending(this IUniTaskAsyncEnumerable source, Func keySelector, IComparer comparer) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); Error.ThrowArgumentNullException(comparer, nameof(comparer)); return new OrderedAsyncEnumerable(source, keySelector, comparer, true, null); } public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwait(this IUniTaskAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return new OrderedAsyncEnumerableAwait(source, keySelector, Comparer.Default, true, null); } public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwait(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); Error.ThrowArgumentNullException(comparer, nameof(comparer)); return new OrderedAsyncEnumerableAwait(source, keySelector, comparer, true, null); } public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, Comparer.Default, true, null); } public static IUniTaskOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellation(this IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); Error.ThrowArgumentNullException(comparer, nameof(comparer)); return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, comparer, true, null); } #endregion #region ThenBy_ThenByDescending public static IUniTaskOrderedAsyncEnumerable ThenBy(this IUniTaskOrderedAsyncEnumerable source, Func keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return source.CreateOrderedEnumerable(keySelector, Comparer.Default, false); } public static IUniTaskOrderedAsyncEnumerable ThenBy(this IUniTaskOrderedAsyncEnumerable source, Func keySelector, IComparer 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 ThenByAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return source.CreateOrderedEnumerable(keySelector, Comparer.Default, false); } public static IUniTaskOrderedAsyncEnumerable ThenByAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer 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 ThenByAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return source.CreateOrderedEnumerable(keySelector, Comparer.Default, false); } public static IUniTaskOrderedAsyncEnumerable ThenByAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer 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 ThenByDescending(this IUniTaskOrderedAsyncEnumerable source, Func keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return source.CreateOrderedEnumerable(keySelector, Comparer.Default, true); } public static IUniTaskOrderedAsyncEnumerable ThenByDescending(this IUniTaskOrderedAsyncEnumerable source, Func keySelector, IComparer 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 ThenByDescendingAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return source.CreateOrderedEnumerable(keySelector, Comparer.Default, true); } public static IUniTaskOrderedAsyncEnumerable ThenByDescendingAwait(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer 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 ThenByDescendingAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(keySelector, nameof(keySelector)); return source.CreateOrderedEnumerable(keySelector, Comparer.Default, true); } public static IUniTaskOrderedAsyncEnumerable ThenByDescendingAwaitWithCancellation(this IUniTaskOrderedAsyncEnumerable source, Func> keySelector, IComparer 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 { internal abstract UniTask ComputeKeysAsync(TElement[] elements, int count); internal abstract int CompareKeys(int index1, int index2); internal async UniTask 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 : AsyncEnumerableSorter { readonly Func keySelector; readonly IComparer comparer; readonly bool descending; readonly AsyncEnumerableSorter next; TKey[] keys; internal SyncSelectorAsyncEnumerableSorter(Func keySelector, IComparer comparer, bool descending, AsyncEnumerableSorter 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 : AsyncEnumerableSorter { readonly Func> keySelector; readonly IComparer comparer; readonly bool descending; readonly AsyncEnumerableSorter next; TKey[] keys; internal AsyncSelectorEnumerableSorter(Func> keySelector, IComparer comparer, bool descending, AsyncEnumerableSorter 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 : AsyncEnumerableSorter { readonly Func> keySelector; readonly IComparer comparer; readonly bool descending; readonly AsyncEnumerableSorter next; CancellationToken cancellationToken; TKey[] keys; internal AsyncSelectorWithCancellationEnumerableSorter(Func> keySelector, IComparer comparer, bool descending, AsyncEnumerableSorter 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 : IUniTaskOrderedAsyncEnumerable { protected readonly IUniTaskAsyncEnumerable source; public OrderedAsyncEnumerable(IUniTaskAsyncEnumerable source) { this.source = source; } public IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func keySelector, IComparer comparer, bool descending) { return new OrderedAsyncEnumerable(source, keySelector, comparer, descending, this); } public IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func> keySelector, IComparer comparer, bool descending) { return new OrderedAsyncEnumerableAwait(source, keySelector, comparer, descending, this); } public IUniTaskOrderedAsyncEnumerable CreateOrderedEnumerable(Func> keySelector, IComparer comparer, bool descending) { return new OrderedAsyncEnumerableAwaitWithCancellation(source, keySelector, comparer, descending, this); } internal abstract AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken); public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { return new _OrderedAsyncEnumerator(this, cancellationToken); } class _OrderedAsyncEnumerator : MoveNextSource, IUniTaskAsyncEnumerator { protected readonly OrderedAsyncEnumerable parent; CancellationToken cancellationToken; TElement[] buffer; int[] map; int index; public _OrderedAsyncEnumerator(OrderedAsyncEnumerable parent, CancellationToken cancellationToken) { this.parent = parent; this.cancellationToken = cancellationToken; TaskTracker.TrackActiveTask(this, 3); } public TElement Current { get; private set; } public UniTask MoveNextAsync() { cancellationToken.ThrowIfCancellationRequested(); if (map == null) { completionSource.Reset(); CreateSortSource().Forget(); return new UniTask(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() { TaskTracker.RemoveTracking(this); return default; } } } internal class OrderedAsyncEnumerable : OrderedAsyncEnumerable { readonly Func keySelector; readonly IComparer comparer; readonly bool descending; readonly OrderedAsyncEnumerable parent; public OrderedAsyncEnumerable(IUniTaskAsyncEnumerable source, Func keySelector, IComparer comparer, bool descending, OrderedAsyncEnumerable parent) : base(source) { this.keySelector = keySelector; this.comparer = comparer; this.descending = descending; this.parent = parent; } internal override AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken) { AsyncEnumerableSorter sorter = new SyncSelectorAsyncEnumerableSorter(keySelector, comparer, descending, next); if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken); return sorter; } } internal class OrderedAsyncEnumerableAwait : OrderedAsyncEnumerable { readonly Func> keySelector; readonly IComparer comparer; readonly bool descending; readonly OrderedAsyncEnumerable parent; public OrderedAsyncEnumerableAwait(IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer, bool descending, OrderedAsyncEnumerable parent) : base(source) { this.keySelector = keySelector; this.comparer = comparer; this.descending = descending; this.parent = parent; } internal override AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken) { AsyncEnumerableSorter sorter = new AsyncSelectorEnumerableSorter(keySelector, comparer, descending, next); if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken); return sorter; } } internal class OrderedAsyncEnumerableAwaitWithCancellation : OrderedAsyncEnumerable { readonly Func> keySelector; readonly IComparer comparer; readonly bool descending; readonly OrderedAsyncEnumerable parent; public OrderedAsyncEnumerableAwaitWithCancellation(IUniTaskAsyncEnumerable source, Func> keySelector, IComparer comparer, bool descending, OrderedAsyncEnumerable parent) : base(source) { this.keySelector = keySelector; this.comparer = comparer; this.descending = descending; this.parent = parent; } internal override AsyncEnumerableSorter GetAsyncEnumerableSorter(AsyncEnumerableSorter next, CancellationToken cancellationToken) { AsyncEnumerableSorter sorter = new AsyncSelectorWithCancellationEnumerableSorter(keySelector, comparer, descending, next, cancellationToken); if (parent != null) sorter = parent.GetAsyncEnumerableSorter(sorter, cancellationToken); return sorter; } } }