1+ // ReSharper disable once CheckNamespace
2+
3+ using System ;
4+ using System . Runtime . CompilerServices ;
5+
6+ // ReSharper disable once CheckNamespace
7+ namespace StructLinq
8+ {
9+ public static partial class StructEnumerable
10+ {
11+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
12+ private static ref T RefInnerFirst < T , TEnumerator > ( TEnumerator enumerator )
13+ where TEnumerator : struct , IRefStructEnumerator < T >
14+ {
15+ if ( enumerator . MoveNext ( ) )
16+ {
17+ ref var current = ref enumerator . Current ;
18+ enumerator . Dispose ( ) ;
19+ return ref current ;
20+ }
21+ enumerator . Dispose ( ) ;
22+ throw new Exception ( "No Elements" ) ;
23+ }
24+
25+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
26+ private static ref T RefInnerFirst < T , TEnumerator > ( TEnumerator enumerator , Func < T , bool > predicate )
27+ where TEnumerator : struct , IRefStructEnumerator < T >
28+ {
29+ while ( enumerator . MoveNext ( ) )
30+ {
31+ ref var current = ref enumerator . Current ;
32+ if ( predicate ( current ) )
33+ {
34+ enumerator . Dispose ( ) ;
35+ return ref current ;
36+ }
37+ }
38+ enumerator . Dispose ( ) ;
39+ throw new Exception ( "No Match" ) ;
40+ }
41+
42+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
43+ private static ref T RefInnerFirst < T , TEnumerator , TFunc > ( TEnumerator enumerator , ref TFunc predicate )
44+ where TEnumerator : struct , IRefStructEnumerator < T >
45+ where TFunc : struct , IInFunction < T , bool >
46+ {
47+ while ( enumerator . MoveNext ( ) )
48+ {
49+ ref var current = ref enumerator . Current ;
50+ if ( predicate . Eval ( in current ) )
51+ {
52+ enumerator . Dispose ( ) ;
53+ return ref current ;
54+ }
55+ }
56+ enumerator . Dispose ( ) ;
57+ throw new Exception ( "No Match" ) ;
58+ }
59+
60+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
61+ public static ref T First < T , TEnumerable , TEnumerator > ( this TEnumerable enumerable , Func < TEnumerable , IRefStructEnumerable < T , TEnumerator > > _ )
62+ where TEnumerator : struct , IRefStructEnumerator < T >
63+ where TEnumerable : IRefStructEnumerable < T , TEnumerator >
64+ {
65+ var enumerator = enumerable . GetEnumerator ( ) ;
66+ return ref RefInnerFirst < T , TEnumerator > ( enumerator ) ;
67+ }
68+
69+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
70+ public static ref T First < T , TEnumerator > ( this IRefStructEnumerable < T , TEnumerator > enumerable )
71+ where TEnumerator : struct , IRefStructEnumerator < T >
72+ {
73+ var enumerator = enumerable . GetEnumerator ( ) ;
74+ return ref RefInnerFirst < T , TEnumerator > ( enumerator ) ;
75+ }
76+
77+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
78+ public static ref T First < T , TEnumerable , TEnumerator > ( this TEnumerable enumerable , Func < T , bool > predicate , Func < TEnumerable , IRefStructEnumerable < T , TEnumerator > > _ )
79+ where TEnumerator : struct , IRefStructEnumerator < T >
80+ where TEnumerable : IRefStructEnumerable < T , TEnumerator >
81+ {
82+ var enumerator = enumerable . GetEnumerator ( ) ;
83+ return ref RefInnerFirst ( enumerator , predicate ) ;
84+ }
85+
86+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
87+ public static ref T First < T , TEnumerator > ( this IRefStructEnumerable < T , TEnumerator > enumerable , Func < T , bool > predicate )
88+ where TEnumerator : struct , IRefStructEnumerator < T >
89+ {
90+ var enumerator = enumerable . GetEnumerator ( ) ;
91+ return ref RefInnerFirst < T , TEnumerator > ( enumerator , predicate ) ;
92+ }
93+
94+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
95+ public static ref T First < T , TEnumerable , TEnumerator , TFunc > ( this TEnumerable enumerable , ref TFunc predicate , Func < TEnumerable , IRefStructEnumerable < T , TEnumerator > > _ )
96+ where TEnumerator : struct , IRefStructEnumerator < T >
97+ where TEnumerable : IRefStructEnumerable < T , TEnumerator >
98+ where TFunc : struct , IInFunction < T , bool >
99+ {
100+ var enumerator = enumerable . GetEnumerator ( ) ;
101+ return ref RefInnerFirst < T , TEnumerator , TFunc > ( enumerator , ref predicate ) ;
102+ }
103+ }
104+ }
0 commit comments