Skip to content

Commit edd59cc

Browse files
committed
[First] Complete first extension.
1 parent 87efb30 commit edd59cc

File tree

5 files changed

+410
-83
lines changed

5 files changed

+410
-83
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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 First<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 first = default;
17+
if (TryFirst(collection, ref first, x => x))
18+
return first;
19+
throw new Exception("No Elements");
20+
}
21+
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
public static T First<T, TEnumerator>(this IRefStructCollection<T, TEnumerator> collection)
24+
where TEnumerator : struct, IRefStructEnumerator<T>
25+
{
26+
T first = default;
27+
if (TryFirst(collection, ref first))
28+
return first;
29+
throw new Exception("No Elements");
30+
}
31+
32+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
33+
public static T First<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 first = default;
38+
if (TryFirst(collection, predicate, ref first, x => x))
39+
return first;
40+
throw new Exception("No Elements");
41+
}
42+
43+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
44+
public static T First<T, TEnumerator>(this IRefStructCollection<T, TEnumerator> collection, Func<T, bool> predicate)
45+
where TEnumerator : struct, IRefStructEnumerator<T>
46+
{
47+
T first = default;
48+
if (TryFirst(collection, predicate, ref first))
49+
return first;
50+
throw new Exception("No Elements");
51+
}
52+
53+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
54+
public static T First<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 first = default;
60+
if (TryFirst(collection, ref predicate, ref first, x => x))
61+
return first;
62+
throw new Exception("No Elements");
63+
}
64+
65+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
66+
public static bool TryFirst<T, TCollection, TEnumerator>(this TCollection collection, ref T first, 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+
ref var result = ref collection.Get(0);
73+
first = result;
74+
return true;
75+
}
76+
77+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
78+
public static bool TryFirst<T, TEnumerator>(this IRefStructCollection<T, TEnumerator> collection, ref T first)
79+
where TEnumerator : struct, IRefStructEnumerator<T>
80+
{
81+
if (collection.Count == 0)
82+
return false;
83+
ref var result = ref collection.Get(0);
84+
first = result;
85+
return true;
86+
}
87+
88+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
89+
public static bool TryFirst<T, TCollection, TEnumerator>(this TCollection collection, Func<T, bool> predicate, ref T first, Func<TCollection, IRefStructCollection<T, TEnumerator>> _)
90+
where TEnumerator : struct, IRefStructEnumerator<T>
91+
where TCollection : IRefStructCollection<T, TEnumerator>
92+
{
93+
if (collection.Count == 0)
94+
return false;
95+
for (int i = 0; i < collection.Count; i++)
96+
{
97+
ref var result = ref collection.Get(i);
98+
if (predicate(result))
99+
{
100+
first = result;
101+
return true;
102+
}
103+
}
104+
return false;
105+
}
106+
107+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
108+
public static bool TryFirst<T, TEnumerator>(this IRefStructCollection<T, TEnumerator> collection, Func<T, bool> predicate, ref T first)
109+
where TEnumerator : struct, IRefStructEnumerator<T>
110+
{
111+
if (collection.Count == 0)
112+
return false;
113+
for (int i = 0; i < collection.Count; i++)
114+
{
115+
ref var result = ref collection.Get(i);
116+
if (predicate(result))
117+
{
118+
first = result;
119+
return true;
120+
}
121+
}
122+
return false;
123+
}
124+
125+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
126+
public static bool TryFirst<T, TCollection, TEnumerator, TFunc>(this TCollection collection, ref TFunc predicate, ref T first, Func<TCollection, IRefStructCollection<T, TEnumerator>> _)
127+
where TEnumerator : struct, IRefStructEnumerator<T>
128+
where TCollection : IRefStructCollection<T, TEnumerator>
129+
where TFunc : struct, IInFunction<T, bool>
130+
{
131+
if (collection.Count == 0)
132+
return false;
133+
134+
for (int i = 0; i < collection.Count; i++)
135+
{
136+
ref var result = ref collection.Get(i);
137+
if (predicate.Eval(in result))
138+
{
139+
first = result;
140+
return true;
141+
}
142+
}
143+
return false;
144+
}
145+
146+
}
147+
}

0 commit comments

Comments
 (0)