Generate.cs 3.5 KB

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