Publish.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 view over the source sequence, causing each enumerator to obtain access to the remainder of
  12. /// the sequence from the current index in the buffer.
  13. /// </summary>
  14. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  15. /// <param name="source">Source sequence.</param>
  16. /// <returns>
  17. /// Buffer enabling each enumerator to retrieve elements from the shared source sequence, starting from the index
  18. /// at the point of obtaining the enumerator.
  19. /// </returns>
  20. /// <example>
  21. /// var rng = Enumerable.Range(0, 10).Publish();
  22. /// var e1 = rng.GetEnumerator(); // e1 has a view on the source starting from element 0
  23. /// Assert.IsTrue(e1.MoveNext());
  24. /// Assert.AreEqual(0, e1.Current);
  25. /// Assert.IsTrue(e1.MoveNext());
  26. /// Assert.AreEqual(1, e1.Current);
  27. /// var e2 = rng.GetEnumerator();
  28. /// Assert.IsTrue(e2.MoveNext()); // e2 has a view on the source starting from element 2
  29. /// Assert.AreEqual(2, e2.Current);
  30. /// Assert.IsTrue(e1.MoveNext()); // e1 continues to enumerate over its view
  31. /// Assert.AreEqual(2, e1.Current);
  32. /// </example>
  33. public static IBuffer<TSource> Publish<TSource>(this IEnumerable<TSource> source)
  34. {
  35. if (source == null)
  36. {
  37. throw new ArgumentNullException(nameof(source));
  38. }
  39. return new PublishedBuffer<TSource>(source.GetEnumerator());
  40. }
  41. /// <summary>
  42. /// Publishes the source sequence within a selector function where each enumerator can obtain a view over a tail of the
  43. /// source sequence.
  44. /// </summary>
  45. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  46. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  47. /// <param name="source">Source sequence.</param>
  48. /// <param name="selector">Selector function with published access to the source sequence for each enumerator.</param>
  49. /// <returns>Sequence resulting from applying the selector function to the published view over the source sequence.</returns>
  50. public static IEnumerable<TResult> Publish<TSource, TResult>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  51. {
  52. if (source == null)
  53. {
  54. throw new ArgumentNullException(nameof(source));
  55. }
  56. if (selector == null)
  57. {
  58. throw new ArgumentNullException(nameof(selector));
  59. }
  60. return Create(() => selector(source.Publish())
  61. .GetEnumerator());
  62. }
  63. private class PublishedBuffer<T> : IBuffer<T>
  64. {
  65. private RefCountList<T> _buffer;
  66. private bool _disposed;
  67. private Exception _error;
  68. private IEnumerator<T> _source;
  69. private bool _stopped;
  70. public PublishedBuffer(IEnumerator<T> source)
  71. {
  72. _buffer = new RefCountList<T>(0);
  73. _source = source;
  74. }
  75. public IEnumerator<T> GetEnumerator()
  76. {
  77. if (_disposed)
  78. {
  79. throw new ObjectDisposedException("");
  80. }
  81. var i = default(int);
  82. lock (_source)
  83. {
  84. i = _buffer.Count;
  85. _buffer.ReaderCount++;
  86. }
  87. return GetEnumerator_(i);
  88. }
  89. IEnumerator IEnumerable.GetEnumerator()
  90. {
  91. if (_disposed)
  92. {
  93. throw new ObjectDisposedException("");
  94. }
  95. return GetEnumerator();
  96. }
  97. public void Dispose()
  98. {
  99. lock (_source)
  100. {
  101. if (!_disposed)
  102. {
  103. _source.Dispose();
  104. _source = null;
  105. _buffer.Clear();
  106. _buffer = null;
  107. }
  108. _disposed = true;
  109. }
  110. }
  111. private IEnumerator<T> GetEnumerator_(int i)
  112. {
  113. try
  114. {
  115. while (true)
  116. {
  117. if (_disposed)
  118. {
  119. throw new ObjectDisposedException("");
  120. }
  121. var hasValue = default(bool);
  122. var current = default(T);
  123. lock (_source)
  124. {
  125. if (i >= _buffer.Count)
  126. {
  127. if (!_stopped)
  128. {
  129. try
  130. {
  131. hasValue = _source.MoveNext();
  132. if (hasValue)
  133. {
  134. current = _source.Current;
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. _stopped = true;
  140. _error = ex;
  141. _source.Dispose();
  142. }
  143. }
  144. if (_stopped)
  145. {
  146. if (_error != null)
  147. {
  148. throw _error;
  149. }
  150. else
  151. {
  152. break;
  153. }
  154. }
  155. if (hasValue)
  156. {
  157. _buffer.Add(current);
  158. }
  159. }
  160. else
  161. {
  162. hasValue = true;
  163. }
  164. }
  165. if (hasValue)
  166. {
  167. yield return _buffer[i];
  168. }
  169. else
  170. {
  171. break;
  172. }
  173. i++;
  174. }
  175. }
  176. finally
  177. {
  178. if (_buffer != null)
  179. {
  180. _buffer.Done(i + 1);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }