Skip to content

RemoveAll #78190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Compilers/CSharp/Portable/CodeGen/Optimizer.cs
Original file line number Diff line number Diff line change
@@ -94,21 +94,22 @@ private static void FilterValidStackLocals(Dictionary<LocalSymbol, LocalDefUseIn
// remove fake dummies and variable that cannot be scheduled
var dummies = ArrayBuilder<LocalDefUseInfo>.GetInstance();

foreach (var local in info.Keys.ToArray())
info.RemoveAll(static (local, locInfo, dummies) =>
{
var locInfo = info[local];

if (local.SynthesizedKind == SynthesizedLocalKind.OptimizerTemp)
{
dummies.Add(locInfo);
info.Remove(local);
return true;
}
else if (locInfo.CannotSchedule)

if (locInfo.CannotSchedule)
{
locInfo.Free();
info.Remove(local);
return true;
}
}

return false;
}, dummies);

if (info.Count != 0)
{
13 changes: 1 addition & 12 deletions src/Compilers/CSharp/Portable/Utilities/FirstAmongEqualsSet.cs
Original file line number Diff line number Diff line change
@@ -80,18 +80,7 @@ public void IntersectWith(IEnumerable<T> items)
// is a hash set; in practice it will not typically be.)
_hashSet.UnionWith(items);

// Remove from the dictionary all items that are not
// in the input item set.

// Make a copy of the keys so that we are not changing the
// dictionary as we enumerate it.
foreach (var key in _dictionary.Keys.ToList())
{
if (!_hashSet.Contains(key))
{
_dictionary.Remove(key);
}
}
_dictionary.RemoveAll(static (key, value, hashSet) => !hashSet.Contains(key), _hashSet);

// Now update the dictionary so that it contains the more
// canonical of the items that are in both sets.
Original file line number Diff line number Diff line change
@@ -36,16 +36,18 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGen
' remove fake dummies and variable that cannot be scheduled
Dim dummies As New List(Of LocalDefUseInfo)

For Each local In info.Keys.ToArray()
Dim locInfo = info(local)

If TypeOf local Is DummyLocal Then
dummies.Add(locInfo)
info.Remove(local)
ElseIf locInfo.CannotSchedule Then
info.Remove(local)
End If
Next
info.RemoveAll(
Function(local, locInfo, dummiesList)
If TypeOf local Is DummyLocal Then
dummiesList.Add(locInfo)
Return True
ElseIf locInfo.CannotSchedule Then
Return True
End If

Return False
End Function,
dummies)

If info.Count = 0 Then
' nothing to filter
64 changes: 64 additions & 0 deletions src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis;

internal static class IDictionaryExtensions
{
/// <summary>
/// Removes entries from a dictionary based on a specified condition. The condition is defined by a function that
/// evaluates each key-value pair.
/// </summary>
public static void RemoveAll<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TKey, TValue, bool> predicate)
where TKey : notnull
=> RemoveAll(dictionary, static (key, value, predicate) => predicate(key, value), predicate);

/// <summary>
/// Removes entries from a dictionary based on a specified condition. The condition is defined by a function that
/// evaluates each key-value pair.
/// </summary>
public static void RemoveAll<TKey, TValue, TArg>(this IDictionary<TKey, TValue> dictionary, Func<TKey, TValue, TArg, bool> predicate, TArg arg)
where TKey : notnull
{
if (dictionary.Count == 0)
{
return;
}
#if NET
// .NET implementation of Dictionary<,> supports removing while enumerating:
if (dictionary is Dictionary<TKey, TValue> dict)
{
foreach (var (key, value) in dict)
{
if (predicate(key, value, arg))
{
dict.Remove(key);
}
}

return;
}
#endif
var keysToRemove = ArrayBuilder<TKey>.GetInstance();
foreach (var (key, value) in dictionary)

Check failure on line 49 in src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs#L49

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs(49,38): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'KeyValuePair<TKey, TValue>' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'KeyValuePair<TKey, TValue>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 49 in src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs#L49

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs(49,38): error CS8129: (NETCORE_ENGINEERING_TELEMETRY=Build) No suitable 'Deconstruct' instance or extension method was found for type 'KeyValuePair<TKey, TValue>', with 2 out parameters and a void return type.

Check failure on line 49 in src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs#L49

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs(49,23): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'key'.

Check failure on line 49 in src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs#L49

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs(49,28): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'value'.

Check failure on line 49 in src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs#L49

src/Dependencies/Collections/Extensions/IDictionaryExtensions.cs(49,38): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'KeyValuePair<TKey, TValue>' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'KeyValuePair<TKey, TValue>' could be found (are you missing a using directive or an assembly reference?)
{
if (predicate(key, value, arg))
{
keysToRemove.Add(key);
}
}

foreach (var key in keysToRemove)
{
dictionary.Remove(key);
}

keysToRemove.Free();
}
}
Original file line number Diff line number Diff line change
@@ -71,6 +71,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ImmutableArrayExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ICollectionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IListExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IDictionaryExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\MemoryExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Specialized\SpecializedCollections.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Specialized\SpecializedCollections.Empty.Collection.cs" />
Original file line number Diff line number Diff line change
@@ -181,13 +181,7 @@ public static void Serialize(IDictionary<string, string> builder, IOption2 optio
if (value is NamingStylePreferences preferences)
{
// remove existing naming style values:
foreach (var name in builder.Keys)
{
if (name.StartsWith("dotnet_naming_rule.") || name.StartsWith("dotnet_naming_symbols.") || name.StartsWith("dotnet_naming_style."))
{
builder.Remove(name);
}
}
builder.RemoveAll(static (name, _) => name.StartsWith("dotnet_naming_rule.") || name.StartsWith("dotnet_naming_symbols.") || name.StartsWith("dotnet_naming_style."));

NamingStylePreferencesEditorConfigSerializer.WriteNamingStylePreferencesToEditorConfig(
preferences.SymbolSpecifications,
Original file line number Diff line number Diff line change
@@ -224,42 +224,4 @@ public static void MultiRemove<TKey, TValue>(this IDictionary<TKey, ImmutableArr
}
}
}

/// <summary>
/// Removes entries from a dictionary based on a specified condition. The condition is defined by a function that
/// evaluates each key-value pair.
/// </summary>
public static void RemoveAll<TKey, TValue, TArg>(this Dictionary<TKey, TValue> dictionary, Func<TKey, TValue, TArg, bool> predicate, TArg arg)
where TKey : notnull
{
#if NET
// .NET supports removing while enumerating:
foreach (var entry in dictionary)
{
if (predicate(entry.Key, entry.Value, arg))
{
dictionary.Remove(entry.Key);
}
}
#else
if (dictionary.Count == 0)
{
return;
}

using var _ = ArrayBuilder<TKey>.GetInstance(out var keysToRemove);
foreach (var entry in dictionary)
{
if (predicate(entry.Key, entry.Value, arg))
{
keysToRemove.Add(entry.Key);
}
}

foreach (var key in keysToRemove)
{
dictionary.Remove(key);
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -597,13 +597,12 @@ static void RemoveNonOverriddableMembers(
if (member.IsImplicitlyDeclared)
continue;

var matches = result.Where(kvp =>
comparer.Equals(member.Name, kvp.Key.Name) &&
SignatureComparer.Instance.HaveSameSignature(member, kvp.Key, caseSensitive));

// realize the matches since we're mutating the collection we're querying.
foreach (var match in matches.ToImmutableArray())
result.Remove(match.Key);
result.RemoveAll(static (symbol, value, arg) =>
{
return arg.comparer.Equals(arg.member.Name, symbol.Name) &&
SignatureComparer.Instance.HaveSameSignature(arg.member, symbol, arg.caseSensitive);
},
(comparer, member, caseSensitive));
}
}
}