|
@@ -86,8 +86,8 @@ public class ListSort<T> {
|
|
*/
|
|
*/
|
|
private int nbRuns = 0;
|
|
private int nbRuns = 0;
|
|
|
|
|
|
- /* Try to used a kind of structure like in the original implementation.
|
|
|
|
- * Ended up using 2 arrays as done in the java 7 Timsort.
|
|
|
|
|
|
+ /* Tried to use a struct, as in the original implementation, but
|
|
|
|
+ * ended up using 2 arrays, like the Java7 Timsort.
|
|
* Original implementation used a struct, but instantiation of this inner
|
|
* Original implementation used a struct, but instantiation of this inner
|
|
* class + array was a convoluted pain.
|
|
* class + array was a convoluted pain.
|
|
*/
|
|
*/
|
|
@@ -138,13 +138,13 @@ public class ListSort<T> {
|
|
/*
|
|
/*
|
|
* We allocate a temp array of half the size of the array to sort.
|
|
* We allocate a temp array of half the size of the array to sort.
|
|
* the original implementation had a 256 maximum size for this and made
|
|
* the original implementation had a 256 maximum size for this and made
|
|
- * the temp array grow on demand
|
|
|
|
|
|
+ * the temp array grow on demand.
|
|
*
|
|
*
|
|
* Timsort consumes half the size of the original array to merge at WORST.
|
|
* Timsort consumes half the size of the original array to merge at WORST.
|
|
- * But considering we use the same temp array over and over across frames
|
|
|
|
- * There is a good chance we stumble upon the worst case scenario one
|
|
|
|
- * moment or another.
|
|
|
|
- * So we just always take half of the original array size.
|
|
|
|
|
|
+ * But considering we use the same temp array over and over across frames,
|
|
|
|
+ * there is a good chance we will stumble upon the worst-case scenario at one
|
|
|
|
+ * time or another.
|
|
|
|
+ * So we use half of the original array size.
|
|
*/
|
|
*/
|
|
int tmpLen = len >>> 1;
|
|
int tmpLen = len >>> 1;
|
|
|
|
|
|
@@ -155,7 +155,7 @@ public class ListSort<T> {
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
- * this part was taken from java 7 TimSort.
|
|
|
|
|
|
+ * This part was taken from the Java7 TimSort.
|
|
* The original implementation use a stack of length 85, but this seems
|
|
* The original implementation use a stack of length 85, but this seems
|
|
* to boost performance for mid-sized arrays.
|
|
* to boost performance for mid-sized arrays.
|
|
* I changed the numbers so they fit our MIN_SIZE parameter.
|
|
* I changed the numbers so they fit our MIN_SIZE parameter.
|
|
@@ -200,9 +200,9 @@ public class ListSort<T> {
|
|
int remaining = high - low;
|
|
int remaining = high - low;
|
|
|
|
|
|
/*
|
|
/*
|
|
- * If array's size is below min_size we perform a binary insertion sort
|
|
|
|
|
|
+ * If array's size is below min_size, we perform a binary insertion sort,
|
|
* but first we check if some existing ordered pattern exists to reduce
|
|
* but first we check if some existing ordered pattern exists to reduce
|
|
- * the size of data to be sorted
|
|
|
|
|
|
+ * the size of data to be sorted.
|
|
*/
|
|
*/
|
|
if (remaining < MIN_SIZE) {
|
|
if (remaining < MIN_SIZE) {
|
|
int runLength = getRunLength(array, low, high, comparator);
|
|
int runLength = getRunLength(array, low, high, comparator);
|
|
@@ -211,7 +211,7 @@ public class ListSort<T> {
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
- * main iteration : compute minimum run length, then iterate through the
|
|
|
|
|
|
+ * Main iteration: compute minimum run length, then iterate through the
|
|
* array to find runs and merge them until they can be binary sorted
|
|
* array to find runs and merge them until they can be binary sorted
|
|
* if their length < minLength
|
|
* if their length < minLength
|
|
*/
|
|
*/
|
|
@@ -219,8 +219,8 @@ public class ListSort<T> {
|
|
while (remaining != 0) {
|
|
while (remaining != 0) {
|
|
int runLength = getRunLength(array, low, high, comparator);
|
|
int runLength = getRunLength(array, low, high, comparator);
|
|
|
|
|
|
- /* if run length is below the threshold we binary sort the remaining
|
|
|
|
- * elements
|
|
|
|
|
|
+ /* If the run length is below the threshold, binary sort the remaining
|
|
|
|
+ * elements.
|
|
*/
|
|
*/
|
|
if (runLength < minLength) {
|
|
if (runLength < minLength) {
|
|
int newLength = remaining <= minLength ? remaining : minLength;
|
|
int newLength = remaining <= minLength ? remaining : minLength;
|
|
@@ -344,8 +344,8 @@ public class ListSort<T> {
|
|
*/
|
|
*/
|
|
int nbElems = start - left;
|
|
int nbElems = start - left;
|
|
/*
|
|
/*
|
|
- * grabbed from java7 TimSort, the switch is an optimization to
|
|
|
|
- * arraycopy in case there are 1 or 2 elements only to copy
|
|
|
|
|
|
+ * Grabbed from the Java7 TimSort, this switch optimizes
|
|
|
|
+ * arraycopy() in case there are only 1 or 2 elements to copy.
|
|
*/
|
|
*/
|
|
switch (nbElems) {
|
|
switch (nbElems) {
|
|
case 2:
|
|
case 2:
|
|
@@ -514,9 +514,9 @@ public class ListSort<T> {
|
|
while (offset < maxOffset && comparator.compare(key, array[idx + hint + offset]) > 0) {
|
|
while (offset < maxOffset && comparator.compare(key, array[idx + hint + offset]) > 0) {
|
|
lastOffset = offset;
|
|
lastOffset = offset;
|
|
offset = (offset << 1) + 1;
|
|
offset = (offset << 1) + 1;
|
|
- /* int overflow.
|
|
|
|
- * Note : not sure if that can happen but it's here in both
|
|
|
|
- * original and java 7 TimSort implementation
|
|
|
|
|
|
+ /* int overflow.
|
|
|
|
+ * Note: not sure if this can happen, but it's included in both the
|
|
|
|
+ * original and Java7 TimSort implementations.
|
|
*/
|
|
*/
|
|
if (offset <= 0) {
|
|
if (offset <= 0) {
|
|
offset = maxOffset;
|
|
offset = maxOffset;
|
|
@@ -526,7 +526,7 @@ public class ListSort<T> {
|
|
offset = maxOffset;
|
|
offset = maxOffset;
|
|
}
|
|
}
|
|
|
|
|
|
- // Translate back to offsets relative to idx.
|
|
|
|
|
|
+ // Translate back into offsets relative to idx.
|
|
lastOffset += hint;
|
|
lastOffset += hint;
|
|
offset += hint;
|
|
offset += hint;
|
|
} else {
|
|
} else {
|
|
@@ -537,9 +537,9 @@ public class ListSort<T> {
|
|
while (offset < maxOffset && comparator.compare(key, array[idx + hint - offset]) <= 0) {
|
|
while (offset < maxOffset && comparator.compare(key, array[idx + hint - offset]) <= 0) {
|
|
lastOffset = offset;
|
|
lastOffset = offset;
|
|
offset = (offset << 1) + 1;
|
|
offset = (offset << 1) + 1;
|
|
- /* int overflow.
|
|
|
|
- * Note : not sure if that can happen but it's here in both
|
|
|
|
- * original and java 7 TimSort implementation
|
|
|
|
|
|
+ /* int overflow.
|
|
|
|
+ * Note: not sure if this can happen, but it's included in both the
|
|
|
|
+ * original and Java7 TimSort implementations.
|
|
*/
|
|
*/
|
|
if (offset <= 0) {
|
|
if (offset <= 0) {
|
|
offset = maxOffset;
|
|
offset = maxOffset;
|
|
@@ -606,8 +606,8 @@ public class ListSort<T> {
|
|
lastOffset = offset;
|
|
lastOffset = offset;
|
|
offset = (offset << 1) + 1;
|
|
offset = (offset << 1) + 1;
|
|
/* int overflow.
|
|
/* int overflow.
|
|
- * Note : not sure if that can happen but it's here in both
|
|
|
|
- * original and java 7 TimSort implementation
|
|
|
|
|
|
+ * Note: not sure if this can happen, but it's included in both
|
|
|
|
+ * the original and Java7 TimSort implementations.
|
|
*/
|
|
*/
|
|
if (offset <= 0) {
|
|
if (offset <= 0) {
|
|
offset = maxOffset;
|
|
offset = maxOffset;
|