| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Internal |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Replacements for System.Linq to avoid an unnecessary dependency. |
| | | 10 | | /// Parameter and return types strengthened to actual internal usage as an optimization. |
| | | 11 | | /// </summary> |
| | | 12 | | internal static class EnumerableExtensions |
| | | 13 | | { |
| | | 14 | | // used only in debugger display so we needn't get fancy with optimizations. |
| | | 15 | | public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TRes |
| | 0 | 16 | | { |
| | 0 | 17 | | foreach (var item in source) |
| | 0 | 18 | | { |
| | 0 | 19 | | yield return selector(item); |
| | 0 | 20 | | } |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public static IEnumerable<T> OrderBy<T>(this List<T> source, Comparison<T> comparison) |
| | 0 | 24 | | { |
| | | 25 | | // Produce an iterator that represents a stable sort of source. |
| | | 26 | | // Implement by creating an int array that represents the initial ordering of elements in |
| | | 27 | | // the source list, then sort those integers with a sort function that sorts by the values |
| | | 28 | | // in source, but for cases where the values are equivalent, sort by initial index in |
| | | 29 | | // the source array |
| | 0 | 30 | | int[] map = new int[source.Count]; |
| | 0 | 31 | | for (int i = 0; i < map.Length; i++) |
| | 0 | 32 | | map[i] = i; |
| | | 33 | | |
| | 0 | 34 | | Array.Sort(map, (int left, int right) => |
| | 0 | 35 | | { |
| | 0 | 36 | | if (left == right) |
| | 0 | 37 | | return 0; |
| | 0 | 38 | | |
| | 0 | 39 | | int result = comparison(source[left], source[right]); |
| | 0 | 40 | | if (result == 0) |
| | 0 | 41 | | { |
| | 0 | 42 | | return left - right; |
| | 0 | 43 | | } |
| | 0 | 44 | | return result; |
| | 0 | 45 | | }); |
| | | 46 | | |
| | 0 | 47 | | foreach (int index in map) |
| | 0 | 48 | | { |
| | 0 | 49 | | yield return source[index]; |
| | 0 | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |