Permalink
Browse files

IRID-191: Sorting to support sorting only a sub-range

  • Loading branch information...
1 parent b24aedc commit 67c9e2f5507b993fe988ce7acf77c6736c961038 @cdrnet cdrnet committed Nov 21, 2008
Showing with 90 additions and 19 deletions.
  1. +1 −19 src/app/MathNet.Iridium/Library/Set.cs
  2. +89 −0 src/app/MathNet.Iridium/Library/Sorting.cs
@@ -1214,25 +1214,7 @@ IComparer<T> comparer
IComparer<T> comparer
)
{
- // TODO (cdr, 2008-11-21): Forward to commong Sorting class as well.
-
- IList<T> items = Items;
-
- List<T> list = items as List<T>;
- if(list != null)
- {
- list.Sort(index, count, comparer);
- return;
- }
-
- T[] array = items as T[];
- if(array != null)
- {
- Array.Sort<T>(array, index, count, comparer);
- return;
- }
-
- throw new NotSupportedException();
+ Sorting.Sort(Items, index, count, comparer);
}
#endregion
@@ -55,6 +55,23 @@ IList<TItem2> items2
}
/// <summary>
+ /// Sort a range of a list of keys, inplace.
+ /// </summary>
+ /// <param name="keys">List to sort.</param>
+ /// <param name="index">The zero-based starting index of the range to sort.</param>
+ /// <param name="count">The length of the range to sort.</param>
+ public static
+ void
+ Sort<T>(
+ IList<T> keys,
+ int index,
+ int count
+ )
+ {
+ Sort(keys, index, count, Comparer<T>.Default);
+ }
+
+ /// <summary>
/// Sort a list of keys, inplace.
/// </summary>
/// <param name="keys">List to sort.</param>
@@ -194,6 +211,78 @@ IComparer<TKey> comparer
QuickSort(keys, items1, items2, comparer, 0, keys.Count - 1);
}
+ /// <summary>
+ /// Sort a range of a list of keys, inplace.
+ /// </summary>
+ /// <param name="keys">List to sort.</param>
+ /// <param name="index">The zero-based starting index of the range to sort.</param>
+ /// <param name="count">The length of the range to sort.</param>
+ /// <param name="comparer">Comparison, defining the sort order.</param>
+ public static
+ void
+ Sort<T>(
+ IList<T> keys,
+ int index,
+ int count,
+ IComparer<T> comparer
+ )
+ {
+ if(null == keys)
+ {
+ throw new ArgumentNullException("keys");
+ }
+
+ if(null == comparer)
+ {
+ throw new ArgumentNullException("comparer");
+ }
+
+ if(index < 0 || index >= keys.Count)
+ {
+ throw new ArgumentOutOfRangeException("index");
+ }
+
+ if(count < 0 || index + count > keys.Count)
+ {
+ throw new ArgumentOutOfRangeException("count");
+ }
+
+ // basic cases
+ if(count <= 1)
+ {
+ return;
+ }
+
+ if(count == 2)
+ {
+ if(comparer.Compare(keys[index], keys[index + 1]) > 0)
+ {
+ Swap(keys, index, index + 1);
+ }
+
+ return;
+ }
+
+ // generic list case
+ List<T> list = keys as List<T>;
+ if(null != list)
+ {
+ list.Sort(index, count, comparer);
+ return;
+ }
+
+ // array case
+ T[] array = keys as T[];
+ if(null != array)
+ {
+ Array.Sort(array, index, count, comparer);
+ return;
+ }
+
+ // local sort implementation
+ QuickSort(keys, comparer, index, count - 1);
+ }
+
static
void
QuickSort<T>(

0 comments on commit 67c9e2f

Please sign in to comment.