From 48dce94f478d5857823f6e0fd5006ee3e63d496d Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Wed, 2 Nov 2022 18:54:21 +0000 Subject: [PATCH] Added an IList extensions class that can sort IList using the insertion sort algorithm git-svn-id: file:///srv/devel/repo-conversion/nusu@337 d2e56fa2-650e-0410-a79f-9358c0239efd --- Nuclex.Support (net-4.0).csproj | 2 + Source/Collections/IListExtensions.Test.cs | 67 +++++++++++++++++ Source/Collections/IListExtensions.cs | 86 ++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 Source/Collections/IListExtensions.Test.cs create mode 100644 Source/Collections/IListExtensions.cs diff --git a/Nuclex.Support (net-4.0).csproj b/Nuclex.Support (net-4.0).csproj index 1b25d10..346adfe 100644 --- a/Nuclex.Support (net-4.0).csproj +++ b/Nuclex.Support (net-4.0).csproj @@ -105,6 +105,8 @@ Deque.cs + + diff --git a/Source/Collections/IListExtensions.Test.cs b/Source/Collections/IListExtensions.Test.cs new file mode 100644 index 0000000..ae1b27e --- /dev/null +++ b/Source/Collections/IListExtensions.Test.cs @@ -0,0 +1,67 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2017 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; +using System.IO; + +#if UNITTEST + +using NUnit.Framework; + +namespace Nuclex.Support.Collections { + + /// Unit Test for the IList extension methods + [TestFixture] + internal class IListExtensionsTest { + + /// Tests whether the insertion sort algorithm can be applied to 'Text' property works as expected + [Test] + public void InsertionSortCanSortWholeList() { + var testList = new List(capacity: 5) { 1, 5, 2, 4, 3 }; + var testListAsIList = (IList)testList; + + testListAsIList.InsertionSort(); + + CollectionAssert.AreEqual( + new List(capacity: 5) { 1, 2, 3, 4, 5 }, + testList + ); + } + + /// Tests whether the 'Text' property works as expected + [Test] + public void InsertionSortCanSortListSegment() { + var testList = new List(capacity: 7) { 9, 1, 5, 2, 4, 3, 0 }; + var testListAsIList = (IList)testList; + + testListAsIList.InsertionSort(1, 5, Comparer.Default); + + CollectionAssert.AreEqual( + new List(capacity: 7) { 9, 1, 2, 3, 4, 5, 0 }, + testList + ); + } + + } + +} // namespace Nuclex.Support.Collections + +#endif // UNITTEST diff --git a/Source/Collections/IListExtensions.cs b/Source/Collections/IListExtensions.cs new file mode 100644 index 0000000..c00e025 --- /dev/null +++ b/Source/Collections/IListExtensions.cs @@ -0,0 +1,86 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2017 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Nuclex.Support.Collections { + + /// Extension methods for the IList interface + public static class ListExtensions { + + /// + /// Sorts a subset of the elements in an IList<T> using the insertion sort algorithm + /// + /// Type of elements the list contains + /// List in which a subset will be sorted + /// Index at which the sorting process will begin + /// Index one past the last element that will be sorted + /// Comparison function to use for comparing list elements + public static void InsertionSort( + this IList list, int startIndex, int count, IComparer comparer + ) { + int index = startIndex; + int endIndex = startIndex + count - 1; + + while(index < endIndex) { + int rightIndex = index; + + ++index; + TElement temp = list[index]; + + while(rightIndex >= startIndex) { + if(comparer.Compare(list[rightIndex], temp) < 0) { + break; + } + + list[rightIndex + 1] = list[rightIndex]; + --rightIndex; + } + + list[rightIndex + 1] = temp; + } + } + + /// + /// Sorts all the elements in an IList<T> using the insertion sort algorithm + /// + /// Type of elements the list contains + /// List in which a subset will be sorted + /// Comparison function to use for comparing list elements + public static void InsertionSort( + this IList list, IComparer comparer + ) { + InsertionSort(list, 0, list.Count, comparer); + } + + /// + /// Sorts all the elements in an IList<T> using the insertion sort algorithm + /// + /// Type of elements the list contains + /// List in which a subset will be sorted + public static void InsertionSort(this IList list) { + InsertionSort(list, Comparer.Default); + } + + } + +} // namespace Nuclex.Support.Collections