Share.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 shared view over the source sequence, causing each enumerator to fetch the next element
  15. /// from the source sequence.
  16. /// </summary>
  17. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  18. /// <param name="source">Source sequence.</param>
  19. /// <returns>Buffer enabling each enumerator to retrieve elements from the shared source sequence.</returns>
  20. /// <example>
  21. /// var rng = Enumerable.Range(0, 10).Share();
  22. /// var e1 = rng.GetEnumerator(); // Both e1 and e2 will consume elements from
  23. /// var e2 = rng.GetEnumerator(); // the source sequence.
  24. /// Assert.IsTrue(e1.MoveNext());
  25. /// Assert.AreEqual(0, e1.Current);
  26. /// Assert.IsTrue(e1.MoveNext());
  27. /// Assert.AreEqual(1, e1.Current);
  28. /// Assert.IsTrue(e2.MoveNext()); // e2 "steals" element 2
  29. /// Assert.AreEqual(2, e2.Current);
  30. /// Assert.IsTrue(e1.MoveNext()); // e1 can't see element 2
  31. /// Assert.AreEqual(3, e1.Current);
  32. /// </example>
  33. public static IBuffer<TSource> Share<TSource>(this IEnumerable<TSource> source)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. return new SharedBuffer<TSource>(source.GetEnumerator());
  38. }
  39. /// <summary>
  40. /// Shares the source sequence within a selector function where each enumerator can fetch the next element from the
  41. /// source sequence.
  42. /// </summary>
  43. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  44. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  45. /// <param name="source">Source sequence.</param>
  46. /// <param name="selector">Selector function with shared access to the source sequence for each enumerator.</param>
  47. /// <returns>Sequence resulting from applying the selector function to the shared view over the source sequence.</returns>
  48. public static IEnumerable<TResult> Share<TSource, TResult>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (selector == null)
  53. throw new ArgumentNullException(nameof(selector));
  54. return Create(() => selector(source.Share())
  55. .GetEnumerator());
  56. }
  57. private class SharedBuffer<T> : IBuffer<T>
  58. {
  59. private bool _disposed;
  60. private IEnumerator<T> _source;
  61. public SharedBuffer(IEnumerator<T> source)
  62. {
  63. _source = source;
  64. }
  65. public IEnumerator<T> GetEnumerator()
  66. {
  67. if (_disposed)
  68. throw new ObjectDisposedException("");
  69. return GetEnumerator_();
  70. }
  71. IEnumerator IEnumerable.GetEnumerator()
  72. {
  73. if (_disposed)
  74. throw new ObjectDisposedException("");
  75. return GetEnumerator();
  76. }
  77. public void Dispose()
  78. {
  79. lock (_source)
  80. {
  81. if (!_disposed)
  82. {
  83. _source.Dispose();
  84. _source = null;
  85. }
  86. _disposed = true;
  87. }
  88. }
  89. private IEnumerator<T> GetEnumerator_()
  90. {
  91. return new ShareEnumerator(this);
  92. }
  93. sealed class ShareEnumerator : IEnumerator<T>
  94. {
  95. readonly SharedBuffer<T> _parent;
  96. T _current;
  97. bool _disposed;
  98. public ShareEnumerator(SharedBuffer<T> parent)
  99. {
  100. _parent = parent;
  101. }
  102. public T Current => _current;
  103. object IEnumerator.Current => _current;
  104. public void Dispose()
  105. {
  106. _disposed = true;
  107. }
  108. public bool MoveNext()
  109. {
  110. if (_disposed)
  111. {
  112. return false;
  113. }
  114. if (_parent._disposed)
  115. {
  116. throw new ObjectDisposedException("");
  117. }
  118. var hasValue = false;
  119. var src = _parent._source;
  120. lock (src)
  121. {
  122. hasValue = src.MoveNext();
  123. if (hasValue)
  124. {
  125. _current = src.Current;
  126. }
  127. }
  128. if (hasValue)
  129. {
  130. return true;
  131. }
  132. _disposed = true;
  133. _current = default(T);
  134. return false;
  135. }
  136. public void Reset()
  137. {
  138. throw new NotSupportedException();
  139. }
  140. }
  141. }
  142. }
  143. }