Max.cs 4.5 KB

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