1
0

AppendPrepend.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace System.Linq
  11. {
  12. public static partial class AsyncEnumerable
  13. {
  14. public static IAsyncEnumerable<TSource> Append<TSource>(this IAsyncEnumerable<TSource> source, TSource element)
  15. {
  16. if (source == null)
  17. throw new ArgumentNullException(nameof(source));
  18. if (source is AppendPrepentAsyncIterator<TSource> appendable)
  19. {
  20. return appendable.Append(element);
  21. }
  22. return new AppendPrepend1AsyncIterator<TSource>(source, element, true);
  23. }
  24. public static IAsyncEnumerable<TSource> Prepend<TSource>(this IAsyncEnumerable<TSource> source, TSource element)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. if (source is AppendPrepentAsyncIterator<TSource> appendable)
  29. {
  30. return appendable.Prepend(element);
  31. }
  32. return new AppendPrepend1AsyncIterator<TSource>(source, element, false);
  33. }
  34. private abstract class AppendPrepentAsyncIterator<TSource> : AsyncIterator<TSource>, IIListProvider<TSource>
  35. {
  36. protected readonly IAsyncEnumerable<TSource> source;
  37. protected IAsyncEnumerator<TSource> enumerator;
  38. protected AppendPrepentAsyncIterator(IAsyncEnumerable<TSource> source)
  39. {
  40. Debug.Assert(source != null);
  41. this.source = source;
  42. }
  43. protected void GetSourceEnumerator()
  44. {
  45. Debug.Assert(enumerator == null);
  46. enumerator = source.GetAsyncEnumerator();
  47. }
  48. public abstract AppendPrepentAsyncIterator<TSource> Append(TSource item);
  49. public abstract AppendPrepentAsyncIterator<TSource> Prepend(TSource item);
  50. protected async Task<bool> LoadFromEnumerator()
  51. {
  52. if (await enumerator.MoveNextAsync()
  53. .ConfigureAwait(false))
  54. {
  55. current = enumerator.Current;
  56. return true;
  57. }
  58. if (enumerator != null)
  59. {
  60. await enumerator.DisposeAsync().ConfigureAwait(false);
  61. enumerator = null;
  62. }
  63. return false;
  64. }
  65. public override async Task DisposeAsync()
  66. {
  67. if (enumerator != null)
  68. {
  69. await enumerator.DisposeAsync().ConfigureAwait(false);
  70. enumerator = null;
  71. }
  72. await base.DisposeAsync().ConfigureAwait(false);
  73. }
  74. public abstract Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken);
  75. public abstract Task<List<TSource>> ToListAsync(CancellationToken cancellationToken);
  76. public abstract Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken);
  77. }
  78. private sealed class AppendPrepend1AsyncIterator<TSource> : AppendPrepentAsyncIterator<TSource>
  79. {
  80. private readonly TSource item;
  81. private readonly bool appending;
  82. bool hasEnumerator;
  83. public AppendPrepend1AsyncIterator(IAsyncEnumerable<TSource> source, TSource item, bool appending)
  84. : base(source)
  85. {
  86. this.item = item;
  87. this.appending = appending;
  88. }
  89. public override AsyncIterator<TSource> Clone()
  90. {
  91. return new AppendPrepend1AsyncIterator<TSource>(source, item, appending);
  92. }
  93. protected override async Task<bool> MoveNextCore()
  94. {
  95. switch (state)
  96. {
  97. case AsyncIteratorState.Allocated:
  98. hasEnumerator = false;
  99. state = AsyncIteratorState.Iterating;
  100. if (!appending)
  101. {
  102. current = item;
  103. return true;
  104. }
  105. goto case AsyncIteratorState.Iterating;
  106. case AsyncIteratorState.Iterating:
  107. if (!hasEnumerator)
  108. {
  109. GetSourceEnumerator();
  110. hasEnumerator = true;
  111. }
  112. if (enumerator != null)
  113. {
  114. if (await LoadFromEnumerator()
  115. .ConfigureAwait(false))
  116. {
  117. return true;
  118. }
  119. if (appending)
  120. {
  121. current = item;
  122. return true;
  123. }
  124. }
  125. break;
  126. }
  127. await DisposeAsync().ConfigureAwait(false);
  128. return false;
  129. }
  130. public override AppendPrepentAsyncIterator<TSource> Append(TSource element)
  131. {
  132. if (appending)
  133. {
  134. return new AppendPrependNAsyncIterator<TSource>(source, null, new SingleLinkedNode<TSource>(item, element));
  135. }
  136. return new AppendPrependNAsyncIterator<TSource>(source, new SingleLinkedNode<TSource>(item), new SingleLinkedNode<TSource>(element));
  137. }
  138. public override AppendPrepentAsyncIterator<TSource> Prepend(TSource element)
  139. {
  140. if (appending)
  141. {
  142. return new AppendPrependNAsyncIterator<TSource>(source, new SingleLinkedNode<TSource>(element), new SingleLinkedNode<TSource>(item));
  143. }
  144. return new AppendPrependNAsyncIterator<TSource>(source, new SingleLinkedNode<TSource>(item, element), null);
  145. }
  146. public override async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  147. {
  148. var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
  149. if (count == -1)
  150. {
  151. return await AsyncEnumerableHelpers.ToArray(this, cancellationToken).ConfigureAwait(false);
  152. }
  153. var array = new TSource[count];
  154. int index;
  155. if (appending)
  156. {
  157. index = 0;
  158. }
  159. else
  160. {
  161. array[0] = item;
  162. index = 1;
  163. }
  164. if (source is ICollection<TSource> sourceCollection)
  165. {
  166. sourceCollection.CopyTo(array, index);
  167. }
  168. else
  169. {
  170. var en = source.GetAsyncEnumerator();
  171. try
  172. {
  173. while (await en.MoveNextAsync(cancellationToken)
  174. .ConfigureAwait(false))
  175. {
  176. array[index] = en.Current;
  177. ++index;
  178. }
  179. }
  180. finally
  181. {
  182. await en.DisposeAsync().ConfigureAwait(false);
  183. }
  184. }
  185. if (appending)
  186. {
  187. array[array.Length - 1] = item;
  188. }
  189. return array;
  190. }
  191. public override async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  192. {
  193. var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
  194. var list = count == -1 ? new List<TSource>() : new List<TSource>(count);
  195. if (!appending)
  196. {
  197. list.Add(item);
  198. }
  199. var en = source.GetAsyncEnumerator();
  200. try
  201. {
  202. while (await en.MoveNextAsync(cancellationToken)
  203. .ConfigureAwait(false))
  204. {
  205. list.Add(en.Current);
  206. }
  207. }
  208. finally
  209. {
  210. await en.DisposeAsync().ConfigureAwait(false);
  211. }
  212. if (appending)
  213. {
  214. list.Add(item);
  215. }
  216. return list;
  217. }
  218. public override async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  219. {
  220. if (source is IIListProvider<TSource> listProv)
  221. {
  222. var count = await listProv.GetCountAsync(onlyIfCheap, cancellationToken).ConfigureAwait(false);
  223. return count == -1 ? -1 : count + 1;
  224. }
  225. return !onlyIfCheap || source is ICollection<TSource> ? await source.Count(cancellationToken).ConfigureAwait(false) + 1 : -1;
  226. }
  227. }
  228. private sealed class SingleLinkedNode<TSource>
  229. {
  230. public SingleLinkedNode(TSource first, TSource second)
  231. {
  232. Linked = new SingleLinkedNode<TSource>(first);
  233. Item = second;
  234. Count = 2;
  235. }
  236. public SingleLinkedNode(TSource item)
  237. {
  238. Item = item;
  239. Count = 1;
  240. }
  241. private SingleLinkedNode(SingleLinkedNode<TSource> linked, TSource item)
  242. {
  243. Debug.Assert(linked != null);
  244. Linked = linked;
  245. Item = item;
  246. Count = linked.Count + 1;
  247. }
  248. public TSource Item { get; }
  249. public SingleLinkedNode<TSource> Linked { get; }
  250. public int Count { get; }
  251. public SingleLinkedNode<TSource> Add(TSource item) => new SingleLinkedNode<TSource>(this, item);
  252. public IEnumerator<TSource> GetEnumerator()
  253. {
  254. var array = new TSource[Count];
  255. var index = Count;
  256. for (var n = this; n != null; n = n.Linked)
  257. {
  258. --index;
  259. array[index] = n.Item;
  260. }
  261. Debug.Assert(index == 0);
  262. return ((IEnumerable<TSource>)array).GetEnumerator();
  263. }
  264. }
  265. private sealed class AppendPrependNAsyncIterator<TSource> : AppendPrepentAsyncIterator<TSource>
  266. {
  267. private readonly SingleLinkedNode<TSource> prepended;
  268. private readonly SingleLinkedNode<TSource> appended;
  269. private SingleLinkedNode<TSource> node;
  270. public AppendPrependNAsyncIterator(IAsyncEnumerable<TSource> source, SingleLinkedNode<TSource> prepended, SingleLinkedNode<TSource> appended)
  271. : base(source)
  272. {
  273. Debug.Assert(prepended != null || appended != null);
  274. this.prepended = prepended;
  275. this.appended = appended;
  276. }
  277. public override AsyncIterator<TSource> Clone()
  278. {
  279. return new AppendPrependNAsyncIterator<TSource>(source, prepended, appended);
  280. }
  281. int mode;
  282. IEnumerator<TSource> appendedEnumerator;
  283. public override async Task DisposeAsync()
  284. {
  285. if (appendedEnumerator != null)
  286. {
  287. appendedEnumerator.Dispose();
  288. appendedEnumerator = null;
  289. }
  290. await base.DisposeAsync().ConfigureAwait(false);
  291. }
  292. protected override async Task<bool> MoveNextCore()
  293. {
  294. switch (state)
  295. {
  296. case AsyncIteratorState.Allocated:
  297. mode = 1;
  298. state = AsyncIteratorState.Iterating;
  299. goto case AsyncIteratorState.Iterating;
  300. case AsyncIteratorState.Iterating:
  301. switch (mode)
  302. {
  303. case 1:
  304. node = prepended;
  305. mode = 2;
  306. goto case 2;
  307. case 2:
  308. if (node != null)
  309. {
  310. current = node.Item;
  311. node = node.Linked;
  312. return true;
  313. }
  314. GetSourceEnumerator();
  315. mode = 3;
  316. goto case 3;
  317. case 3:
  318. if (await LoadFromEnumerator()
  319. .ConfigureAwait(false))
  320. {
  321. return true;
  322. }
  323. if (appended != null)
  324. {
  325. appendedEnumerator = appended.GetEnumerator();
  326. mode = 4;
  327. goto case 4;
  328. }
  329. break;
  330. case 4:
  331. if (appendedEnumerator.MoveNext())
  332. {
  333. current = appendedEnumerator.Current;
  334. return true;
  335. }
  336. break;
  337. }
  338. break;
  339. }
  340. await DisposeAsync().ConfigureAwait(false);
  341. return false;
  342. }
  343. public override AppendPrepentAsyncIterator<TSource> Append(TSource item)
  344. {
  345. return new AppendPrependNAsyncIterator<TSource>(source, prepended, appended != null ? appended.Add(item) : new SingleLinkedNode<TSource>(item));
  346. }
  347. public override AppendPrepentAsyncIterator<TSource> Prepend(TSource item)
  348. {
  349. return new AppendPrependNAsyncIterator<TSource>(source, prepended != null ? prepended.Add(item) : new SingleLinkedNode<TSource>(item), appended);
  350. }
  351. public override async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  352. {
  353. var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
  354. if (count == -1)
  355. {
  356. return await AsyncEnumerableHelpers.ToArray(this, cancellationToken).ConfigureAwait(false);
  357. }
  358. var array = new TSource[count];
  359. var index = 0;
  360. for (var n = prepended; n != null; n = n.Linked)
  361. {
  362. array[index] = n.Item;
  363. ++index;
  364. }
  365. if (source is ICollection<TSource> sourceCollection)
  366. {
  367. sourceCollection.CopyTo(array, index);
  368. }
  369. else
  370. {
  371. var en = source.GetAsyncEnumerator();
  372. try
  373. {
  374. while (await en.MoveNextAsync(cancellationToken)
  375. .ConfigureAwait(false))
  376. {
  377. array[index] = en.Current;
  378. ++index;
  379. }
  380. }
  381. finally
  382. {
  383. await en.DisposeAsync().ConfigureAwait(false);
  384. }
  385. }
  386. index = array.Length;
  387. for (var n = appended; n != null; n = n.Linked)
  388. {
  389. --index;
  390. array[index] = n.Item;
  391. }
  392. return array;
  393. }
  394. public override async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  395. {
  396. var count = await GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
  397. var list = count == -1 ? new List<TSource>() : new List<TSource>(count);
  398. for (var n = prepended; n != null; n = n.Linked)
  399. {
  400. list.Add(n.Item);
  401. }
  402. var en = source.GetAsyncEnumerator();
  403. try
  404. {
  405. while (await en.MoveNextAsync(cancellationToken)
  406. .ConfigureAwait(false))
  407. {
  408. list.Add(en.Current);
  409. }
  410. }
  411. finally
  412. {
  413. await en.DisposeAsync().ConfigureAwait(false);
  414. }
  415. if (appended != null)
  416. {
  417. using (var en2 = appended.GetEnumerator())
  418. {
  419. while (en2.MoveNext())
  420. {
  421. list.Add(en2.Current);
  422. }
  423. }
  424. }
  425. return list;
  426. }
  427. public override async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  428. {
  429. if (source is IIListProvider<TSource> listProv)
  430. {
  431. var count = await listProv.GetCountAsync(onlyIfCheap, cancellationToken).ConfigureAwait(false);
  432. return count == -1 ? -1 : count + (appended == null ? 0 : appended.Count) + (prepended == null ? 0 : prepended.Count);
  433. }
  434. return !onlyIfCheap || source is ICollection<TSource> ? await source.Count(cancellationToken).ConfigureAwait(false) + (appended == null ? 0 : appended.Count) + (prepended == null ? 0 : prepended.Count) : -1;
  435. }
  436. }
  437. }
  438. }