Share.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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> : IBuffer<T>
  54. {
  55. private bool _disposed;
  56. private IEnumerator<T> _source;
  57. public SharedBuffer(IEnumerator<T> source)
  58. {
  59. _source = source;
  60. }
  61. public IEnumerator<T> GetEnumerator()
  62. {
  63. if (_disposed)
  64. throw new ObjectDisposedException("");
  65. return GetEnumerator_();
  66. }
  67. IEnumerator IEnumerable.GetEnumerator()
  68. {
  69. if (_disposed)
  70. throw new ObjectDisposedException("");
  71. return GetEnumerator();
  72. }
  73. public void Dispose()
  74. {
  75. lock (_source)
  76. {
  77. if (!_disposed)
  78. {
  79. _source.Dispose();
  80. _source = null;
  81. }
  82. _disposed = true;
  83. }
  84. }
  85. private IEnumerator<T> GetEnumerator_() => new ShareEnumerator(this);
  86. private sealed class ShareEnumerator : IEnumerator<T>
  87. {
  88. private readonly SharedBuffer<T> _parent;
  89. private bool _disposed;
  90. public ShareEnumerator(SharedBuffer<T> parent) => _parent = parent;
  91. public T Current { get; private set; }
  92. object IEnumerator.Current => Current;
  93. public void Dispose() => _disposed = true;
  94. public bool MoveNext()
  95. {
  96. if (_disposed)
  97. {
  98. return false;
  99. }
  100. if (_parent._disposed)
  101. {
  102. throw new ObjectDisposedException("");
  103. }
  104. var hasValue = false;
  105. var src = _parent._source;
  106. lock (src)
  107. {
  108. hasValue = src.MoveNext();
  109. if (hasValue)
  110. {
  111. Current = src.Current;
  112. }
  113. }
  114. if (hasValue)
  115. {
  116. return true;
  117. }
  118. _disposed = true;
  119. Current = default;
  120. return false;
  121. }
  122. public void Reset() => throw new NotSupportedException();
  123. }
  124. }
  125. }
  126. }