author | psandoz |
Thu, 16 Jan 2014 18:20:31 +0100 | |
changeset 22289 | bb9c71b84919 |
parent 22078 | bdec5d53e98c |
child 22297 | 1c62c67d9dd2 |
permissions | -rw-r--r-- |
17182 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, 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 |
package java.util.stream; |
|
26 |
||
27 |
import java.util.ArrayList; |
|
28 |
import java.util.Arrays; |
|
29 |
import java.util.Comparator; |
|
30 |
import java.util.Objects; |
|
31 |
import java.util.Spliterator; |
|
32 |
import java.util.concurrent.ForkJoinTask; |
|
33 |
import java.util.function.IntFunction; |
|
34 |
||
35 |
||
36 |
/** |
|
37 |
* Factory methods for transforming streams into sorted streams. |
|
38 |
* |
|
39 |
* @since 1.8 |
|
40 |
*/ |
|
41 |
final class SortedOps { |
|
42 |
||
43 |
private SortedOps() { } |
|
44 |
||
45 |
/** |
|
46 |
* Appends a "sorted" operation to the provided stream. |
|
47 |
* |
|
48 |
* @param <T> the type of both input and output elements |
|
49 |
* @param upstream a reference stream with element type T |
|
50 |
*/ |
|
51 |
static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream) { |
|
52 |
return new OfRef<>(upstream); |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Appends a "sorted" operation to the provided stream. |
|
57 |
* |
|
58 |
* @param <T> the type of both input and output elements |
|
59 |
* @param upstream a reference stream with element type T |
|
60 |
* @param comparator the comparator to order elements by |
|
61 |
*/ |
|
62 |
static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream, |
|
63 |
Comparator<? super T> comparator) { |
|
64 |
return new OfRef<>(upstream, comparator); |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* Appends a "sorted" operation to the provided stream. |
|
69 |
* |
|
70 |
* @param <T> the type of both input and output elements |
|
71 |
* @param upstream a reference stream with element type T |
|
72 |
*/ |
|
73 |
static <T> IntStream makeInt(AbstractPipeline<?, Integer, ?> upstream) { |
|
74 |
return new OfInt(upstream); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Appends a "sorted" operation to the provided stream. |
|
79 |
* |
|
80 |
* @param <T> the type of both input and output elements |
|
81 |
* @param upstream a reference stream with element type T |
|
82 |
*/ |
|
83 |
static <T> LongStream makeLong(AbstractPipeline<?, Long, ?> upstream) { |
|
84 |
return new OfLong(upstream); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Appends a "sorted" operation to the provided stream. |
|
89 |
* |
|
90 |
* @param <T> the type of both input and output elements |
|
91 |
* @param upstream a reference stream with element type T |
|
92 |
*/ |
|
93 |
static <T> DoubleStream makeDouble(AbstractPipeline<?, Double, ?> upstream) { |
|
94 |
return new OfDouble(upstream); |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Specialized subtype for sorting reference streams |
|
99 |
*/ |
|
100 |
private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> { |
|
101 |
/** |
|
102 |
* Comparator used for sorting |
|
103 |
*/ |
|
104 |
private final boolean isNaturalSort; |
|
105 |
private final Comparator<? super T> comparator; |
|
106 |
||
107 |
/** |
|
108 |
* Sort using natural order of {@literal <T>} which must be |
|
109 |
* {@code Comparable}. |
|
110 |
*/ |
|
111 |
OfRef(AbstractPipeline<?, T, ?> upstream) { |
|
112 |
super(upstream, StreamShape.REFERENCE, |
|
113 |
StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED); |
|
114 |
this.isNaturalSort = true; |
|
115 |
// Will throw CCE when we try to sort if T is not Comparable |
|
18571 | 116 |
this.comparator = (Comparator<? super T>) Comparator.naturalOrder(); |
17182 | 117 |
} |
118 |
||
119 |
/** |
|
120 |
* Sort using the provided comparator. |
|
121 |
* |
|
122 |
* @param comparator The comparator to be used to evaluate ordering. |
|
123 |
*/ |
|
124 |
OfRef(AbstractPipeline<?, T, ?> upstream, Comparator<? super T> comparator) { |
|
125 |
super(upstream, StreamShape.REFERENCE, |
|
126 |
StreamOpFlag.IS_ORDERED | StreamOpFlag.NOT_SORTED); |
|
127 |
this.isNaturalSort = false; |
|
128 |
this.comparator = Objects.requireNonNull(comparator); |
|
129 |
} |
|
130 |
||
131 |
@Override |
|
19593 | 132 |
public Sink<T> opWrapSink(int flags, Sink<T> sink) { |
17182 | 133 |
Objects.requireNonNull(sink); |
134 |
||
135 |
// If the input is already naturally sorted and this operation |
|
136 |
// also naturally sorted then this is a no-op |
|
137 |
if (StreamOpFlag.SORTED.isKnown(flags) && isNaturalSort) |
|
138 |
return sink; |
|
139 |
else if (StreamOpFlag.SIZED.isKnown(flags)) |
|
140 |
return new SizedRefSortingSink<>(sink, comparator); |
|
141 |
else |
|
142 |
return new RefSortingSink<>(sink, comparator); |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
public <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper, |
|
147 |
Spliterator<P_IN> spliterator, |
|
148 |
IntFunction<T[]> generator) { |
|
149 |
// If the input is already naturally sorted and this operation |
|
150 |
// naturally sorts then collect the output |
|
151 |
if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags()) && isNaturalSort) { |
|
152 |
return helper.evaluate(spliterator, false, generator); |
|
153 |
} |
|
154 |
else { |
|
155 |
// @@@ Weak two-pass parallel implementation; parallel collect, parallel sort |
|
156 |
T[] flattenedData = helper.evaluate(spliterator, true, generator).asArray(generator); |
|
157 |
Arrays.parallelSort(flattenedData, comparator); |
|
158 |
return Nodes.node(flattenedData); |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Specialized subtype for sorting int streams. |
|
165 |
*/ |
|
166 |
private static final class OfInt extends IntPipeline.StatefulOp<Integer> { |
|
167 |
OfInt(AbstractPipeline<?, Integer, ?> upstream) { |
|
168 |
super(upstream, StreamShape.INT_VALUE, |
|
169 |
StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED); |
|
170 |
} |
|
171 |
||
172 |
@Override |
|
173 |
public Sink<Integer> opWrapSink(int flags, Sink sink) { |
|
174 |
Objects.requireNonNull(sink); |
|
175 |
||
176 |
if (StreamOpFlag.SORTED.isKnown(flags)) |
|
177 |
return sink; |
|
178 |
else if (StreamOpFlag.SIZED.isKnown(flags)) |
|
179 |
return new SizedIntSortingSink(sink); |
|
180 |
else |
|
181 |
return new IntSortingSink(sink); |
|
182 |
} |
|
183 |
||
184 |
@Override |
|
185 |
public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper, |
|
186 |
Spliterator<P_IN> spliterator, |
|
187 |
IntFunction<Integer[]> generator) { |
|
188 |
if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) { |
|
189 |
return helper.evaluate(spliterator, false, generator); |
|
190 |
} |
|
191 |
else { |
|
192 |
Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator); |
|
193 |
||
18171 | 194 |
int[] content = n.asPrimitiveArray(); |
17182 | 195 |
Arrays.parallelSort(content); |
196 |
||
197 |
return Nodes.node(content); |
|
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Specialized subtype for sorting long streams. |
|
204 |
*/ |
|
205 |
private static final class OfLong extends LongPipeline.StatefulOp<Long> { |
|
206 |
OfLong(AbstractPipeline<?, Long, ?> upstream) { |
|
207 |
super(upstream, StreamShape.LONG_VALUE, |
|
208 |
StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED); |
|
209 |
} |
|
210 |
||
211 |
@Override |
|
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
18571
diff
changeset
|
212 |
public Sink<Long> opWrapSink(int flags, Sink<Long> sink) { |
17182 | 213 |
Objects.requireNonNull(sink); |
214 |
||
215 |
if (StreamOpFlag.SORTED.isKnown(flags)) |
|
216 |
return sink; |
|
217 |
else if (StreamOpFlag.SIZED.isKnown(flags)) |
|
218 |
return new SizedLongSortingSink(sink); |
|
219 |
else |
|
220 |
return new LongSortingSink(sink); |
|
221 |
} |
|
222 |
||
223 |
@Override |
|
224 |
public <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper, |
|
225 |
Spliterator<P_IN> spliterator, |
|
226 |
IntFunction<Long[]> generator) { |
|
227 |
if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) { |
|
228 |
return helper.evaluate(spliterator, false, generator); |
|
229 |
} |
|
230 |
else { |
|
231 |
Node.OfLong n = (Node.OfLong) helper.evaluate(spliterator, true, generator); |
|
232 |
||
18171 | 233 |
long[] content = n.asPrimitiveArray(); |
17182 | 234 |
Arrays.parallelSort(content); |
235 |
||
236 |
return Nodes.node(content); |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Specialized subtype for sorting double streams. |
|
243 |
*/ |
|
244 |
private static final class OfDouble extends DoublePipeline.StatefulOp<Double> { |
|
245 |
OfDouble(AbstractPipeline<?, Double, ?> upstream) { |
|
246 |
super(upstream, StreamShape.DOUBLE_VALUE, |
|
247 |
StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED); |
|
248 |
} |
|
249 |
||
250 |
@Override |
|
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
18571
diff
changeset
|
251 |
public Sink<Double> opWrapSink(int flags, Sink<Double> sink) { |
17182 | 252 |
Objects.requireNonNull(sink); |
253 |
||
254 |
if (StreamOpFlag.SORTED.isKnown(flags)) |
|
255 |
return sink; |
|
256 |
else if (StreamOpFlag.SIZED.isKnown(flags)) |
|
257 |
return new SizedDoubleSortingSink(sink); |
|
258 |
else |
|
259 |
return new DoubleSortingSink(sink); |
|
260 |
} |
|
261 |
||
262 |
@Override |
|
263 |
public <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper, |
|
264 |
Spliterator<P_IN> spliterator, |
|
265 |
IntFunction<Double[]> generator) { |
|
266 |
if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) { |
|
267 |
return helper.evaluate(spliterator, false, generator); |
|
268 |
} |
|
269 |
else { |
|
270 |
Node.OfDouble n = (Node.OfDouble) helper.evaluate(spliterator, true, generator); |
|
271 |
||
18171 | 272 |
double[] content = n.asPrimitiveArray(); |
17182 | 273 |
Arrays.parallelSort(content); |
274 |
||
275 |
return Nodes.node(content); |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
/** |
|
20503
074dd13d9cdf
8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents:
19593
diff
changeset
|
281 |
* {@link Sink} for implementing sort on SIZED reference streams. |
17182 | 282 |
*/ |
19593 | 283 |
private static final class SizedRefSortingSink<T> extends Sink.ChainedReference<T, T> { |
17182 | 284 |
private final Comparator<? super T> comparator; |
285 |
private T[] array; |
|
286 |
private int offset; |
|
287 |
||
19593 | 288 |
SizedRefSortingSink(Sink<? super T> sink, Comparator<? super T> comparator) { |
17182 | 289 |
super(sink); |
290 |
this.comparator = comparator; |
|
291 |
} |
|
292 |
||
293 |
@Override |
|
294 |
public void begin(long size) { |
|
295 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
296 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
17182 | 297 |
array = (T[]) new Object[(int) size]; |
298 |
} |
|
299 |
||
300 |
@Override |
|
301 |
public void end() { |
|
302 |
Arrays.sort(array, 0, offset, comparator); |
|
303 |
downstream.begin(offset); |
|
304 |
for (int i = 0; i < offset; i++) |
|
305 |
downstream.accept(array[i]); |
|
306 |
downstream.end(); |
|
307 |
array = null; |
|
308 |
} |
|
309 |
||
310 |
@Override |
|
311 |
public void accept(T t) { |
|
312 |
array[offset++] = t; |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
/** |
|
317 |
* {@link Sink} for implementing sort on reference streams. |
|
318 |
*/ |
|
19593 | 319 |
private static final class RefSortingSink<T> extends Sink.ChainedReference<T, T> { |
17182 | 320 |
private final Comparator<? super T> comparator; |
321 |
private ArrayList<T> list; |
|
322 |
||
19593 | 323 |
RefSortingSink(Sink<? super T> sink, Comparator<? super T> comparator) { |
17182 | 324 |
super(sink); |
325 |
this.comparator = comparator; |
|
326 |
} |
|
327 |
||
328 |
@Override |
|
329 |
public void begin(long size) { |
|
20503
074dd13d9cdf
8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents:
19593
diff
changeset
|
330 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
331 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
20507
diff
changeset
|
332 |
list = (size >= 0) ? new ArrayList<>((int) size) : new ArrayList<>(); |
17182 | 333 |
} |
334 |
||
335 |
@Override |
|
336 |
public void end() { |
|
337 |
list.sort(comparator); |
|
338 |
downstream.begin(list.size()); |
|
339 |
list.forEach(downstream::accept); |
|
340 |
downstream.end(); |
|
341 |
list = null; |
|
342 |
} |
|
343 |
||
344 |
@Override |
|
345 |
public void accept(T t) { |
|
346 |
list.add(t); |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* {@link Sink} for implementing sort on SIZED int streams. |
|
352 |
*/ |
|
19593 | 353 |
private static final class SizedIntSortingSink extends Sink.ChainedInt<Integer> { |
17182 | 354 |
private int[] array; |
355 |
private int offset; |
|
356 |
||
19593 | 357 |
SizedIntSortingSink(Sink<? super Integer> downstream) { |
17182 | 358 |
super(downstream); |
359 |
} |
|
360 |
||
361 |
@Override |
|
362 |
public void begin(long size) { |
|
363 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
364 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
17182 | 365 |
array = new int[(int) size]; |
366 |
} |
|
367 |
||
368 |
@Override |
|
369 |
public void end() { |
|
370 |
Arrays.sort(array, 0, offset); |
|
371 |
downstream.begin(offset); |
|
372 |
for (int i = 0; i < offset; i++) |
|
373 |
downstream.accept(array[i]); |
|
374 |
downstream.end(); |
|
375 |
array = null; |
|
376 |
} |
|
377 |
||
378 |
@Override |
|
379 |
public void accept(int t) { |
|
380 |
array[offset++] = t; |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
* {@link Sink} for implementing sort on int streams. |
|
386 |
*/ |
|
19593 | 387 |
private static final class IntSortingSink extends Sink.ChainedInt<Integer> { |
17182 | 388 |
private SpinedBuffer.OfInt b; |
389 |
||
19593 | 390 |
IntSortingSink(Sink<? super Integer> sink) { |
17182 | 391 |
super(sink); |
392 |
} |
|
393 |
||
394 |
@Override |
|
395 |
public void begin(long size) { |
|
20503
074dd13d9cdf
8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents:
19593
diff
changeset
|
396 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
397 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
17182 | 398 |
b = (size > 0) ? new SpinedBuffer.OfInt((int) size) : new SpinedBuffer.OfInt(); |
399 |
} |
|
400 |
||
401 |
@Override |
|
402 |
public void end() { |
|
18171 | 403 |
int[] ints = b.asPrimitiveArray(); |
17182 | 404 |
Arrays.sort(ints); |
405 |
downstream.begin(ints.length); |
|
406 |
for (int anInt : ints) |
|
407 |
downstream.accept(anInt); |
|
408 |
downstream.end(); |
|
409 |
} |
|
410 |
||
411 |
@Override |
|
412 |
public void accept(int t) { |
|
413 |
b.accept(t); |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
/** |
|
418 |
* {@link Sink} for implementing sort on SIZED long streams. |
|
419 |
*/ |
|
19593 | 420 |
private static final class SizedLongSortingSink extends Sink.ChainedLong<Long> { |
17182 | 421 |
private long[] array; |
422 |
private int offset; |
|
423 |
||
19593 | 424 |
SizedLongSortingSink(Sink<? super Long> downstream) { |
17182 | 425 |
super(downstream); |
426 |
} |
|
427 |
||
428 |
@Override |
|
429 |
public void begin(long size) { |
|
430 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
431 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
17182 | 432 |
array = new long[(int) size]; |
433 |
} |
|
434 |
||
435 |
@Override |
|
436 |
public void end() { |
|
437 |
Arrays.sort(array, 0, offset); |
|
438 |
downstream.begin(offset); |
|
439 |
for (int i = 0; i < offset; i++) |
|
440 |
downstream.accept(array[i]); |
|
441 |
downstream.end(); |
|
442 |
array = null; |
|
443 |
} |
|
444 |
||
445 |
@Override |
|
446 |
public void accept(long t) { |
|
447 |
array[offset++] = t; |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
/** |
|
452 |
* {@link Sink} for implementing sort on long streams. |
|
453 |
*/ |
|
19593 | 454 |
private static final class LongSortingSink extends Sink.ChainedLong<Long> { |
17182 | 455 |
private SpinedBuffer.OfLong b; |
456 |
||
19593 | 457 |
LongSortingSink(Sink<? super Long> sink) { |
17182 | 458 |
super(sink); |
459 |
} |
|
460 |
||
461 |
@Override |
|
462 |
public void begin(long size) { |
|
20503
074dd13d9cdf
8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents:
19593
diff
changeset
|
463 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
464 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
17182 | 465 |
b = (size > 0) ? new SpinedBuffer.OfLong((int) size) : new SpinedBuffer.OfLong(); |
466 |
} |
|
467 |
||
468 |
@Override |
|
469 |
public void end() { |
|
18171 | 470 |
long[] longs = b.asPrimitiveArray(); |
17182 | 471 |
Arrays.sort(longs); |
472 |
downstream.begin(longs.length); |
|
473 |
for (long aLong : longs) |
|
474 |
downstream.accept(aLong); |
|
475 |
downstream.end(); |
|
476 |
} |
|
477 |
||
478 |
@Override |
|
479 |
public void accept(long t) { |
|
480 |
b.accept(t); |
|
481 |
} |
|
482 |
} |
|
483 |
||
484 |
/** |
|
485 |
* {@link Sink} for implementing sort on SIZED double streams. |
|
486 |
*/ |
|
19593 | 487 |
private static final class SizedDoubleSortingSink extends Sink.ChainedDouble<Double> { |
17182 | 488 |
private double[] array; |
489 |
private int offset; |
|
490 |
||
19593 | 491 |
SizedDoubleSortingSink(Sink<? super Double> downstream) { |
17182 | 492 |
super(downstream); |
493 |
} |
|
494 |
||
495 |
@Override |
|
496 |
public void begin(long size) { |
|
497 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
498 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
17182 | 499 |
array = new double[(int) size]; |
500 |
} |
|
501 |
||
502 |
@Override |
|
503 |
public void end() { |
|
504 |
Arrays.sort(array, 0, offset); |
|
505 |
downstream.begin(offset); |
|
506 |
for (int i = 0; i < offset; i++) |
|
507 |
downstream.accept(array[i]); |
|
508 |
downstream.end(); |
|
509 |
array = null; |
|
510 |
} |
|
511 |
||
512 |
@Override |
|
513 |
public void accept(double t) { |
|
514 |
array[offset++] = t; |
|
515 |
} |
|
516 |
} |
|
517 |
||
518 |
/** |
|
519 |
* {@link Sink} for implementing sort on double streams. |
|
520 |
*/ |
|
19593 | 521 |
private static final class DoubleSortingSink extends Sink.ChainedDouble<Double> { |
17182 | 522 |
private SpinedBuffer.OfDouble b; |
523 |
||
19593 | 524 |
DoubleSortingSink(Sink<? super Double> sink) { |
17182 | 525 |
super(sink); |
526 |
} |
|
527 |
||
528 |
@Override |
|
529 |
public void begin(long size) { |
|
20503
074dd13d9cdf
8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents:
19593
diff
changeset
|
530 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
20503
diff
changeset
|
531 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
17182 | 532 |
b = (size > 0) ? new SpinedBuffer.OfDouble((int) size) : new SpinedBuffer.OfDouble(); |
533 |
} |
|
534 |
||
535 |
@Override |
|
536 |
public void end() { |
|
18171 | 537 |
double[] doubles = b.asPrimitiveArray(); |
17182 | 538 |
Arrays.sort(doubles); |
539 |
downstream.begin(doubles.length); |
|
540 |
for (double aDouble : doubles) |
|
541 |
downstream.accept(aDouble); |
|
542 |
downstream.end(); |
|
543 |
} |
|
544 |
||
545 |
@Override |
|
546 |
public void accept(double t) { |
|
547 |
b.accept(t); |
|
548 |
} |
|
549 |
} |
|
550 |
} |