Publish.cs 6.9 KB

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