Ignore:
Timestamp:
06/12/14 13:26:18 (5 years ago)
Author:
pfleck
Message:
  • Merged trunk into preprocessing branch.
Location:
branches/DataPreprocessing
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing

  • branches/DataPreprocessing/HeuristicLab.Common/3.3/ArrayExtensions.cs

    r10538 r11009  
    2626namespace HeuristicLab.Common {
    2727  public static class ArrayExtensions {
    28     public static void StableSort<T>(this T[] values) {
    29       values.StableSort(0, values.Length);
     28
     29    public static void StableSort<T>(this T[] values, Comparison<T> comparison) {
     30      values.StableSort(new StableSortComparer<T>(comparison));
    3031    }
    3132
    32     public static void StableSort<T>(this T[] values, int index, int count) {
    33       var sortedArray = values.Skip(index).Take(count).OrderBy(x => x).ToArray();
    34       Array.ConstrainedCopy(sortedArray, 0, values, index, count);
     33    public static void StableSort<T>(this T[] values, IComparer<T> comparer = null) {
     34      var sorted = values.OrderBy(x => x, comparer ?? Comparer<T>.Default).ToArray();
     35      Array.ConstrainedCopy(sorted, 0, values, 0, values.Length);
    3536    }
    3637
    37     public static void StableSort<T>(this T[] values, IComparer<T> comparer) {
    38       values.StableSort(0, values.Length, comparer);
     38    public static void StableSort<T>(this T[] values, int index, int length, Comparison<T> comparison) {
     39      values.StableSort(index, length, new StableSortComparer<T>(comparison));
    3940    }
    4041
    41     public static void StableSort<T>(this T[] values, int index, int count, IComparer<T> comparer) {
    42       var sortedArray = values.Skip(index).Take(count).OrderBy(x => x, comparer).ToArray();
    43       Array.ConstrainedCopy(sortedArray, 0, values, index, count);
     42    public static void StableSort<T>(this T[] values, int index, int length, IComparer<T> comparer = null) {
     43      if (index < 0) throw new ArgumentOutOfRangeException("index is less than zero.");
     44      if (length < 0) throw new ArgumentOutOfRangeException("length is less than zero.");
     45      if (index + length > values.Length) throw new ArgumentException("index and length do not specify a valid range in the array.");
     46      var sortedArray = values.Skip(index).Take(length).OrderBy(x => x, comparer ?? Comparer<T>.Default).ToArray();
     47      Array.ConstrainedCopy(sortedArray, 0, values, index, length);
    4448    }
    4549
    46     public static void StableSort<T>(this T[] values, Comparison<T> comparison) {
    47       values.StableSort(0, values.Length, comparison);
    48     }
    49 
    50     public static void StableSort<T>(this T[] values, int index, int count, Comparison<T> comparison) {
    51       var sortedArray = values.Skip(index).Take(count).OrderBy(x => x, new StableSortComparer<T>(comparison)).ToArray();
    52       Array.ConstrainedCopy(sortedArray, 0, values, index, count);
    53     }
    54 
    55     public class StableSortComparer<T> : IComparer<T> {
     50    private class StableSortComparer<T> : IComparer<T> {
    5651      public StableSortComparer(Comparison<T> comparison) {
    5752        this.comparison = comparison;
Note: See TracChangeset for help on using the changeset viewer.