Generate.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  13. {
  14. if (condition == null)
  15. throw Error.ArgumentNull(nameof(condition));
  16. if (iterate == null)
  17. throw Error.ArgumentNull(nameof(iterate));
  18. if (resultSelector == null)
  19. throw Error.ArgumentNull(nameof(resultSelector));
  20. #if USE_ASYNC_ITERATOR
  21. return AsyncEnumerable.Create(Core);
  22. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  23. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  24. {
  25. for (var state = initialState; condition(state); state = iterate(state))
  26. {
  27. // REVIEW: Check for cancellation?
  28. yield return resultSelector(state);
  29. }
  30. }
  31. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  32. #else
  33. return new GenerateAsyncIterator<TState, TResult>(initialState, condition, iterate, resultSelector);
  34. #endif
  35. }
  36. // REVIEW: Add async variant?
  37. #if !USE_ASYNC_ITERATOR
  38. private sealed class GenerateAsyncIterator<TState, TResult> : AsyncIterator<TResult>
  39. {
  40. private readonly Func<TState, bool> _condition;
  41. private readonly TState _initialState;
  42. private readonly Func<TState, TState> _iterate;
  43. private readonly Func<TState, TResult> _resultSelector;
  44. private TState _currentState;
  45. private bool _started;
  46. public GenerateAsyncIterator(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  47. {
  48. Debug.Assert(condition != null);
  49. Debug.Assert(iterate != null);
  50. Debug.Assert(resultSelector != null);
  51. _initialState = initialState;
  52. _condition = condition;
  53. _iterate = iterate;
  54. _resultSelector = resultSelector;
  55. }
  56. public override AsyncIteratorBase<TResult> Clone()
  57. {
  58. return new GenerateAsyncIterator<TState, TResult>(_initialState, _condition, _iterate, _resultSelector);
  59. }
  60. public override async ValueTask DisposeAsync()
  61. {
  62. _currentState = default;
  63. await base.DisposeAsync().ConfigureAwait(false);
  64. }
  65. protected override async ValueTask<bool> MoveNextCore()
  66. {
  67. switch (_state)
  68. {
  69. case AsyncIteratorState.Allocated:
  70. _started = false;
  71. _currentState = _initialState;
  72. _state = AsyncIteratorState.Iterating;
  73. goto case AsyncIteratorState.Iterating;
  74. case AsyncIteratorState.Iterating:
  75. if (_started)
  76. {
  77. _currentState = _iterate(_currentState);
  78. }
  79. _started = true;
  80. if (_condition(_currentState))
  81. {
  82. _current = _resultSelector(_currentState);
  83. return true;
  84. }
  85. break;
  86. }
  87. await DisposeAsync().ConfigureAwait(false);
  88. return false;
  89. }
  90. }
  91. #endif
  92. }
  93. }