Skip to content

Commit

Permalink
Change extension methods to take in an IList instead of IEnumerable a…
Browse files Browse the repository at this point in the history
…s this can avoid enumerator allocation
  • Loading branch information
ToddGrun committed Jul 30, 2024
1 parent f129eeb commit 0fd3911
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions build/Shared/SharedExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ public static void AppendInt(this StringBuilder sb, int value)
}
}

public static int SingleIndex<T>(this IEnumerable<T> lst, Predicate<T> isMatch)
public static int SingleIndex<T>(this IList<T> lst, Predicate<T> isMatch)
{
var index = 0;
var foundIndex = -1;

foreach (var item in lst)
for (var index = 0; index < lst.Count; index++)
{
var item = lst[index];
if (isMatch(item))
{
if (foundIndex != -1)
Expand All @@ -142,8 +142,6 @@ public static int SingleIndex<T>(this IEnumerable<T> lst, Predicate<T> isMatch)

foundIndex = index;
}

index++;
}

if (foundIndex == -1)
Expand All @@ -154,17 +152,15 @@ public static int SingleIndex<T>(this IEnumerable<T> lst, Predicate<T> isMatch)
return foundIndex;
}

public static int FirstIndex<T>(this IEnumerable<T> lst, Predicate<T> isMatch)
public static int FirstIndex<T>(this IList<T> lst, Predicate<T> isMatch)
{
var index = 0;
foreach (var item in lst)
for (var index = 0; index < lst.Count; index++)
{
var item = lst[index];
if (isMatch(item))
{
return index;
}

index++;
}

throw new InvalidOperationException();
Expand Down

0 comments on commit 0fd3911

Please sign in to comment.