59042
|
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 2019.09.19
|
|
35 |
*
|
|
36 |
* @since 14
|
|
37 |
*/
|
|
38 |
public enum SortingHelper {
|
|
39 |
|
|
40 |
DUAL_PIVOT_QUICKSORT("Dual-Pivot Quicksort") {
|
|
41 |
|
|
42 |
@Override
|
|
43 |
public void sort(Object a) {
|
|
44 |
if (a instanceof int[]) {
|
|
45 |
DualPivotQuicksort.sort((int[]) a, SEQUENTIAL, 0, ((int[]) a).length);
|
|
46 |
} else if (a instanceof long[]) {
|
|
47 |
DualPivotQuicksort.sort((long[]) a, SEQUENTIAL, 0, ((long[]) a).length);
|
|
48 |
} else if (a instanceof byte[]) {
|
|
49 |
DualPivotQuicksort.sort((byte[]) a, 0, ((byte[]) a).length);
|
|
50 |
} else if (a instanceof char[]) {
|
|
51 |
DualPivotQuicksort.sort((char[]) a, SEQUENTIAL, 0, ((char[]) a).length);
|
|
52 |
} else if (a instanceof short[]) {
|
|
53 |
DualPivotQuicksort.sort((short[]) a, SEQUENTIAL, 0, ((short[]) a).length);
|
|
54 |
} else if (a instanceof float[]) {
|
|
55 |
DualPivotQuicksort.sort((float[]) a, SEQUENTIAL, 0, ((float[]) a).length);
|
|
56 |
} else if (a instanceof double[]) {
|
|
57 |
DualPivotQuicksort.sort((double[]) a, SEQUENTIAL, 0, ((double[]) a).length);
|
|
58 |
} else {
|
|
59 |
fail(a);
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
@Override
|
|
64 |
public void sort(Object a, int low, int high) {
|
|
65 |
if (a instanceof int[]) {
|
|
66 |
DualPivotQuicksort.sort((int[]) a, SEQUENTIAL, low, high);
|
|
67 |
} else if (a instanceof long[]) {
|
|
68 |
DualPivotQuicksort.sort((long[]) a, SEQUENTIAL, low, high);
|
|
69 |
} else if (a instanceof byte[]) {
|
|
70 |
DualPivotQuicksort.sort((byte[]) a, low, high);
|
|
71 |
} else if (a instanceof char[]) {
|
|
72 |
DualPivotQuicksort.sort((char[]) a, SEQUENTIAL, low, high);
|
|
73 |
} else if (a instanceof short[]) {
|
|
74 |
DualPivotQuicksort.sort((short[]) a, SEQUENTIAL, low, high);
|
|
75 |
} else if (a instanceof float[]) {
|
|
76 |
DualPivotQuicksort.sort((float[]) a, SEQUENTIAL, low, high);
|
|
77 |
} else if (a instanceof double[]) {
|
|
78 |
DualPivotQuicksort.sort((double[]) a, SEQUENTIAL, low, high);
|
|
79 |
} else {
|
|
80 |
fail(a);
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
@Override
|
|
85 |
public void sort(Object[] a) {
|
|
86 |
fail(a);
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
public void sort(Object[] a, Comparator comparator) {
|
|
91 |
fail(a);
|
|
92 |
}
|
|
93 |
},
|
|
94 |
|
|
95 |
PARALLEL_SORT("Parallel sort") {
|
|
96 |
|
|
97 |
@Override
|
|
98 |
public void sort(Object a) {
|
|
99 |
if (a instanceof int[]) {
|
|
100 |
DualPivotQuicksort.sort((int[]) a, PARALLEL, 0, ((int[]) a).length);
|
|
101 |
} else if (a instanceof long[]) {
|
|
102 |
DualPivotQuicksort.sort((long[]) a, PARALLEL, 0, ((long[]) a).length);
|
|
103 |
} else if (a instanceof byte[]) {
|
|
104 |
DualPivotQuicksort.sort((byte[]) a, 0, ((byte[]) a).length);
|
|
105 |
} else if (a instanceof char[]) {
|
|
106 |
DualPivotQuicksort.sort((char[]) a, PARALLEL, 0, ((char[]) a).length);
|
|
107 |
} else if (a instanceof short[]) {
|
|
108 |
DualPivotQuicksort.sort((short[]) a, PARALLEL, 0, ((short[]) a).length);
|
|
109 |
} else if (a instanceof float[]) {
|
|
110 |
DualPivotQuicksort.sort((float[]) a, PARALLEL, 0, ((float[]) a).length);
|
|
111 |
} else if (a instanceof double[]) {
|
|
112 |
DualPivotQuicksort.sort((double[]) a, PARALLEL, 0, ((double[]) a).length);
|
|
113 |
} else {
|
|
114 |
fail(a);
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
@Override
|
|
119 |
public void sort(Object a, int low, int high) {
|
|
120 |
if (a instanceof int[]) {
|
|
121 |
DualPivotQuicksort.sort((int[]) a, PARALLEL, low, high);
|
|
122 |
} else if (a instanceof long[]) {
|
|
123 |
DualPivotQuicksort.sort((long[]) a, PARALLEL, low, high);
|
|
124 |
} else if (a instanceof byte[]) {
|
|
125 |
DualPivotQuicksort.sort((byte[]) a, low, high);
|
|
126 |
} else if (a instanceof char[]) {
|
|
127 |
DualPivotQuicksort.sort((char[]) a, PARALLEL, low, high);
|
|
128 |
} else if (a instanceof short[]) {
|
|
129 |
DualPivotQuicksort.sort((short[]) a, PARALLEL, low, high);
|
|
130 |
} else if (a instanceof float[]) {
|
|
131 |
DualPivotQuicksort.sort((float[]) a, PARALLEL, low, high);
|
|
132 |
} else if (a instanceof double[]) {
|
|
133 |
DualPivotQuicksort.sort((double[]) a, PARALLEL, low, high);
|
|
134 |
} else {
|
|
135 |
fail(a);
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
@Override
|
|
140 |
public void sort(Object[] a) {
|
|
141 |
fail(a);
|
|
142 |
}
|
|
143 |
|
|
144 |
@Override
|
|
145 |
public void sort(Object[] a, Comparator comparator) {
|
|
146 |
fail(a);
|
|
147 |
}
|
|
148 |
},
|
|
149 |
|
|
150 |
HEAP_SORT("Heap sort") {
|
|
151 |
|
|
152 |
@Override
|
|
153 |
public void sort(Object a) {
|
|
154 |
if (a instanceof int[]) {
|
|
155 |
DualPivotQuicksort.sort(null, (int[]) a, BIG_DEPTH, 0, ((int[]) a).length);
|
|
156 |
} else if (a instanceof long[]) {
|
|
157 |
DualPivotQuicksort.sort(null, (long[]) a, BIG_DEPTH, 0, ((long[]) a).length);
|
|
158 |
} else if (a instanceof byte[]) {
|
|
159 |
DualPivotQuicksort.sort((byte[]) a, 0, ((byte[]) a).length);
|
|
160 |
} else if (a instanceof char[]) {
|
|
161 |
DualPivotQuicksort.sort((char[]) a, BIG_DEPTH, 0, ((char[]) a).length);
|
|
162 |
} else if (a instanceof short[]) {
|
|
163 |
DualPivotQuicksort.sort((short[]) a, BIG_DEPTH, 0, ((short[]) a).length);
|
|
164 |
} else if (a instanceof float[]) {
|
|
165 |
DualPivotQuicksort.sort(null, (float[]) a, BIG_DEPTH, 0, ((float[]) a).length);
|
|
166 |
} else if (a instanceof double[]) {
|
|
167 |
DualPivotQuicksort.sort(null, (double[]) a, BIG_DEPTH, 0, ((double[]) a).length);
|
|
168 |
} else {
|
|
169 |
fail(a);
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
@Override
|
|
174 |
public void sort(Object a, int low, int high) {
|
|
175 |
if (a instanceof int[]) {
|
|
176 |
DualPivotQuicksort.sort(null, (int[]) a, BIG_DEPTH, low, high);
|
|
177 |
} else if (a instanceof long[]) {
|
|
178 |
DualPivotQuicksort.sort(null, (long[]) a, BIG_DEPTH, low, high);
|
|
179 |
} else if (a instanceof byte[]) {
|
|
180 |
DualPivotQuicksort.sort((byte[]) a, low, high);
|
|
181 |
} else if (a instanceof char[]) {
|
|
182 |
DualPivotQuicksort.sort((char[]) a, BIG_DEPTH, low, high);
|
|
183 |
} else if (a instanceof short[]) {
|
|
184 |
DualPivotQuicksort.sort((short[]) a, BIG_DEPTH, low, high);
|
|
185 |
} else if (a instanceof float[]) {
|
|
186 |
DualPivotQuicksort.sort(null, (float[]) a, BIG_DEPTH, low, high);
|
|
187 |
} else if (a instanceof double[]) {
|
|
188 |
DualPivotQuicksort.sort(null, (double[]) a, BIG_DEPTH, low, high);
|
|
189 |
} else {
|
|
190 |
fail(a);
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
@Override
|
|
195 |
public void sort(Object[] a) {
|
|
196 |
fail(a);
|
|
197 |
}
|
|
198 |
|
|
199 |
@Override
|
|
200 |
public void sort(Object[] a, Comparator comparator) {
|
|
201 |
fail(a);
|
|
202 |
}
|
|
203 |
},
|
|
204 |
|
|
205 |
ARRAYS_SORT("Arrays.sort") {
|
|
206 |
|
|
207 |
@Override
|
|
208 |
public void sort(Object a) {
|
|
209 |
if (a instanceof int[]) {
|
|
210 |
Arrays.sort((int[]) a);
|
|
211 |
} else if (a instanceof long[]) {
|
|
212 |
Arrays.sort((long[]) a);
|
|
213 |
} else if (a instanceof byte[]) {
|
|
214 |
Arrays.sort((byte[]) a);
|
|
215 |
} else if (a instanceof char[]) {
|
|
216 |
Arrays.sort((char[]) a);
|
|
217 |
} else if (a instanceof short[]) {
|
|
218 |
Arrays.sort((short[]) a);
|
|
219 |
} else if (a instanceof float[]) {
|
|
220 |
Arrays.sort((float[]) a);
|
|
221 |
} else if (a instanceof double[]) {
|
|
222 |
Arrays.sort((double[]) a);
|
|
223 |
} else {
|
|
224 |
fail(a);
|
|
225 |
}
|
|
226 |
}
|
|
227 |
|
|
228 |
@Override
|
|
229 |
public void sort(Object a, int low, int high) {
|
|
230 |
if (a instanceof int[]) {
|
|
231 |
Arrays.sort((int[]) a, low, high);
|
|
232 |
} else if (a instanceof long[]) {
|
|
233 |
Arrays.sort((long[]) a, low, high);
|
|
234 |
} else if (a instanceof byte[]) {
|
|
235 |
Arrays.sort((byte[]) a, low, high);
|
|
236 |
} else if (a instanceof char[]) {
|
|
237 |
Arrays.sort((char[]) a, low, high);
|
|
238 |
} else if (a instanceof short[]) {
|
|
239 |
Arrays.sort((short[]) a, low, high);
|
|
240 |
} else if (a instanceof float[]) {
|
|
241 |
Arrays.sort((float[]) a, low, high);
|
|
242 |
} else if (a instanceof double[]) {
|
|
243 |
Arrays.sort((double[]) a, low, high);
|
|
244 |
} else {
|
|
245 |
fail(a);
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
@Override
|
|
250 |
public void sort(Object[] a) {
|
|
251 |
Arrays.sort(a);
|
|
252 |
}
|
|
253 |
|
|
254 |
@Override
|
|
255 |
@SuppressWarnings("unchecked")
|
|
256 |
public void sort(Object[] a, Comparator comparator) {
|
|
257 |
Arrays.sort(a, comparator);
|
|
258 |
}
|
|
259 |
},
|
|
260 |
|
|
261 |
ARRAYS_PARALLEL_SORT("Arrays.parallelSort") {
|
|
262 |
|
|
263 |
@Override
|
|
264 |
public void sort(Object a) {
|
|
265 |
if (a instanceof int[]) {
|
|
266 |
Arrays.parallelSort((int[]) a);
|
|
267 |
} else if (a instanceof long[]) {
|
|
268 |
Arrays.parallelSort((long[]) a);
|
|
269 |
} else if (a instanceof byte[]) {
|
|
270 |
Arrays.parallelSort((byte[]) a);
|
|
271 |
} else if (a instanceof char[]) {
|
|
272 |
Arrays.parallelSort((char[]) a);
|
|
273 |
} else if (a instanceof short[]) {
|
|
274 |
Arrays.parallelSort((short[]) a);
|
|
275 |
} else if (a instanceof float[]) {
|
|
276 |
Arrays.parallelSort((float[]) a);
|
|
277 |
} else if (a instanceof double[]) {
|
|
278 |
Arrays.parallelSort((double[]) a);
|
|
279 |
} else {
|
|
280 |
fail(a);
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
@Override
|
|
285 |
public void sort(Object a, int low, int high) {
|
|
286 |
if (a instanceof int[]) {
|
|
287 |
Arrays.parallelSort((int[]) a, low, high);
|
|
288 |
} else if (a instanceof long[]) {
|
|
289 |
Arrays.parallelSort((long[]) a, low, high);
|
|
290 |
} else if (a instanceof byte[]) {
|
|
291 |
Arrays.parallelSort((byte[]) a, low, high);
|
|
292 |
} else if (a instanceof char[]) {
|
|
293 |
Arrays.parallelSort((char[]) a, low, high);
|
|
294 |
} else if (a instanceof short[]) {
|
|
295 |
Arrays.parallelSort((short[]) a, low, high);
|
|
296 |
} else if (a instanceof float[]) {
|
|
297 |
Arrays.parallelSort((float[]) a, low, high);
|
|
298 |
} else if (a instanceof double[]) {
|
|
299 |
Arrays.parallelSort((double[]) a, low, high);
|
|
300 |
} else {
|
|
301 |
fail(a);
|
|
302 |
}
|
|
303 |
}
|
|
304 |
|
|
305 |
@Override
|
|
306 |
@SuppressWarnings("unchecked")
|
|
307 |
public void sort(Object[] a) {
|
|
308 |
Arrays.parallelSort((Comparable[]) a);
|
|
309 |
}
|
|
310 |
|
|
311 |
@Override
|
|
312 |
@SuppressWarnings("unchecked")
|
|
313 |
public void sort(Object[] a, Comparator comparator) {
|
|
314 |
Arrays.parallelSort(a, comparator);
|
|
315 |
}
|
|
316 |
};
|
|
317 |
|
|
318 |
abstract public void sort(Object a);
|
|
319 |
|
|
320 |
abstract public void sort(Object a, int low, int high);
|
|
321 |
|
|
322 |
abstract public void sort(Object[] a);
|
|
323 |
|
|
324 |
abstract public void sort(Object[] a, Comparator comparator);
|
|
325 |
|
|
326 |
private SortingHelper(String name) {
|
|
327 |
this.name = name;
|
|
328 |
}
|
|
329 |
|
|
330 |
@Override
|
|
331 |
public String toString() {
|
|
332 |
return name;
|
|
333 |
}
|
|
334 |
|
|
335 |
private static void fail(Object a) {
|
|
336 |
throw new RuntimeException("Unexpected type of array: " + a.getClass().getName());
|
|
337 |
}
|
|
338 |
|
|
339 |
private String name;
|
|
340 |
|
|
341 |
/**
|
|
342 |
* Parallelism level for sequential and parallel sorting.
|
|
343 |
*/
|
|
344 |
private static final int SEQUENTIAL = 0;
|
|
345 |
private static final int PARALLEL = 87;
|
|
346 |
|
|
347 |
/**
|
|
348 |
* Heap sort will be invoked, if recursion depth is too big.
|
|
349 |
* Value is taken from DualPivotQuicksort.MAX_RECURSION_DEPTH.
|
|
350 |
*/
|
|
351 |
private static final int BIG_DEPTH = 64 * (3 << 1);
|
|
352 |
}
|