MaxByWithTies.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Returns the elements with the maximum key value by using the default comparer to compare key values.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <typeparam name="TKey">Key type.</typeparam>
  14. /// <param name="source">Source sequence.</param>
  15. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  16. /// <returns>List with the elements that share the same maximum key value.</returns>
  17. public static IList<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (keySelector == null)
  22. throw new ArgumentNullException(nameof(keySelector));
  23. return MaxByWithTies(source, keySelector, Comparer<TKey>.Default);
  24. }
  25. /// <summary>
  26. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  27. /// </summary>
  28. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  29. /// <typeparam name="TKey">Key type.</typeparam>
  30. /// <param name="source">Source sequence.</param>
  31. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  32. /// <param name="comparer">Comparer used to determine the maximum key value.</param>
  33. /// <returns>List with the elements that share the same maximum key value.</returns>
  34. public static IList<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  35. {
  36. if (source == null)
  37. throw new ArgumentNullException(nameof(source));
  38. if (keySelector == null)
  39. throw new ArgumentNullException(nameof(keySelector));
  40. if (comparer == null)
  41. throw new ArgumentNullException(nameof(comparer));
  42. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue));
  43. }
  44. private static IList<TSource> ExtremaBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare)
  45. {
  46. var result = new List<TSource>();
  47. using (var e = source.GetEnumerator())
  48. {
  49. if (!e.MoveNext())
  50. throw new InvalidOperationException("Source sequence doesn't contain any elements.");
  51. var current = e.Current;
  52. var resKey = keySelector(current);
  53. result.Add(current);
  54. while (e.MoveNext())
  55. {
  56. var cur = e.Current;
  57. var key = keySelector(cur);
  58. var cmp = compare(key, resKey);
  59. if (cmp == 0)
  60. {
  61. result.Add(cur);
  62. }
  63. else if (cmp > 0)
  64. {
  65. result = new List<TSource> { cur };
  66. resKey = key;
  67. }
  68. }
  69. }
  70. return result;
  71. }
  72. }
  73. }