|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +namespace System.Linq; |
| 5 | + |
| 6 | +#if NET462 |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Helper class that provide some extension methods to <see cref="IEnumerable{T}"/> API. |
| 10 | +/// </summary> |
| 11 | +static class CollectionExtensions |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Attempts to add the specified key and value to the dictionary. |
| 15 | + /// </summary> |
| 16 | + /// <param name="dictionary">The dictionary in which to insert the item.</param> |
| 17 | + /// <param name="key">The key of the element to add.</param> |
| 18 | + /// <param name="value">The value of the element to add. It can be <see langword="null"/>.</param> |
| 19 | + /// <returns><see langword="true"/> if the key/value pair was added to the dictionary successfully; otherwise, <see langword="false"/>.</returns> |
| 20 | + /// <summary>Returns the maximum value in a generic sequence according to a specified key selector function.</summary> |
| 21 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 22 | + /// <typeparam name="TKey">The type of key to compare elements by.</typeparam> |
| 23 | + /// <param name="source">A sequence of values to determine the maximum value of.</param> |
| 24 | + /// <param name="keySelector">A function to extract the key for each element.</param> |
| 25 | + /// <param name="comparer">The <see cref="IComparer{TKey}" /> to compare keys.</param> |
| 26 | + /// <returns>The value with the maximum key in the sequence.</returns> |
| 27 | + /// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception> |
| 28 | + /// <exception cref="ArgumentException">No key extracted from <paramref name="source" /> implements the <see cref="IComparable" /> or <see cref="IComparable{TKey}" /> interface.</exception> |
| 29 | + /// <remarks> |
| 30 | + /// <para>If <typeparamref name="TKey" /> is a reference type and the source sequence is empty or contains only values that are <see langword="null" />, this method returns <see langword="null" />.</para> |
| 31 | + /// </remarks> |
| 32 | + public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 33 | + { |
| 34 | + if (source == null) |
| 35 | + throw new ArgumentNullException(nameof(source)); |
| 36 | + |
| 37 | + if (keySelector == null) |
| 38 | + throw new ArgumentNullException(nameof(keySelector)); |
| 39 | + |
| 40 | + var comparer = Comparer<TKey>.Default; |
| 41 | + |
| 42 | + using IEnumerator<TSource> e = source.GetEnumerator(); |
| 43 | + |
| 44 | + if (!e.MoveNext()) |
| 45 | + { |
| 46 | + if (default(TSource) is null) |
| 47 | + { |
| 48 | + return default; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + throw new InvalidOperationException("Sequence contains no elements"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + TSource value = e.Current; |
| 57 | + TKey key = keySelector(value); |
| 58 | + |
| 59 | + if (default(TKey) is null) |
| 60 | + { |
| 61 | + if (key == null) |
| 62 | + { |
| 63 | + TSource firstValue = value; |
| 64 | + |
| 65 | + do |
| 66 | + { |
| 67 | + if (!e.MoveNext()) |
| 68 | + { |
| 69 | + // All keys are null, surface the first element. |
| 70 | + return firstValue; |
| 71 | + } |
| 72 | + |
| 73 | + value = e.Current; |
| 74 | + key = keySelector(value); |
| 75 | + } |
| 76 | + while (key == null); |
| 77 | + } |
| 78 | + |
| 79 | + while (e.MoveNext()) |
| 80 | + { |
| 81 | + TSource nextValue = e.Current; |
| 82 | + TKey nextKey = keySelector(nextValue); |
| 83 | + if (nextKey != null && comparer.Compare(nextKey, key) > 0) |
| 84 | + { |
| 85 | + key = nextKey; |
| 86 | + value = nextValue; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + if (comparer == Comparer<TKey>.Default) |
| 93 | + { |
| 94 | + while (e.MoveNext()) |
| 95 | + { |
| 96 | + TSource nextValue = e.Current; |
| 97 | + TKey nextKey = keySelector(nextValue); |
| 98 | + if (Comparer<TKey>.Default.Compare(nextKey, key) > 0) |
| 99 | + { |
| 100 | + key = nextKey; |
| 101 | + value = nextValue; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + while (e.MoveNext()) |
| 108 | + { |
| 109 | + TSource nextValue = e.Current; |
| 110 | + TKey nextKey = keySelector(nextValue); |
| 111 | + if (comparer.Compare(nextKey, key) > 0) |
| 112 | + { |
| 113 | + key = nextKey; |
| 114 | + value = nextValue; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return value; |
| 121 | + } |
| 122 | +} |
| 123 | +#endif |
0 commit comments