test/jdk/java/util/Arrays/Sorting.java
author bchristi
Tue, 12 Nov 2019 13:49:40 -0800
changeset 59042 8910b995a2ee
parent 47216 71c04702a3d5
permissions -rw-r--r--
8226297: Dual-pivot quicksort improvements Reviewed-by: dl, lbourges Contributed-by: Vladimir Yaroslavskiy <vlv.spb.ru@mail.ru>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     1
/*
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
     2
 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     4
 *
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     8
 *
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    13
 * accompanied this code).
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    14
 *
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4233
diff changeset
    21
 * questions.
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    22
 */
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    23
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    24
/*
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    25
 * @test
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    26
 * @compile/module=java.base java/util/SortingHelper.java
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    27
 * @bug 6880672 6896573 6899694 6976036 7013585 7018258 8003981 8226297
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    28
 * @build Sorting
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    29
 * @run main Sorting -shortrun
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    30
 * @summary Exercise Arrays.sort, Arrays.parallelSort
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    31
 *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    32
 * @author Vladimir Yaroslavskiy
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    33
 * @author Jon Bentley
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    34
 * @author Josh Bloch
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    35
 */
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    36
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    37
import java.io.PrintStream;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    38
import java.util.Comparator;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    39
import java.util.Random;
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    40
import java.util.SortingHelper;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    41
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    42
public class Sorting {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    43
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    44
    private static final PrintStream out = System.out;
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    45
    private static final PrintStream err = System.err;
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    46
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    47
    // Array lengths used in a long run (default)
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    48
    private static final int[] LONG_RUN_LENGTHS = {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    49
        1, 3, 8, 21, 55, 100, 1_000, 10_000, 100_000 };
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    50
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    51
    // Array lengths used in a short run
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
    52
    private static final int[] SHORT_RUN_LENGTHS = {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    53
        1, 8, 55, 100, 10_000 };
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    54
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    55
    // Random initial values used in a long run (default)
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    56
    private static final TestRandom[] LONG_RUN_RANDOMS = {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    57
        TestRandom.BABA, TestRandom.DEDA, TestRandom.C0FFEE };
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    58
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    59
    // Random initial values used in a short run
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    60
    private static final TestRandom[] SHORT_RUN_RANDOMS = {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    61
        TestRandom.C0FFEE };
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    62
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    63
    // Constants used in subarray sorting
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    64
    private static final int A380 = 0xA380;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    65
    private static final int B747 = 0xB747;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    66
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    67
    private final SortingHelper sortingHelper;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    68
    private final TestRandom[] randoms;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    69
    private final int[] lengths;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    70
    private Object[] gold;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    71
    private Object[] test;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    72
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    73
    public static void main(String[] args) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    74
        long start = System.currentTimeMillis();
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    75
        boolean shortRun = args.length > 0 && args[0].equals("-shortrun");
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    76
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    77
        int[] lengths = shortRun ? SHORT_RUN_LENGTHS : LONG_RUN_LENGTHS;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    78
        TestRandom[] randoms = shortRun ? SHORT_RUN_RANDOMS : LONG_RUN_RANDOMS;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    79
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    80
        new Sorting(SortingHelper.DUAL_PIVOT_QUICKSORT, randoms, lengths).testCore();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    81
        new Sorting(SortingHelper.PARALLEL_SORT, randoms, lengths).testCore();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    82
        new Sorting(SortingHelper.HEAP_SORT, randoms, lengths).testBasic();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    83
        new Sorting(SortingHelper.ARRAYS_SORT, randoms, lengths).testAll();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    84
        new Sorting(SortingHelper.ARRAYS_PARALLEL_SORT, randoms, lengths).testAll();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    85
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    86
        long end = System.currentTimeMillis();
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    87
        out.format("PASSED in %d sec.\n", (end - start) / 1000);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
    88
    }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
    89
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    90
    private Sorting(SortingHelper sortingHelper, TestRandom[] randoms, int[] lengths) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    91
        this.sortingHelper = sortingHelper;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    92
        this.randoms = randoms;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    93
        this.lengths = lengths;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    94
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    95
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    96
    private void testBasic() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
    97
        testEmptyArray();
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
    98
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
    99
        for (int length : lengths) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   100
            createData(length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   101
            testBasic(length);
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   102
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   103
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   104
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   105
    private void testBasic(int length) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   106
        for (TestRandom random : randoms) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   107
            testWithInsertionSort(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   108
            testWithCheckSum(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   109
            testWithScrambling(length, random);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   110
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   111
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   112
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   113
    private void testCore() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   114
        for (int length : lengths) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   115
            createData(length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   116
            testCore(length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   117
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   118
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   119
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   120
    private void testCore(int length) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   121
        testBasic(length);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   122
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   123
        for (TestRandom random : randoms) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   124
            testMergingSort(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   125
            testSubArray(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   126
            testNegativeZero(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   127
            testFloatingPointSorting(length, random);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   128
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   129
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   130
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   131
    private void testAll() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   132
        for (int length : lengths) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   133
            createData(length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   134
            testAll(length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   135
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   136
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   137
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   138
    private void testAll(int length) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   139
        testCore(length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   140
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   141
        for (TestRandom random : randoms) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   142
            testRange(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   143
            testStability(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   144
        }
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   145
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   146
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   147
    private void testEmptyArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   148
        testEmptyAndNullIntArray();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   149
        testEmptyAndNullLongArray();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   150
        testEmptyAndNullByteArray();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   151
        testEmptyAndNullCharArray();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   152
        testEmptyAndNullShortArray();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   153
        testEmptyAndNullFloatArray();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   154
        testEmptyAndNullDoubleArray();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   155
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   156
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   157
    private void testStability(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   158
        printTestName("Test stability", random, length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   159
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   160
        Pair[] a = build(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   161
        sortingHelper.sort(a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   162
        checkSorted(a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   163
        checkStable(a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   164
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   165
        a = build(length, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   166
        sortingHelper.sort(a, pairComparator);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   167
        checkSorted(a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   168
        checkStable(a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   169
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   170
        out.println();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   171
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   172
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   173
    private void testEmptyAndNullIntArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   174
        sortingHelper.sort(new int[] {});
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   175
        sortingHelper.sort(new int[] {}, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   176
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   177
        try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   178
            sortingHelper.sort(null);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   179
        } catch (NullPointerException expected) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   180
            try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   181
                sortingHelper.sort(null, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   182
            } catch (NullPointerException expected2) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   183
                return;
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   184
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   185
            fail(sortingHelper + "(int[],fromIndex,toIndex) shouldn't " +
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   186
                "catch null array");
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   187
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   188
        fail(sortingHelper + "(int[]) shouldn't catch null array");
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   189
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   190
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   191
    private void testEmptyAndNullLongArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   192
        sortingHelper.sort(new long[] {});
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   193
        sortingHelper.sort(new long[] {}, 0, 0);
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   194
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   195
        try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   196
            sortingHelper.sort(null);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   197
        } catch (NullPointerException expected) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   198
            try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   199
                sortingHelper.sort(null, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   200
            } catch (NullPointerException expected2) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   201
                return;
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   202
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   203
            fail(sortingHelper + "(long[],fromIndex,toIndex) shouldn't " +
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   204
                "catch null array");
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   205
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   206
        fail(sortingHelper + "(long[]) shouldn't catch null array");
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   207
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   208
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   209
    private void testEmptyAndNullByteArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   210
        sortingHelper.sort(new byte[] {});
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   211
        sortingHelper.sort(new byte[] {}, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   212
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   213
        try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   214
            sortingHelper.sort(null);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   215
        } catch (NullPointerException expected) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   216
            try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   217
                sortingHelper.sort(null, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   218
            } catch (NullPointerException expected2) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   219
                return;
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   220
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   221
            fail(sortingHelper + "(byte[],fromIndex,toIndex) shouldn't " +
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   222
                "catch null array");
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   223
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   224
        fail(sortingHelper + "(byte[]) shouldn't catch null array");
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   225
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   226
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   227
    private void testEmptyAndNullCharArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   228
        sortingHelper.sort(new char[] {});
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   229
        sortingHelper.sort(new char[] {}, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   230
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   231
        try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   232
            sortingHelper.sort(null);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   233
        } catch (NullPointerException expected) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   234
            try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   235
                sortingHelper.sort(null, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   236
            } catch (NullPointerException expected2) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   237
                return;
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   238
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   239
            fail(sortingHelper + "(char[],fromIndex,toIndex) shouldn't " +
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   240
                "catch null array");
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   241
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   242
        fail(sortingHelper + "(char[]) shouldn't catch null array");
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   243
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   244
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   245
    private void testEmptyAndNullShortArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   246
        sortingHelper.sort(new short[] {});
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   247
        sortingHelper.sort(new short[] {}, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   248
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   249
        try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   250
            sortingHelper.sort(null);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   251
        } catch (NullPointerException expected) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   252
            try {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   253
                sortingHelper.sort(null, 0, 0);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   254
            } catch (NullPointerException expected2) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   255
                return;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   256
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   257
            fail(sortingHelper + "(short[],fromIndex,toIndex) shouldn't " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   258
                "catch null array");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   259
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   260
        fail(sortingHelper + "(short[]) shouldn't catch null array");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   261
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   262
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   263
    private void testEmptyAndNullFloatArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   264
        sortingHelper.sort(new float[] {});
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   265
        sortingHelper.sort(new float[] {}, 0, 0);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   266
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   267
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   268
            sortingHelper.sort(null);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   269
        } catch (NullPointerException expected) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   270
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   271
                sortingHelper.sort(null, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   272
            } catch (NullPointerException expected2) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   273
                return;
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   274
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   275
            fail(sortingHelper + "(float[],fromIndex,toIndex) shouldn't " +
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   276
                "catch null array");
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   277
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   278
        fail(sortingHelper + "(float[]) shouldn't catch null array");
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   279
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   280
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   281
    private void testEmptyAndNullDoubleArray() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   282
        sortingHelper.sort(new double[] {});
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   283
        sortingHelper.sort(new double[] {}, 0, 0);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   284
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   285
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   286
            sortingHelper.sort(null);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   287
        } catch (NullPointerException expected) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   288
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   289
                sortingHelper.sort(null, 0, 0);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   290
            } catch (NullPointerException expected2) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   291
                return;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   292
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   293
            fail(sortingHelper + "(double[],fromIndex,toIndex) shouldn't " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   294
                "catch null array");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   295
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   296
        fail(sortingHelper + "(double[]) shouldn't catch null array");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   297
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   298
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   299
    private void testSubArray(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   300
        if (length < 4) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   301
            return;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   302
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   303
        for (int m = 1; m < length / 2; m <<= 1) {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   304
            int fromIndex = m;
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   305
            int toIndex = length - m;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   306
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   307
            prepareSubArray((int[]) gold[0], fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   308
            convertData(length);
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   309
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   310
            for (int i = 0; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   311
                printTestName("Test subarray", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   312
                    ", m = " + m + ", " + getType(i));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   313
                sortingHelper.sort(test[i], fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   314
                checkSubArray(test[i], fromIndex, toIndex);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   315
            }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   316
        }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   317
        out.println();
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   318
    }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   319
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   320
    private void testRange(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   321
        if (length < 2) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   322
            return;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   323
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   324
        for (int m = 1; m < length; m <<= 1) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   325
            for (int i = 1; i <= length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   326
                ((int[]) gold[0]) [i - 1] = i % m + m % i;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   327
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   328
            convertData(length);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   329
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   330
            for (int i = 0; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   331
                printTestName("Test range check", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   332
                    ", m = " + m + ", " + getType(i));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   333
                checkRange(test[i], m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   334
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   335
        }
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   336
        out.println();
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   337
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   338
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   339
    private void checkSorted(Pair[] a) {
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   340
        for (int i = 0; i < a.length - 1; i++) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   341
            if (a[i].getKey() > a[i + 1].getKey()) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   342
                fail("Array is not sorted at " + i + "-th position: " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   343
                    a[i].getKey() + " and " + a[i + 1].getKey());
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   344
            }
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   345
        }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   346
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   347
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   348
    private void checkStable(Pair[] a) {
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   349
        for (int i = 0; i < a.length / 4; ) {
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   350
            int key1 = a[i].getKey();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   351
            int value1 = a[i++].getValue();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   352
            int key2 = a[i].getKey();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   353
            int value2 = a[i++].getValue();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   354
            int key3 = a[i].getKey();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   355
            int value3 = a[i++].getValue();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   356
            int key4 = a[i].getKey();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   357
            int value4 = a[i++].getValue();
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   358
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   359
            if (!(key1 == key2 && key2 == key3 && key3 == key4)) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   360
                fail("Keys are different " + key1 + ", " + key2 + ", " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   361
                    key3 + ", " + key4 + " at position " + i);
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   362
            }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   363
            if (!(value1 < value2 && value2 < value3 && value3 < value4)) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   364
                fail("Sorting is not stable at position " + i +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   365
                    ". Second values have been changed: " + value1 + ", " +
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   366
                    value2 + ", " + value3 + ", " + value4);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   367
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   368
        }
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   369
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   370
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   371
    private Pair[] build(int length, Random random) {
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   372
        Pair[] a = new Pair[length * 4];
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   373
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   374
        for (int i = 0; i < a.length; ) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   375
            int key = random.nextInt();
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   376
            a[i++] = new Pair(key, 1);
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   377
            a[i++] = new Pair(key, 2);
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   378
            a[i++] = new Pair(key, 3);
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   379
            a[i++] = new Pair(key, 4);
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   380
        }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   381
        return a;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   382
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   383
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   384
    private void testWithInsertionSort(int length, TestRandom random) {
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   385
        if (length > 1000) {
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   386
            return;
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   387
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   388
        for (int m = 1; m <= length; m <<= 1) {
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   389
            for (UnsortedBuilder builder : UnsortedBuilder.values()) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   390
                builder.build((int[]) gold[0], m, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   391
                convertData(length);
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   392
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   393
                for (int i = 0; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   394
                    printTestName("Test with insertion sort", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   395
                        ", m = " + m + ", " + getType(i) + " " + builder);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   396
                    sortingHelper.sort(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   397
                    sortByInsertionSort(gold[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   398
                    compare(test[i], gold[i]);
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   399
                }
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   400
            }
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   401
        }
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   402
        out.println();
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   403
    }
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
   404
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   405
    private void testMergingSort(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   406
        if (length < (4 << 10)) { // DualPivotQuicksort.MIN_TRY_MERGE_SIZE
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   407
            return;
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   408
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   409
        final int PERIOD = 50;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   410
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   411
        for (int m = PERIOD - 2; m <= PERIOD + 2; m++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   412
            for (MergingBuilder builder : MergingBuilder.values()) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   413
                builder.build((int[]) gold[0], m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   414
                convertData(length);
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   415
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   416
                for (int i = 0; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   417
                    printTestName("Test merging sort", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   418
                        ", m = " + m + ", " +  getType(i) + " " + builder);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   419
                    sortingHelper.sort(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   420
                    checkSorted(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   421
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   422
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   423
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   424
        out.println();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   425
    }
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   426
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   427
    private void testWithCheckSum(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   428
        for (int m = 1; m <= length; m <<= 1) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   429
            for (UnsortedBuilder builder : UnsortedBuilder.values()) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   430
                builder.build((int[]) gold[0], m, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   431
                convertData(length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   432
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   433
                for (int i = 0; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   434
                    printTestName("Test with check sum", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   435
                        ", m = " + m + ", " + getType(i) + " " + builder);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   436
                    sortingHelper.sort(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   437
                    checkWithCheckSum(test[i], gold[i]);
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   438
                }
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   439
            }
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   440
        }
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   441
        out.println();
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   442
    }
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   443
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   444
    private void testWithScrambling(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   445
        for (int m = 1; m <= length; m <<= 1) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   446
            for (SortedBuilder builder : SortedBuilder.values()) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   447
                builder.build((int[]) gold[0], m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   448
                convertData(length);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   449
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   450
                for (int i = 0; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   451
                    printTestName("Test with scrambling", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   452
                        ", m = " + m + ", " + getType(i) + " " + builder);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   453
                    scramble(test[i], random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   454
                    sortingHelper.sort(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   455
                    compare(test[i], gold[i]);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   456
                }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   457
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   458
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   459
        out.println();
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   460
    }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   461
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   462
    private void testNegativeZero(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   463
        for (int i = 5; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   464
            printTestName("Test negative zero -0.0", random, length, " " + getType(i));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   465
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   466
            NegativeZeroBuilder builder = NegativeZeroBuilder.values() [i - 5];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   467
            builder.build(test[i], random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   468
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   469
            sortingHelper.sort(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   470
            checkNegativeZero(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   471
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   472
        out.println();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   473
    }
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   474
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   475
    private void testFloatingPointSorting(int length, TestRandom random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   476
        if (length < 2) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   477
            return;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   478
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   479
        final int MAX = 13;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   480
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   481
        for (int a = 0; a < MAX; a++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   482
            for (int g = 0; g < MAX; g++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   483
                for (int z = 0; z < MAX; z++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   484
                    for (int n = 0; n < MAX; n++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   485
                        for (int p = 0; p < MAX; p++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   486
                            if (a + g + z + n + p != length) {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   487
                                continue;
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   488
                            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   489
                            for (int i = 5; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   490
                                printTestName("Test float-pointing sorting", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   491
                                    ", a = " + a + ", g = " + g + ", z = " + z +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   492
                                    ", n = " + n + ", p = " + p + ", " + getType(i));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   493
                                FloatingPointBuilder builder = FloatingPointBuilder.values()[i - 5];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   494
                                builder.build(gold[i], a, g, z, n, p, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   495
                                copy(test[i], gold[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   496
                                scramble(test[i], random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   497
                                sortingHelper.sort(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   498
                                compare(test[i], gold[i], a, n, g);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   499
                            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   500
                        }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   501
                    }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   502
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   503
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   504
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   505
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   506
        for (int m = 13; m > 4; m--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   507
            int t = length / m;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   508
            int g = t, z = t, n = t, p = t;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   509
            int a = length - g - z - n - p;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   510
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   511
            for (int i = 5; i < test.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   512
                printTestName("Test float-pointing sorting", random, length,
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   513
                    ", a = " + a + ", g = " + g + ", z = " + z +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   514
                    ", n = " + n + ", p = " + p + ", " + getType(i));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   515
                FloatingPointBuilder builder = FloatingPointBuilder.values() [i - 5];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   516
                builder.build(gold[i], a, g, z, n, p, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   517
                copy(test[i], gold[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   518
                scramble(test[i], random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   519
                sortingHelper.sort(test[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   520
                compare(test[i], gold[i], a, n, g);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   521
            }
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   522
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   523
        out.println();
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   524
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   525
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   526
    private void prepareSubArray(int[] a, int fromIndex, int toIndex) {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   527
        for (int i = 0; i < fromIndex; i++) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   528
            a[i] = A380;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   529
        }
8188
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   530
        int middle = (fromIndex + toIndex) >>> 1;
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   531
        int k = 0;
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   532
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   533
        for (int i = fromIndex; i < middle; i++) {
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   534
            a[i] = k++;
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   535
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   536
8188
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   537
        for (int i = middle; i < toIndex; i++) {
b90884cf34f5 7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents: 6896
diff changeset
   538
            a[i] = k--;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   539
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   540
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   541
        for (int i = toIndex; i < a.length; i++) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   542
            a[i] = B747;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   543
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   544
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   545
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   546
    private void scramble(Object a, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   547
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   548
            scramble((int[]) a, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   549
        } else if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   550
            scramble((long[]) a, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   551
        } else if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   552
            scramble((byte[]) a, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   553
        } else if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   554
            scramble((char[]) a, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   555
        } else if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   556
            scramble((short[]) a, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   557
        } else if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   558
            scramble((float[]) a, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   559
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   560
            scramble((double[]) a, random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   561
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   562
            fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   563
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   564
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   565
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   566
    private void scramble(int[] a, Random random) {
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   567
        for (int i = 0; i < a.length * 7; i++) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   568
            swap(a, random.nextInt(a.length), random.nextInt(a.length));
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   569
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   570
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   571
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   572
    private void scramble(long[] a, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   573
        for (int i = 0; i < a.length * 7; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   574
            swap(a, random.nextInt(a.length), random.nextInt(a.length));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   575
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   576
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   577
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   578
    private void scramble(byte[] a, Random random) {
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   579
        for (int i = 0; i < a.length * 7; i++) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   580
            swap(a, random.nextInt(a.length), random.nextInt(a.length));
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   581
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   582
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   583
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   584
    private void scramble(char[] a, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   585
        for (int i = 0; i < a.length * 7; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   586
            swap(a, random.nextInt(a.length), random.nextInt(a.length));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   587
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   588
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   589
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   590
    private void scramble(short[] a, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   591
        for (int i = 0; i < a.length * 7; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   592
            swap(a, random.nextInt(a.length), random.nextInt(a.length));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   593
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   594
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   595
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   596
    private void scramble(float[] a, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   597
        for (int i = 0; i < a.length * 7; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   598
            swap(a, random.nextInt(a.length), random.nextInt(a.length));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   599
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   600
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   601
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   602
    private void scramble(double[] a, Random random) {
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
   603
        for (int i = 0; i < a.length * 7; i++) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
   604
            swap(a, random.nextInt(a.length), random.nextInt(a.length));
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   605
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   606
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   607
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   608
    private void swap(int[] a, int i, int j) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   609
        int t = a[i]; a[i] = a[j]; a[j] = t;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   610
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   611
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   612
    private void swap(long[] a, int i, int j) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   613
        long t = a[i]; a[i] = a[j]; a[j] = t;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   614
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   615
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   616
    private void swap(byte[] a, int i, int j) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   617
        byte t = a[i]; a[i] = a[j]; a[j] = t;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   618
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   619
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   620
    private void swap(char[] a, int i, int j) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   621
        char t = a[i]; a[i] = a[j]; a[j] = t;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   622
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   623
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   624
    private void swap(short[] a, int i, int j) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   625
        short t = a[i]; a[i] = a[j]; a[j] = t;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   626
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   627
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   628
    private void swap(float[] a, int i, int j) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   629
        float t = a[i]; a[i] = a[j]; a[j] = t;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   630
    }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   631
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   632
    private void swap(double[] a, int i, int j) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   633
        double t = a[i]; a[i] = a[j]; a[j] = t;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   634
    }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   635
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   636
    private void checkWithCheckSum(Object test, Object gold) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   637
        checkSorted(test);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   638
        checkCheckSum(test, gold);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   639
    }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   640
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   641
    private void fail(String message) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   642
        err.format("\n*** TEST FAILED ***\n\n%s\n\n", message);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   643
        throw new RuntimeException("Test failed");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   644
    }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   645
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   646
    private void checkNegativeZero(Object a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   647
        if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   648
            checkNegativeZero((float[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   649
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   650
            checkNegativeZero((double[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   651
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   652
            fail("Unknown type of array: " + a.getClass().getName());
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   653
        }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   654
    }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
   655
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   656
    private void checkNegativeZero(float[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   657
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   658
            if (Float.floatToRawIntBits(a[i]) == 0 && Float.floatToRawIntBits(a[i + 1]) < 0) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   659
                fail(a[i] + " before " + a[i + 1] + " at position " + i);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   660
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   661
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   662
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   663
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   664
    private void checkNegativeZero(double[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   665
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   666
            if (Double.doubleToRawLongBits(a[i]) == 0 && Double.doubleToRawLongBits(a[i + 1]) < 0) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   667
                fail(a[i] + " before " + a[i + 1] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   668
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   669
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   670
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   671
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   672
    private void compare(Object a, Object b, int numNaN, int numNeg, int numNegZero) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   673
        if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   674
            compare((float[]) a, (float[]) b, numNaN, numNeg, numNegZero);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   675
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   676
            compare((double[]) a, (double[]) b, numNaN, numNeg, numNegZero);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   677
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   678
            fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   679
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   680
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   681
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   682
    private void compare(float[] a, float[] b, int numNaN, int numNeg, int numNegZero) {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   683
        for (int i = a.length - numNaN; i < a.length; i++) {
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   684
            if (a[i] == a[i]) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   685
                fail("There must be NaN instead of " + a[i] + " at position " + i);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   686
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   687
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   688
        final int NEGATIVE_ZERO = Float.floatToIntBits(-0.0f);
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   689
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   690
        for (int i = numNeg; i < numNeg + numNegZero; i++) {
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   691
            if (NEGATIVE_ZERO != Float.floatToIntBits(a[i])) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   692
                fail("There must be -0.0 instead of " + a[i] + " at position " + i);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   693
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   694
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   695
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   696
        for (int i = 0; i < a.length - numNaN; i++) {
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   697
            if (a[i] != b[i]) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   698
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   699
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   700
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   701
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   702
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   703
    private void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   704
        for (int i = a.length - numNaN; i < a.length; i++) {
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   705
            if (a[i] == a[i]) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   706
                fail("There must be NaN instead of " + a[i] + " at position " + i);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   707
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   708
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   709
        final long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0d);
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   710
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   711
        for (int i = numNeg; i < numNeg + numNegZero; i++) {
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   712
            if (NEGATIVE_ZERO != Double.doubleToLongBits(a[i])) {
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   713
                fail("There must be -0.0 instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   714
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   715
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   716
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   717
        for (int i = 0; i < a.length - numNaN; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   718
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   719
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   720
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   721
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   722
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   723
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   724
    private void compare(Object a, Object b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   725
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   726
            compare((int[]) a, (int[]) b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   727
        } else if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   728
            compare((long[]) a, (long[]) b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   729
        } else if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   730
            compare((byte[]) a, (byte[]) b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   731
        } else if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   732
            compare((char[]) a, (char[]) b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   733
        } else if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   734
            compare((short[]) a, (short[]) b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   735
        } else if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   736
            compare((float[]) a, (float[]) b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   737
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   738
            compare((double[]) a, (double[]) b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   739
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   740
            fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   741
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   742
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   743
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   744
    private void compare(int[] a, int[] b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   745
        for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   746
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   747
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   748
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   749
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   750
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   751
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   752
    private void compare(long[] a, long[] b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   753
        for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   754
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   755
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   756
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   757
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   758
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   759
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   760
    private void compare(byte[] a, byte[] b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   761
        for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   762
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   763
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   764
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   765
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   766
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   767
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   768
    private void compare(char[] a, char[] b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   769
        for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   770
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   771
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   772
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   773
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   774
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   775
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   776
    private void compare(short[] a, short[] b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   777
        for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   778
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   779
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   780
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   781
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   782
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   783
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   784
    private void compare(float[] a, float[] b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   785
        for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   786
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   787
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   788
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   789
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   790
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   791
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   792
    private void compare(double[] a, double[] b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   793
        for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   794
            if (a[i] != b[i]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   795
                fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   796
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   797
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   798
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   799
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   800
    private String getType(int i) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   801
        Object a = test[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   802
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   803
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   804
            return "INT   ";
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   805
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   806
        if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   807
            return "LONG  ";
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   808
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   809
        if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   810
            return "BYTE  ";
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   811
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   812
        if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   813
            return "CHAR  ";
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   814
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   815
        if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   816
            return "SHORT ";
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   817
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   818
        if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   819
            return "FLOAT ";
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   820
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   821
        if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   822
            return "DOUBLE";
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   823
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   824
        fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   825
        return null;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   826
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   827
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   828
    private void checkSorted(Object a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   829
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   830
            checkSorted((int[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   831
        } else if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   832
            checkSorted((long[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   833
        } else if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   834
            checkSorted((byte[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   835
        } else if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   836
            checkSorted((char[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   837
        } else if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   838
            checkSorted((short[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   839
        } else if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   840
            checkSorted((float[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   841
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   842
            checkSorted((double[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   843
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   844
            fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   845
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   846
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   847
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   848
    private void checkSorted(int[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   849
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   850
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   851
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   852
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   853
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   854
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   855
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   856
    private void checkSorted(long[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   857
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   858
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   859
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   860
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   861
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   862
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   863
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   864
    private void checkSorted(byte[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   865
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   866
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   867
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   868
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   869
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   870
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   871
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   872
    private void checkSorted(char[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   873
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   874
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   875
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   876
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   877
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   878
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   879
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   880
    private void checkSorted(short[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   881
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   882
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   883
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   884
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   885
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   886
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   887
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   888
    private void checkSorted(float[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   889
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   890
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   891
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   892
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   893
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   894
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   895
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   896
    private void checkSorted(double[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   897
        for (int i = 0; i < a.length - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   898
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   899
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   900
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   901
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   902
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
   903
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   904
    private void checkCheckSum(Object test, Object gold) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   905
        if (checkSumXor(test) != checkSumXor(gold)) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   906
            fail("Original and sorted arrays are not identical [^]");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   907
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   908
        if (checkSumPlus(test) != checkSumPlus(gold)) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   909
            fail("Original and sorted arrays are not identical [+]");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   910
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   911
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   912
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   913
    private int checkSumXor(Object a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   914
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   915
            return checkSumXor((int[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   916
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   917
        if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   918
            return checkSumXor((long[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   919
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   920
        if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   921
            return checkSumXor((byte[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   922
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   923
        if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   924
            return checkSumXor((char[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   925
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   926
        if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   927
            return checkSumXor((short[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   928
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   929
        if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   930
            return checkSumXor((float[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   931
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   932
        if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   933
            return checkSumXor((double[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   934
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   935
        fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   936
        return -1;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   937
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   938
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   939
    private int checkSumXor(int[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   940
        int checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   941
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   942
        for (int e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   943
            checkSum ^= e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   944
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   945
        return checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   946
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   947
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   948
    private int checkSumXor(long[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   949
        long checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   950
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   951
        for (long e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   952
            checkSum ^= e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   953
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   954
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   955
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   956
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   957
    private int checkSumXor(byte[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   958
        byte checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   959
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   960
        for (byte e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   961
            checkSum ^= e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   962
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   963
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   964
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   965
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   966
    private int checkSumXor(char[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   967
        char checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   968
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   969
        for (char e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   970
            checkSum ^= e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   971
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   972
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   973
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   974
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   975
    private int checkSumXor(short[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   976
        short checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   977
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   978
        for (short e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   979
            checkSum ^= e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   980
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   981
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   982
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   983
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   984
    private int checkSumXor(float[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   985
        int checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   986
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   987
        for (float e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   988
            checkSum ^= (int) e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   989
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   990
        return checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   991
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   992
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   993
    private int checkSumXor(double[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   994
        int checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   995
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   996
        for (double e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   997
            checkSum ^= (int) e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   998
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
   999
        return checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1000
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1001
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1002
    private int checkSumPlus(Object a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1003
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1004
            return checkSumPlus((int[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1005
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1006
        if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1007
            return checkSumPlus((long[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1008
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1009
        if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1010
            return checkSumPlus((byte[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1011
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1012
        if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1013
            return checkSumPlus((char[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1014
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1015
        if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1016
            return checkSumPlus((short[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1017
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1018
        if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1019
            return checkSumPlus((float[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1020
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1021
        if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1022
            return checkSumPlus((double[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1023
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1024
        fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1025
        return -1;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1026
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1027
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1028
    private int checkSumPlus(int[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1029
        int checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1030
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1031
        for (int e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1032
            checkSum += e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1033
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1034
        return checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1035
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1036
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1037
    private int checkSumPlus(long[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1038
        long checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1039
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1040
        for (long e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1041
            checkSum += e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1042
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1043
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1044
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1045
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1046
    private int checkSumPlus(byte[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1047
        byte checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1048
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1049
        for (byte e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1050
            checkSum += e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1051
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1052
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1053
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1054
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1055
    private int checkSumPlus(char[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1056
        char checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1057
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1058
        for (char e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1059
            checkSum += e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1060
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1061
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1062
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1063
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1064
    private int checkSumPlus(short[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1065
        short checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1066
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1067
        for (short e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1068
            checkSum += e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1069
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1070
        return (int) checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1071
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1072
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1073
    private int checkSumPlus(float[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1074
        int checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1075
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1076
        for (float e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1077
            checkSum += (int) e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1078
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1079
        return checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1080
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1081
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1082
    private int checkSumPlus(double[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1083
        int checkSum = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1084
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1085
        for (double e : a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1086
            checkSum += (int) e;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1087
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1088
        return checkSum;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1089
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1090
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1091
    private void sortByInsertionSort(Object a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1092
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1093
            sortByInsertionSort((int[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1094
        } else if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1095
            sortByInsertionSort((long[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1096
        } else if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1097
            sortByInsertionSort((byte[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1098
        } else if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1099
            sortByInsertionSort((char[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1100
        } else if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1101
            sortByInsertionSort((short[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1102
        } else if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1103
            sortByInsertionSort((float[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1104
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1105
            sortByInsertionSort((double[]) a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1106
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1107
            fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1108
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1109
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1110
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1111
    private void sortByInsertionSort(int[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1112
        for (int j, i = 1; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1113
            int ai = a[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1114
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1115
            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1116
                a[j + 1] = a[j];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1117
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1118
            a[j + 1] = ai;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1119
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1120
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1121
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1122
    private void sortByInsertionSort(long[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1123
        for (int j, i = 1; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1124
            long ai = a[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1125
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1126
            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1127
                a[j + 1] = a[j];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1128
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1129
            a[j + 1] = ai;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1130
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1131
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1132
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1133
    private void sortByInsertionSort(byte[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1134
        for (int j, i = 1; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1135
            byte ai = a[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1136
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1137
            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1138
                a[j + 1] = a[j];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1139
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1140
            a[j + 1] = ai;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1141
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1142
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1143
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1144
    private void sortByInsertionSort(char[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1145
        for (int j, i = 1; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1146
            char ai = a[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1147
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1148
            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1149
                a[j + 1] = a[j];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1150
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1151
            a[j + 1] = ai;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1152
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1153
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1154
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1155
    private void sortByInsertionSort(short[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1156
        for (int j, i = 1; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1157
            short ai = a[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1158
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1159
            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1160
                a[j + 1] = a[j];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1161
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1162
            a[j + 1] = ai;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1163
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1164
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1165
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1166
    private void sortByInsertionSort(float[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1167
        for (int j, i = 1; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1168
            float ai = a[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1169
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1170
            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1171
                a[j + 1] = a[j];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1172
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1173
            a[j + 1] = ai;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1174
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1175
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1176
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1177
    private void sortByInsertionSort(double[] a) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1178
        for (int j, i = 1; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1179
            double ai = a[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1180
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1181
            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1182
                a[j + 1] = a[j];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1183
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1184
            a[j + 1] = ai;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1185
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1186
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1187
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1188
    private void checkSubArray(Object a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1189
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1190
            checkSubArray((int[]) a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1191
        } else if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1192
            checkSubArray((long[]) a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1193
        } else if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1194
            checkSubArray((byte[]) a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1195
        } else if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1196
            checkSubArray((char[]) a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1197
        } else if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1198
            checkSubArray((short[]) a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1199
        } else if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1200
            checkSubArray((float[]) a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1201
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1202
            checkSubArray((double[]) a, fromIndex, toIndex);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1203
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1204
            fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1205
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1206
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1207
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1208
    private void checkSubArray(int[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1209
        for (int i = 0; i < fromIndex; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1210
            if (a[i] != A380) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1211
                fail("Range sort changes left element at position " + i + hex(a[i], A380));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1212
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1213
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1214
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1215
        for (int i = fromIndex; i < toIndex - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1216
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1217
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1218
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1219
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1220
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1221
        for (int i = toIndex; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1222
            if (a[i] != B747) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1223
                fail("Range sort changes right element at position " + i + hex(a[i], B747));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1224
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1225
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1226
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1227
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1228
    private void checkSubArray(long[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1229
        for (int i = 0; i < fromIndex; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1230
            if (a[i] != (long) A380) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1231
                fail("Range sort changes left element at position " + i + hex(a[i], A380));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1232
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1233
        }
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1234
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1235
        for (int i = fromIndex; i < toIndex - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1236
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1237
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1238
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1239
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1240
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1241
        for (int i = toIndex; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1242
            if (a[i] != (long) B747) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1243
                fail("Range sort changes right element at position " + i + hex(a[i], B747));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1244
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1245
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1246
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1247
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1248
    private void checkSubArray(byte[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1249
        for (int i = 0; i < fromIndex; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1250
            if (a[i] != (byte) A380) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1251
                fail("Range sort changes left element at position " + i + hex(a[i], A380));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1252
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1253
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1254
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1255
        for (int i = fromIndex; i < toIndex - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1256
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1257
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1258
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1259
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1260
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1261
        for (int i = toIndex; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1262
            if (a[i] != (byte) B747) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1263
                fail("Range sort changes right element at position " + i + hex(a[i], B747));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1264
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1265
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1266
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1267
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1268
    private void checkSubArray(char[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1269
        for (int i = 0; i < fromIndex; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1270
            if (a[i] != (char) A380) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1271
                fail("Range sort changes left element at position " + i + hex(a[i], A380));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1272
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1273
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1274
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1275
        for (int i = fromIndex; i < toIndex - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1276
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1277
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1278
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1279
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1280
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1281
        for (int i = toIndex; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1282
            if (a[i] != (char) B747) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1283
                fail("Range sort changes right element at position " + i + hex(a[i], B747));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1284
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1285
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1286
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1287
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1288
    private void checkSubArray(short[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1289
        for (int i = 0; i < fromIndex; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1290
            if (a[i] != (short) A380) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1291
                fail("Range sort changes left element at position " + i + hex(a[i], A380));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1292
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1293
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1294
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1295
        for (int i = fromIndex; i < toIndex - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1296
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1297
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1298
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1299
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1300
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1301
        for (int i = toIndex; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1302
            if (a[i] != (short) B747) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1303
                fail("Range sort changes right element at position " + i + hex(a[i], B747));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1304
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1305
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1306
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1307
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1308
    private void checkSubArray(float[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1309
        for (int i = 0; i < fromIndex; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1310
            if (a[i] != (float) A380) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1311
                fail("Range sort changes left element at position " + i + hex((long) a[i], A380));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1312
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1313
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1314
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1315
        for (int i = fromIndex; i < toIndex - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1316
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1317
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1318
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1319
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1320
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1321
        for (int i = toIndex; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1322
            if (a[i] != (float) B747) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1323
                fail("Range sort changes right element at position " + i + hex((long) a[i], B747));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1324
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1325
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1326
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1327
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1328
    private void checkSubArray(double[] a, int fromIndex, int toIndex) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1329
        for (int i = 0; i < fromIndex; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1330
            if (a[i] != (double) A380) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1331
                fail("Range sort changes left element at position " + i + hex((long) a[i], A380));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1332
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1333
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1334
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1335
        for (int i = fromIndex; i < toIndex - 1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1336
            if (a[i] > a[i + 1]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1337
                fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1338
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1339
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1340
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1341
        for (int i = toIndex; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1342
            if (a[i] != (double) B747) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1343
                fail("Range sort changes right element at position " + i + hex((long) a[i], B747));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1344
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1345
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1346
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1347
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1348
    private void checkRange(Object a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1349
        if (a instanceof int[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1350
            checkRange((int[]) a, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1351
        } else if (a instanceof long[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1352
            checkRange((long[]) a, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1353
        } else if (a instanceof byte[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1354
            checkRange((byte[]) a, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1355
        } else if (a instanceof char[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1356
            checkRange((char[]) a, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1357
        } else if (a instanceof short[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1358
            checkRange((short[]) a, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1359
        } else if (a instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1360
            checkRange((float[]) a, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1361
        } else if (a instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1362
            checkRange((double[]) a, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1363
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1364
            fail("Unknown type of array: " + a.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1365
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1366
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1367
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1368
    private void checkRange(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1369
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1370
            sortingHelper.sort(a, m + 1, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1371
            fail(sortingHelper + " does not throw IllegalArgumentException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1372
                "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1373
        } catch (IllegalArgumentException iae) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1374
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1375
                sortingHelper.sort(a, -m, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1376
                fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1377
                    "as expected: fromIndex = " + (-m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1378
            } catch (ArrayIndexOutOfBoundsException aoe) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1379
                try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1380
                    sortingHelper.sort(a, 0, a.length + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1381
                    fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1382
                        "as expected: toIndex = " + (a.length + m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1383
                } catch (ArrayIndexOutOfBoundsException expected) {}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1384
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1385
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1386
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1387
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1388
    private void checkRange(long[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1389
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1390
            sortingHelper.sort(a, m + 1, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1391
            fail(sortingHelper + " does not throw IllegalArgumentException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1392
                "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1393
        } catch (IllegalArgumentException iae) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1394
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1395
                sortingHelper.sort(a, -m, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1396
                fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1397
                    "as expected: fromIndex = " + (-m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1398
            } catch (ArrayIndexOutOfBoundsException aoe) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1399
                try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1400
                    sortingHelper.sort(a, 0, a.length + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1401
                    fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1402
                        "as expected: toIndex = " + (a.length + m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1403
                } catch (ArrayIndexOutOfBoundsException expected) {}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1404
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1405
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1406
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1407
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1408
    private void checkRange(byte[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1409
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1410
            sortingHelper.sort(a, m + 1, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1411
            fail(sortingHelper + " does not throw IllegalArgumentException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1412
                "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1413
        } catch (IllegalArgumentException iae) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1414
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1415
                sortingHelper.sort(a, -m, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1416
                fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1417
                    "as expected: fromIndex = " + (-m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1418
            } catch (ArrayIndexOutOfBoundsException aoe) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1419
                try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1420
                    sortingHelper.sort(a, 0, a.length + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1421
                    fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1422
                        "as expected: toIndex = " + (a.length + m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1423
                } catch (ArrayIndexOutOfBoundsException expected) {}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1424
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1425
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1426
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1427
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1428
    private void checkRange(char[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1429
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1430
            sortingHelper.sort(a, m + 1, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1431
            fail(sortingHelper + " does not throw IllegalArgumentException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1432
                "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1433
        } catch (IllegalArgumentException iae) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1434
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1435
                sortingHelper.sort(a, -m, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1436
                fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1437
                    "as expected: fromIndex = " + (-m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1438
            } catch (ArrayIndexOutOfBoundsException aoe) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1439
                try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1440
                    sortingHelper.sort(a, 0, a.length + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1441
                    fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1442
                        "as expected: toIndex = " + (a.length + m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1443
                } catch (ArrayIndexOutOfBoundsException expected) {}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1444
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1445
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1446
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1447
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1448
    private void checkRange(short[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1449
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1450
            sortingHelper.sort(a, m + 1, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1451
            fail(sortingHelper + " does not throw IllegalArgumentException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1452
                "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1453
        } catch (IllegalArgumentException iae) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1454
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1455
                sortingHelper.sort(a, -m, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1456
                fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1457
                    "as expected: fromIndex = " + (-m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1458
            } catch (ArrayIndexOutOfBoundsException aoe) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1459
                try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1460
                    sortingHelper.sort(a, 0, a.length + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1461
                    fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1462
                        "as expected: toIndex = " + (a.length + m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1463
                } catch (ArrayIndexOutOfBoundsException expected) {}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1464
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1465
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1466
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1467
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1468
    private void checkRange(float[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1469
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1470
            sortingHelper.sort(a, m + 1, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1471
            fail(sortingHelper + " does not throw IllegalArgumentException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1472
                "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1473
        } catch (IllegalArgumentException iae) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1474
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1475
                sortingHelper.sort(a, -m, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1476
                fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1477
                    "as expected: fromIndex = " + (-m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1478
            } catch (ArrayIndexOutOfBoundsException aoe) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1479
                try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1480
                    sortingHelper.sort(a, 0, a.length + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1481
                    fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1482
                        "as expected: toIndex = " + (a.length + m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1483
                } catch (ArrayIndexOutOfBoundsException expected) {}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1484
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1485
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1486
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1487
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1488
    private void checkRange(double[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1489
        try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1490
            sortingHelper.sort(a, m + 1, m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1491
            fail(sortingHelper + " does not throw IllegalArgumentException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1492
                "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1493
        } catch (IllegalArgumentException iae) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1494
            try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1495
                sortingHelper.sort(a, -m, a.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1496
                fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1497
                    "as expected: fromIndex = " + (-m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1498
            } catch (ArrayIndexOutOfBoundsException aoe) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1499
                try {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1500
                    sortingHelper.sort(a, 0, a.length + m);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1501
                    fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1502
                        "as expected: toIndex = " + (a.length + m));
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1503
                } catch (ArrayIndexOutOfBoundsException expected) {}
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1504
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1505
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1506
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1507
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1508
    private void copy(Object dst, Object src) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1509
        if (src instanceof float[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1510
            copy((float[]) dst, (float[]) src);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1511
        } else if (src instanceof double[]) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1512
            copy((double[]) dst, (double[]) src);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1513
        } else {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1514
            fail("Unknown type of array: " + src.getClass().getName());
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1515
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1516
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1517
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1518
    private void copy(float[] dst, float[] src) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1519
        System.arraycopy(src, 0, dst, 0, src.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1520
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1521
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1522
    private void copy(double[] dst, double[] src) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1523
        System.arraycopy(src, 0, dst, 0, src.length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1524
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1525
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1526
    private void printTestName(String test, TestRandom random, int length) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1527
        printTestName(test, random, length, "");
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1528
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1529
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1530
    private void createData(int length) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1531
        gold = new Object[] {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1532
            new int[length], new long[length],
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1533
            new byte[length], new char[length], new short[length],
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1534
            new float[length], new double[length]
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1535
        };
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1536
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1537
        test = new Object[] {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1538
            new int[length], new long[length],
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1539
            new byte[length], new char[length], new short[length],
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1540
            new float[length], new double[length]
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1541
        };
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1542
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1543
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1544
    private void convertData(int length) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1545
        for (int i = 1; i < gold.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1546
            TypeConverter converter = TypeConverter.values()[i - 1];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1547
            converter.convert((int[])gold[0], gold[i]);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1548
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1549
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1550
        for (int i = 0; i < gold.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1551
            System.arraycopy(gold[i], 0, test[i], 0, length);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1552
        }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1553
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1554
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1555
    private String hex(long a, int b) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1556
        return ": " + Long.toHexString(a) + ", must be " + Integer.toHexString(b);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1557
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1558
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1559
    private void printTestName(String test, TestRandom random, int length, String message) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1560
        out.println( "[" + sortingHelper + "] '" + test +
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1561
            "' length = " + length + ", random = " + random + message);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1562
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1563
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1564
    private static enum TypeConverter {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1565
        LONG {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1566
            void convert(int[] src, Object dst) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1567
                long[] b = (long[]) dst;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1568
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1569
                for (int i = 0; i < src.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1570
                    b[i] = (long) src[i];
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1571
                }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1572
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1573
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1574
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1575
        BYTE {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1576
            void convert(int[] src, Object dst) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1577
                byte[] b = (byte[]) dst;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1578
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1579
                for (int i = 0; i < src.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1580
                    b[i] = (byte) src[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1581
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1582
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1583
        },
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1584
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1585
        CHAR {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1586
            void convert(int[] src, Object dst) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1587
                char[] b = (char[]) dst;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1588
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1589
                for (int i = 0; i < src.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1590
                    b[i] = (char) src[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1591
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1592
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1593
        },
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1594
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1595
        SHORT {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1596
            void convert(int[] src, Object dst) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1597
                short[] b = (short[]) dst;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1598
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1599
                for (int i = 0; i < src.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1600
                    b[i] = (short) src[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1601
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1602
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1603
        },
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1604
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1605
        FLOAT {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1606
            void convert(int[] src, Object dst) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1607
                float[] b = (float[]) dst;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1608
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1609
                for (int i = 0; i < src.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1610
                    b[i] = (float) src[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1611
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1612
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1613
        },
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1614
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1615
        DOUBLE {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1616
            void convert(int[] src, Object dst) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1617
                double[] b = (double[]) dst;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1618
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1619
                for (int i = 0; i < src.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1620
                    b[i] = (double) src[i];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1621
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1622
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1623
        };
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1624
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1625
        abstract void convert(int[] src, Object dst);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1626
    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1627
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1628
    private static enum SortedBuilder {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1629
        STEPS {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1630
            void build(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1631
                for (int i = 0; i < m; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1632
                    a[i] = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1633
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1634
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1635
                for (int i = m; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1636
                    a[i] = 1;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1637
                }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1638
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1639
        };
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1640
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1641
        abstract void build(int[] a, int m);
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1642
    }
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1643
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1644
    private static enum UnsortedBuilder {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1645
        RANDOM {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1646
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1647
                for (int i = 0; i < a.length; i++) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1648
                    a[i] = random.nextInt();
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1649
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1650
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1651
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1652
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1653
        ASCENDING {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1654
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1655
                for (int i = 0; i < a.length; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1656
                    a[i] = m + i;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1657
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1658
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1659
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1660
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1661
        DESCENDING {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1662
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1663
                for (int i = 0; i < a.length; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1664
                    a[i] = a.length - m - i;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1665
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1666
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1667
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1668
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1669
        EQUAL {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1670
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1671
                for (int i = 0; i < a.length; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1672
                    a[i] = m;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1673
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1674
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1675
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1676
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1677
        SAW {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1678
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1679
                int incCount = 1;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1680
                int decCount = a.length;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1681
                int i = 0;
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1682
                int period = m--;
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1683
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1684
                while (true) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1685
                    for (int k = 1; k <= period; k++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1686
                        if (i >= a.length) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1687
                            return;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1688
                        }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1689
                        a[i++] = incCount++;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1690
                    }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1691
                    period += m;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1692
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1693
                    for (int k = 1; k <= period; k++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1694
                        if (i >= a.length) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1695
                            return;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1696
                        }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1697
                        a[i++] = decCount--;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1698
                    }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1699
                    period += m;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1700
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1701
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1702
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1703
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1704
        REPEATED {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1705
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1706
                for (int i = 0; i < a.length; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1707
                    a[i] = i % m;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1708
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1709
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1710
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1711
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1712
        DUPLICATED {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1713
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1714
                for (int i = 0; i < a.length; i++) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1715
                    a[i] = random.nextInt(m);
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1716
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1717
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1718
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1719
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1720
        ORGAN_PIPES {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1721
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1722
                int middle = a.length / (m + 1);
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1723
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1724
                for (int i = 0; i < middle; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1725
                    a[i] = i;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1726
                }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1727
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1728
                for (int i = middle; i < a.length; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1729
                    a[i] = a.length - i - 1;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1730
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1731
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1732
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1733
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1734
        STAGGER {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1735
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1736
                for (int i = 0; i < a.length; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1737
                    a[i] = (i * m + i) % a.length;
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1738
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1739
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1740
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1741
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1742
        PLATEAU {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1743
            void build(int[] a, int m, Random random) {
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1744
                for (int i = 0; i < a.length; i++) {
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1745
                    a[i] = Math.min(i, m);
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1746
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1747
            }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1748
        },
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1749
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1750
        SHUFFLE {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1751
            void build(int[] a, int m, Random random) {
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1752
                int x = 0, y = 0;
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1753
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1754
                for (int i = 0; i < a.length; i++) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1755
                    a[i] = random.nextBoolean() ? (x += 2) : (y += 2);
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1756
                }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1757
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1758
        },
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1759
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1760
        LATCH {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1761
            void build(int[] a, int m, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1762
                int max = a.length / m;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1763
                max = max < 2 ? 2 : max;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1764
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1765
                for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1766
                    a[i] = i % max;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1767
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1768
            }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1769
        };
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1770
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  1771
        abstract void build(int[] a, int m, Random random);
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1772
    }
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1773
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1774
    private static enum MergingBuilder {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1775
        ASCENDING {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1776
            void build(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1777
                int period = a.length / m;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1778
                int v = 1, i = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1779
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1780
                for (int k = 0; k < m; k++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1781
                    v = 1;
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1782
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1783
                    for (int p = 0; p < period; p++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1784
                        a[i++] = v++;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1785
                    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1786
                }
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1787
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1788
                for (int j = i; j < a.length - 1; j++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1789
                    a[j] = v++;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1790
                }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1791
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1792
                a[a.length - 1] = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1793
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1794
        },
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1795
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1796
        DESCENDING {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1797
            void build(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1798
                int period = a.length / m;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1799
                int v = -1, i = 0;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1800
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1801
                for (int k = 0; k < m; k++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1802
                    v = -1;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1803
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1804
                    for (int p = 0; p < period; p++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1805
                        a[i++] = v--;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1806
                    }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1807
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1808
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1809
                for (int j = i; j < a.length - 1; j++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1810
                    a[j] = v--;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1811
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1812
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1813
                a[a.length - 1] = 0;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1814
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1815
        },
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1816
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1817
        POINT {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1818
            void build(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1819
                for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1820
                    a[i] = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1821
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1822
                a[a.length / 2] = m;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1823
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1824
        },
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1825
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1826
        LINE {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1827
            void build(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1828
                for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1829
                    a[i] = i;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1830
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1831
                reverse(a, 0, a.length - 1);
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1832
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1833
        },
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1834
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1835
        PEARL {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1836
            void build(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1837
                for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1838
                    a[i] = i;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1839
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1840
                reverse(a, 0, 2);
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1841
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1842
        },
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1843
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1844
        RING {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1845
            void build(int[] a, int m) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1846
                int k1 = a.length / 3;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1847
                int k2 = a.length / 3 * 2;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1848
                int level = a.length / 3;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1849
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1850
                for (int i = 0, k = level; i < k1; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1851
                    a[i] = k--;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1852
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1853
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1854
                for (int i = k1; i < k2; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1855
                    a[i] = 0;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1856
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1857
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1858
                for (int i = k2, k = level; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1859
                    a[i] = k--;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1860
                }
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1861
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1862
        };
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1863
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1864
        abstract void build(int[] a, int m);
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1865
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1866
        private static void reverse(int[] a, int lo, int hi) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1867
            for (--hi; lo < hi; ) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1868
                int tmp = a[lo];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1869
                a[lo++] = a[hi];
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1870
                a[hi--] = tmp;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1871
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1872
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1873
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1874
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1875
    private static enum NegativeZeroBuilder {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1876
        FLOAT {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1877
            void build(Object o, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1878
                float[] a = (float[]) o;
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1879
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1880
                for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1881
                    a[i] = random.nextBoolean() ? -0.0f : 0.0f;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1882
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1883
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1884
        },
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1885
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1886
        DOUBLE {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1887
            void build(Object o, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1888
                double[] a = (double[]) o;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1889
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1890
                for (int i = 0; i < a.length; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1891
                    a[i] = random.nextBoolean() ? -0.0d : 0.0d;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1892
                }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1893
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1894
        };
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1895
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1896
        abstract void build(Object o, Random random);
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1897
    }
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1898
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1899
    private static enum FloatingPointBuilder {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1900
        FLOAT {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1901
            void build(Object o, int a, int g, int z, int n, int p, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1902
                float negativeValue = -random.nextFloat();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1903
                float positiveValue =  random.nextFloat();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1904
                float[] x = (float[]) o;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1905
                int fromIndex = 0;
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1906
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1907
                writeValue(x, negativeValue, fromIndex, n);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1908
                fromIndex += n;
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1909
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1910
                writeValue(x, -0.0f, fromIndex, g);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1911
                fromIndex += g;
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1912
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1913
                writeValue(x, 0.0f, fromIndex, z);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1914
                fromIndex += z;
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1915
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1916
                writeValue(x, positiveValue, fromIndex, p);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1917
                fromIndex += p;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1918
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1919
                writeValue(x, Float.NaN, fromIndex, a);
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1920
            }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1921
        },
6896
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5995
diff changeset
  1922
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1923
        DOUBLE {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1924
            void build(Object o, int a, int g, int z, int n, int p, Random random) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1925
                double negativeValue = -random.nextFloat();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1926
                double positiveValue =  random.nextFloat();
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1927
                double[] x = (double[]) o;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1928
                int fromIndex = 0;
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  1929
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1930
                writeValue(x, negativeValue, fromIndex, n);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1931
                fromIndex += n;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1932
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1933
                writeValue(x, -0.0d, fromIndex, g);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1934
                fromIndex += g;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1935
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1936
                writeValue(x, 0.0d, fromIndex, z);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1937
                fromIndex += z;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1938
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1939
                writeValue(x, positiveValue, fromIndex, p);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1940
                fromIndex += p;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1941
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1942
                writeValue(x, Double.NaN, fromIndex, a);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1943
            }
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1944
        };
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1945
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1946
        abstract void build(Object o, int a, int g, int z, int n, int p, Random random);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1947
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1948
        private static void writeValue(float[] a, float value, int fromIndex, int count) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1949
            for (int i = fromIndex; i < fromIndex + count; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1950
                a[i] = value;
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
  1951
            }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
  1952
        }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
  1953
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1954
        private static void writeValue(double[] a, double value, int fromIndex, int count) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1955
            for (int i = fromIndex; i < fromIndex + count; i++) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1956
                a[i] = value;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1957
            }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1958
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1959
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1960
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1961
    private static Comparator<Pair> pairComparator = new Comparator<Pair>() {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1962
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1963
        @Override
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1964
        public int compare(Pair p1, Pair p2) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1965
            return p1.compareTo(p2);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1966
        }
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1967
    };
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1968
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1969
    private static class Pair implements Comparable<Pair> {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1970
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1971
        private Pair(int key, int value) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1972
            this.key = key;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1973
            this.value = value;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1974
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1975
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1976
        int getKey() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1977
            return key;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1978
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1979
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1980
        int getValue() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1981
            return value;
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1982
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1983
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1984
        @Override
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1985
        public int compareTo(Pair pair) {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1986
            return Integer.compare(key, pair.key);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1987
        }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1988
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1989
        @Override
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1990
        public String toString() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1991
            return "(" + key + ", " + value + ")";
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1992
        }
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
  1993
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1994
        private int key;
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1995
        private int value;
5995
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
  1996
    }
0b76e67c2054 6947216: Even more Dual-pivot quicksort improvements
alanb
parents: 5506
diff changeset
  1997
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  1998
    private static class TestRandom extends Random {
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  1999
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2000
        private static final TestRandom BABA = new TestRandom(0xBABA);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2001
        private static final TestRandom DEDA = new TestRandom(0xDEDA);
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2002
        private static final TestRandom C0FFEE = new TestRandom(0xC0FFEE);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4177
diff changeset
  2003
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2004
        private TestRandom(long seed) {
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  2005
            super(seed);
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2006
            this.seed = Long.toHexString(seed).toUpperCase();
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  2007
        }
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  2008
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2009
        @Override
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2010
        public String toString() {
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2011
            return seed;
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  2012
        }
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  2013
59042
8910b995a2ee 8226297: Dual-pivot quicksort improvements
bchristi
parents: 47216
diff changeset
  2014
        private String seed;
8193
626b859aabb2 7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents: 8188
diff changeset
  2015
    }
4177
208f8c11e7ba 6896573: Arrays.sort(long[]) fails with StackOverflowError
alanb
parents:
diff changeset
  2016
}