Skip to content

Commit 9439ee2

Browse files
committed
Add TimSorterSettings for configuration and flexibility
Refactored TimSorter to introduce a TimSorterSettings class, which encapsulates configuration parameters like minMerge and minGallop. This change separates configuration concerns from the sorting logic, improving code readability, maintainability, and testability. - Introduced TimSorterSettings class with minMerge and minGallop parameters. - Updated TimSorter constructor to accept a settings object for configuration. - Enhanced testability by allowing customizable settings for different test scenarios. - Simplified TimSorter’s constructor and reduced parameter clutter. - Facilitated future scalability by allowing easy extension of configuration options. This change adheres to the Single Responsibility Principle (SRP) and improves flexibility in sorting behavior across different contexts.
1 parent af9eb69 commit 9439ee2

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Algorithms.Tests/Sorters/Comparison/TimSorterTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void ArraySorted(
1515
[Random(0, 10_000, 2000)] int n)
1616
{
1717
// Arrange
18-
var sorter = new TimSorter<int>();
18+
var sorter = new TimSorter<int>(new TimSorterSettings());
1919
var (correctArray, testArray) = RandomHelper.GetArrays(n);
2020

2121
// Act
@@ -30,7 +30,7 @@ public static void ArraySorted(
3030
public static void TinyArray()
3131
{
3232
// Arrange
33-
var sorter = new TimSorter<int>();
33+
var sorter = new TimSorter<int>(new TimSorterSettings());
3434
var tinyArray = new[] { 1 };
3535
var correctArray = new[] { 1 };
3636

@@ -45,7 +45,7 @@ public static void TinyArray()
4545
public static void SmallChunks()
4646
{
4747
// Arrange
48-
var sorter = new TimSorter<int>();
48+
var sorter = new TimSorter<int>(new TimSorterSettings());
4949
var (correctArray, testArray) = RandomHelper.GetArrays(800);
5050
Array.Sort(correctArray, IntComparer);
5151
Array.Sort(testArray, IntComparer);

Algorithms/Sorters/Comparison/TimSorter.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class TimSorter<T> : IComparisonSorter<T>
2727
{
2828
private readonly int minMerge;
2929
private readonly int initMinGallop;
30+
31+
// Pool of reusable TimChunk objects for memory efficiency.
32+
private readonly TimChunk<T>[] chunkPool = new TimChunk<T>[2];
33+
3034
private readonly int[] runBase;
3135
private readonly int[] runLengths;
3236

@@ -50,15 +54,16 @@ private class TimChunk<Tc>
5054
public int Wins { get; set; }
5155
}
5256

53-
public TimSorter(int minMerge = 32, int minGallop = 7)
57+
public TimSorter(TimSorterSettings settings)
5458
{
5559
initMinGallop = minGallop;
56-
this.minMerge = minMerge;
5760
runBase = new int[85];
5861
runLengths = new int[85];
5962

6063
stackSize = 0;
61-
this.minGallop = minGallop;
64+
65+
minGallop = settings.MinGallop;
66+
minMerge = settings.MinMerge;
6267
}
6368

6469
/// <summary>
@@ -631,3 +636,16 @@ private bool GallopMerge(TimChunk<T> left, TimChunk<T> right, ref int dest)
631636
return false;
632637
}
633638
}
639+
640+
public class TimSorterSettings
641+
{
642+
public int MinMerge { get; }
643+
644+
public int MinGallop { get; }
645+
646+
public TimSorterSettings(int minMerge = 32, int minGallop = 7)
647+
{
648+
MinMerge = minMerge;
649+
MinGallop = minGallop;
650+
}
651+
}

0 commit comments

Comments
 (0)