Share.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace System.Linq
  7. {
  8. public static partial class EnumerableEx
  9. {
  10. /// <summary>
  11. /// Creates a buffer with a shared view over the source sequence, causing each enumerator to fetch the next element
  12. /// from the source sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  15. /// <param name="source">Source sequence.</param>
  16. /// <returns>Buffer enabling each enumerator to retrieve elements from the shared source sequence.</returns>
  17. /// <example>
  18. /// var rng = Enumerable.Range(0, 10).Share();
  19. /// var e1 = rng.GetEnumerator(); // Both e1 and e2 will consume elements from
  20. /// var e2 = rng.GetEnumerator(); // the source sequence.
  21. /// Assert.IsTrue(e1.MoveNext());
  22. /// Assert.AreEqual(0, e1.Current);
  23. /// Assert.IsTrue(e1.MoveNext());
  24. /// Assert.AreEqual(1, e1.Current);
  25. /// Assert.IsTrue(e2.MoveNext()); // e2 "steals" element 2
  26. /// Assert.AreEqual(2, e2.Current);
  27. /// Assert.IsTrue(e1.MoveNext()); // e1 can't see element 2
  28. /// Assert.AreEqual(3, e1.Current);
  29. /// </example>
  30. public static IBuffer<TSource> Share<TSource>(this IEnumerable<TSource> source)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. return new SharedBuffer<TSource>(source.GetEnumerator());
  35. }
  36. /// <summary>
  37. /// Shares the source sequence within a selector function where each enumerator can fetch the next element from the
  38. /// source sequence.
  39. /// </summary>
  40. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  41. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  42. /// <param name="source">Source sequence.</param>
  43. /// <param name="selector">Selector function with shared access to the source sequence for each enumerator.</param>
  44. /// <returns>Sequence resulting from applying the selector function to the shared view over the source sequence.</returns>
  45. public static IEnumerable<TResult> Share<TSource, TResult>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  46. {
  47. if (source == null)
  48. throw new ArgumentNullException(nameof(source));
  49. if (selector == null)
  50. throw new ArgumentNullException(nameof(selector));
  51. return Create(() => selector(source.Share()).GetEnumerator());
  52. }
  53. private class SharedBuffer<T>(IEnumerator<T> source) : IBuffer<T>
  54. {
  55. private readonly IEnumerator<T> _source = source;
  56. private bool _disposed;
  57. public IEnumerator<T> GetEnumerator()
  58. {
  59. if (_disposed)
  60. throw new ObjectDisposedException("");
  61. return GetEnumerator_();
  62. }
  63. IEnumerator IEnumerable.GetEnumerator()
  64. {
  65. if (_disposed)
  66. throw new ObjectDisposedException("");
  67. return GetEnumerator();
  68. }
  69. public void Dispose()
  70. {
  71. lock (_source)
  72. {
  73. if (!_disposed)
  74. {
  75. _source.Dispose();
  76. }
  77. _disposed = true;
  78. }
  79. }
  80. private IEnumerator<T> GetEnumerator_() => new ShareEnumerator(this);
  81. private sealed class ShareEnumerator : IEnumerator<T>
  82. {
  83. private readonly SharedBuffer<T> _parent;
  84. private bool _disposed;
  85. public ShareEnumerator(SharedBuffer<T> parent)
  86. {
  87. _parent = parent;
  88. Current = default!;
  89. }
  90. public T Current { get; private set; }
  91. object? IEnumerator.Current => Current;
  92. public void Dispose() => _disposed = true;
  93. public bool MoveNext()
  94. {
  95. if (_disposed)
  96. {
  97. return false;
  98. }
  99. if (_parent._disposed)
  100. {
  101. throw new ObjectDisposedException("");
  102. }
  103. var hasValue = false;
  104. var src = _parent._source;
  105. lock (src)
  106. {
  107. hasValue = src.MoveNext();
  108. if (hasValue)
  109. {
  110. Current = src.Current;
  111. }
  112. }
  113. if (hasValue)
  114. {
  115. return true;
  116. }
  117. _disposed = true;
  118. Current = default!;
  119. return false;
  120. }
  121. public void Reset() => throw new NotSupportedException();
  122. }
  123. }
  124. }
  125. }