1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 /** 29 * This class provides access to package-private 30 * methods of DualPivotQuicksort class. 31 * 32 * @author Vladimir Yaroslavskiy 33 * 34 * @version 2018.08.18 35 * 36 * @since 14 37 */ 38 public final class SortingHelper { 39 40 // Heap sort is invoked for this depth 41 private static final int BIG_DEPTH = 100; 42 43 private static final SortingHelper DUAL_PIVOT_QUICKSORT_HELPER = new SortingHelper(0); 44 private static final SortingHelper PARALLEL_SORT_HELPER = new SortingHelper(87); 45 private static final SortingHelper HEAP_SORT_HELPER = new SortingHelper(-1); 46 47 private int parallelism; 48 49 public static SortingHelper getDualPivotQuicksortHelper() { 50 return DUAL_PIVOT_QUICKSORT_HELPER; 51 } 52 53 public static SortingHelper getParallelSortHelper() { 54 return PARALLEL_SORT_HELPER; 55 } 56 57 public static SortingHelper getHeapSortHelper() { 58 return HEAP_SORT_HELPER; 59 } 60 61 private SortingHelper(int parallelism) { 62 this.parallelism = parallelism; 63 } 64 65 public void sort(int[] a) { 66 if (parallelism < 0) { 67 DualPivotQuicksort.sort(null, a, BIG_DEPTH, 0, a.length); 68 } else { 69 DualPivotQuicksort.sort(a, parallelism, 0, a.length); 70 } 71 } 72 73 public void sort(int[] a, int low, int high) { 74 Arrays.rangeCheck(a.length, low, high); 75 76 if (parallelism < 0) { 77 DualPivotQuicksort.sort(null, a, BIG_DEPTH, low, high); 78 } else { 79 DualPivotQuicksort.sort(a, parallelism, low, high); 80 } 81 } 82 83 public void sort(long[] a) { 84 if (parallelism < 0) { 85 DualPivotQuicksort.sort(null, a, BIG_DEPTH, 0, a.length); 86 } else { 87 DualPivotQuicksort.sort(a, parallelism, 0, a.length); 88 } 89 } 90 91 public void sort(long[] a, int low, int high) { 92 Arrays.rangeCheck(a.length, low, high); 93 94 if (parallelism < 0) { 95 DualPivotQuicksort.sort(null, a, BIG_DEPTH, low, high); 96 } else { 97 DualPivotQuicksort.sort(a, parallelism, low, high); 98 } 99 } 100 101 public void sort(byte[] a) { 102 DualPivotQuicksort.sort(a, 0, a.length); 103 } 104 105 public void sort(byte[] a, int low, int high) { 106 Arrays.rangeCheck(a.length, low, high); 107 DualPivotQuicksort.sort(a, low, high); 108 } 109 110 public void sort(char[] a) { 111 if (parallelism < 0) { 112 DualPivotQuicksort.sort(a, BIG_DEPTH, 0, a.length); 113 } else { 114 DualPivotQuicksort.sort(a, 0, a.length); 115 } 116 } 117 118 public void sort(char[] a, int low, int high) { 119 Arrays.rangeCheck(a.length, low, high); 120 121 if (parallelism < 0) { 122 DualPivotQuicksort.sort(a, BIG_DEPTH, low, high); 123 } else { 124 DualPivotQuicksort.sort(a, low, high); 125 } 126 } 127 128 public void sort(short[] a) { 129 if (parallelism < 0) { 130 DualPivotQuicksort.sort(a, BIG_DEPTH, 0, a.length); 131 } else { 132 DualPivotQuicksort.sort(a, 0, a.length); 133 } 134 } 135 136 public void sort(short[] a, int low, int high) { 137 Arrays.rangeCheck(a.length, low, high); 138 139 if (parallelism < 0) { 140 DualPivotQuicksort.sort(a, BIG_DEPTH, low, high); 141 } else { 142 DualPivotQuicksort.sort(a, low, high); 143 } 144 } 145 146 public void sort(float[] a) { 147 if (parallelism < 0) { 148 DualPivotQuicksort.sort(null, a, BIG_DEPTH, 0, a.length); 149 } else { 150 DualPivotQuicksort.sort(a, parallelism, 0, a.length); 151 } 152 } 153 154 public void sort(float[] a, int low, int high) { 155 Arrays.rangeCheck(a.length, low, high); 156 157 if (parallelism < 0) { 158 DualPivotQuicksort.sort(null, a, BIG_DEPTH, low, high); 159 } else { 160 DualPivotQuicksort.sort(a, parallelism, low, high); 161 } 162 } 163 164 public void sort(double[] a) { 165 if (parallelism < 0) { 166 DualPivotQuicksort.sort(null, a, BIG_DEPTH, 0, a.length); 167 } else { 168 DualPivotQuicksort.sort(a, parallelism, 0, a.length); 169 } 170 } 171 172 public void sort(double[] a, int low, int high) { 173 Arrays.rangeCheck(a.length, low, high); 174 175 if (parallelism < 0) { 176 DualPivotQuicksort.sort(null, a, BIG_DEPTH, low, high); 177 } else { 178 DualPivotQuicksort.sort(a, parallelism, low, high); 179 } 180 } 181 182 @Override 183 public String toString() { 184 if (parallelism < 0) { 185 return "Heap sort"; 186 } 187 if (parallelism == 0) { 188 return "Dual-Pivot Quicksort"; 189 } 190 return "Parallel Sorting"; 191 } 192 }