Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
55 changes: 15 additions & 40 deletions src/libraries/System.Linq/src/System/Linq/ToCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace System.Linq
{
Expand Down Expand Up @@ -108,23 +109,22 @@ public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumer
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}

int capacity = 0;
if (source is ICollection<TSource> collection)
if (source.TryGetNonEnumeratedCount(out int capacity))
{
capacity = collection.Count;
if (capacity == 0)
{
return new Dictionary<TKey, TSource>(comparer);
}

if (collection is TSource[] array)
if (source is TSource[] array)
{
return ToDictionary(array, keySelector, comparer);
return SpanToDictionary(array, keySelector, comparer);
}

if (collection is List<TSource> list)
if (source is List<TSource> list)
{
return ToDictionary(list, keySelector, comparer);
ReadOnlySpan<TSource> span = CollectionsMarshal.AsSpan(list);
return SpanToDictionary(span, keySelector, comparer);
}
}

Expand All @@ -137,25 +137,13 @@ public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumer
return d;
}

private static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(TSource[] source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer) where TKey : notnull
private static Dictionary<TKey, TSource> SpanToDictionary<TSource, TKey>(ReadOnlySpan<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer) where TKey : notnull
{
Dictionary<TKey, TSource> d = new Dictionary<TKey, TSource>(source.Length, comparer);
for (int i = 0; i < source.Length; i++)
{
d.Add(keySelector(source[i]), source[i]);
}

return d;
}

private static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(List<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer) where TKey : notnull
{
Dictionary<TKey, TSource> d = new Dictionary<TKey, TSource>(source.Count, comparer);
foreach (TSource element in source)
{
d.Add(keySelector(element), element);
}

return d;
}

Expand All @@ -179,23 +167,22 @@ public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(t
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementSelector);
}

int capacity = 0;
if (source is ICollection<TSource> collection)
if (source.TryGetNonEnumeratedCount(out int capacity))
{
capacity = collection.Count;
if (capacity == 0)
{
return new Dictionary<TKey, TElement>(comparer);
}

if (collection is TSource[] array)
if (source is TSource[] array)
{
return ToDictionary(array, keySelector, elementSelector, comparer);
return SpanToDictionary(array, keySelector, elementSelector, comparer);
}

if (collection is List<TSource> list)
if (source is List<TSource> list)
{
return ToDictionary(list, keySelector, elementSelector, comparer);
ReadOnlySpan<TSource> span = CollectionsMarshal.AsSpan(list);
return SpanToDictionary(span, keySelector, elementSelector, comparer);
}
}

Expand All @@ -208,25 +195,13 @@ public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(t
return d;
}

private static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(TSource[] source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer) where TKey : notnull
private static Dictionary<TKey, TElement> SpanToDictionary<TSource, TKey, TElement>(ReadOnlySpan<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer) where TKey : notnull
{
Dictionary<TKey, TElement> d = new Dictionary<TKey, TElement>(source.Length, comparer);
for (int i = 0; i < source.Length; i++)
{
d.Add(keySelector(source[i]), elementSelector(source[i]));
}

return d;
}

private static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(List<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer) where TKey : notnull
{
Dictionary<TKey, TElement> d = new Dictionary<TKey, TElement>(source.Count, comparer);
foreach (TSource element in source)
{
d.Add(keySelector(element), elementSelector(element));
}

return d;
}

Expand Down