Changeset 10787
- Timestamp:
- 04/23/14 18:33:14 (11 years ago)
- Location:
- trunk/sources/HeuristicLab.Common/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Common/3.3/ArrayExtensions.cs
r10324 r10787 26 26 namespace HeuristicLab.Common { 27 27 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)); 30 31 } 31 32 32 public static void StableSort<T>(this T[] values, int index, int count) {33 var sorted Array = values.Skip(index).Take(count).OrderBy(x => x).ToArray();34 Array.ConstrainedCopy(sorted Array, 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); 35 36 } 36 37 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)); 39 40 } 40 41 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); 44 48 } 45 49 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> { 56 51 public StableSortComparer(Comparison<T> comparison) { 57 52 this.comparison = comparison; -
trunk/sources/HeuristicLab.Common/3.3/ListExtensions.cs
r10477 r10787 26 26 namespace HeuristicLab.Common { 27 27 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)); 30 31 } 31 32 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); 34 48 int i = index; 35 49 foreach (var e in orderedList) … … 37 51 } 38 52 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> { 62 54 public StableSortComparer(Comparison<T> comparison) { 63 55 this.comparison = comparison;
Note: See TracChangeset
for help on using the changeset viewer.