Max.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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 maximum value in the enumerable sequence by using the specified comparer to compare values.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <param name="comparer">Comparer used to determine the maximum value.</param>
  15. /// <returns>Maximum value in the sequence.</returns>
  16. public static TSource Max<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
  17. {
  18. if (source == null)
  19. {
  20. throw new ArgumentNullException(nameof(source));
  21. }
  22. if (comparer == null)
  23. {
  24. throw new ArgumentNullException(nameof(comparer));
  25. }
  26. return MaxBy(source, x => x, comparer)
  27. .First();
  28. }
  29. /// <summary>
  30. /// Returns the elements with the maximum key value by using the default comparer to compare key values.
  31. /// </summary>
  32. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  33. /// <typeparam name="TKey">Key type.</typeparam>
  34. /// <param name="source">Source sequence.</param>
  35. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  36. /// <returns>List with the elements that share the same maximum key value.</returns>
  37. public static IList<TSource> MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  38. {
  39. if (source == null)
  40. {
  41. throw new ArgumentNullException(nameof(source));
  42. }
  43. if (keySelector == null)
  44. {
  45. throw new ArgumentNullException(nameof(keySelector));
  46. }
  47. return MaxBy(source, keySelector, Comparer<TKey>.Default);
  48. }
  49. /// <summary>
  50. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  51. /// </summary>
  52. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  53. /// <typeparam name="TKey">Key type.</typeparam>
  54. /// <param name="source">Source sequence.</param>
  55. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  56. /// <param name="comparer">Comparer used to determine the maximum key value.</param>
  57. /// <returns>List with the elements that share the same maximum key value.</returns>
  58. public static IList<TSource> MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  59. {
  60. if (source == null)
  61. {
  62. throw new ArgumentNullException(nameof(source));
  63. }
  64. if (keySelector == null)
  65. {
  66. throw new ArgumentNullException(nameof(keySelector));
  67. }
  68. if (comparer == null)
  69. {
  70. throw new ArgumentNullException(nameof(comparer));
  71. }
  72. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue));
  73. }
  74. private static IList<TSource> ExtremaBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare)
  75. {
  76. var result = new List<TSource>();
  77. using (var e = source.GetEnumerator())
  78. {
  79. if (!e.MoveNext())
  80. {
  81. throw new InvalidOperationException("Source sequence doesn't contain any elements.");
  82. }
  83. var current = e.Current;
  84. var resKey = keySelector(current);
  85. result.Add(current);
  86. while (e.MoveNext())
  87. {
  88. var cur = e.Current;
  89. var key = keySelector(cur);
  90. var cmp = compare(key, resKey);
  91. if (cmp == 0)
  92. {
  93. result.Add(cur);
  94. }
  95. else if (cmp > 0)
  96. {
  97. result = new List<TSource>
  98. {
  99. cur
  100. };
  101. resKey = key;
  102. }
  103. }
  104. }
  105. return result;
  106. }
  107. }
  108. }