Skip to content

Commit 8a398ca

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 8a398ca

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

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)