SequenceEqual.cs 12 KB

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