/// <summary> /// This will take 'table' and merge all of the NamespaceData instances that point to the same /// namespace. It has to create 'stringTable' as an intermediate dictionary, so it will hand it /// back to the caller should the caller want to use it. /// </summary> private static void MergeDuplicateNamespaces(Dictionary <NamespaceDefinitionHandle, NamespaceDataBuilder> table, out Dictionary <string, NamespaceDataBuilder> stringTable) { var namespaces = new Dictionary <string, NamespaceDataBuilder>(); List <KeyValuePair <NamespaceDefinitionHandle, NamespaceDataBuilder> >?remaps = null; foreach (var group in table) { NamespaceDataBuilder data = group.Value; NamespaceDataBuilder?existingRecord; if (namespaces.TryGetValue(data.FullName, out existingRecord)) { // Children should not exist until the next step. Debug.Assert(data.Namespaces !.Count == 0); data.MergeInto(existingRecord); remaps ??= new List <KeyValuePair <NamespaceDefinitionHandle, NamespaceDataBuilder> >(); remaps.Add(new KeyValuePair <NamespaceDefinitionHandle, NamespaceDataBuilder>(group.Key, existingRecord)); } else { namespaces.Add(data.FullName, data); } } // Needs to be done outside of foreach (var group in table) to avoid modifying the dictionary while foreach'ing over it. if (remaps != null) { foreach (var tuple in remaps) { table[tuple.Key] = tuple.Value; } } stringTable = namespaces; }