Generate.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  12. {
  13. if (condition == null)
  14. {
  15. throw new ArgumentNullException(nameof(condition));
  16. }
  17. if (iterate == null)
  18. {
  19. throw new ArgumentNullException(nameof(iterate));
  20. }
  21. if (resultSelector == null)
  22. {
  23. throw new ArgumentNullException(nameof(resultSelector));
  24. }
  25. return new GenerateAsyncIterator<TState, TResult>(initialState, condition, iterate, resultSelector);
  26. }
  27. private sealed class GenerateAsyncIterator<TState, TResult> : AsyncIterator<TResult>
  28. {
  29. private readonly Func<TState, bool> condition;
  30. private readonly TState initialState;
  31. private readonly Func<TState, TState> iterate;
  32. private readonly Func<TState, TResult> resultSelector;
  33. private TState currentState;
  34. private bool started;
  35. public GenerateAsyncIterator(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  36. {
  37. Debug.Assert(condition != null);
  38. Debug.Assert(iterate != null);
  39. Debug.Assert(resultSelector != null);
  40. this.initialState = initialState;
  41. this.condition = condition;
  42. this.iterate = iterate;
  43. this.resultSelector = resultSelector;
  44. }
  45. public override AsyncIterator<TResult> Clone()
  46. {
  47. return new GenerateAsyncIterator<TState, TResult>(initialState, condition, iterate, resultSelector);
  48. }
  49. public override async ValueTask DisposeAsync()
  50. {
  51. currentState = default;
  52. await base.DisposeAsync().ConfigureAwait(false);
  53. }
  54. protected override async ValueTask<bool> MoveNextCore()
  55. {
  56. switch (state)
  57. {
  58. case AsyncIteratorState.Allocated:
  59. started = false;
  60. currentState = initialState;
  61. state = AsyncIteratorState.Iterating;
  62. goto case AsyncIteratorState.Iterating;
  63. case AsyncIteratorState.Iterating:
  64. if (started)
  65. {
  66. currentState = iterate(currentState);
  67. }
  68. started = true;
  69. if (condition(currentState))
  70. {
  71. current = resultSelector(currentState);
  72. return true;
  73. }
  74. break;
  75. }
  76. await DisposeAsync().ConfigureAwait(false);
  77. return false;
  78. }
  79. }
  80. }
  81. }