Line | |
---|
1 | #region License Information |
---|
2 | /* |
---|
3 | * This file is part of SimSharp which is licensed under the MIT license. |
---|
4 | * See the LICENSE file in the project root for more information. |
---|
5 | */ |
---|
6 | #endregion |
---|
7 | |
---|
8 | using System; |
---|
9 | using System.Collections.Generic; |
---|
10 | using System.Linq; |
---|
11 | |
---|
12 | namespace SimSharp { |
---|
13 | public static class Extensions { |
---|
14 | public static IEnumerable<T> MaxItems<T>(this IEnumerable<T> source, Func<T, IComparable> selector) { |
---|
15 | var enumerator = source.GetEnumerator(); |
---|
16 | if (!enumerator.MoveNext()) return Enumerable.Empty<T>(); |
---|
17 | var item = enumerator.Current; |
---|
18 | var max = selector(item); |
---|
19 | var result = new List<T> { item }; |
---|
20 | |
---|
21 | while (enumerator.MoveNext()) { |
---|
22 | item = enumerator.Current; |
---|
23 | var comparable = selector(item); |
---|
24 | var comparison = comparable.CompareTo(max); |
---|
25 | if (comparison > 0) { |
---|
26 | result.Clear(); |
---|
27 | result.Add(item); |
---|
28 | max = comparable; |
---|
29 | } else if (comparison == 0) { |
---|
30 | result.Add(item); |
---|
31 | } |
---|
32 | } |
---|
33 | return result; |
---|
34 | } |
---|
35 | } |
---|
36 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.