123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- public static IAsyncEnumerable<TSource> Append<TSource>(this IAsyncEnumerable<TSource> source, TSource element)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (source is AppendPrependAsyncIterator<TSource> appendable)
- {
- return appendable.Append(element);
- }
- return new AppendPrepend1AsyncIterator<TSource>(source, element, appending: true);
- }
- public static IAsyncEnumerable<TSource> Prepend<TSource>(this IAsyncEnumerable<TSource> source, TSource element)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (source is AppendPrependAsyncIterator<TSource> appendable)
- {
- return appendable.Prepend(element);
- }
- return new AppendPrepend1AsyncIterator<TSource>(source, element, appending: false);
- }
- private abstract class AppendPrependAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
- {
- protected readonly IAsyncEnumerable<TSource> source;
- protected IAsyncEnumerator<TSource> enumerator;
- protected AppendPrependAsyncIterator(IAsyncEnumerable<TSource> source)
- {
- Debug.Assert(source != null);
- this.source = source;
- }
- protected void GetSourceEnumerator()
- {
- Debug.Assert(enumerator == null);
- enumerator = source.GetAsyncEnumerator();
- }
- public abstract AppendPrependAsyncIterator<TSource> Append(TSource item);
- public abstract AppendPrependAsyncIterator<TSource> Prepend(TSource item);
- protected async Task<bool> LoadFromEnumeratorAsync()
- {
- if (await enumerator.MoveNextAsync().ConfigureAwait(false))
- {
- current = enumerator.Current;
- return true;
- }
- if (enumerator != null)
- {
- await enumerator.DisposeAsync().ConfigureAwait(false);
- enumerator = null;
- }
- return false;
- }
- public override async Task DisposeAsync()
- {
- if (enumerator != null)
- {
- await enumerator.DisposeAsync().ConfigureAwait(false);
- enumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- public abstract Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken);
- public abstract Task<List<TSource>> ToListAsync(CancellationToken cancellationToken);
- public abstract Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken);
- }
- private sealed class AppendPrepend1AsyncIterator<TSource> : AppendPrependAsyncIterator<TSource>
- {
- private readonly TSource item;
- private readonly bool appending;
- bool hasEnumerator;
- public AppendPrepend1AsyncIterator(IAsyncEnumerable<TSource> source, TSource item, bool appending)
- : base(source)
- {
- this.item = item;
- this.appending = appending;
- }
- public override AsyncIterator<TSource> Clone()
- {
- return new AppendPrepend1AsyncIterator<TSource>(source, item, appending);
- }
- protected override async Task<bool> MoveNextCore()
- {
- switch (state)
- {
- case AsyncIteratorState.Allocated:
- hasEnumerator = false;
- state = AsyncIteratorState.Iterating;
- if (!appending)
- {
- current = item;
- return true;
- }
- goto case AsyncIteratorState.Iterating;
- case AsyncIteratorState.Iterating:
- if (!hasEnumerator)
- {
- GetSourceEnumerator();
- hasEnumerator = true;
- }
- if (enumerator != null)
- {
- if (await LoadFromEnumeratorAsync()
- .ConfigureAwait(false))
- {
- return true;
- }
- if (appending)
- {
- current = item;
- return true;
- }
- }
- break;
- }
- await DisposeAsync().ConfigureAwait(false);
- return false;
- }
- public override AppendPrependAsyncIterator<TSource> Append(TSource element)
- {
- if (appending)
- {
- return new AppendPrependNAsyncIterator<TSource>(source, null, new SingleLinkedNode<TSource>(item).Add(element), prependCount: 0, appendCount: 2);
- }
- else
- {
- return new AppendPrependNAsyncIterator<TSource>(source, new SingleLinkedNode<TSource>(item), new SingleLinkedNode<TSource>(element), prependCount: 1, appendCount: 1);
- }
- }
- public override AppendPrependAsyncIterator<TSource> Prepend(TSource element)
- {
- if (appending)
- {
- return new AppendPrependNAsyncIterator<TSource>(source, new SingleLinkedNode<TSource>(element), new SingleLinkedNode<TSource>(item), prependCount: 1, appendCount: 1);
- }
- else
- {
- return new AppendPrependNAsyncIterator<TSource>(source, new SingleLinkedNode<TSource>(item).Add(element), null, prependCount: 2, appendCount: 0);
- }
- }
- public override async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
- if (count == -1)
- {
- return await AsyncEnumerableHelpers.ToArray(this, cancellationToken).ConfigureAwait(false);
- }
- var array = new TSource[count];
- int index;
- if (appending)
- {
- index = 0;
- }
- else
- {
- array[0] = item;
- index = 1;
- }
- if (source is ICollection<TSource> sourceCollection)
- {
- sourceCollection.CopyTo(array, index);
- }
- else
- {
- var en = source.GetAsyncEnumerator();
- try
- {
- while (await en.MoveNextAsync(cancellationToken).ConfigureAwait(false))
- {
- array[index] = en.Current;
- ++index;
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- }
- if (appending)
- {
- array[array.Length - 1] = item;
- }
- return array;
- }
- public override async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
- {
- var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
- var list = count == -1 ? new List<TSource>() : new List<TSource>(count);
- if (!appending)
- {
- list.Add(item);
- }
- var en = source.GetAsyncEnumerator();
- try
- {
- while (await en.MoveNextAsync(cancellationToken).ConfigureAwait(false))
- {
- list.Add(en.Current);
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- if (appending)
- {
- list.Add(item);
- }
- return list;
- }
- public override async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- if (source is IAsyncIListProvider<TSource> listProv)
- {
- var count = await listProv.GetCountAsync(onlyIfCheap, cancellationToken).ConfigureAwait(false);
- return count == -1 ? -1 : count + 1;
- }
- return !onlyIfCheap || source is ICollection<TSource> ? await source.Count(cancellationToken).ConfigureAwait(false) + 1 : -1;
- }
- }
- private sealed class AppendPrependNAsyncIterator<TSource> : AppendPrependAsyncIterator<TSource>
- {
- private readonly SingleLinkedNode<TSource> prepended;
- private readonly SingleLinkedNode<TSource> appended;
- private readonly int prependCount;
- private readonly int appendCount;
- private SingleLinkedNode<TSource> node;
- public AppendPrependNAsyncIterator(IAsyncEnumerable<TSource> source, SingleLinkedNode<TSource> prepended, SingleLinkedNode<TSource> appended, int prependCount, int appendCount)
- : base(source)
- {
- Debug.Assert(prepended != null || appended != null);
- Debug.Assert(prependCount > 0 || appendCount > 0);
- Debug.Assert(prependCount + appendCount >= 2);
- Debug.Assert((prepended?.GetCount() ?? 0) == prependCount);
- Debug.Assert((appended?.GetCount() ?? 0) == appendCount);
- this.prepended = prepended;
- this.appended = appended;
- this.prependCount = prependCount;
- this.appendCount = appendCount;
- }
- public override AsyncIterator<TSource> Clone()
- {
- return new AppendPrependNAsyncIterator<TSource>(source, prepended, appended, prependCount, appendCount);
- }
- int mode;
- IEnumerator<TSource> appendedEnumerator;
- public override async Task DisposeAsync()
- {
- if (appendedEnumerator != null)
- {
- appendedEnumerator.Dispose();
- appendedEnumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- protected override async Task<bool> MoveNextCore()
- {
- switch (state)
- {
- case AsyncIteratorState.Allocated:
- mode = 1;
- state = AsyncIteratorState.Iterating;
- goto case AsyncIteratorState.Iterating;
- case AsyncIteratorState.Iterating:
- switch (mode)
- {
- case 1:
- node = prepended;
- mode = 2;
- goto case 2;
- case 2:
- if (node != null)
- {
- current = node.Item;
- node = node.Linked;
- return true;
- }
- GetSourceEnumerator();
- mode = 3;
- goto case 3;
- case 3:
- if (await LoadFromEnumeratorAsync().ConfigureAwait(false))
- {
- return true;
- }
- if (appended != null)
- {
- appendedEnumerator = appended.GetEnumerator(appendCount);
- mode = 4;
- goto case 4;
- }
- break;
- case 4:
- if (appendedEnumerator.MoveNext())
- {
- current = appendedEnumerator.Current;
- return true;
- }
- break;
- }
- break;
- }
- await DisposeAsync().ConfigureAwait(false);
- return false;
- }
- public override AppendPrependAsyncIterator<TSource> Append(TSource item)
- {
- var res = appended != null ? appended.Add(item) : new SingleLinkedNode<TSource>(item);
- return new AppendPrependNAsyncIterator<TSource>(source, prepended, res, prependCount, appendCount + 1);
- }
- public override AppendPrependAsyncIterator<TSource> Prepend(TSource item)
- {
- var res = prepended != null ? prepended.Add(item) : new SingleLinkedNode<TSource>(item);
- return new AppendPrependNAsyncIterator<TSource>(source, res, appended, prependCount + 1, appendCount);
- }
- public override async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
- if (count == -1)
- {
- return await AsyncEnumerableHelpers.ToArray(this, cancellationToken).ConfigureAwait(false);
- }
- var array = new TSource[count];
- var index = 0;
- for (var n = prepended; n != null; n = n.Linked)
- {
- array[index] = n.Item;
- ++index;
- }
- if (source is ICollection<TSource> sourceCollection)
- {
- sourceCollection.CopyTo(array, index);
- }
- else
- {
- var en = source.GetAsyncEnumerator();
- try
- {
- while (await en.MoveNextAsync(cancellationToken).ConfigureAwait(false))
- {
- array[index] = en.Current;
- ++index;
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- }
- index = array.Length;
- for (var n = appended; n != null; n = n.Linked)
- {
- --index;
- array[index] = n.Item;
- }
- return array;
- }
- public override async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
- {
- var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
- var list = count == -1 ? new List<TSource>() : new List<TSource>(count);
- for (var n = prepended; n != null; n = n.Linked)
- {
- list.Add(n.Item);
- }
- var en = source.GetAsyncEnumerator();
- try
- {
- while (await en.MoveNextAsync(cancellationToken).ConfigureAwait(false))
- {
- list.Add(en.Current);
- }
- }
- finally
- {
- await en.DisposeAsync().ConfigureAwait(false);
- }
- if (appended != null)
- {
- using (var en2 = appended.GetEnumerator(appendCount))
- {
- while (en2.MoveNext())
- {
- list.Add(en2.Current);
- }
- }
- }
- return list;
- }
- public override async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- if (source is IAsyncIListProvider<TSource> listProv)
- {
- var count = await listProv.GetCountAsync(onlyIfCheap, cancellationToken).ConfigureAwait(false);
- return count == -1 ? -1 : count + appendCount + prependCount;
- }
- return !onlyIfCheap || source is ICollection<TSource> ? await source.Count(cancellationToken).ConfigureAwait(false) + appendCount + prependCount : -1;
- }
- }
- }
- }
|