Added an IList extensions class that can sort IList<T> using the insertion sort algorithm

git-svn-id: file:///srv/devel/repo-conversion/nusu@337 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2022-11-02 18:54:21 +00:00
parent c7dd57f59f
commit 48dce94f47
3 changed files with 155 additions and 0 deletions

View File

@ -105,6 +105,8 @@
<Compile Include="Source\Collections\Deque.Test.cs"> <Compile Include="Source\Collections\Deque.Test.cs">
<DependentUpon>Deque.cs</DependentUpon> <DependentUpon>Deque.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Collections\IListExtensions.cs" />
<Compile Include="Source\Collections\IListExtensions.Test.cs" />
<Compile Include="Source\Collections\IMultiDictionary.cs" /> <Compile Include="Source\Collections\IMultiDictionary.cs" />
<Compile Include="Source\Collections\IObservableCollection.cs" /> <Compile Include="Source\Collections\IObservableCollection.cs" />
<Compile Include="Source\Collections\IRecyclable.cs" /> <Compile Include="Source\Collections\IRecyclable.cs" />

View File

@ -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 {
/// <summary>Unit Test for the IList extension methods</summary>
[TestFixture]
internal class IListExtensionsTest {
/// <summary>Tests whether the insertion sort algorithm can be applied to 'Text' property works as expected</summary>
[Test]
public void InsertionSortCanSortWholeList() {
var testList = new List<int>(capacity: 5) { 1, 5, 2, 4, 3 };
var testListAsIList = (IList<int>)testList;
testListAsIList.InsertionSort();
CollectionAssert.AreEqual(
new List<int>(capacity: 5) { 1, 2, 3, 4, 5 },
testList
);
}
/// <summary>Tests whether the 'Text' property works as expected</summary>
[Test]
public void InsertionSortCanSortListSegment() {
var testList = new List<int>(capacity: 7) { 9, 1, 5, 2, 4, 3, 0 };
var testListAsIList = (IList<int>)testList;
testListAsIList.InsertionSort(1, 5, Comparer<int>.Default);
CollectionAssert.AreEqual(
new List<int>(capacity: 7) { 9, 1, 2, 3, 4, 5, 0 },
testList
);
}
}
} // namespace Nuclex.Support.Collections
#endif // UNITTEST

View File

@ -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 {
/// <summary>Extension methods for the IList interface</summary>
public static class ListExtensions {
/// <summary>
/// Sorts a subset of the elements in an IList&lt;T&gt; using the insertion sort algorithm
/// </summary>
/// <typeparam name="TElement">Type of elements the list contains</typeparam>
/// <param name="list">List in which a subset will be sorted</param>
/// <param name="startIndex">Index at which the sorting process will begin</param>
/// <param name="count">Index one past the last element that will be sorted</param>
/// <param name="comparer">Comparison function to use for comparing list elements</param>
public static void InsertionSort<TElement>(
this IList<TElement> list, int startIndex, int count, IComparer<TElement> 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;
}
}
/// <summary>
/// Sorts all the elements in an IList&lt;T&gt; using the insertion sort algorithm
/// </summary>
/// <typeparam name="TElement">Type of elements the list contains</typeparam>
/// <param name="list">List in which a subset will be sorted</param>
/// <param name="comparer">Comparison function to use for comparing list elements</param>
public static void InsertionSort<TElement>(
this IList<TElement> list, IComparer<TElement> comparer
) {
InsertionSort(list, 0, list.Count, comparer);
}
/// <summary>
/// Sorts all the elements in an IList&lt;T&gt; using the insertion sort algorithm
/// </summary>
/// <typeparam name="TElement">Type of elements the list contains</typeparam>
/// <param name="list">List in which a subset will be sorted</param>
public static void InsertionSort<TElement>(this IList<TElement> list) {
InsertionSort(list, Comparer<TElement>.Default);
}
}
} // namespace Nuclex.Support.Collections