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+ public static T Last < T , TCollection , TEnumerator > ( this TCollection collection , Func < TCollection , IRefStructCollection < T , TEnumerator > > _ )
13+ where TEnumerator : struct , IRefStructEnumerator < T >
14+ where TCollection : IRefStructCollection < T , TEnumerator >
15+ {
16+ T last = default ;
17+ if ( TryLast ( collection , ref last , x=> x ) )
18+ return last ;
19+ throw new Exception ( "No Elements" ) ;
20+ }
21+
22+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
23+ public static T Last < T , TEnumerator > ( this IRefStructCollection < T , TEnumerator > collection )
24+ where TEnumerator : struct , IRefStructEnumerator < T >
25+ {
26+ T last = default ;
27+ if ( TryLast ( collection , ref last ) )
28+ return last ;
29+ throw new Exception ( "No Elements" ) ;
30+ }
31+
32+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
33+ public static T Last < T , TCollection , TEnumerator > ( this TCollection collection , Func < T , bool > predicate , Func < TCollection , IRefStructCollection < T , TEnumerator > > _ )
34+ where TEnumerator : struct , IRefStructEnumerator < T >
35+ where TCollection : IRefStructCollection < T , TEnumerator >
36+ {
37+ T last = default ;
38+ if ( TryLast ( collection , predicate , ref last , x=> x ) )
39+ return last ;
40+ throw new Exception ( "No Elements" ) ;
41+ }
42+
43+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
44+ public static T Last < T , TEnumerator > ( this IRefStructCollection < T , TEnumerator > collection , Func < T , bool > predicate )
45+ where TEnumerator : struct , IRefStructEnumerator < T >
46+ {
47+ T last = default ;
48+ if ( TryLast ( collection , predicate , ref last ) )
49+ return last ;
50+ throw new Exception ( "No Elements" ) ;
51+ }
52+
53+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
54+ public static T Last < T , TCollection , TEnumerator , TFunc > ( this TCollection collection , ref TFunc predicate , Func < TCollection , IRefStructCollection < T , TEnumerator > > _ )
55+ where TEnumerator : struct , IRefStructEnumerator < T >
56+ where TCollection : IRefStructCollection < T , TEnumerator >
57+ where TFunc : struct , IInFunction < T , bool >
58+ {
59+ T last = default ;
60+ if ( TryLast ( collection , ref predicate , ref last , x=> x ) )
61+ return last ;
62+ throw new Exception ( "No Elements" ) ;
63+ }
64+
65+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
66+ public static bool TryLast < T , TCollection , TEnumerator > ( this TCollection collection , ref T last , Func < TCollection , IRefStructCollection < T , TEnumerator > > _ )
67+ where TEnumerator : struct , IRefStructEnumerator < T >
68+ where TCollection : IRefStructCollection < T , TEnumerator >
69+ {
70+ if ( collection . Count == 0 )
71+ return false ;
72+ last = collection . Get ( collection . Count - 1 ) ;
73+ return true ;
74+ }
75+
76+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
77+ public static bool TryLast < T , TEnumerator > ( this IRefStructCollection < T , TEnumerator > collection , ref T last )
78+ where TEnumerator : struct , IRefStructEnumerator < T >
79+ {
80+ if ( collection . Count == 0 )
81+ return false ;
82+ last = collection . Get ( collection . Count - 1 ) ;
83+ return true ;
84+ }
85+
86+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
87+ public static bool TryLast < T , TCollection , TEnumerator > ( this TCollection collection , Func < T , bool > predicate , ref T last , Func < TCollection , IRefStructCollection < T , TEnumerator > > _ )
88+ where TEnumerator : struct , IRefStructEnumerator < T >
89+ where TCollection : IRefStructCollection < T , TEnumerator >
90+ {
91+ if ( collection . Count == 0 )
92+ return false ;
93+ for ( int i = collection . Count - 1 ; i >= 0 ; i -- )
94+ {
95+ last = collection . Get ( i ) ;
96+ if ( predicate ( last ) )
97+ return true ;
98+ }
99+ return false ;
100+ }
101+
102+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
103+ public static bool TryLast < T , TEnumerator > ( this IRefStructCollection < T , TEnumerator > collection , Func < T , bool > predicate , ref T last )
104+ where TEnumerator : struct , IRefStructEnumerator < T >
105+ {
106+ if ( collection . Count == 0 )
107+ return false ;
108+ for ( int i = collection . Count - 1 ; i >= 0 ; i -- )
109+ {
110+ last = collection . Get ( i ) ;
111+ if ( predicate ( last ) )
112+ return true ;
113+ }
114+ return false ;
115+ }
116+
117+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
118+ public static bool TryLast < T , TCollection , TEnumerator , TFunc > ( this TCollection collection , ref TFunc predicate , ref T last , Func < TCollection , IRefStructCollection < T , TEnumerator > > _ )
119+ where TEnumerator : struct , IRefStructEnumerator < T >
120+ where TCollection : IRefStructCollection < T , TEnumerator >
121+ where TFunc : struct , IInFunction < T , bool >
122+ {
123+ if ( collection . Count == 0 )
124+ return false ;
125+ for ( int i = collection . Count - 1 ; i >= 0 ; i -- )
126+ {
127+ last = collection . Get ( i ) ;
128+ if ( predicate . Eval ( in last ) )
129+ return true ;
130+ }
131+ return false ;
132+ }
133+
134+ }
135+ }
0 commit comments