FasterFinnSort: change access

This commit is contained in:
Daniel Langbein 2025-01-21 13:10:12 +00:00
parent 42a0c24f67
commit 9a6359d576
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002

View File

@ -81,7 +81,7 @@ public class FasterFinnSort<T> {
* of the minimum stack length required as a function of the length * of the minimum stack length required as a function of the length
* of the array being sorted and the minimum merge sequence length. * of the array being sorted and the minimum merge sequence length.
*/ */
private static final int MIN_MERGE = 32; static final int MIN_MERGE = 32;
/** /**
* The array being sorted. * The array being sorted.
@ -134,7 +134,7 @@ public class FasterFinnSort<T> {
* so we could cut the storage for this, but it's a minor amount, * so we could cut the storage for this, but it's a minor amount,
* and keeping all the info explicit simplifies the code. * and keeping all the info explicit simplifies the code.
*/ */
private int stackSize = 0; // Number of pending runs on stack int stackSize = 0; // Number of pending runs on stack
private final int[] runBase; private final int[] runBase;
private final int[] runLen; private final int[] runLen;
private final int[] runPower; private final int[] runPower;
@ -148,7 +148,7 @@ public class FasterFinnSort<T> {
* @param workBase origin of usable space in work array * @param workBase origin of usable space in work array
* @param workLen usable size of work array * @param workLen usable size of work array
*/ */
private FasterFinnSort(T[] a, Comparator<? super T> c, T[] work, int workBase, int workLen) { FasterFinnSort(T[] a, Comparator<? super T> c, T[] work, int workBase, int workLen) {
this.a = a; this.a = a;
this.c = c; this.c = c;
@ -234,7 +234,7 @@ public class FasterFinnSort<T> {
* to maintain stack invariant. * to maintain stack invariant.
*/ */
FasterFinnSort<T> ts = new FasterFinnSort<>(a, c, work, workBase, workLen); FasterFinnSort<T> ts = new FasterFinnSort<>(a, c, work, workBase, workLen);
int minRun = minRunLength(nRemaining); int minRun = ts.minRunLength(nRemaining);
do { do {
// Identify next run // Identify next run
int runLen = countRunAndMakeAscending(a, lo, hi, c); int runLen = countRunAndMakeAscending(a, lo, hi, c);
@ -280,7 +280,7 @@ public class FasterFinnSort<T> {
* @param c comparator to used for the sort * @param c comparator to used for the sort
*/ */
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
private static <T> void binarySort(T[] a, int lo, int hi, int start, static <T> void binarySort(T[] a, int lo, int hi, int start,
Comparator<? super T> c) { Comparator<? super T> c) {
assert lo <= start && start <= hi; assert lo <= start && start <= hi;
if (start == lo) if (start == lo)
@ -350,7 +350,7 @@ public class FasterFinnSort<T> {
* @return the length of the run beginning at the specified position in * @return the length of the run beginning at the specified position in
* the specified array * the specified array
*/ */
private static <T> int countRunAndMakeAscending(T[] a, int lo, int hi, static <T> int countRunAndMakeAscending(T[] a, int lo, int hi,
Comparator<? super T> c) { Comparator<? super T> c) {
assert lo < hi; assert lo < hi;
int runHi = lo + 1; int runHi = lo + 1;
@ -403,7 +403,7 @@ public class FasterFinnSort<T> {
* @param n the length of the array to be sorted * @param n the length of the array to be sorted
* @return the length of the minimum run to be merged * @return the length of the minimum run to be merged
*/ */
private static int minRunLength(int n) { int minRunLength(int n) {
assert n >= 0; assert n >= 0;
int r = 0; // Becomes 1 if any 1 bits are shifted off int r = 0; // Becomes 1 if any 1 bits are shifted off
while (n >= MIN_MERGE) { while (n >= MIN_MERGE) {
@ -419,7 +419,7 @@ public class FasterFinnSort<T> {
* @param runBase index of the first element in the run * @param runBase index of the first element in the run
* @param runLen the number of elements in the run * @param runLen the number of elements in the run
*/ */
private void pushRun(int runBase, int runLen, int rangeSize) { void pushRun(int runBase, int runLen, int rangeSize) {
this.runBase[stackSize] = runBase; this.runBase[stackSize] = runBase;
this.runLen[stackSize] = runLen; this.runLen[stackSize] = runLen;
this.runPower[stackSize] = power(stackSize, rangeSize); this.runPower[stackSize] = power(stackSize, rangeSize);
@ -442,7 +442,7 @@ public class FasterFinnSort<T> {
* the analysis in "On the Worst-Case Complexity of TimSort" by * the analysis in "On the Worst-Case Complexity of TimSort" by
* Nicolas Auger, Vincent Jug, Cyril Nicaud, and Carine Pivoteau. * Nicolas Auger, Vincent Jug, Cyril Nicaud, and Carine Pivoteau.
*/ */
private void mergeCollapse() { void mergeCollapse() {
while (stackSize > 1) { while (stackSize > 1) {
int n = stackSize - 2; int n = stackSize - 2;
if (n > 0 && runPower[n + 1] < runPower[n]) { if (n > 0 && runPower[n + 1] < runPower[n]) {
@ -453,7 +453,7 @@ public class FasterFinnSort<T> {
} }
} }
private int power(int stackSize, int rangeSize) { int power(int stackSize, int rangeSize) {
if (stackSize == 0) if (stackSize == 0)
return 0; return 0;
@ -500,7 +500,7 @@ public class FasterFinnSort<T> {
* Merges all runs on the stack until only one remains. This method is * Merges all runs on the stack until only one remains. This method is
* called once, to complete the sort. * called once, to complete the sort.
*/ */
private void mergeForceCollapse() { void mergeForceCollapse() {
while (stackSize > 1) { while (stackSize > 1) {
int n = stackSize - 2; int n = stackSize - 2;
if (n > 0 && runLen[n - 1] < runLen[n + 1]) if (n > 0 && runLen[n - 1] < runLen[n + 1])