Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.3.2/SimSharp-3.3.2/Collections/Extensions.cs @ 18190

Last change on this file since 18190 was 18023, checked in by jkarder, 3 years ago

#3065: update Sim# to 3.3.2

File size: 1.0 KB
RevLine 
[18023]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
8using System;
9using System.Collections.Generic;
10using System.Linq;
11
12namespace 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.