Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix ToFrozenDictionary(selectors) on empty sources#90595
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 4 additions & 2 deletions
6 src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -41,7 +41,7 @@ public static FrozenDictionary<TKey, TValue> ToFrozenDictionary<TKey, TValue>(th | ||
| public static FrozenDictionary<TKey, TSource> ToFrozenDictionary<TSource, TKey>( | ||
| this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null) | ||
| where TKey : notnull => | ||
| CreateFromDictionary(source.ToDictionary(keySelector, comparer)); | ||
| source.ToDictionary(keySelector, comparer).ToFrozenDictionary(comparer); | ||
| /// <summary>Creates a <see cref="FrozenDictionary{TKey, TElement}"/> from an <see cref="IEnumerable{TSource}"/> according to specified key selector and element selector functions.</summary> | ||
| /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> | ||
| @@ -55,7 +55,7 @@ public static FrozenDictionary<TKey, TSource> ToFrozenDictionary<TSource, TKey>( | ||
| public static FrozenDictionary<TKey, TElement> ToFrozenDictionary<TSource, TKey, TElement>( | ||
| this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer = null) | ||
| where TKey : notnull => | ||
| CreateFromDictionary(source.ToDictionary(keySelector, elementSelector, comparer)); | ||
| source.ToDictionary(keySelector, elementSelector, comparer).ToFrozenDictionary(comparer); | ||
| /// <summary> | ||
| /// Extracts from the source either an existing <see cref="FrozenDictionary{TKey,TValue}"/> instance or a <see cref="Dictionary{TKey,TValue}"/> | ||
| @@ -113,6 +113,8 @@ public static FrozenDictionary<TKey, TElement> ToFrozenDictionary<TSource, TKey, | ||
| private static FrozenDictionary<TKey, TValue> CreateFromDictionary<TKey, TValue>(Dictionary<TKey, TValue> source) | ||
| where TKey : notnull | ||
| { | ||
| Debug.Assert(source.Count > 0, "Empty sources should have been filtered out by caller"); | ||
stephentoub marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| IEqualityComparer<TKey> comparer = source.Comparer; | ||
| // Optimize for value types when the default comparer is being used. In such a case, the implementation | ||
2 changes: 2 additions & 0 deletions
2 src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSet.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 26 additions & 14 deletions
40 src/libraries/System.Collections.Immutable/tests/Frozen/FrozenDictionaryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 15 additions & 14 deletions
29 src/libraries/System.Collections.Immutable/tests/Frozen/FrozenSetTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.