using System; namespace MemoryPools.Collections.Linq { public static partial class PoolingEnumerable { /// /// Gets first element from sequence. Complexity = O(1) /// public static T First(this IPoolingEnumerable source) { var enumerator = source.GetEnumerator(); var hasItems = enumerator.MoveNext(); if (!hasItems) { throw new InvalidOperationException("Sequence is empty"); } var element = enumerator.Current; enumerator.Dispose(); return element; } /// /// Gets first element from sequence by given . Complexity = O(1) - O(N) /// public static T First(this IPoolingEnumerable source, Func condition) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (condition(enumerator.Current)) { var item = enumerator.Current; enumerator.Dispose(); return item; } } enumerator.Dispose(); throw new InvalidOperationException("Sequence is empty"); } /// /// Gets first element from sequence by given . Complexity = O(1) - O(N) /// public static T First(this IPoolingEnumerable source, TContext context, Func condition) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (!condition(context, enumerator.Current)) continue; var item = enumerator.Current; enumerator.Dispose(); return item; } enumerator.Dispose(); throw new InvalidOperationException("Sequence is empty"); } /// /// Gets first element from sequence. Complexity = O(1) /// public static T FirstOrDefault(this IPoolingEnumerable source) { var enumerator = source.GetEnumerator(); var hasItem = enumerator.MoveNext(); var item= hasItem ? enumerator.Current : default; enumerator.Dispose(); return item; } /// /// Gets first element from sequence by given . Complexity = O(1) - O(N) /// public static T FirstOrDefault(this IPoolingEnumerable source, Func condition) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (!condition(enumerator.Current)) continue; var elem = enumerator.Current; enumerator.Dispose(); return elem; } enumerator.Dispose(); return default; } /// /// Gets first element from sequence by given . Complexity = O(1) - O(N) /// public static T FirstOrDefault(this IPoolingEnumerable source, TContext context, Func condition) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { if (!condition(context, enumerator.Current)) continue; var elem = enumerator.Current; enumerator.Dispose(); return elem; } enumerator.Dispose(); return default; } } }