Skip to content

Commit

Permalink
Merge pull request #71894 from sharwell/unnecessary-box
Browse files Browse the repository at this point in the history
Provide overloads to avoid unnecessary boxing
  • Loading branch information
sharwell authored Feb 1, 2024
2 parents 2df7dde + 5f8e721 commit 0deb93a
Showing 1 changed file with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this Imm
return builder.ToImmutableAndFree();
}

/// <summary>
/// Maps and flattens a subset of immutable array to another immutable array.
/// </summary>
/// <typeparam name="TItem">Type of the source array items</typeparam>
/// <typeparam name="TResult">Type of the transformed array items</typeparam>
/// <param name="array">The array to transform</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>If the array's length is 0, this will return an empty immutable array.</returns>
public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this ImmutableArray<TItem> array, Func<TItem, ImmutableArray<TResult>> selector)
{
if (array.Length == 0)
return ImmutableArray<TResult>.Empty;

var builder = ArrayBuilder<TResult>.GetInstance();
foreach (var item in array)
builder.AddRange(selector(item));

return builder.ToImmutableAndFree();
}

/// <summary>
/// Maps and flattens a subset of immutable array to another immutable array.
/// </summary>
Expand All @@ -288,17 +308,37 @@ public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this Imm
public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this ImmutableArray<TItem> array, Func<TItem, bool> predicate, Func<TItem, IEnumerable<TResult>> selector)
{
if (array.Length == 0)
{
return ImmutableArray<TResult>.Empty;

var builder = ArrayBuilder<TResult>.GetInstance();
foreach (var item in array)
{
if (predicate(item))
builder.AddRange(selector(item));
}

return builder.ToImmutableAndFree();
}

/// <summary>
/// Maps and flattens a subset of immutable array to another immutable array.
/// </summary>
/// <typeparam name="TItem">Type of the source array items</typeparam>
/// <typeparam name="TResult">Type of the transformed array items</typeparam>
/// <param name="array">The array to transform</param>
/// <param name="predicate">The condition to use for filtering the array content.</param>
/// <param name="selector">A transform function to apply to each element that is not filtered out by <paramref name="predicate"/>.</param>
/// <returns>If the items's length is 0, this will return an empty immutable array.</returns>
public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this ImmutableArray<TItem> array, Func<TItem, bool> predicate, Func<TItem, ImmutableArray<TResult>> selector)
{
if (array.Length == 0)
return ImmutableArray<TResult>.Empty;

var builder = ArrayBuilder<TResult>.GetInstance();
foreach (var item in array)
{
if (predicate(item))
{
builder.AddRange(selector(item));
}
}

return builder.ToImmutableAndFree();
Expand Down

0 comments on commit 0deb93a

Please sign in to comment.