Share.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  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. {
  34. throw new ArgumentNullException(nameof(source));
  35. }
  36. return new SharedBuffer<TSource>(source.GetEnumerator());
  37. }
  38. /// <summary>
  39. /// Shares the source sequence within a selector function where each enumerator can fetch the next element from the
  40. /// source sequence.
  41. /// </summary>
  42. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  43. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  44. /// <param name="source">Source sequence.</param>
  45. /// <param name="selector">Selector function with shared access to the source sequence for each enumerator.</param>
  46. /// <returns>Sequence resulting from applying the selector function to the shared view over the source sequence.</returns>
  47. public static IEnumerable<TResult> Share<TSource, TResult>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  48. {
  49. if (source == null)
  50. {
  51. throw new ArgumentNullException(nameof(source));
  52. }
  53. if (selector == null)
  54. {
  55. throw new ArgumentNullException(nameof(selector));
  56. }
  57. return Create(() => selector(source.Share())
  58. .GetEnumerator());
  59. }
  60. private class SharedBuffer<T> : IBuffer<T>
  61. {
  62. private bool _disposed;
  63. private IEnumerator<T> _source;
  64. public SharedBuffer(IEnumerator<T> source)
  65. {
  66. _source = source;
  67. }
  68. public IEnumerator<T> GetEnumerator()
  69. {
  70. if (_disposed)
  71. {
  72. throw new ObjectDisposedException("");
  73. }
  74. return GetEnumerator_();
  75. }
  76. IEnumerator IEnumerable.GetEnumerator()
  77. {
  78. if (_disposed)
  79. {
  80. throw new ObjectDisposedException("");
  81. }
  82. return GetEnumerator();
  83. }
  84. public void Dispose()
  85. {
  86. lock (_source)
  87. {
  88. if (!_disposed)
  89. {
  90. _source.Dispose();
  91. _source = null;
  92. }
  93. _disposed = true;
  94. }
  95. }
  96. private IEnumerator<T> GetEnumerator_()
  97. {
  98. return new ShareEnumerator(this);
  99. }
  100. private sealed class ShareEnumerator : IEnumerator<T>
  101. {
  102. private readonly SharedBuffer<T> _parent;
  103. private T _current;
  104. private bool _disposed;
  105. public ShareEnumerator(SharedBuffer<T> parent)
  106. {
  107. _parent = parent;
  108. }
  109. public T Current => _current;
  110. object IEnumerator.Current => _current;
  111. public void Dispose()
  112. {
  113. _disposed = true;
  114. }
  115. public bool MoveNext()
  116. {
  117. if (_disposed)
  118. {
  119. return false;
  120. }
  121. if (_parent._disposed)
  122. {
  123. throw new ObjectDisposedException("");
  124. }
  125. var hasValue = false;
  126. var src = _parent._source;
  127. lock (src)
  128. {
  129. hasValue = src.MoveNext();
  130. if (hasValue)
  131. {
  132. _current = src.Current;
  133. }
  134. }
  135. if (hasValue)
  136. {
  137. return true;
  138. }
  139. _disposed = true;
  140. _current = default;
  141. return false;
  142. }
  143. public void Reset()
  144. {
  145. throw new NotSupportedException();
  146. }
  147. }
  148. }
  149. }
  150. }