Skip to content
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

Replaced some Dictionaries and Hashtables with Hashsets #4177

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ internal static class FontResourceCache
private static void ConstructFontResourceCache(Assembly entryAssembly, Dictionary<string, List<string>> folderResourceMap)
{
// For entryAssembly build a set of mapping from paths to entries that describe each resource.
Dictionary<string, string> contentFiles = ContentFileHelper.GetContentFiles(entryAssembly);
HashSet<string> contentFiles = ContentFileHelper.GetContentFiles(entryAssembly);
if (contentFiles != null)
{
foreach (string contentFile in contentFiles.Keys)
foreach (string contentFile in contentFiles)
{
AddResourceToFolderMap(folderResourceMap, contentFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static bool IsContentFile(string partName)

if (_contentFiles != null && _contentFiles.Count > 0)
{
if (_contentFiles.ContainsKey(partName))
if (_contentFiles.Contains(partName))
{
return true;
}
Expand All @@ -40,9 +40,9 @@ internal static bool IsContentFile(string partName)
//
// Get a list of Content Files for a given Assembly.
//
static internal Dictionary<string, string> GetContentFiles(Assembly asm)
static internal HashSet<string> GetContentFiles(Assembly asm)
{
Dictionary<string, string> contentFiles = null;
HashSet<string> contentFiles = null;

Attribute[] assemblyAttributes;

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

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

if (assemblyAttributes != null && assemblyAttributes.Length > 0)
{
contentFiles = new Dictionary<string, string>(assemblyAttributes.Length, StringComparer.OrdinalIgnoreCase);
contentFiles = new HashSet<string>(assemblyAttributes.Length, StringComparer.OrdinalIgnoreCase);

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

aacf = (AssemblyAssociatedContentFileAttribute) assemblyAttributes[i];
contentFiles.Add(aacf.RelativeContentFilePath, null);
contentFiles.Add(aacf.RelativeContentFilePath);
}
}

return contentFiles;
}

private static Dictionary<string, string> _contentFiles;
private static HashSet<string> _contentFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ internal void UpdateChildrenInternal(int invalidateLimit)
{
List<AutomationPeer> oldChildren = _children;
List<AutomationPeer> addedChildren = null;
Hashtable ht = null;
HashSet<AutomationPeer> hs = null;

_childrenValid = false;
EnsureChildren();
Expand All @@ -1770,14 +1770,13 @@ internal void UpdateChildrenInternal(int invalidateLimit)
if (!EventMap.HasRegisteredEvent(AutomationEvents.StructureChanged))
return;

//store old children in a hashtable
//store old children in a hashset
if(oldChildren != null)
{
ht = new Hashtable();
hs = new HashSet<AutomationPeer>();
for(int count = oldChildren.Count, i = 0; i < count; i++)
{
if(!ht.Contains(oldChildren[i]))
ht.Add(oldChildren[i], null);
hs.Add(oldChildren[i]);
}
}

Expand All @@ -1790,9 +1789,9 @@ internal void UpdateChildrenInternal(int invalidateLimit)
for(int count = _children.Count, i = 0; i < count; i++)
{
AutomationPeer child = _children[i];
if(ht != null && ht.ContainsKey(child))
if(hs != null && hs.Contains(child))
{
ht.Remove(child); //same child, nothing to notify
hs.Remove(child); //same child, nothing to notify
}
else
{
Expand All @@ -1808,9 +1807,9 @@ internal void UpdateChildrenInternal(int invalidateLimit)
}
}

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

if(removedCount + addedCount > invalidateLimit) //bilk invalidation
{
Expand Down Expand Up @@ -1842,11 +1841,9 @@ internal void UpdateChildrenInternal(int invalidateLimit)
IRawElementProviderSimple provider = ProviderFromPeerNoDelegation(this);
if (provider != null)
{
//ht contains removed children by now
foreach (object key in ht.Keys)
//hs contains removed children by now
foreach (AutomationPeer removedChild in hs)
{
AutomationPeer removedChild = (AutomationPeer)key;

int[] rid = removedChild.GetRuntimeId();

AutomationInteropProvider.RaiseStructureChangedEvent(
Expand All @@ -1857,7 +1854,7 @@ internal void UpdateChildrenInternal(int invalidateLimit)
}
if (addedCount > 0)
{
//ht contains removed children by now
//hs contains removed children by now
foreach (AutomationPeer addedChild in addedChildren)
{
//for children added, provider is the child itself
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ internal MediaContext(Dispatcher dispatcher)
_contextGuid = Guid.NewGuid();

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

_renderModeMessage = new DispatcherOperationCallback(InvalidateRenderMode);

Expand Down Expand Up @@ -367,7 +367,7 @@ private object InvalidateRenderMode(object dontCare)
{
Debug.Assert(CheckAccess());

foreach (ICompositionTarget target in _registeredICompositionTargets.Keys)
foreach (ICompositionTarget target in _registeredICompositionTargets)
{
HwndTarget hwndTarget = target as HwndTarget;

Expand Down Expand Up @@ -1441,7 +1441,7 @@ public virtual void Dispose()

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

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

_registeredICompositionTargets.Add(iv, null); // We use the dictionary just as a set.
_registeredICompositionTargets.Add(iv);
}

/// <summary>
Expand Down Expand Up @@ -2067,7 +2067,7 @@ private void Render(ICompositionTarget resizedCompositionTarget)
// ----------------------------------------------------------------
// 1) Render each registered ICompositionTarget to finish up the batch.

foreach (ICompositionTarget registeredTarget in _registeredICompositionTargets.Keys)
foreach (ICompositionTarget registeredTarget in _registeredICompositionTargets)
{
DUCE.ChannelSet channelSet;
channelSet.Channel = _channelManager.Channel;
Expand Down Expand Up @@ -2727,7 +2727,7 @@ internal DUCE.ChannelSet GetChannels()
/// <summary>
/// Set of ICompositionTargets that are currently registered with the MediaSystem;
/// </summary>
private Dictionary<ICompositionTarget, object> _registeredICompositionTargets;
private HashSet<ICompositionTarget> _registeredICompositionTargets;

/// <summary>
/// This are the the permissions the Context has to access Visual APIs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ protected override Size ArrangeOverride(Size finalSize)

// Divide remainingHeight equally among starLayoutProviders
remainingHeight = finalSize.Height - totalDesiredHeight;
Dictionary<UIElement, object> starLayoutTargets = GetStarLayoutProviderTargets();
HashSet<UIElement> starLayoutTargets = GetStarLayoutProviderTargets();
if (DoubleUtil.GreaterThan(remainingHeight, 0.0) && starLayoutTargets.Count > 0)
{
surplusHeight = remainingHeight / starLayoutTargets.Count;
Expand All @@ -230,7 +230,7 @@ protected override Size ArrangeOverride(Size finalSize)
for (int i = 0; i < children.Count; i++)
{
UIElement child = children[i];
if (starLayoutTargets.ContainsKey(child))
if (starLayoutTargets.Contains(child))
{
// if the child is a StarLayoutProvider, give it the surplusHeight.
double availableHeight = child.DesiredSize.Height + surplusHeight;
Expand Down Expand Up @@ -266,16 +266,16 @@ internal void BringIndexIntoViewInternal(int index)
base.BringIndexIntoView(index);
}

private Dictionary<UIElement, object> GetStarLayoutProviderTargets()
private HashSet<UIElement> GetStarLayoutProviderTargets()
{
Dictionary<UIElement, object> targets = new Dictionary<UIElement, object>();
HashSet<UIElement> targets = new HashSet<UIElement>();

foreach (IProvideStarLayoutInfoBase starProvider in _registeredStarLayoutProviders)
{
UIElement starLayoutTarget = starProvider.TargetElement;
if (starLayoutTarget != null)
{
targets.Add(starLayoutTarget, null);
targets.Add(starLayoutTarget);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,16 @@ public override string FindNamespaceByPrefix(string prefix)
public override IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
{
ObjectWriterFrame frame = _stack.CurrentFrame;
Dictionary<string, string> keys = new Dictionary<string, string>();
HashSet<string> keys = new HashSet<string>();

while (frame.Depth > 0)
{
if (frame._namespaces != null)
{
foreach (NamespaceDeclaration namespaceDeclaration in frame.GetNamespacePrefixes())
{
if (!keys.ContainsKey(namespaceDeclaration.Prefix))
if (keys.Add(namespaceDeclaration.Prefix))
{
keys.Add(namespaceDeclaration.Prefix, null);
yield return namespaceDeclaration;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,15 @@ public override string FindNamespaceByPrefix(String prefix)
public override IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
{
XamlParserFrame frame = _stack.CurrentFrame;
Dictionary<string, string> keys = new Dictionary<string, string>();
HashSet<string> keys = new HashSet<string>();
while (frame.Depth > 0)
{
if (frame._namespaces != null)
{
foreach (NamespaceDeclaration namespaceDeclaration in frame.GetNamespacePrefixes())
{
if (!keys.ContainsKey(namespaceDeclaration.Prefix))
if (keys.Add(namespaceDeclaration.Prefix))
{
keys.Add(namespaceDeclaration.Prefix, null);
yield return namespaceDeclaration;
}
}
Expand All @@ -99,9 +98,8 @@ public override IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
{
foreach (KeyValuePair<string, string> kvp in _prescopeNamespaces)
{
if (!keys.ContainsKey(kvp.Key))
if (keys.Add(kvp.Key))
{
keys.Add(kvp.Key, null);
yield return new NamespaceDeclaration(kvp.Value, kvp.Key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public NamespaceComparer(XmlNsInfo nsInfo, Assembly assembly)
// Calculate the subsume count upfront, since this also serves as our cycle detection
_subsumeCount = new Dictionary<string,int>(nsInfo.OldToNewNs.Count);

Dictionary<string, object> visited = new Dictionary<string, object>();
HashSet<string> visited = new HashSet<string>();

// for every XmlnsCompatAttribute
foreach (string newNs in nsInfo.OldToNewNs.Values)
Expand All @@ -447,11 +447,10 @@ public NamespaceComparer(XmlNsInfo nsInfo, Assembly assembly)
string ns = newNs;
do
{
if (visited.ContainsKey(ns))
if (!visited.Add(ns))
{
throw new XamlSchemaException(SR.Get(SRID.XmlnsCompatCycle, assembly.FullName, ns));
}
visited.Add(ns, null);
IncrementSubsumeCount(ns);
ns = GetNewNs(ns);
}
Expand Down