ToLookup.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. namespace Cysharp.Threading.Tasks.Linq
  8. {
  9. public static partial class UniTaskAsyncEnumerable
  10. {
  11. public static UniTask<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default)
  12. {
  13. Error.ThrowArgumentNullException(source, nameof(source));
  14. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  15. return ToLookup.ToLookupAsync(source, keySelector, EqualityComparer<TKey>.Default, cancellationToken);
  16. }
  17. public static UniTask<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  18. {
  19. Error.ThrowArgumentNullException(source, nameof(source));
  20. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  21. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  22. return ToLookup.ToLookupAsync(source, keySelector, comparer, cancellationToken);
  23. }
  24. public static UniTask<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default)
  25. {
  26. Error.ThrowArgumentNullException(source, nameof(source));
  27. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  28. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  29. return ToLookup.ToLookupAsync(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  30. }
  31. public static UniTask<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  32. {
  33. Error.ThrowArgumentNullException(source, nameof(source));
  34. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  35. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  36. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  37. return ToLookup.ToLookupAsync(source, keySelector, elementSelector, comparer, cancellationToken);
  38. }
  39. public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  40. {
  41. Error.ThrowArgumentNullException(source, nameof(source));
  42. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  43. return ToLookup.ToLookupAwaitAsync(source, keySelector, EqualityComparer<TKey>.Default, cancellationToken);
  44. }
  45. public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  46. {
  47. Error.ThrowArgumentNullException(source, nameof(source));
  48. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  49. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  50. return ToLookup.ToLookupAwaitAsync(source, keySelector, comparer, cancellationToken);
  51. }
  52. public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
  53. {
  54. Error.ThrowArgumentNullException(source, nameof(source));
  55. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  56. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  57. return ToLookup.ToLookupAwaitAsync(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  58. }
  59. public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  60. {
  61. Error.ThrowArgumentNullException(source, nameof(source));
  62. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  63. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  64. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  65. return ToLookup.ToLookupAwaitAsync(source, keySelector, elementSelector, comparer, cancellationToken);
  66. }
  67. public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  68. {
  69. Error.ThrowArgumentNullException(source, nameof(source));
  70. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  71. return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, EqualityComparer<TKey>.Default, cancellationToken);
  72. }
  73. public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  74. {
  75. Error.ThrowArgumentNullException(source, nameof(source));
  76. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  77. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  78. return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, comparer, cancellationToken);
  79. }
  80. public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
  81. {
  82. Error.ThrowArgumentNullException(source, nameof(source));
  83. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  84. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  85. return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  86. }
  87. public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  88. {
  89. Error.ThrowArgumentNullException(source, nameof(source));
  90. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  91. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  92. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  93. return ToLookup.ToLookupAwaitWithCancellationAsync(source, keySelector, elementSelector, comparer, cancellationToken);
  94. }
  95. }
  96. internal static class ToLookup
  97. {
  98. internal static async UniTask<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  99. {
  100. var pool = ArrayPool<TSource>.Shared;
  101. var array = pool.Rent(16);
  102. var e = source.GetAsyncEnumerator(cancellationToken);
  103. try
  104. {
  105. var i = 0;
  106. while (await e.MoveNextAsync())
  107. {
  108. ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
  109. array[i++] = e.Current;
  110. }
  111. if (i == 0)
  112. {
  113. return Lookup<TKey, TSource>.CreateEmpty();
  114. }
  115. else
  116. {
  117. return Lookup<TKey, TSource>.Create(new ArraySegment<TSource>(array, 0, i), keySelector, comparer);
  118. }
  119. }
  120. finally
  121. {
  122. pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<TSource>());
  123. if (e != null)
  124. {
  125. await e.DisposeAsync();
  126. }
  127. }
  128. }
  129. internal static async UniTask<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  130. {
  131. var pool = ArrayPool<TSource>.Shared;
  132. var array = pool.Rent(16);
  133. IUniTaskAsyncEnumerator<TSource> e = default;
  134. try
  135. {
  136. e = source.GetAsyncEnumerator(cancellationToken);
  137. var i = 0;
  138. while (await e.MoveNextAsync())
  139. {
  140. ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
  141. array[i++] = e.Current;
  142. }
  143. if (i == 0)
  144. {
  145. return Lookup<TKey, TElement>.CreateEmpty();
  146. }
  147. else
  148. {
  149. return Lookup<TKey, TElement>.Create(new ArraySegment<TSource>(array, 0, i), keySelector, elementSelector, comparer);
  150. }
  151. }
  152. finally
  153. {
  154. pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<TSource>());
  155. if (e != null)
  156. {
  157. await e.DisposeAsync();
  158. }
  159. }
  160. }
  161. // with await
  162. internal static async UniTask<ILookup<TKey, TSource>> ToLookupAwaitAsync<TSource, TKey>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  163. {
  164. var pool = ArrayPool<TSource>.Shared;
  165. var array = pool.Rent(16);
  166. IUniTaskAsyncEnumerator<TSource> e = default;
  167. try
  168. {
  169. e = source.GetAsyncEnumerator(cancellationToken);
  170. var i = 0;
  171. while (await e.MoveNextAsync())
  172. {
  173. ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
  174. array[i++] = e.Current;
  175. }
  176. if (i == 0)
  177. {
  178. return Lookup<TKey, TSource>.CreateEmpty();
  179. }
  180. else
  181. {
  182. return await Lookup<TKey, TSource>.CreateAsync(new ArraySegment<TSource>(array, 0, i), keySelector, comparer);
  183. }
  184. }
  185. finally
  186. {
  187. pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<TSource>());
  188. if (e != null)
  189. {
  190. await e.DisposeAsync();
  191. }
  192. }
  193. }
  194. internal static async UniTask<ILookup<TKey, TElement>> ToLookupAwaitAsync<TSource, TKey, TElement>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  195. {
  196. var pool = ArrayPool<TSource>.Shared;
  197. var array = pool.Rent(16);
  198. IUniTaskAsyncEnumerator<TSource> e = default;
  199. try
  200. {
  201. e = source.GetAsyncEnumerator(cancellationToken);
  202. var i = 0;
  203. while (await e.MoveNextAsync())
  204. {
  205. ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
  206. array[i++] = e.Current;
  207. }
  208. if (i == 0)
  209. {
  210. return Lookup<TKey, TElement>.CreateEmpty();
  211. }
  212. else
  213. {
  214. return await Lookup<TKey, TElement>.CreateAsync(new ArraySegment<TSource>(array, 0, i), keySelector, elementSelector, comparer);
  215. }
  216. }
  217. finally
  218. {
  219. pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<TSource>());
  220. if (e != null)
  221. {
  222. await e.DisposeAsync();
  223. }
  224. }
  225. }
  226. // with cancellation
  227. internal static async UniTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsync<TSource, TKey>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  228. {
  229. var pool = ArrayPool<TSource>.Shared;
  230. var array = pool.Rent(16);
  231. IUniTaskAsyncEnumerator<TSource> e = default;
  232. try
  233. {
  234. e = source.GetAsyncEnumerator(cancellationToken);
  235. var i = 0;
  236. while (await e.MoveNextAsync())
  237. {
  238. ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
  239. array[i++] = e.Current;
  240. }
  241. if (i == 0)
  242. {
  243. return Lookup<TKey, TSource>.CreateEmpty();
  244. }
  245. else
  246. {
  247. return await Lookup<TKey, TSource>.CreateAsync(new ArraySegment<TSource>(array, 0, i), keySelector, comparer, cancellationToken);
  248. }
  249. }
  250. finally
  251. {
  252. pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<TSource>());
  253. if (e != null)
  254. {
  255. await e.DisposeAsync();
  256. }
  257. }
  258. }
  259. internal static async UniTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsync<TSource, TKey, TElement>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  260. {
  261. var pool = ArrayPool<TSource>.Shared;
  262. var array = pool.Rent(16);
  263. IUniTaskAsyncEnumerator<TSource> e = default;
  264. try
  265. {
  266. e = source.GetAsyncEnumerator(cancellationToken);
  267. var i = 0;
  268. while (await e.MoveNextAsync())
  269. {
  270. ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
  271. array[i++] = e.Current;
  272. }
  273. if (i == 0)
  274. {
  275. return Lookup<TKey, TElement>.CreateEmpty();
  276. }
  277. else
  278. {
  279. return await Lookup<TKey, TElement>.CreateAsync(new ArraySegment<TSource>(array, 0, i), keySelector, elementSelector, comparer, cancellationToken);
  280. }
  281. }
  282. finally
  283. {
  284. pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType<TSource>());
  285. if (e != null)
  286. {
  287. await e.DisposeAsync();
  288. }
  289. }
  290. }
  291. // Lookup
  292. class Lookup<TKey, TElement> : ILookup<TKey, TElement>
  293. {
  294. static readonly Lookup<TKey, TElement> empty = new Lookup<TKey, TElement>(new Dictionary<TKey, Grouping<TKey, TElement>>());
  295. // original lookup keeps order but this impl does not(dictionary not guarantee)
  296. readonly Dictionary<TKey, Grouping<TKey, TElement>> dict;
  297. Lookup(Dictionary<TKey, Grouping<TKey, TElement>> dict)
  298. {
  299. this.dict = dict;
  300. }
  301. public static Lookup<TKey, TElement> CreateEmpty()
  302. {
  303. return empty;
  304. }
  305. public static Lookup<TKey, TElement> Create(ArraySegment<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer)
  306. {
  307. var dict = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer);
  308. var arr = source.Array;
  309. var c = source.Count;
  310. for (int i = source.Offset; i < c; i++)
  311. {
  312. var key = keySelector(arr[i]);
  313. if (!dict.TryGetValue(key, out var list))
  314. {
  315. list = new Grouping<TKey, TElement>(key);
  316. dict[key] = list;
  317. }
  318. list.Add(arr[i]);
  319. }
  320. return new Lookup<TKey, TElement>(dict);
  321. }
  322. public static Lookup<TKey, TElement> Create<TSource>(ArraySegment<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  323. {
  324. var dict = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer);
  325. var arr = source.Array;
  326. var c = source.Count;
  327. for (int i = source.Offset; i < c; i++)
  328. {
  329. var key = keySelector(arr[i]);
  330. var elem = elementSelector(arr[i]);
  331. if (!dict.TryGetValue(key, out var list))
  332. {
  333. list = new Grouping<TKey, TElement>(key);
  334. dict[key] = list;
  335. }
  336. list.Add(elem);
  337. }
  338. return new Lookup<TKey, TElement>(dict);
  339. }
  340. public static async UniTask<Lookup<TKey, TElement>> CreateAsync(ArraySegment<TElement> source, Func<TElement, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  341. {
  342. var dict = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer);
  343. var arr = source.Array;
  344. var c = source.Count;
  345. for (int i = source.Offset; i < c; i++)
  346. {
  347. var key = await keySelector(arr[i]);
  348. if (!dict.TryGetValue(key, out var list))
  349. {
  350. list = new Grouping<TKey, TElement>(key);
  351. dict[key] = list;
  352. }
  353. list.Add(arr[i]);
  354. }
  355. return new Lookup<TKey, TElement>(dict);
  356. }
  357. public static async UniTask<Lookup<TKey, TElement>> CreateAsync<TSource>(ArraySegment<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
  358. {
  359. var dict = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer);
  360. var arr = source.Array;
  361. var c = source.Count;
  362. for (int i = source.Offset; i < c; i++)
  363. {
  364. var key = await keySelector(arr[i]);
  365. var elem = await elementSelector(arr[i]);
  366. if (!dict.TryGetValue(key, out var list))
  367. {
  368. list = new Grouping<TKey, TElement>(key);
  369. dict[key] = list;
  370. }
  371. list.Add(elem);
  372. }
  373. return new Lookup<TKey, TElement>(dict);
  374. }
  375. public static async UniTask<Lookup<TKey, TElement>> CreateAsync(ArraySegment<TElement> source, Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  376. {
  377. var dict = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer);
  378. var arr = source.Array;
  379. var c = source.Count;
  380. for (int i = source.Offset; i < c; i++)
  381. {
  382. var key = await keySelector(arr[i], cancellationToken);
  383. if (!dict.TryGetValue(key, out var list))
  384. {
  385. list = new Grouping<TKey, TElement>(key);
  386. dict[key] = list;
  387. }
  388. list.Add(arr[i]);
  389. }
  390. return new Lookup<TKey, TElement>(dict);
  391. }
  392. public static async UniTask<Lookup<TKey, TElement>> CreateAsync<TSource>(ArraySegment<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  393. {
  394. var dict = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer);
  395. var arr = source.Array;
  396. var c = source.Count;
  397. for (int i = source.Offset; i < c; i++)
  398. {
  399. var key = await keySelector(arr[i], cancellationToken);
  400. var elem = await elementSelector(arr[i], cancellationToken);
  401. if (!dict.TryGetValue(key, out var list))
  402. {
  403. list = new Grouping<TKey, TElement>(key);
  404. dict[key] = list;
  405. }
  406. list.Add(elem);
  407. }
  408. return new Lookup<TKey, TElement>(dict);
  409. }
  410. public IEnumerable<TElement> this[TKey key] => dict.TryGetValue(key, out var g) ? g : Enumerable.Empty<TElement>();
  411. public int Count => dict.Count;
  412. public bool Contains(TKey key)
  413. {
  414. return dict.ContainsKey(key);
  415. }
  416. public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
  417. {
  418. return dict.Values.GetEnumerator();
  419. }
  420. IEnumerator IEnumerable.GetEnumerator()
  421. {
  422. return dict.Values.GetEnumerator();
  423. }
  424. }
  425. class Grouping<TKey, TElement> : IGrouping<TKey, TElement> // , IUniTaskAsyncGrouping<TKey, TElement>
  426. {
  427. readonly List<TElement> elements;
  428. public TKey Key { get; private set; }
  429. public Grouping(TKey key)
  430. {
  431. this.Key = key;
  432. this.elements = new List<TElement>();
  433. }
  434. public void Add(TElement value)
  435. {
  436. elements.Add(value);
  437. }
  438. public IEnumerator<TElement> GetEnumerator()
  439. {
  440. return elements.GetEnumerator();
  441. }
  442. IEnumerator IEnumerable.GetEnumerator()
  443. {
  444. return elements.GetEnumerator();
  445. }
  446. public IUniTaskAsyncEnumerator<TElement> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  447. {
  448. return this.ToUniTaskAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
  449. }
  450. public override string ToString()
  451. {
  452. return "Key: " + Key + ", Count: " + elements.Count;
  453. }
  454. }
  455. }
  456. }