Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.3.1/SimSharp-3.3.1/Collections/Extensions.cs @ 17487

Last change on this file since 17487 was 17487, checked in by abeham, 4 years ago

#3065: update Sim# to 3.3.1

File size: 1.1 KB
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
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.