Generate.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. return new GenerateAsyncIterator<TState, TResult>(initialState, condition, iterate, resultSelector);
  21. }
  22. // REVIEW: Add async variant?
  23. private sealed class GenerateAsyncIterator<TState, TResult> : AsyncIterator<TResult>
  24. {
  25. private readonly Func<TState, bool> _condition;
  26. private readonly TState _initialState;
  27. private readonly Func<TState, TState> _iterate;
  28. private readonly Func<TState, TResult> _resultSelector;
  29. private TState _currentState;
  30. private bool _started;
  31. public GenerateAsyncIterator(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  32. {
  33. Debug.Assert(condition != null);
  34. Debug.Assert(iterate != null);
  35. Debug.Assert(resultSelector != null);
  36. _initialState = initialState;
  37. _condition = condition;
  38. _iterate = iterate;
  39. _resultSelector = resultSelector;
  40. }
  41. public override AsyncIteratorBase<TResult> Clone()
  42. {
  43. return new GenerateAsyncIterator<TState, TResult>(_initialState, _condition, _iterate, _resultSelector);
  44. }
  45. public override async ValueTask DisposeAsync()
  46. {
  47. _currentState = default;
  48. await base.DisposeAsync().ConfigureAwait(false);
  49. }
  50. protected override async ValueTask<bool> MoveNextCore()
  51. {
  52. switch (_state)
  53. {
  54. case AsyncIteratorState.Allocated:
  55. _started = false;
  56. _currentState = _initialState;
  57. _state = AsyncIteratorState.Iterating;
  58. goto case AsyncIteratorState.Iterating;
  59. case AsyncIteratorState.Iterating:
  60. if (_started)
  61. {
  62. _currentState = _iterate(_currentState);
  63. }
  64. _started = true;
  65. if (_condition(_currentState))
  66. {
  67. _current = _resultSelector(_currentState);
  68. return true;
  69. }
  70. break;
  71. }
  72. await DisposeAsync().ConfigureAwait(false);
  73. return false;
  74. }
  75. }
  76. }
  77. }