Skip to content

Commit 52dbcf7

Browse files
Replaced some Dictionaries and Hashtables with Hashsets (#4177)
Co-authored-by: Thomas Goulet <[email protected]>
1 parent eeffa7c commit 52dbcf7

File tree

8 files changed

+38
-45
lines changed

8 files changed

+38
-45
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontResourceCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ internal static class FontResourceCache
2828
private static void ConstructFontResourceCache(Assembly entryAssembly, Dictionary<string, List<string>> folderResourceMap)
2929
{
3030
// For entryAssembly build a set of mapping from paths to entries that describe each resource.
31-
Dictionary<string, string> contentFiles = ContentFileHelper.GetContentFiles(entryAssembly);
31+
HashSet<string> contentFiles = ContentFileHelper.GetContentFiles(entryAssembly);
3232
if (contentFiles != null)
3333
{
34-
foreach (string contentFile in contentFiles.Keys)
34+
foreach (string contentFile in contentFiles)
3535
{
3636
AddResourceToFolderMap(folderResourceMap, contentFile);
3737
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Resources/ContentFileHelper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static bool IsContentFile(string partName)
2828

2929
if (_contentFiles != null && _contentFiles.Count > 0)
3030
{
31-
if (_contentFiles.ContainsKey(partName))
31+
if (_contentFiles.Contains(partName))
3232
{
3333
return true;
3434
}
@@ -40,9 +40,9 @@ internal static bool IsContentFile(string partName)
4040
//
4141
// Get a list of Content Files for a given Assembly.
4242
//
43-
static internal Dictionary<string, string> GetContentFiles(Assembly asm)
43+
static internal HashSet<string> GetContentFiles(Assembly asm)
4444
{
45-
Dictionary<string, string> contentFiles = null;
45+
HashSet<string> contentFiles = null;
4646

4747
Attribute[] assemblyAttributes;
4848

@@ -53,7 +53,7 @@ static internal Dictionary<string, string> GetContentFiles(Assembly asm)
5353
{
5454
// If we have no entry assembly return an empty list because
5555
// we can't have any content files.
56-
return new Dictionary<string, string>();
56+
return new HashSet<string>();
5757
}
5858
}
5959

@@ -63,20 +63,20 @@ static internal Dictionary<string, string> GetContentFiles(Assembly asm)
6363

6464
if (assemblyAttributes != null && assemblyAttributes.Length > 0)
6565
{
66-
contentFiles = new Dictionary<string, string>(assemblyAttributes.Length, StringComparer.OrdinalIgnoreCase);
66+
contentFiles = new HashSet<string>(assemblyAttributes.Length, StringComparer.OrdinalIgnoreCase);
6767

6868
for (int i=0; i<assemblyAttributes.Length; i++)
6969
{
7070
AssemblyAssociatedContentFileAttribute aacf;
7171

7272
aacf = (AssemblyAssociatedContentFileAttribute) assemblyAttributes[i];
73-
contentFiles.Add(aacf.RelativeContentFilePath, null);
73+
contentFiles.Add(aacf.RelativeContentFilePath);
7474
}
7575
}
7676

7777
return contentFiles;
7878
}
7979

80-
private static Dictionary<string, string> _contentFiles;
80+
private static HashSet<string> _contentFiles;
8181
}
8282
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/AutomationPeer.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ internal void UpdateChildrenInternal(int invalidateLimit)
17611761
{
17621762
List<AutomationPeer> oldChildren = _children;
17631763
List<AutomationPeer> addedChildren = null;
1764-
Hashtable ht = null;
1764+
HashSet<AutomationPeer> hs = null;
17651765

17661766
_childrenValid = false;
17671767
EnsureChildren();
@@ -1770,14 +1770,13 @@ internal void UpdateChildrenInternal(int invalidateLimit)
17701770
if (!EventMap.HasRegisteredEvent(AutomationEvents.StructureChanged))
17711771
return;
17721772

1773-
//store old children in a hashtable
1773+
//store old children in a hashset
17741774
if(oldChildren != null)
17751775
{
1776-
ht = new Hashtable();
1776+
hs = new HashSet<AutomationPeer>();
17771777
for(int count = oldChildren.Count, i = 0; i < count; i++)
17781778
{
1779-
if(!ht.Contains(oldChildren[i]))
1780-
ht.Add(oldChildren[i], null);
1779+
hs.Add(oldChildren[i]);
17811780
}
17821781
}
17831782

@@ -1790,9 +1789,9 @@ internal void UpdateChildrenInternal(int invalidateLimit)
17901789
for(int count = _children.Count, i = 0; i < count; i++)
17911790
{
17921791
AutomationPeer child = _children[i];
1793-
if(ht != null && ht.ContainsKey(child))
1792+
if(hs != null && hs.Contains(child))
17941793
{
1795-
ht.Remove(child); //same child, nothing to notify
1794+
hs.Remove(child); //same child, nothing to notify
17961795
}
17971796
else
17981797
{
@@ -1808,9 +1807,9 @@ internal void UpdateChildrenInternal(int invalidateLimit)
18081807
}
18091808
}
18101809

1811-
//now the ht only has "removed" children. If the count does not yet
1810+
//now the hs only has "removed" children. If the count does not yet
18121811
//calls for "bulk" notification, use per-child notification, otherwise use "bulk"
1813-
int removedCount = (ht == null ? 0 : ht.Count);
1812+
int removedCount = (hs == null ? 0 : hs.Count);
18141813

18151814
if(removedCount + addedCount > invalidateLimit) //bilk invalidation
18161815
{
@@ -1842,11 +1841,9 @@ internal void UpdateChildrenInternal(int invalidateLimit)
18421841
IRawElementProviderSimple provider = ProviderFromPeerNoDelegation(this);
18431842
if (provider != null)
18441843
{
1845-
//ht contains removed children by now
1846-
foreach (object key in ht.Keys)
1844+
//hs contains removed children by now
1845+
foreach (AutomationPeer removedChild in hs)
18471846
{
1848-
AutomationPeer removedChild = (AutomationPeer)key;
1849-
18501847
int[] rid = removedChild.GetRuntimeId();
18511848

18521849
AutomationInteropProvider.RaiseStructureChangedEvent(
@@ -1857,7 +1854,7 @@ internal void UpdateChildrenInternal(int invalidateLimit)
18571854
}
18581855
if (addedCount > 0)
18591856
{
1860-
//ht contains removed children by now
1857+
//hs contains removed children by now
18611858
foreach (AutomationPeer addedChild in addedChildren)
18621859
{
18631860
//for children added, provider is the child itself

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ internal MediaContext(Dispatcher dispatcher)
223223
_contextGuid = Guid.NewGuid();
224224

225225
// Create a dictionary in which we manage the CompositionTargets.
226-
_registeredICompositionTargets = new Dictionary<ICompositionTarget, object>();
226+
_registeredICompositionTargets = new HashSet<ICompositionTarget>();
227227

228228
_renderModeMessage = new DispatcherOperationCallback(InvalidateRenderMode);
229229

@@ -367,7 +367,7 @@ private object InvalidateRenderMode(object dontCare)
367367
{
368368
Debug.Assert(CheckAccess());
369369

370-
foreach (ICompositionTarget target in _registeredICompositionTargets.Keys)
370+
foreach (ICompositionTarget target in _registeredICompositionTargets)
371371
{
372372
HwndTarget hwndTarget = target as HwndTarget;
373373

@@ -1441,7 +1441,7 @@ public virtual void Dispose()
14411441

14421442
// First make a copy of the dictionarys contents, because ICompositionTarget.Dispose modifies this collection.
14431443
ICompositionTarget[] registeredVTs = new ICompositionTarget[_registeredICompositionTargets.Count];
1444-
_registeredICompositionTargets.Keys.CopyTo(registeredVTs, 0);
1444+
_registeredICompositionTargets.CopyTo(registeredVTs, 0);
14451445

14461446
// Iterate through the ICompositionTargets and dispose them. Be careful, ICompositionTarget.Dispose
14471447
// removes the ICompositionTargets from the Dictionary. This is why we don't iterate the Dictionary directly.
@@ -1519,7 +1519,7 @@ private void RegisterICompositionTargetInternal(ICompositionTarget iv)
15191519
iv.AddRefOnChannel(channelSet.Channel, channelSet.OutOfBandChannel);
15201520
}
15211521

1522-
_registeredICompositionTargets.Add(iv, null); // We use the dictionary just as a set.
1522+
_registeredICompositionTargets.Add(iv);
15231523
}
15241524

15251525
/// <summary>
@@ -2067,7 +2067,7 @@ private void Render(ICompositionTarget resizedCompositionTarget)
20672067
// ----------------------------------------------------------------
20682068
// 1) Render each registered ICompositionTarget to finish up the batch.
20692069

2070-
foreach (ICompositionTarget registeredTarget in _registeredICompositionTargets.Keys)
2070+
foreach (ICompositionTarget registeredTarget in _registeredICompositionTargets)
20712071
{
20722072
DUCE.ChannelSet channelSet;
20732073
channelSet.Channel = _channelManager.Channel;
@@ -2727,7 +2727,7 @@ internal DUCE.ChannelSet GetChannels()
27272727
/// <summary>
27282728
/// Set of ICompositionTargets that are currently registered with the MediaSystem;
27292729
/// </summary>
2730-
private Dictionary<ICompositionTarget, object> _registeredICompositionTargets;
2730+
private HashSet<ICompositionTarget> _registeredICompositionTargets;
27312731

27322732
/// <summary>
27332733
/// This are the the permissions the Context has to access Visual APIs.

src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/Primitives/RibbonMenuItemsPanel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ protected override Size ArrangeOverride(Size finalSize)
219219

220220
// Divide remainingHeight equally among starLayoutProviders
221221
remainingHeight = finalSize.Height - totalDesiredHeight;
222-
Dictionary<UIElement, object> starLayoutTargets = GetStarLayoutProviderTargets();
222+
HashSet<UIElement> starLayoutTargets = GetStarLayoutProviderTargets();
223223
if (DoubleUtil.GreaterThan(remainingHeight, 0.0) && starLayoutTargets.Count > 0)
224224
{
225225
surplusHeight = remainingHeight / starLayoutTargets.Count;
@@ -230,7 +230,7 @@ protected override Size ArrangeOverride(Size finalSize)
230230
for (int i = 0; i < children.Count; i++)
231231
{
232232
UIElement child = children[i];
233-
if (starLayoutTargets.ContainsKey(child))
233+
if (starLayoutTargets.Contains(child))
234234
{
235235
// if the child is a StarLayoutProvider, give it the surplusHeight.
236236
double availableHeight = child.DesiredSize.Height + surplusHeight;
@@ -266,16 +266,16 @@ internal void BringIndexIntoViewInternal(int index)
266266
base.BringIndexIntoView(index);
267267
}
268268

269-
private Dictionary<UIElement, object> GetStarLayoutProviderTargets()
269+
private HashSet<UIElement> GetStarLayoutProviderTargets()
270270
{
271-
Dictionary<UIElement, object> targets = new Dictionary<UIElement, object>();
271+
HashSet<UIElement> targets = new HashSet<UIElement>();
272272

273273
foreach (IProvideStarLayoutInfoBase starProvider in _registeredStarLayoutProviders)
274274
{
275275
UIElement starLayoutTarget = starProvider.TargetElement;
276276
if (starLayoutTarget != null)
277277
{
278-
targets.Add(starLayoutTarget, null);
278+
targets.Add(starLayoutTarget);
279279
}
280280
}
281281

src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,16 @@ public override string FindNamespaceByPrefix(string prefix)
258258
public override IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
259259
{
260260
ObjectWriterFrame frame = _stack.CurrentFrame;
261-
Dictionary<string, string> keys = new Dictionary<string, string>();
261+
HashSet<string> keys = new HashSet<string>();
262262

263263
while (frame.Depth > 0)
264264
{
265265
if (frame._namespaces != null)
266266
{
267267
foreach (NamespaceDeclaration namespaceDeclaration in frame.GetNamespacePrefixes())
268268
{
269-
if (!keys.ContainsKey(namespaceDeclaration.Prefix))
269+
if (keys.Add(namespaceDeclaration.Prefix))
270270
{
271-
keys.Add(namespaceDeclaration.Prefix, null);
272271
yield return namespaceDeclaration;
273272
}
274273
}

src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlParserContext.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,15 @@ public override string FindNamespaceByPrefix(String prefix)
7878
public override IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
7979
{
8080
XamlParserFrame frame = _stack.CurrentFrame;
81-
Dictionary<string, string> keys = new Dictionary<string, string>();
81+
HashSet<string> keys = new HashSet<string>();
8282
while (frame.Depth > 0)
8383
{
8484
if (frame._namespaces != null)
8585
{
8686
foreach (NamespaceDeclaration namespaceDeclaration in frame.GetNamespacePrefixes())
8787
{
88-
if (!keys.ContainsKey(namespaceDeclaration.Prefix))
88+
if (keys.Add(namespaceDeclaration.Prefix))
8989
{
90-
keys.Add(namespaceDeclaration.Prefix, null);
9190
yield return namespaceDeclaration;
9291
}
9392
}
@@ -99,9 +98,8 @@ public override IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
9998
{
10099
foreach (KeyValuePair<string, string> kvp in _prescopeNamespaces)
101100
{
102-
if (!keys.ContainsKey(kvp.Key))
101+
if (keys.Add(kvp.Key))
103102
{
104-
keys.Add(kvp.Key, null);
105103
yield return new NamespaceDeclaration(kvp.Value, kvp.Key);
106104
}
107105
}

src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/XmlNsInfo.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public NamespaceComparer(XmlNsInfo nsInfo, Assembly assembly)
436436
// Calculate the subsume count upfront, since this also serves as our cycle detection
437437
_subsumeCount = new Dictionary<string,int>(nsInfo.OldToNewNs.Count);
438438

439-
Dictionary<string, object> visited = new Dictionary<string, object>();
439+
HashSet<string> visited = new HashSet<string>();
440440

441441
// for every XmlnsCompatAttribute
442442
foreach (string newNs in nsInfo.OldToNewNs.Values)
@@ -447,11 +447,10 @@ public NamespaceComparer(XmlNsInfo nsInfo, Assembly assembly)
447447
string ns = newNs;
448448
do
449449
{
450-
if (visited.ContainsKey(ns))
450+
if (!visited.Add(ns))
451451
{
452452
throw new XamlSchemaException(SR.Get(SRID.XmlnsCompatCycle, assembly.FullName, ns));
453453
}
454-
visited.Add(ns, null);
455454
IncrementSubsumeCount(ns);
456455
ns = GetNewNs(ns);
457456
}

0 commit comments

Comments
 (0)