Generate.cs 3.3 KB

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