Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/23/14 18:33:14 (10 years ago)
Author:
abeham
Message:

#2106: Changed StableSort extension methods:

  • Parameters index and count were ignored in List<T>.StableSort(int, int, Comparison<T>)
  • Made both StableSortComparer<T> private classes
  • Added sanity checks similar to those of Array.Sort and List<T>.Sort
    • Before it was possible that index + count was bigger than the size of the list
  • Reduced number of method overloads to 2 by introducing IComparer<T> as optional parameter
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Common/3.3/ListExtensions.cs

    r10477 r10787  
    2626namespace HeuristicLab.Common {
    2727  public static class ListExtensions {
    28     public static void StableSort<T>(this List<T> values) {
    29       values.StableSort(0, values.Count);
     28
     29    public static void StableSort<T>(this List<T> values, Comparison<T> comparison) {
     30      values.StableSort(new StableSortComparer<T>(comparison));
    3031    }
    3132
    32     public static void StableSort<T>(this List<T> values, int index, int count) {
    33       var orderedList = values.Skip(index).Take(count).OrderBy(x => x);
     33    public static void StableSort<T>(this List<T> values, IComparer<T> comparer = null) {
     34      int i = 0;
     35      foreach (var e in values.OrderBy(x => x, comparer ?? Comparer<T>.Default))
     36        values[i++] = e;
     37    }
     38
     39    public static void StableSort<T>(this List<T> values, int index, int count, Comparison<T> comparison) {
     40      values.StableSort(index, count, new StableSortComparer<T>(comparison));
     41    }
     42
     43    public static void StableSort<T>(this List<T> values, int index, int count, IComparer<T> comparer = null) {
     44      if (index < 0) throw new ArgumentOutOfRangeException("index is less than zero.");
     45      if (count < 0) throw new ArgumentOutOfRangeException("count is less than zero.");
     46      if (index + count > values.Count) throw new ArgumentException("index and count do not specify a valid range in the List<T>.");
     47      var orderedList = values.Skip(index).Take(count).OrderBy(x => x, comparer ?? Comparer<T>.Default);
    3448      int i = index;
    3549      foreach (var e in orderedList)
     
    3751    }
    3852
    39     public static void StableSort<T>(this List<T> values, IComparer<T> comparer) {
    40       values.StableSort(0, values.Count, comparer);
    41     }
    42 
    43     public static void StableSort<T>(this List<T> values, int index, int count, IComparer<T> comparer) {
    44       var orderedList = values.OrderBy(x => x, comparer);
    45       int i = 0;
    46       foreach (var e in orderedList)
    47         values[i++] = e;
    48     }
    49 
    50     public static void StableSort<T>(this List<T> values, Comparison<T> comparison) {
    51       values.StableSort(0, values.Count, comparison);
    52     }
    53 
    54     public static void StableSort<T>(this List<T> values, int index, int count, Comparison<T> comparison) {
    55       var orderedList = values.OrderBy(x => x, new StableSortComparer<T>(comparison));
    56       int i = 0;
    57       foreach (var e in orderedList)
    58         values[i++] = e;
    59     }
    60 
    61     public class StableSortComparer<T> : IComparer<T> {
     53    private class StableSortComparer<T> : IComparer<T> {
    6254      public StableSortComparer(Comparison<T> comparison) {
    6355        this.comparison = comparison;
Note: See TracChangeset for help on using the changeset viewer.