Max.cs 4.6 KB

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