2021-03-02 23:40:53 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
|
|
namespace BililiveRecorder.Flv
|
|
|
|
{
|
|
|
|
public static class LinqExtensions
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static bool Any2<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, bool> predicate)
|
|
|
|
{
|
|
|
|
using var iterator = source.GetEnumerator();
|
|
|
|
|
|
|
|
if (!iterator.MoveNext())
|
|
|
|
return false;
|
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
var previousItem = iterator.Current;
|
2021-03-02 23:40:53 +08:00
|
|
|
|
|
|
|
while (iterator.MoveNext())
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
var currentItem = iterator.Current;
|
2021-03-02 23:40:53 +08:00
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
if (predicate(previousItem, currentItem))
|
2021-03-02 23:40:53 +08:00
|
|
|
return true;
|
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
previousItem = currentItem;
|
2021-03-02 23:40:53 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-29 23:51:06 +08:00
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static bool Any2<TIn, TFunction>(this IEnumerable<TIn> source, ref TFunction function)
|
|
|
|
where TFunction : ITwoInputFunction<TIn, bool>
|
|
|
|
{
|
|
|
|
using var iterator = source.GetEnumerator();
|
|
|
|
|
|
|
|
if (!iterator.MoveNext())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var previousItem = iterator.Current;
|
|
|
|
|
|
|
|
while (iterator.MoveNext())
|
|
|
|
{
|
|
|
|
var currentItem = iterator.Current;
|
|
|
|
|
|
|
|
if (function.Eval(previousItem, currentItem))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
previousItem = currentItem;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface ITwoInputFunction<in TIn, out TOut>
|
|
|
|
{
|
|
|
|
TOut Eval(TIn a, TIn b);
|
2021-03-02 23:40:53 +08:00
|
|
|
}
|
|
|
|
}
|