|
| 1 | +#if INTEGRATED_ASYNC |
| 2 | +#pragma warning disable SA1010 // StyleCop support for collection expressions is missing |
| 3 | +namespace Funcky; |
| 4 | + |
| 5 | +public static partial class AsyncSequence |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Generates a sequence that contains the same sequence of elements over and over again as an endless generator. |
| 9 | + /// </summary> |
| 10 | + /// <typeparam name="TSource">Type of the elements to be cycled.</typeparam> |
| 11 | + /// <param name="sequence">The sequence of elements which are cycled. Throws an exception if the sequence is empty.</param> |
| 12 | + /// <returns>Returns an infinite IEnumerable repeating the same sequence of elements.</returns> |
| 13 | + [Pure] |
| 14 | + public static IAsyncBuffer<TSource> CycleRange<TSource>(IAsyncEnumerable<TSource> sequence) |
| 15 | + => CycleBuffer.Create(sequence); |
| 16 | + |
| 17 | + private static class CycleBuffer |
| 18 | + { |
| 19 | + public static AsyncCycleBuffer<TSource> Create<TSource>(IAsyncEnumerable<TSource> source, Option<int> maxCycles = default) |
| 20 | + => new(source, maxCycles); |
| 21 | + } |
| 22 | + |
| 23 | + private sealed class AsyncCycleBuffer<T>(IAsyncEnumerable<T> source, Option<int> maxCycles = default) : IAsyncBuffer<T> |
| 24 | + { |
| 25 | + private readonly List<T> _buffer = []; |
| 26 | + private readonly IAsyncEnumerator<T> _source = source.GetAsyncEnumerator(); |
| 27 | + |
| 28 | + private bool _disposed; |
| 29 | + |
| 30 | + public async ValueTask DisposeAsync() |
| 31 | + { |
| 32 | + if (!_disposed) |
| 33 | + { |
| 34 | + await _source.DisposeAsync().ConfigureAwait(false); |
| 35 | + _buffer.Clear(); |
| 36 | + _disposed = true; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) |
| 41 | + { |
| 42 | + ThrowIfDisposed(); |
| 43 | + |
| 44 | + return GetEnumeratorInternal(); |
| 45 | + } |
| 46 | + |
| 47 | + private async IAsyncEnumerator<T> GetEnumeratorInternal() |
| 48 | + { |
| 49 | + if (HasNoCycles()) |
| 50 | + { |
| 51 | + yield break; |
| 52 | + } |
| 53 | + |
| 54 | + for (var index = 0; true; ++index) |
| 55 | + { |
| 56 | + ThrowIfDisposed(); |
| 57 | + |
| 58 | + if (index == _buffer.Count) |
| 59 | + { |
| 60 | + if (await _source.MoveNextAsync().ConfigureAwait(false)) |
| 61 | + { |
| 62 | + _buffer.Add(_source.Current); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + yield return _buffer[index]; |
| 71 | + } |
| 72 | + |
| 73 | + if (_buffer.Count is 0) |
| 74 | + { |
| 75 | + if (maxCycles.Match(none: true, some: False)) |
| 76 | + { |
| 77 | + throw new InvalidOperationException("you cannot cycle an empty enumerable"); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + yield break; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // this can change on Dispose! |
| 86 | + var bufferCount = _buffer.Count; |
| 87 | + |
| 88 | + for (int cycle = 1; IsCycling(cycle); ++cycle) |
| 89 | + { |
| 90 | + for (var index = 0; index < bufferCount; ++index) |
| 91 | + { |
| 92 | + ThrowIfDisposed(); |
| 93 | + |
| 94 | + yield return _buffer[index]; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private bool HasNoCycles() |
| 100 | + => maxCycles.Match(none: false, some: maxCycles => maxCycles is 0); |
| 101 | + |
| 102 | + private bool IsCycling(int cycle) |
| 103 | + => maxCycles.Match( |
| 104 | + none: true, |
| 105 | + some: maxCycles => cycle < maxCycles); |
| 106 | + |
| 107 | + private void ThrowIfDisposed() |
| 108 | + { |
| 109 | + if (_disposed) |
| 110 | + { |
| 111 | + throw new ObjectDisposedException(nameof(CycleBuffer)); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | +#endif |
0 commit comments