Skip to content

Commit

Permalink
Move existing RemoveWhere code into an extension method on ArrayBuild…
Browse files Browse the repository at this point in the history
…er (#74620)

* Move existing RemoveWhere code into an extension method on ArrayBuilder

I have an upcoming PR that needs to update an ArrayBuilder in place and I'd prefer not to duplicate that logic. Seems like a useful enough API to warrant an extension method.
  • Loading branch information
ToddGrun authored Jul 31, 2024
1 parent 020db28 commit f92d3ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
20 changes: 1 addition & 19 deletions src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ private ImmutableArray<ISymbol> LookupSymbolsInternal(
}

if (name == null)
FilterNotReferenceable(results);
results.RemoveWhere(static (symbol, _, _) => !symbol.CanBeReferencedByName, arg: default(VoidResult));

return results.ToImmutableAndFree();
}
Expand Down Expand Up @@ -1795,24 +1795,6 @@ private Symbol RemapSymbolIfNecessary(Symbol symbol)
/// </summary>
internal abstract Symbol RemapSymbolIfNecessaryCore(Symbol symbol);

private static void FilterNotReferenceable(ArrayBuilder<ISymbol> sealedResults)
{
var writeIndex = 0;
for (var i = 0; i < sealedResults.Count; i++)
{
var symbol = sealedResults[i];
if (symbol.CanBeReferencedByName)
{
if (writeIndex != i)
sealedResults[writeIndex] = symbol;

writeIndex++;
}
}

sealedResults.Count = writeIndex;
}

/// <summary>
/// Determines if the symbol is accessible from the specified location.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/Compilers/Core/Portable/Collections/ArrayBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,23 @@ public static OneOrMany<T> ToOneOrManyAndFree<T>(this ArrayBuilder<T> builder)
}

#endif

public static void RemoveWhere<TItem, TArg>(this ArrayBuilder<TItem> builder, Func<TItem, int, TArg, bool> filter, TArg arg)
{
var writeIndex = 0;
for (var i = 0; i < builder.Count; i++)
{
var item = builder[i];
if (!filter(item, i, arg))
{
if (writeIndex != i)
builder[writeIndex] = item;

writeIndex++;
}
}

builder.Count = writeIndex;
}
}
}

0 comments on commit f92d3ac

Please sign in to comment.