SequenceEqual.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #nullable disable
  5. using System.Collections.Generic;
  6. using System.Reactive.Disposables;
  7. using System.Threading;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. internal static class SequenceEqual<TSource>
  11. {
  12. internal sealed class Observable : Producer<bool, Observable._>
  13. {
  14. private readonly IObservable<TSource> _first;
  15. private readonly IObservable<TSource> _second;
  16. private readonly IEqualityComparer<TSource> _comparer;
  17. public Observable(IObservable<TSource> first, IObservable<TSource> second, IEqualityComparer<TSource> comparer)
  18. {
  19. _first = first;
  20. _second = second;
  21. _comparer = comparer;
  22. }
  23. protected override _ CreateSink(IObserver<bool> observer) => new _(_comparer, observer);
  24. protected override void Run(_ sink) => sink.Run(this);
  25. internal sealed class _ : IdentitySink<bool>
  26. {
  27. private readonly IEqualityComparer<TSource> _comparer;
  28. private readonly object _gate;
  29. private readonly Queue<TSource> _ql;
  30. private readonly Queue<TSource> _qr;
  31. public _(IEqualityComparer<TSource> comparer, IObserver<bool> observer)
  32. : base(observer)
  33. {
  34. _comparer = comparer;
  35. _gate = new object();
  36. _ql = new Queue<TSource>();
  37. _qr = new Queue<TSource>();
  38. }
  39. private bool _donel;
  40. private bool _doner;
  41. private IDisposable _second;
  42. public void Run(Observable parent)
  43. {
  44. SetUpstream(parent._first.SubscribeSafe(new FirstObserver(this)));
  45. Disposable.SetSingle(ref _second, parent._second.SubscribeSafe(new SecondObserver(this)));
  46. }
  47. protected override void Dispose(bool disposing)
  48. {
  49. if (disposing)
  50. {
  51. Disposable.Dispose(ref _second);
  52. }
  53. base.Dispose(disposing);
  54. }
  55. private sealed class FirstObserver : IObserver<TSource>
  56. {
  57. private readonly _ _parent;
  58. public FirstObserver(_ parent)
  59. {
  60. _parent = parent;
  61. }
  62. public void OnNext(TSource value)
  63. {
  64. lock (_parent._gate)
  65. {
  66. if (_parent._qr.Count > 0)
  67. {
  68. var equal = false;
  69. var v = _parent._qr.Dequeue();
  70. try
  71. {
  72. equal = _parent._comparer.Equals(value, v);
  73. }
  74. catch (Exception exception)
  75. {
  76. _parent.ForwardOnError(exception);
  77. return;
  78. }
  79. if (!equal)
  80. {
  81. _parent.ForwardOnNext(false);
  82. _parent.ForwardOnCompleted();
  83. }
  84. }
  85. else if (_parent._doner)
  86. {
  87. _parent.ForwardOnNext(false);
  88. _parent.ForwardOnCompleted();
  89. }
  90. else
  91. {
  92. _parent._ql.Enqueue(value);
  93. }
  94. }
  95. }
  96. public void OnError(Exception error)
  97. {
  98. lock (_parent._gate)
  99. {
  100. _parent.ForwardOnError(error);
  101. }
  102. }
  103. public void OnCompleted()
  104. {
  105. lock (_parent._gate)
  106. {
  107. _parent._donel = true;
  108. if (_parent._ql.Count == 0)
  109. {
  110. if (_parent._qr.Count > 0)
  111. {
  112. _parent.ForwardOnNext(false);
  113. _parent.ForwardOnCompleted();
  114. }
  115. else if (_parent._doner)
  116. {
  117. _parent.ForwardOnNext(true);
  118. _parent.ForwardOnCompleted();
  119. }
  120. }
  121. }
  122. }
  123. }
  124. private sealed class SecondObserver : IObserver<TSource>
  125. {
  126. private readonly _ _parent;
  127. public SecondObserver(_ parent)
  128. {
  129. _parent = parent;
  130. }
  131. public void OnNext(TSource value)
  132. {
  133. lock (_parent._gate)
  134. {
  135. if (_parent._ql.Count > 0)
  136. {
  137. var equal = false;
  138. var v = _parent._ql.Dequeue();
  139. try
  140. {
  141. equal = _parent._comparer.Equals(v, value);
  142. }
  143. catch (Exception exception)
  144. {
  145. _parent.ForwardOnError(exception);
  146. return;
  147. }
  148. if (!equal)
  149. {
  150. _parent.ForwardOnNext(false);
  151. _parent.ForwardOnCompleted();
  152. }
  153. }
  154. else if (_parent._donel)
  155. {
  156. _parent.ForwardOnNext(false);
  157. _parent.ForwardOnCompleted();
  158. }
  159. else
  160. {
  161. _parent._qr.Enqueue(value);
  162. }
  163. }
  164. }
  165. public void OnError(Exception error)
  166. {
  167. lock (_parent._gate)
  168. {
  169. _parent.ForwardOnError(error);
  170. }
  171. }
  172. public void OnCompleted()
  173. {
  174. lock (_parent._gate)
  175. {
  176. _parent._doner = true;
  177. if (_parent._qr.Count == 0)
  178. {
  179. if (_parent._ql.Count > 0)
  180. {
  181. _parent.ForwardOnNext(false);
  182. _parent.ForwardOnCompleted();
  183. }
  184. else if (_parent._donel)
  185. {
  186. _parent.ForwardOnNext(true);
  187. _parent.ForwardOnCompleted();
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. internal sealed class Enumerable : Producer<bool, Enumerable._>
  196. {
  197. private readonly IObservable<TSource> _first;
  198. private readonly IEnumerable<TSource> _second;
  199. private readonly IEqualityComparer<TSource> _comparer;
  200. public Enumerable(IObservable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  201. {
  202. _first = first;
  203. _second = second;
  204. _comparer = comparer;
  205. }
  206. protected override _ CreateSink(IObserver<bool> observer) => new _(_comparer, observer);
  207. protected override void Run(_ sink) => sink.Run(this);
  208. internal sealed class _ : Sink<TSource, bool>
  209. {
  210. private readonly IEqualityComparer<TSource> _comparer;
  211. public _(IEqualityComparer<TSource> comparer, IObserver<bool> observer)
  212. : base(observer)
  213. {
  214. _comparer = comparer;
  215. }
  216. private IEnumerator<TSource> _enumerator;
  217. private static readonly IEnumerator<TSource> DisposedEnumerator = MakeDisposedEnumerator();
  218. private static IEnumerator<TSource> MakeDisposedEnumerator()
  219. {
  220. yield break;
  221. }
  222. public void Run(Enumerable parent)
  223. {
  224. //
  225. // Notice the evaluation order of obtaining the enumerator and subscribing to the
  226. // observable sequence is reversed compared to the operator's signature. This is
  227. // required to make sure the enumerator is available as soon as the observer can
  228. // be called. Otherwise, we end up having a race for the initialization and use
  229. // of the _rightEnumerator field.
  230. //
  231. try
  232. {
  233. var enumerator = parent._second.GetEnumerator();
  234. if (Interlocked.CompareExchange(ref _enumerator, enumerator, null) != null)
  235. {
  236. enumerator.Dispose();
  237. return;
  238. }
  239. }
  240. catch (Exception exception)
  241. {
  242. ForwardOnError(exception);
  243. return;
  244. }
  245. SetUpstream(parent._first.SubscribeSafe(this));
  246. }
  247. protected override void Dispose(bool disposing)
  248. {
  249. if (disposing)
  250. {
  251. Interlocked.Exchange(ref _enumerator, DisposedEnumerator)?.Dispose();
  252. }
  253. base.Dispose(disposing);
  254. }
  255. public override void OnNext(TSource value)
  256. {
  257. var equal = false;
  258. try
  259. {
  260. if (_enumerator.MoveNext())
  261. {
  262. var current = _enumerator.Current;
  263. equal = _comparer.Equals(value, current);
  264. }
  265. }
  266. catch (Exception exception)
  267. {
  268. ForwardOnError(exception);
  269. return;
  270. }
  271. if (!equal)
  272. {
  273. ForwardOnNext(false);
  274. ForwardOnCompleted();
  275. }
  276. }
  277. public override void OnCompleted()
  278. {
  279. bool hasNext;
  280. try
  281. {
  282. hasNext = _enumerator.MoveNext();
  283. }
  284. catch (Exception exception)
  285. {
  286. ForwardOnError(exception);
  287. return;
  288. }
  289. ForwardOnNext(!hasNext);
  290. ForwardOnCompleted();
  291. }
  292. }
  293. }
  294. }
  295. }