You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/MemoryPools/Collections/Linq/LastLastOrDefault.cs

99 lines
3.4 KiB

using System;
namespace MemoryPools.Collections.Linq
{
public static partial class PoolingEnumerable
{
public static T Last<T>(this IPoolingEnumerable<T> source)
{
var enumerator = source.GetEnumerator();
7 months ago
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<T>(this IPoolingEnumerable<T> source, Func<T, bool> condition)
{
var enumerator = source.GetEnumerator();
7 months ago
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<T, TContext>(this IPoolingEnumerable<T> source, TContext context, Func<TContext, T, bool> condition)
{
var enumerator = source.GetEnumerator();
7 months ago
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<T>(this IPoolingEnumerable<T> source)
{
var enumerator = source.GetEnumerator();
7 months ago
T element = default!;
var hasItems = false;
while (enumerator.MoveNext())
{
element = enumerator.Current;
hasItems = true;
}
enumerator.Dispose();
7 months ago
return hasItems ? element : default!;
}
public static T LastOrDefault<T>(this IPoolingEnumerable<T> source, Func<T, bool> condition)
{
var enumerator = source.GetEnumerator();
7 months ago
T element = default!;
var hasItems = false;
while (enumerator.MoveNext())
{
if (!condition(enumerator.Current)) continue;
element = enumerator.Current;
hasItems = true;
}
enumerator.Dispose();
7 months ago
return (hasItems ? element : default)!;
}
public static T LastOrDefault<T, TContext>(this IPoolingEnumerable<T> source, TContext context, Func<TContext, T, bool> condition)
{
var enumerator = source.GetEnumerator();
7 months ago
T element = default!;
var hasItems = false;
while (enumerator.MoveNext())
{
if (!condition(context, enumerator.Current)) continue;
element = enumerator.Current;
hasItems = true;
}
enumerator.Dispose();
7 months ago
return (hasItems ? element : default)!;
}
}
}

Powered by TurnKey Linux.