Share.cs 4.7 KB

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