|
| 1 | + |
| 2 | + |
| 3 | +// ReSharper disable once CheckNamespace |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +// ReSharper disable once CheckNamespace |
| 9 | +namespace StructLinq |
| 10 | +{ |
| 11 | + public static partial class StructEnumerable |
| 12 | + { |
| 13 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 14 | + private static bool RefInnerAll<T, TEnumerator>(ref TEnumerator enumerator, Func<T, bool> predicate) |
| 15 | + where TEnumerator : struct, IRefStructEnumerator<T> |
| 16 | + { |
| 17 | + while (enumerator.MoveNext()) |
| 18 | + { |
| 19 | + ref var current = ref enumerator.Current; |
| 20 | + if (!predicate(current)) |
| 21 | + { |
| 22 | + enumerator.Dispose(); |
| 23 | + return false; |
| 24 | + } |
| 25 | + } |
| 26 | + enumerator.Dispose(); |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 31 | + private static bool RefInnerAll<T, TEnumerator, TFunction>(ref TEnumerator enumerator, ref TFunction predicate) |
| 32 | + where TEnumerator : struct, IRefStructEnumerator<T> |
| 33 | + where TFunction : IInFunction<T, bool> |
| 34 | + { |
| 35 | + while (enumerator.MoveNext()) |
| 36 | + { |
| 37 | + ref var current = ref enumerator.Current; |
| 38 | + if (!predicate.Eval(in current)) |
| 39 | + { |
| 40 | + enumerator.Dispose(); |
| 41 | + return false; |
| 42 | + } |
| 43 | + } |
| 44 | + enumerator.Dispose(); |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 49 | + public static bool All<T, TEnumerable, TEnumerator>(this TEnumerable enumerable, Func<T, bool> predicate, Func<TEnumerable, IRefStructEnumerable<T, TEnumerator>> _) |
| 50 | + where TEnumerable : IRefStructEnumerable<T, TEnumerator> |
| 51 | + where TEnumerator : struct, IRefStructEnumerator<T> |
| 52 | + { |
| 53 | + var enumerator = enumerable.GetEnumerator(); |
| 54 | + return RefInnerAll(ref enumerator, predicate); |
| 55 | + } |
| 56 | + |
| 57 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 58 | + public static bool All<T, TEnumerator>(this IRefStructEnumerable<T, TEnumerator> enumerable, Func<T, bool> predicate) |
| 59 | + where TEnumerator : struct, IRefStructEnumerator<T> |
| 60 | + { |
| 61 | + var enumerator = enumerable.GetEnumerator(); |
| 62 | + return RefInnerAll(ref enumerator, predicate); |
| 63 | + } |
| 64 | + |
| 65 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 66 | + public static bool All<T, TEnumerable, TEnumerator, TFunction>(this TEnumerable enumerable, ref TFunction predicate, Func<TEnumerable, IRefStructEnumerable<T, TEnumerator>> _) |
| 67 | + where TEnumerable : IRefStructEnumerable<T, TEnumerator> |
| 68 | + where TEnumerator : struct, IRefStructEnumerator<T> |
| 69 | + where TFunction : IInFunction<T, bool> |
| 70 | + { |
| 71 | + var enumerator = enumerable.GetEnumerator(); |
| 72 | + return RefInnerAll<T, TEnumerator, TFunction>(ref enumerator, ref predicate); |
| 73 | + } |
| 74 | + |
| 75 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 76 | + public static bool All<T, TEnumerator>(this IRefStructEnumerable<T, TEnumerator> enumerable, IInFunction<T, bool> predicate) |
| 77 | + where TEnumerator : struct, IRefStructEnumerator<T> |
| 78 | + { |
| 79 | + var enumerator = enumerable.GetEnumerator(); |
| 80 | + return RefInnerAll<T, TEnumerator, IInFunction<T, bool>>(ref enumerator, ref predicate); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments