using System; namespace MemoryPools.Collections.Linq { public static partial class PoolingEnumerable { public static T Last(this IPoolingEnumerable source) { var enumerator = source.GetEnumerator(); T element = default!; var hasItems = false; while (enumerator.MoveNext()) { element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return hasItems ? element : throw new InvalidOperationException("Sequence is empty"); } public static T Last(this IPoolingEnumerable source, Func condition) { var enumerator = source.GetEnumerator(); T element = default!; var hasItems = false; while (enumerator.MoveNext()) { if (!condition(enumerator.Current)) continue; element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return hasItems ? element : throw new InvalidOperationException("Sequence is empty"); } public static T Last(this IPoolingEnumerable source, TContext context, Func condition) { var enumerator = source.GetEnumerator(); T element = default!; var hasItems = false; while (enumerator.MoveNext()) { if (!condition(context, enumerator.Current)) continue; element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return hasItems ? element : throw new InvalidOperationException("Sequence is empty"); } public static T LastOrDefault(this IPoolingEnumerable source) { var enumerator = source.GetEnumerator(); T element = default!; var hasItems = false; while (enumerator.MoveNext()) { element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return hasItems ? element : default!; } public static T LastOrDefault(this IPoolingEnumerable source, Func condition) { var enumerator = source.GetEnumerator(); T element = default!; var hasItems = false; while (enumerator.MoveNext()) { if (!condition(enumerator.Current)) continue; element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return (hasItems ? element : default)!; } public static T LastOrDefault(this IPoolingEnumerable source, TContext context, Func condition) { var enumerator = source.GetEnumerator(); T element = default!; var hasItems = false; while (enumerator.MoveNext()) { if (!condition(context, enumerator.Current)) continue; element = enumerator.Current; hasItems = true; } enumerator.Dispose(); return (hasItems ? element : default)!; } } }