Share.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 sealed 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 GetEnumeratorCore();
  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> GetEnumeratorCore()
  86. {
  87. while (true)
  88. {
  89. if (_disposed)
  90. throw new ObjectDisposedException("");
  91. var hasValue = default(bool);
  92. var current = default(T);
  93. lock (_source)
  94. {
  95. hasValue = _source.MoveNext();
  96. if (hasValue)
  97. {
  98. current = _source.Current;
  99. }
  100. }
  101. if (hasValue)
  102. {
  103. yield return current;
  104. }
  105. else
  106. {
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }