Skip to content

Commit eb95c41

Browse files
author
LoneWandererProductions
committed
improve my linq
1 parent 28b5f07 commit eb95c41

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

ExtendedSystemObjects/FastLinq.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using System;
1313
using System.Collections.Generic;
14+
using System.Runtime.CompilerServices;
1415

1516
namespace ExtendedSystemObjects
1617
{
@@ -136,6 +137,7 @@ public static TResult AggregateFast<T, TResult>(
136137
/// <typeparam name="T">The element type of the collection.</typeparam>
137138
/// <param name="span">The source span to check.</param>
138139
/// <returns>True if the collection contains at least one element; otherwise false.</returns>
140+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
139141
public static bool AnyFast<T>(this ReadOnlySpan<T> span)
140142
{
141143
return span.Length != 0;
@@ -149,11 +151,38 @@ public static bool AnyFast<T>(this ReadOnlySpan<T> span)
149151
/// <typeparam name="T">The element type of the collection.</typeparam>
150152
/// <param name="span">The source span to check.</param>
151153
/// <returns>True if the collection contains at least one element; otherwise false.</returns>
154+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
152155
public static bool AnyFast<T>(this Span<T> span)
153156
{
154157
return span.Length != 0;
155158
}
156159

160+
/// <summary>
161+
/// High-performance 'Any' check for Lists.
162+
/// Avoids the IEnumerable/IEnumerator allocation.
163+
/// </summary>
164+
/// <typeparam name="T">The element type of the collection.</typeparam>
165+
/// <param name="list">The list.</param>
166+
/// <returns>True if the collection contains at least one element; otherwise false.</returns>
167+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
168+
public static bool AnyFast<T>(this List<T> list)
169+
{
170+
return list != null && list.Count != 0;
171+
}
172+
173+
/// <summary>
174+
/// High-performance 'Any' check for Arrays.
175+
/// Zero allocations, direct length check.
176+
/// </summary>
177+
/// <typeparam name="T">The element type of the collection.</typeparam>
178+
/// <param name="array">The array.</param>
179+
/// <returns>True if the collection contains at least one element; otherwise false.</returns>
180+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
181+
public static bool AnyFast<T>(this T[] array)
182+
{
183+
return array != null && array.Length != 0;
184+
}
185+
157186
/// <summary>
158187
/// Returns true if all elements in the collection satisfy the given predicate.
159188
/// Zero allocations and works on any collection type.
@@ -221,6 +250,7 @@ public static bool AllFast<T>(this List<T> list, Func<T, bool> predicate)
221250
/// <typeparam name="T">The element type of the collection.</typeparam>
222251
/// <param name="span">The source span to count.</param>
223252
/// <returns>The number of elements in the collection.</returns>
253+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
224254
public static int CountFast<T>(this ReadOnlySpan<T> span)
225255
{
226256
return span.Length;

0 commit comments

Comments
 (0)