AppendPrepend.cs 17 KB

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