author | psandoz |
Thu, 16 Jan 2014 18:20:31 +0100 | |
changeset 22289 | bb9c71b84919 |
parent 21422 | 6fca66995a27 |
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.Comparator; |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
28 |
import java.util.Objects; |
17182 | 29 |
import java.util.Spliterator; |
21422
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
30 |
import java.util.concurrent.ConcurrentHashMap; |
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
31 |
import java.util.concurrent.atomic.AtomicLong; |
17182 | 32 |
import java.util.function.BooleanSupplier; |
33 |
import java.util.function.Consumer; |
|
34 |
import java.util.function.DoubleConsumer; |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
35 |
import java.util.function.DoubleSupplier; |
17182 | 36 |
import java.util.function.IntConsumer; |
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
37 |
import java.util.function.IntSupplier; |
17182 | 38 |
import java.util.function.LongConsumer; |
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
39 |
import java.util.function.LongSupplier; |
17182 | 40 |
import java.util.function.Supplier; |
41 |
||
42 |
/** |
|
43 |
* Spliterator implementations for wrapping and delegating spliterators, used |
|
44 |
* in the implementation of the {@link Stream#spliterator()} method. |
|
45 |
* |
|
46 |
* @since 1.8 |
|
47 |
*/ |
|
48 |
class StreamSpliterators { |
|
49 |
||
50 |
/** |
|
51 |
* Abstract wrapping spliterator that binds to the spliterator of a |
|
52 |
* pipeline helper on first operation. |
|
53 |
* |
|
54 |
* <p>This spliterator is not late-binding and will bind to the source |
|
55 |
* spliterator when first operated on. |
|
56 |
* |
|
57 |
* <p>A wrapping spliterator produced from a sequential stream |
|
58 |
* cannot be split if there are stateful operations present. |
|
59 |
*/ |
|
60 |
private static abstract class AbstractWrappingSpliterator<P_IN, P_OUT, |
|
61 |
T_BUFFER extends AbstractSpinedBuffer> |
|
62 |
implements Spliterator<P_OUT> { |
|
63 |
||
64 |
// @@@ Detect if stateful operations are present or not |
|
65 |
// If not then can split otherwise cannot |
|
66 |
||
67 |
/** |
|
68 |
* True if this spliterator supports splitting |
|
69 |
*/ |
|
70 |
final boolean isParallel; |
|
71 |
||
72 |
final PipelineHelper<P_OUT> ph; |
|
73 |
||
74 |
/** |
|
75 |
* Supplier for the source spliterator. Client provides either a |
|
76 |
* spliterator or a supplier. |
|
77 |
*/ |
|
78 |
private Supplier<Spliterator<P_IN>> spliteratorSupplier; |
|
79 |
||
80 |
/** |
|
81 |
* Source spliterator. Either provided from client or obtained from |
|
82 |
* supplier. |
|
83 |
*/ |
|
84 |
Spliterator<P_IN> spliterator; |
|
85 |
||
86 |
/** |
|
87 |
* Sink chain for the downstream stages of the pipeline, ultimately |
|
88 |
* leading to the buffer. Used during partial traversal. |
|
89 |
*/ |
|
90 |
Sink<P_IN> bufferSink; |
|
91 |
||
92 |
/** |
|
93 |
* A function that advances one element of the spliterator, pushing |
|
94 |
* it to bufferSink. Returns whether any elements were processed. |
|
95 |
* Used during partial traversal. |
|
96 |
*/ |
|
97 |
BooleanSupplier pusher; |
|
98 |
||
99 |
/** Next element to consume from the buffer, used during partial traversal */ |
|
100 |
long nextToConsume; |
|
101 |
||
102 |
/** Buffer into which elements are pushed. Used during partial traversal. */ |
|
103 |
T_BUFFER buffer; |
|
104 |
||
105 |
/** |
|
106 |
* True if full traversal has occurred (with possible cancelation). |
|
107 |
* If doing a partial traversal, there may be still elements in buffer. |
|
108 |
*/ |
|
109 |
boolean finished; |
|
110 |
||
111 |
/** |
|
112 |
* Construct an AbstractWrappingSpliterator from a |
|
113 |
* {@code Supplier<Spliterator>}. |
|
114 |
*/ |
|
115 |
AbstractWrappingSpliterator(PipelineHelper<P_OUT> ph, |
|
116 |
Supplier<Spliterator<P_IN>> spliteratorSupplier, |
|
117 |
boolean parallel) { |
|
118 |
this.ph = ph; |
|
119 |
this.spliteratorSupplier = spliteratorSupplier; |
|
120 |
this.spliterator = null; |
|
121 |
this.isParallel = parallel; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Construct an AbstractWrappingSpliterator from a |
|
126 |
* {@code Spliterator}. |
|
127 |
*/ |
|
128 |
AbstractWrappingSpliterator(PipelineHelper<P_OUT> ph, |
|
129 |
Spliterator<P_IN> spliterator, |
|
130 |
boolean parallel) { |
|
131 |
this.ph = ph; |
|
132 |
this.spliteratorSupplier = null; |
|
133 |
this.spliterator = spliterator; |
|
134 |
this.isParallel = parallel; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Called before advancing to set up spliterator, if needed. |
|
139 |
*/ |
|
140 |
final void init() { |
|
141 |
if (spliterator == null) { |
|
142 |
spliterator = spliteratorSupplier.get(); |
|
143 |
spliteratorSupplier = null; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Get an element from the source, pushing it into the sink chain, |
|
149 |
* setting up the buffer if needed |
|
150 |
* @return whether there are elements to consume from the buffer |
|
151 |
*/ |
|
152 |
final boolean doAdvance() { |
|
153 |
if (buffer == null) { |
|
154 |
if (finished) |
|
155 |
return false; |
|
156 |
||
157 |
init(); |
|
158 |
initPartialTraversalState(); |
|
159 |
nextToConsume = 0; |
|
160 |
bufferSink.begin(spliterator.getExactSizeIfKnown()); |
|
161 |
return fillBuffer(); |
|
162 |
} |
|
163 |
else { |
|
164 |
++nextToConsume; |
|
165 |
boolean hasNext = nextToConsume < buffer.count(); |
|
166 |
if (!hasNext) { |
|
167 |
nextToConsume = 0; |
|
168 |
buffer.clear(); |
|
169 |
hasNext = fillBuffer(); |
|
170 |
} |
|
171 |
return hasNext; |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Invokes the shape-specific constructor with the provided arguments |
|
177 |
* and returns the result. |
|
178 |
*/ |
|
179 |
abstract AbstractWrappingSpliterator<P_IN, P_OUT, ?> wrap(Spliterator<P_IN> s); |
|
180 |
||
181 |
/** |
|
182 |
* Initializes buffer, sink chain, and pusher for a shape-specific |
|
183 |
* implementation. |
|
184 |
*/ |
|
185 |
abstract void initPartialTraversalState(); |
|
186 |
||
187 |
@Override |
|
188 |
public Spliterator<P_OUT> trySplit() { |
|
189 |
if (isParallel && !finished) { |
|
190 |
init(); |
|
191 |
||
192 |
Spliterator<P_IN> split = spliterator.trySplit(); |
|
193 |
return (split == null) ? null : wrap(split); |
|
194 |
} |
|
195 |
else |
|
196 |
return null; |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* If the buffer is empty, push elements into the sink chain until |
|
201 |
* the source is empty or cancellation is requested. |
|
202 |
* @return whether there are elements to consume from the buffer |
|
203 |
*/ |
|
204 |
private boolean fillBuffer() { |
|
205 |
while (buffer.count() == 0) { |
|
206 |
if (bufferSink.cancellationRequested() || !pusher.getAsBoolean()) { |
|
207 |
if (finished) |
|
208 |
return false; |
|
209 |
else { |
|
210 |
bufferSink.end(); // might trigger more elements |
|
211 |
finished = true; |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
return true; |
|
216 |
} |
|
217 |
||
218 |
@Override |
|
219 |
public final long estimateSize() { |
|
220 |
init(); |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
221 |
// Use the estimate of the wrapped spliterator |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
222 |
// Note this may not be accurate if there are filter/flatMap |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
223 |
// operations filtering or adding elements to the stream |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
224 |
return spliterator.estimateSize(); |
17182 | 225 |
} |
226 |
||
227 |
@Override |
|
228 |
public final long getExactSizeIfKnown() { |
|
229 |
init(); |
|
230 |
return StreamOpFlag.SIZED.isKnown(ph.getStreamAndOpFlags()) |
|
231 |
? spliterator.getExactSizeIfKnown() |
|
232 |
: -1; |
|
233 |
} |
|
234 |
||
235 |
@Override |
|
236 |
public final int characteristics() { |
|
237 |
init(); |
|
238 |
||
239 |
// Get the characteristics from the pipeline |
|
240 |
int c = StreamOpFlag.toCharacteristics(StreamOpFlag.toStreamFlags(ph.getStreamAndOpFlags())); |
|
241 |
||
242 |
// Mask off the size and uniform characteristics and replace with |
|
243 |
// those of the spliterator |
|
244 |
// Note that a non-uniform spliterator can change from something |
|
245 |
// with an exact size to an estimate for a sub-split, for example |
|
246 |
// with HashSet where the size is known at the top level spliterator |
|
247 |
// but for sub-splits only an estimate is known |
|
248 |
if ((c & Spliterator.SIZED) != 0) { |
|
249 |
c &= ~(Spliterator.SIZED | Spliterator.SUBSIZED); |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
250 |
c |= (spliterator.characteristics() & (Spliterator.SIZED | Spliterator.SUBSIZED)); |
17182 | 251 |
} |
252 |
||
253 |
return c; |
|
254 |
} |
|
255 |
||
256 |
@Override |
|
257 |
public Comparator<? super P_OUT> getComparator() { |
|
258 |
if (!hasCharacteristics(SORTED)) |
|
259 |
throw new IllegalStateException(); |
|
260 |
return null; |
|
261 |
} |
|
262 |
||
263 |
@Override |
|
264 |
public final String toString() { |
|
265 |
return String.format("%s[%s]", getClass().getName(), spliterator); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
static final class WrappingSpliterator<P_IN, P_OUT> |
|
270 |
extends AbstractWrappingSpliterator<P_IN, P_OUT, SpinedBuffer<P_OUT>> { |
|
271 |
||
272 |
WrappingSpliterator(PipelineHelper<P_OUT> ph, |
|
273 |
Supplier<Spliterator<P_IN>> supplier, |
|
274 |
boolean parallel) { |
|
275 |
super(ph, supplier, parallel); |
|
276 |
} |
|
277 |
||
278 |
WrappingSpliterator(PipelineHelper<P_OUT> ph, |
|
279 |
Spliterator<P_IN> spliterator, |
|
280 |
boolean parallel) { |
|
281 |
super(ph, spliterator, parallel); |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
285 |
WrappingSpliterator<P_IN, P_OUT> wrap(Spliterator<P_IN> s) { |
|
286 |
return new WrappingSpliterator<>(ph, s, isParallel); |
|
287 |
} |
|
288 |
||
289 |
@Override |
|
290 |
void initPartialTraversalState() { |
|
291 |
SpinedBuffer<P_OUT> b = new SpinedBuffer<>(); |
|
292 |
buffer = b; |
|
293 |
bufferSink = ph.wrapSink(b::accept); |
|
294 |
pusher = () -> spliterator.tryAdvance(bufferSink); |
|
295 |
} |
|
296 |
||
297 |
@Override |
|
298 |
public boolean tryAdvance(Consumer<? super P_OUT> consumer) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
299 |
Objects.requireNonNull(consumer); |
17182 | 300 |
boolean hasNext = doAdvance(); |
301 |
if (hasNext) |
|
302 |
consumer.accept(buffer.get(nextToConsume)); |
|
303 |
return hasNext; |
|
304 |
} |
|
305 |
||
306 |
@Override |
|
307 |
public void forEachRemaining(Consumer<? super P_OUT> consumer) { |
|
308 |
if (buffer == null && !finished) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
309 |
Objects.requireNonNull(consumer); |
17182 | 310 |
init(); |
311 |
||
312 |
ph.wrapAndCopyInto((Sink<P_OUT>) consumer::accept, spliterator); |
|
313 |
finished = true; |
|
314 |
} |
|
315 |
else { |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
316 |
do { } while (tryAdvance(consumer)); |
17182 | 317 |
} |
318 |
} |
|
319 |
} |
|
320 |
||
321 |
static final class IntWrappingSpliterator<P_IN> |
|
322 |
extends AbstractWrappingSpliterator<P_IN, Integer, SpinedBuffer.OfInt> |
|
323 |
implements Spliterator.OfInt { |
|
324 |
||
325 |
IntWrappingSpliterator(PipelineHelper<Integer> ph, |
|
326 |
Supplier<Spliterator<P_IN>> supplier, |
|
327 |
boolean parallel) { |
|
328 |
super(ph, supplier, parallel); |
|
329 |
} |
|
330 |
||
331 |
IntWrappingSpliterator(PipelineHelper<Integer> ph, |
|
332 |
Spliterator<P_IN> spliterator, |
|
333 |
boolean parallel) { |
|
334 |
super(ph, spliterator, parallel); |
|
335 |
} |
|
336 |
||
337 |
@Override |
|
338 |
AbstractWrappingSpliterator<P_IN, Integer, ?> wrap(Spliterator<P_IN> s) { |
|
339 |
return new IntWrappingSpliterator<>(ph, s, isParallel); |
|
340 |
} |
|
341 |
||
342 |
@Override |
|
343 |
void initPartialTraversalState() { |
|
344 |
SpinedBuffer.OfInt b = new SpinedBuffer.OfInt(); |
|
345 |
buffer = b; |
|
346 |
bufferSink = ph.wrapSink((Sink.OfInt) b::accept); |
|
347 |
pusher = () -> spliterator.tryAdvance(bufferSink); |
|
348 |
} |
|
349 |
||
350 |
@Override |
|
351 |
public Spliterator.OfInt trySplit() { |
|
352 |
return (Spliterator.OfInt) super.trySplit(); |
|
353 |
} |
|
354 |
||
355 |
@Override |
|
356 |
public boolean tryAdvance(IntConsumer consumer) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
357 |
Objects.requireNonNull(consumer); |
17182 | 358 |
boolean hasNext = doAdvance(); |
359 |
if (hasNext) |
|
360 |
consumer.accept(buffer.get(nextToConsume)); |
|
361 |
return hasNext; |
|
362 |
} |
|
363 |
||
364 |
@Override |
|
365 |
public void forEachRemaining(IntConsumer consumer) { |
|
366 |
if (buffer == null && !finished) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
367 |
Objects.requireNonNull(consumer); |
17182 | 368 |
init(); |
369 |
||
370 |
ph.wrapAndCopyInto((Sink.OfInt) consumer::accept, spliterator); |
|
371 |
finished = true; |
|
372 |
} |
|
373 |
else { |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
374 |
do { } while (tryAdvance(consumer)); |
17182 | 375 |
} |
376 |
} |
|
377 |
} |
|
378 |
||
379 |
static final class LongWrappingSpliterator<P_IN> |
|
380 |
extends AbstractWrappingSpliterator<P_IN, Long, SpinedBuffer.OfLong> |
|
381 |
implements Spliterator.OfLong { |
|
382 |
||
383 |
LongWrappingSpliterator(PipelineHelper<Long> ph, |
|
384 |
Supplier<Spliterator<P_IN>> supplier, |
|
385 |
boolean parallel) { |
|
386 |
super(ph, supplier, parallel); |
|
387 |
} |
|
388 |
||
389 |
LongWrappingSpliterator(PipelineHelper<Long> ph, |
|
390 |
Spliterator<P_IN> spliterator, |
|
391 |
boolean parallel) { |
|
392 |
super(ph, spliterator, parallel); |
|
393 |
} |
|
394 |
||
395 |
@Override |
|
396 |
AbstractWrappingSpliterator<P_IN, Long, ?> wrap(Spliterator<P_IN> s) { |
|
397 |
return new LongWrappingSpliterator<>(ph, s, isParallel); |
|
398 |
} |
|
399 |
||
400 |
@Override |
|
401 |
void initPartialTraversalState() { |
|
402 |
SpinedBuffer.OfLong b = new SpinedBuffer.OfLong(); |
|
403 |
buffer = b; |
|
404 |
bufferSink = ph.wrapSink((Sink.OfLong) b::accept); |
|
405 |
pusher = () -> spliterator.tryAdvance(bufferSink); |
|
406 |
} |
|
407 |
||
408 |
@Override |
|
409 |
public Spliterator.OfLong trySplit() { |
|
410 |
return (Spliterator.OfLong) super.trySplit(); |
|
411 |
} |
|
412 |
||
413 |
@Override |
|
414 |
public boolean tryAdvance(LongConsumer consumer) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
415 |
Objects.requireNonNull(consumer); |
17182 | 416 |
boolean hasNext = doAdvance(); |
417 |
if (hasNext) |
|
418 |
consumer.accept(buffer.get(nextToConsume)); |
|
419 |
return hasNext; |
|
420 |
} |
|
421 |
||
422 |
@Override |
|
423 |
public void forEachRemaining(LongConsumer consumer) { |
|
424 |
if (buffer == null && !finished) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
425 |
Objects.requireNonNull(consumer); |
17182 | 426 |
init(); |
427 |
||
428 |
ph.wrapAndCopyInto((Sink.OfLong) consumer::accept, spliterator); |
|
429 |
finished = true; |
|
430 |
} |
|
431 |
else { |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
432 |
do { } while (tryAdvance(consumer)); |
17182 | 433 |
} |
434 |
} |
|
435 |
} |
|
436 |
||
437 |
static final class DoubleWrappingSpliterator<P_IN> |
|
438 |
extends AbstractWrappingSpliterator<P_IN, Double, SpinedBuffer.OfDouble> |
|
439 |
implements Spliterator.OfDouble { |
|
440 |
||
441 |
DoubleWrappingSpliterator(PipelineHelper<Double> ph, |
|
442 |
Supplier<Spliterator<P_IN>> supplier, |
|
443 |
boolean parallel) { |
|
444 |
super(ph, supplier, parallel); |
|
445 |
} |
|
446 |
||
447 |
DoubleWrappingSpliterator(PipelineHelper<Double> ph, |
|
448 |
Spliterator<P_IN> spliterator, |
|
449 |
boolean parallel) { |
|
450 |
super(ph, spliterator, parallel); |
|
451 |
} |
|
452 |
||
453 |
@Override |
|
454 |
AbstractWrappingSpliterator<P_IN, Double, ?> wrap(Spliterator<P_IN> s) { |
|
455 |
return new DoubleWrappingSpliterator<>(ph, s, isParallel); |
|
456 |
} |
|
457 |
||
458 |
@Override |
|
459 |
void initPartialTraversalState() { |
|
460 |
SpinedBuffer.OfDouble b = new SpinedBuffer.OfDouble(); |
|
461 |
buffer = b; |
|
462 |
bufferSink = ph.wrapSink((Sink.OfDouble) b::accept); |
|
463 |
pusher = () -> spliterator.tryAdvance(bufferSink); |
|
464 |
} |
|
465 |
||
466 |
@Override |
|
467 |
public Spliterator.OfDouble trySplit() { |
|
468 |
return (Spliterator.OfDouble) super.trySplit(); |
|
469 |
} |
|
470 |
||
471 |
@Override |
|
472 |
public boolean tryAdvance(DoubleConsumer consumer) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
473 |
Objects.requireNonNull(consumer); |
17182 | 474 |
boolean hasNext = doAdvance(); |
475 |
if (hasNext) |
|
476 |
consumer.accept(buffer.get(nextToConsume)); |
|
477 |
return hasNext; |
|
478 |
} |
|
479 |
||
480 |
@Override |
|
481 |
public void forEachRemaining(DoubleConsumer consumer) { |
|
482 |
if (buffer == null && !finished) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
483 |
Objects.requireNonNull(consumer); |
17182 | 484 |
init(); |
485 |
||
486 |
ph.wrapAndCopyInto((Sink.OfDouble) consumer::accept, spliterator); |
|
487 |
finished = true; |
|
488 |
} |
|
489 |
else { |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
490 |
do { } while (tryAdvance(consumer)); |
17182 | 491 |
} |
492 |
} |
|
493 |
} |
|
494 |
||
495 |
/** |
|
496 |
* Spliterator implementation that delegates to an underlying spliterator, |
|
497 |
* acquiring the spliterator from a {@code Supplier<Spliterator>} on the |
|
498 |
* first call to any spliterator method. |
|
499 |
* @param <T> |
|
500 |
*/ |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
501 |
static class DelegatingSpliterator<T, T_SPLITR extends Spliterator<T>> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
502 |
implements Spliterator<T> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
503 |
private final Supplier<? extends T_SPLITR> supplier; |
17182 | 504 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
505 |
private T_SPLITR s; |
17182 | 506 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
507 |
DelegatingSpliterator(Supplier<? extends T_SPLITR> supplier) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
508 |
this.supplier = supplier; |
17182 | 509 |
} |
510 |
||
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
511 |
T_SPLITR get() { |
17182 | 512 |
if (s == null) { |
513 |
s = supplier.get(); |
|
514 |
} |
|
515 |
return s; |
|
516 |
} |
|
517 |
||
518 |
@Override |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
519 |
public T_SPLITR trySplit() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
520 |
return (T_SPLITR) get().trySplit(); |
17182 | 521 |
} |
522 |
||
523 |
@Override |
|
524 |
public boolean tryAdvance(Consumer<? super T> consumer) { |
|
525 |
return get().tryAdvance(consumer); |
|
526 |
} |
|
527 |
||
528 |
@Override |
|
529 |
public void forEachRemaining(Consumer<? super T> consumer) { |
|
530 |
get().forEachRemaining(consumer); |
|
531 |
} |
|
532 |
||
533 |
@Override |
|
534 |
public long estimateSize() { |
|
535 |
return get().estimateSize(); |
|
536 |
} |
|
537 |
||
538 |
@Override |
|
539 |
public int characteristics() { |
|
540 |
return get().characteristics(); |
|
541 |
} |
|
542 |
||
543 |
@Override |
|
544 |
public Comparator<? super T> getComparator() { |
|
545 |
return get().getComparator(); |
|
546 |
} |
|
547 |
||
548 |
@Override |
|
549 |
public long getExactSizeIfKnown() { |
|
550 |
return get().getExactSizeIfKnown(); |
|
551 |
} |
|
552 |
||
553 |
@Override |
|
554 |
public String toString() { |
|
555 |
return getClass().getName() + "[" + get() + "]"; |
|
556 |
} |
|
557 |
||
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
558 |
static class OfPrimitive<T, T_CONS, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
559 |
extends DelegatingSpliterator<T, T_SPLITR> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
560 |
implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
561 |
OfPrimitive(Supplier<? extends T_SPLITR> supplier) { |
17182 | 562 |
super(supplier); |
563 |
} |
|
564 |
||
565 |
@Override |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
566 |
public boolean tryAdvance(T_CONS consumer) { |
17182 | 567 |
return get().tryAdvance(consumer); |
568 |
} |
|
569 |
||
570 |
@Override |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
571 |
public void forEachRemaining(T_CONS consumer) { |
17182 | 572 |
get().forEachRemaining(consumer); |
573 |
} |
|
574 |
} |
|
575 |
||
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
576 |
static final class OfInt |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
577 |
extends OfPrimitive<Integer, IntConsumer, Spliterator.OfInt> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
578 |
implements Spliterator.OfInt { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
579 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
580 |
OfInt(Supplier<Spliterator.OfInt> supplier) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
581 |
super(supplier); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
582 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
583 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
584 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
585 |
static final class OfLong |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
586 |
extends OfPrimitive<Long, LongConsumer, Spliterator.OfLong> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
587 |
implements Spliterator.OfLong { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
588 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
589 |
OfLong(Supplier<Spliterator.OfLong> supplier) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
590 |
super(supplier); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
591 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
592 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
593 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
594 |
static final class OfDouble |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
595 |
extends OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
596 |
implements Spliterator.OfDouble { |
17182 | 597 |
|
598 |
OfDouble(Supplier<Spliterator.OfDouble> supplier) { |
|
599 |
super(supplier); |
|
600 |
} |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
601 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
602 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
603 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
604 |
/** |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
605 |
* A slice Spliterator from a source Spliterator that reports |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
606 |
* {@code SUBSIZED}. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
607 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
608 |
*/ |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
609 |
static abstract class SliceSpliterator<T, T_SPLITR extends Spliterator<T>> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
610 |
// The start index of the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
611 |
final long sliceOrigin; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
612 |
// One past the last index of the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
613 |
final long sliceFence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
614 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
615 |
// The spliterator to slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
616 |
T_SPLITR s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
617 |
// current (absolute) index, modified on advance/split |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
618 |
long index; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
619 |
// one past last (absolute) index or sliceFence, which ever is smaller |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
620 |
long fence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
621 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
622 |
SliceSpliterator(T_SPLITR s, long sliceOrigin, long sliceFence, long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
623 |
assert s.hasCharacteristics(Spliterator.SUBSIZED); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
624 |
this.s = s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
625 |
this.sliceOrigin = sliceOrigin; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
626 |
this.sliceFence = sliceFence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
627 |
this.index = origin; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
628 |
this.fence = fence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
629 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
630 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
631 |
protected abstract T_SPLITR makeSpliterator(T_SPLITR s, long sliceOrigin, long sliceFence, long origin, long fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
632 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
633 |
public T_SPLITR trySplit() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
634 |
if (sliceOrigin >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
635 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
636 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
637 |
if (index >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
638 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
639 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
640 |
// Keep splitting until the left and right splits intersect with the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
641 |
// thereby ensuring the size estimate decreases. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
642 |
// This also avoids creating empty spliterators which can result in |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
643 |
// existing and additionally created F/J tasks that perform |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
644 |
// redundant work on no elements. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
645 |
while (true) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
646 |
T_SPLITR leftSplit = (T_SPLITR) s.trySplit(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
647 |
if (leftSplit == null) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
648 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
649 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
650 |
long leftSplitFenceUnbounded = index + leftSplit.estimateSize(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
651 |
long leftSplitFence = Math.min(leftSplitFenceUnbounded, sliceFence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
652 |
if (sliceOrigin >= leftSplitFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
653 |
// The left split does not intersect with, and is to the left of, the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
654 |
// The right split does intersect |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
655 |
// Discard the left split and split further with the right split |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
656 |
index = leftSplitFence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
657 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
658 |
else if (leftSplitFence >= sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
659 |
// The right split does not intersect with, and is to the right of, the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
660 |
// The left split does intersect |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
661 |
// Discard the right split and split further with the left split |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
662 |
s = leftSplit; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
663 |
fence = leftSplitFence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
664 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
665 |
else if (index >= sliceOrigin && leftSplitFenceUnbounded <= sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
666 |
// The left split is contained within the slice, return the underlying left split |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
667 |
// Right split is contained within or intersects with the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
668 |
index = leftSplitFence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
669 |
return leftSplit; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
670 |
} else { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
671 |
// The left split intersects with the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
672 |
// Right split is contained within or intersects with the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
673 |
return makeSpliterator(leftSplit, sliceOrigin, sliceFence, index, index = leftSplitFence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
674 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
675 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
676 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
677 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
678 |
public long estimateSize() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
679 |
return (sliceOrigin < fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
680 |
? fence - Math.max(sliceOrigin, index) : 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
681 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
682 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
683 |
public int characteristics() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
684 |
return s.characteristics(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
685 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
686 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
687 |
static final class OfRef<T> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
688 |
extends SliceSpliterator<T, Spliterator<T>> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
689 |
implements Spliterator<T> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
690 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
691 |
OfRef(Spliterator<T> s, long sliceOrigin, long sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
692 |
this(s, sliceOrigin, sliceFence, 0, Math.min(s.estimateSize(), sliceFence)); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
693 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
694 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
695 |
private OfRef(Spliterator<T> s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
696 |
long sliceOrigin, long sliceFence, long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
697 |
super(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
698 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
699 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
700 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
701 |
protected Spliterator<T> makeSpliterator(Spliterator<T> s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
702 |
long sliceOrigin, long sliceFence, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
703 |
long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
704 |
return new OfRef<>(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
705 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
706 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
707 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
708 |
public boolean tryAdvance(Consumer<? super T> action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
709 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
710 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
711 |
if (sliceOrigin >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
712 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
713 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
714 |
while (sliceOrigin > index) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
715 |
s.tryAdvance(e -> {}); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
716 |
index++; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
717 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
718 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
719 |
if (index >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
720 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
721 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
722 |
index++; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
723 |
return s.tryAdvance(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
724 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
725 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
726 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
727 |
public void forEachRemaining(Consumer<? super T> action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
728 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
729 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
730 |
if (sliceOrigin >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
731 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
732 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
733 |
if (index >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
734 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
735 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
736 |
if (index >= sliceOrigin && (index + s.estimateSize()) <= sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
737 |
// The spliterator is contained within the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
738 |
s.forEachRemaining(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
739 |
index = fence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
740 |
} else { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
741 |
// The spliterator intersects with the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
742 |
while (sliceOrigin > index) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
743 |
s.tryAdvance(e -> {}); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
744 |
index++; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
745 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
746 |
// Traverse elements up to the fence |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
747 |
for (;index < fence; index++) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
748 |
s.tryAdvance(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
749 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
750 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
751 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
752 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
753 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
754 |
static abstract class OfPrimitive<T, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
755 |
T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
756 |
T_CONS> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
757 |
extends SliceSpliterator<T, T_SPLITR> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
758 |
implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
759 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
760 |
OfPrimitive(T_SPLITR s, long sliceOrigin, long sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
761 |
this(s, sliceOrigin, sliceFence, 0, Math.min(s.estimateSize(), sliceFence)); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
762 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
763 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
764 |
private OfPrimitive(T_SPLITR s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
765 |
long sliceOrigin, long sliceFence, long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
766 |
super(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
767 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
768 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
769 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
770 |
public boolean tryAdvance(T_CONS action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
771 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
772 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
773 |
if (sliceOrigin >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
774 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
775 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
776 |
while (sliceOrigin > index) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
777 |
s.tryAdvance(emptyConsumer()); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
778 |
index++; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
779 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
780 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
781 |
if (index >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
782 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
783 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
784 |
index++; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
785 |
return s.tryAdvance(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
786 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
787 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
788 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
789 |
public void forEachRemaining(T_CONS action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
790 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
791 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
792 |
if (sliceOrigin >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
793 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
794 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
795 |
if (index >= fence) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
796 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
797 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
798 |
if (index >= sliceOrigin && (index + s.estimateSize()) <= sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
799 |
// The spliterator is contained within the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
800 |
s.forEachRemaining(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
801 |
index = fence; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
802 |
} else { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
803 |
// The spliterator intersects with the slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
804 |
while (sliceOrigin > index) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
805 |
s.tryAdvance(emptyConsumer()); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
806 |
index++; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
807 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
808 |
// Traverse elements up to the fence |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
809 |
for (;index < fence; index++) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
810 |
s.tryAdvance(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
811 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
812 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
813 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
814 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
815 |
protected abstract T_CONS emptyConsumer(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
816 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
817 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
818 |
static final class OfInt extends OfPrimitive<Integer, Spliterator.OfInt, IntConsumer> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
819 |
implements Spliterator.OfInt { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
820 |
OfInt(Spliterator.OfInt s, long sliceOrigin, long sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
821 |
super(s, sliceOrigin, sliceFence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
822 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
823 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
824 |
OfInt(Spliterator.OfInt s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
825 |
long sliceOrigin, long sliceFence, long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
826 |
super(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
827 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
828 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
829 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
830 |
protected Spliterator.OfInt makeSpliterator(Spliterator.OfInt s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
831 |
long sliceOrigin, long sliceFence, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
832 |
long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
833 |
return new SliceSpliterator.OfInt(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
834 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
835 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
836 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
837 |
protected IntConsumer emptyConsumer() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
838 |
return e -> {}; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
839 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
840 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
841 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
842 |
static final class OfLong extends OfPrimitive<Long, Spliterator.OfLong, LongConsumer> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
843 |
implements Spliterator.OfLong { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
844 |
OfLong(Spliterator.OfLong s, long sliceOrigin, long sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
845 |
super(s, sliceOrigin, sliceFence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
846 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
847 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
848 |
OfLong(Spliterator.OfLong s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
849 |
long sliceOrigin, long sliceFence, long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
850 |
super(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
851 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
852 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
853 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
854 |
protected Spliterator.OfLong makeSpliterator(Spliterator.OfLong s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
855 |
long sliceOrigin, long sliceFence, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
856 |
long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
857 |
return new SliceSpliterator.OfLong(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
858 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
859 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
860 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
861 |
protected LongConsumer emptyConsumer() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
862 |
return e -> {}; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
863 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
864 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
865 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
866 |
static final class OfDouble extends OfPrimitive<Double, Spliterator.OfDouble, DoubleConsumer> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
867 |
implements Spliterator.OfDouble { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
868 |
OfDouble(Spliterator.OfDouble s, long sliceOrigin, long sliceFence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
869 |
super(s, sliceOrigin, sliceFence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
870 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
871 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
872 |
OfDouble(Spliterator.OfDouble s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
873 |
long sliceOrigin, long sliceFence, long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
874 |
super(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
875 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
876 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
877 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
878 |
protected Spliterator.OfDouble makeSpliterator(Spliterator.OfDouble s, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
879 |
long sliceOrigin, long sliceFence, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
880 |
long origin, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
881 |
return new SliceSpliterator.OfDouble(s, sliceOrigin, sliceFence, origin, fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
882 |
} |
17182 | 883 |
|
884 |
@Override |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
885 |
protected DoubleConsumer emptyConsumer() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
886 |
return e -> {}; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
887 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
888 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
889 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
890 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
891 |
/** |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
892 |
* A slice Spliterator that does not preserve order, if any, of a source |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
893 |
* Spliterator. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
894 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
895 |
* Note: The source spliterator may report {@code ORDERED} since that |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
896 |
* spliterator be the result of a previous pipeline stage that was |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
897 |
* collected to a {@code Node}. It is the order of the pipeline stage |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
898 |
* that governs whether the this slice spliterator is to be used or not. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
899 |
*/ |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
900 |
static abstract class UnorderedSliceSpliterator<T, T_SPLITR extends Spliterator<T>> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
901 |
static final int CHUNK_SIZE = 1 << 7; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
902 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
903 |
// The spliterator to slice |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
904 |
protected final T_SPLITR s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
905 |
protected final boolean unlimited; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
906 |
private final long skipThreshold; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
907 |
private final AtomicLong permits; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
908 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
909 |
UnorderedSliceSpliterator(T_SPLITR s, long skip, long limit) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
910 |
this.s = s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
911 |
this.unlimited = limit < 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
912 |
this.skipThreshold = limit >= 0 ? limit : 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
913 |
this.permits = new AtomicLong(limit >= 0 ? skip + limit : skip); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
914 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
915 |
|
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19188
diff
changeset
|
916 |
UnorderedSliceSpliterator(T_SPLITR s, |
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19188
diff
changeset
|
917 |
UnorderedSliceSpliterator<T, T_SPLITR> parent) { |
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
918 |
this.s = s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
919 |
this.unlimited = parent.unlimited; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
920 |
this.permits = parent.permits; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
921 |
this.skipThreshold = parent.skipThreshold; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
922 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
923 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
924 |
/** |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
925 |
* Acquire permission to skip or process elements. The caller must |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
926 |
* first acquire the elements, then consult this method for guidance |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
927 |
* as to what to do with the data. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
928 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
929 |
* <p>We use an {@code AtomicLong} to atomically maintain a counter, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
930 |
* which is initialized as skip+limit if we are limiting, or skip only |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
931 |
* if we are not limiting. The user should consult the method |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
932 |
* {@code checkPermits()} before acquiring data elements. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
933 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
934 |
* @param numElements the number of elements the caller has in hand |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
935 |
* @return the number of elements that should be processed; any |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
936 |
* remaining elements should be discarded. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
937 |
*/ |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
938 |
protected final long acquirePermits(long numElements) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
939 |
long remainingPermits; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
940 |
long grabbing; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
941 |
// permits never increase, and don't decrease below zero |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
942 |
assert numElements > 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
943 |
do { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
944 |
remainingPermits = permits.get(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
945 |
if (remainingPermits == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
946 |
return unlimited ? numElements : 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
947 |
grabbing = Math.min(remainingPermits, numElements); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
948 |
} while (grabbing > 0 && |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
949 |
!permits.compareAndSet(remainingPermits, remainingPermits - grabbing)); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
950 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
951 |
if (unlimited) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
952 |
return Math.max(numElements - grabbing, 0); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
953 |
else if (remainingPermits > skipThreshold) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
954 |
return Math.max(grabbing - (remainingPermits - skipThreshold), 0); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
955 |
else |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
956 |
return grabbing; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
957 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
958 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
959 |
enum PermitStatus { NO_MORE, MAYBE_MORE, UNLIMITED } |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
960 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
961 |
/** Call to check if permits might be available before acquiring data */ |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
962 |
protected final PermitStatus permitStatus() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
963 |
if (permits.get() > 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
964 |
return PermitStatus.MAYBE_MORE; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
965 |
else |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
966 |
return unlimited ? PermitStatus.UNLIMITED : PermitStatus.NO_MORE; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
967 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
968 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
969 |
public final T_SPLITR trySplit() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
970 |
// Stop splitting when there are no more limit permits |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
971 |
if (permits.get() == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
972 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
973 |
T_SPLITR split = (T_SPLITR) s.trySplit(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
974 |
return split == null ? null : makeSpliterator(split); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
975 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
976 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
977 |
protected abstract T_SPLITR makeSpliterator(T_SPLITR s); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
978 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
979 |
public final long estimateSize() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
980 |
return s.estimateSize(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
981 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
982 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
983 |
public final int characteristics() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
984 |
return s.characteristics() & |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
985 |
~(Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
986 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
987 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
988 |
static final class OfRef<T> extends UnorderedSliceSpliterator<T, Spliterator<T>> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
989 |
implements Spliterator<T>, Consumer<T> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
990 |
T tmpSlot; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
991 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
992 |
OfRef(Spliterator<T> s, long skip, long limit) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
993 |
super(s, skip, limit); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
994 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
995 |
|
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19188
diff
changeset
|
996 |
OfRef(Spliterator<T> s, OfRef<T> parent) { |
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
997 |
super(s, parent); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
998 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
999 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1000 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1001 |
public final void accept(T t) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1002 |
tmpSlot = t; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1003 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1004 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1005 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1006 |
public boolean tryAdvance(Consumer<? super T> action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1007 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1008 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1009 |
while (permitStatus() != PermitStatus.NO_MORE) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1010 |
if (!s.tryAdvance(this)) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1011 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1012 |
else if (acquirePermits(1) == 1) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1013 |
action.accept(tmpSlot); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1014 |
tmpSlot = null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1015 |
return true; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1016 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1017 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1018 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1019 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1020 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1021 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1022 |
public void forEachRemaining(Consumer<? super T> action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1023 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1024 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1025 |
ArrayBuffer.OfRef<T> sb = null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1026 |
PermitStatus permitStatus; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1027 |
while ((permitStatus = permitStatus()) != PermitStatus.NO_MORE) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1028 |
if (permitStatus == PermitStatus.MAYBE_MORE) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1029 |
// Optimistically traverse elements up to a threshold of CHUNK_SIZE |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1030 |
if (sb == null) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1031 |
sb = new ArrayBuffer.OfRef<>(CHUNK_SIZE); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1032 |
else |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1033 |
sb.reset(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1034 |
long permitsRequested = 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1035 |
do { } while (s.tryAdvance(sb) && ++permitsRequested < CHUNK_SIZE); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1036 |
if (permitsRequested == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1037 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1038 |
sb.forEach(action, acquirePermits(permitsRequested)); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1039 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1040 |
else { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1041 |
// Must be UNLIMITED; let 'er rip |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1042 |
s.forEachRemaining(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1043 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1044 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1045 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1046 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1047 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1048 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1049 |
protected Spliterator<T> makeSpliterator(Spliterator<T> s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1050 |
return new UnorderedSliceSpliterator.OfRef<>(s, this); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1051 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1052 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1053 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1054 |
/** |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1055 |
* Concrete sub-types must also be an instance of type {@code T_CONS}. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1056 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1057 |
* @param <T_BUFF> the type of the spined buffer. Must also be a type of |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1058 |
* {@code T_CONS}. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1059 |
*/ |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1060 |
static abstract class OfPrimitive< |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1061 |
T, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1062 |
T_CONS, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1063 |
T_BUFF extends ArrayBuffer.OfPrimitive<T_CONS>, |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1064 |
T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1065 |
extends UnorderedSliceSpliterator<T, T_SPLITR> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1066 |
implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1067 |
OfPrimitive(T_SPLITR s, long skip, long limit) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1068 |
super(s, skip, limit); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1069 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1070 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1071 |
OfPrimitive(T_SPLITR s, UnorderedSliceSpliterator.OfPrimitive parent) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1072 |
super(s, parent); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1073 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1074 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1075 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1076 |
public boolean tryAdvance(T_CONS action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1077 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1078 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1079 |
while (permitStatus() != PermitStatus.NO_MORE) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1080 |
if (!s.tryAdvance((T_CONS) this)) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1081 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1082 |
else if (acquirePermits(1) == 1) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1083 |
acceptConsumed(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1084 |
return true; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1085 |
} |
17182 | 1086 |
} |
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1087 |
return false; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1088 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1089 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1090 |
protected abstract void acceptConsumed(T_CONS action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1091 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1092 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1093 |
public void forEachRemaining(T_CONS action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1094 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1095 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1096 |
T_BUFF sb = null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1097 |
PermitStatus permitStatus; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1098 |
while ((permitStatus = permitStatus()) != PermitStatus.NO_MORE) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1099 |
if (permitStatus == PermitStatus.MAYBE_MORE) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1100 |
// Optimistically traverse elements up to a threshold of CHUNK_SIZE |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1101 |
if (sb == null) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1102 |
sb = bufferCreate(CHUNK_SIZE); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1103 |
else |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1104 |
sb.reset(); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1105 |
@SuppressWarnings("unchecked") |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1106 |
T_CONS sbc = (T_CONS) sb; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1107 |
long permitsRequested = 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1108 |
do { } while (s.tryAdvance(sbc) && ++permitsRequested < CHUNK_SIZE); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1109 |
if (permitsRequested == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1110 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1111 |
sb.forEach(action, acquirePermits(permitsRequested)); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1112 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1113 |
else { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1114 |
// Must be UNLIMITED; let 'er rip |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1115 |
s.forEachRemaining(action); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1116 |
return; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1117 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1118 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1119 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1120 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1121 |
protected abstract T_BUFF bufferCreate(int initialCapacity); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1122 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1123 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1124 |
static final class OfInt |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1125 |
extends OfPrimitive<Integer, IntConsumer, ArrayBuffer.OfInt, Spliterator.OfInt> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1126 |
implements Spliterator.OfInt, IntConsumer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1127 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1128 |
int tmpValue; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1129 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1130 |
OfInt(Spliterator.OfInt s, long skip, long limit) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1131 |
super(s, skip, limit); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1132 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1133 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1134 |
OfInt(Spliterator.OfInt s, UnorderedSliceSpliterator.OfInt parent) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1135 |
super(s, parent); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1136 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1137 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1138 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1139 |
public void accept(int value) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1140 |
tmpValue = value; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1141 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1142 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1143 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1144 |
protected void acceptConsumed(IntConsumer action) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1145 |
action.accept(tmpValue); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1146 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1147 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1148 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1149 |
protected ArrayBuffer.OfInt bufferCreate(int initialCapacity) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1150 |
return new ArrayBuffer.OfInt(initialCapacity); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1151 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1152 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1153 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1154 |
protected Spliterator.OfInt makeSpliterator(Spliterator.OfInt s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1155 |
return new UnorderedSliceSpliterator.OfInt(s, this); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1156 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1157 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1158 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1159 |
static final class OfLong |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1160 |
extends OfPrimitive<Long, LongConsumer, ArrayBuffer.OfLong, Spliterator.OfLong> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1161 |
implements Spliterator.OfLong, LongConsumer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1162 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1163 |
long tmpValue; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1164 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1165 |
OfLong(Spliterator.OfLong s, long skip, long limit) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1166 |
super(s, skip, limit); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1167 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1168 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1169 |
OfLong(Spliterator.OfLong s, UnorderedSliceSpliterator.OfLong parent) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1170 |
super(s, parent); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1171 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1172 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1173 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1174 |
public void accept(long value) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1175 |
tmpValue = value; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1176 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1177 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1178 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1179 |
protected void acceptConsumed(LongConsumer action) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1180 |
action.accept(tmpValue); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1181 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1182 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1183 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1184 |
protected ArrayBuffer.OfLong bufferCreate(int initialCapacity) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1185 |
return new ArrayBuffer.OfLong(initialCapacity); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1186 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1187 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1188 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1189 |
protected Spliterator.OfLong makeSpliterator(Spliterator.OfLong s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1190 |
return new UnorderedSliceSpliterator.OfLong(s, this); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1191 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1192 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1193 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1194 |
static final class OfDouble |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1195 |
extends OfPrimitive<Double, DoubleConsumer, ArrayBuffer.OfDouble, Spliterator.OfDouble> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1196 |
implements Spliterator.OfDouble, DoubleConsumer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1197 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1198 |
double tmpValue; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1199 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1200 |
OfDouble(Spliterator.OfDouble s, long skip, long limit) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1201 |
super(s, skip, limit); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1202 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1203 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1204 |
OfDouble(Spliterator.OfDouble s, UnorderedSliceSpliterator.OfDouble parent) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1205 |
super(s, parent); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1206 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1207 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1208 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1209 |
public void accept(double value) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1210 |
tmpValue = value; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1211 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1212 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1213 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1214 |
protected void acceptConsumed(DoubleConsumer action) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1215 |
action.accept(tmpValue); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1216 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1217 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1218 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1219 |
protected ArrayBuffer.OfDouble bufferCreate(int initialCapacity) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1220 |
return new ArrayBuffer.OfDouble(initialCapacity); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1221 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1222 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1223 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1224 |
protected Spliterator.OfDouble makeSpliterator(Spliterator.OfDouble s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1225 |
return new UnorderedSliceSpliterator.OfDouble(s, this); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1226 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1227 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1228 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1229 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1230 |
/** |
21422
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1231 |
* A wrapping spliterator that only reports distinct elements of the |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1232 |
* underlying spliterator. Does not preserve size and encounter order. |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1233 |
*/ |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1234 |
static final class DistinctSpliterator<T> implements Spliterator<T>, Consumer<T> { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1235 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1236 |
// The value to represent null in the ConcurrentHashMap |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1237 |
private static final Object NULL_VALUE = new Object(); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1238 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1239 |
// The underlying spliterator |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1240 |
private final Spliterator<T> s; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1241 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1242 |
// ConcurrentHashMap holding distinct elements as keys |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1243 |
private final ConcurrentHashMap<T, Boolean> seen; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1244 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1245 |
// Temporary element, only used with tryAdvance |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1246 |
private T tmpSlot; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1247 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1248 |
DistinctSpliterator(Spliterator<T> s) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1249 |
this(s, new ConcurrentHashMap<>()); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1250 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1251 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1252 |
private DistinctSpliterator(Spliterator<T> s, ConcurrentHashMap<T, Boolean> seen) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1253 |
this.s = s; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1254 |
this.seen = seen; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1255 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1256 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1257 |
@Override |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1258 |
public void accept(T t) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1259 |
this.tmpSlot = t; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1260 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1261 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1262 |
@SuppressWarnings("unchecked") |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1263 |
private T mapNull(T t) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1264 |
return t != null ? t : (T) NULL_VALUE; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1265 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1266 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1267 |
@Override |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1268 |
public boolean tryAdvance(Consumer<? super T> action) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1269 |
while (s.tryAdvance(this)) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1270 |
if (seen.putIfAbsent(mapNull(tmpSlot), Boolean.TRUE) == null) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1271 |
action.accept(tmpSlot); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1272 |
tmpSlot = null; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1273 |
return true; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1274 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1275 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1276 |
return false; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1277 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1278 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1279 |
@Override |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1280 |
public void forEachRemaining(Consumer<? super T> action) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1281 |
s.forEachRemaining(t -> { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1282 |
if (seen.putIfAbsent(mapNull(t), Boolean.TRUE) == null) { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1283 |
action.accept(t); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1284 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1285 |
}); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1286 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1287 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1288 |
@Override |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1289 |
public Spliterator<T> trySplit() { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1290 |
Spliterator<T> split = s.trySplit(); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1291 |
return (split != null) ? new DistinctSpliterator<>(split, seen) : null; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1292 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1293 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1294 |
@Override |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1295 |
public long estimateSize() { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1296 |
return s.estimateSize(); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1297 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1298 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1299 |
@Override |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1300 |
public int characteristics() { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1301 |
return (s.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED | |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1302 |
Spliterator.SORTED | Spliterator.ORDERED)) |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1303 |
| Spliterator.DISTINCT; |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1304 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1305 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1306 |
@Override |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1307 |
public Comparator<? super T> getComparator() { |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1308 |
return s.getComparator(); |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1309 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1310 |
} |
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1311 |
|
6fca66995a27
8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents:
19850
diff
changeset
|
1312 |
/** |
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1313 |
* A Spliterator that infinitely supplies elements in no particular order. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1314 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1315 |
* <p>Splitting divides the estimated size in two and stops when the |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1316 |
* estimate size is 0. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1317 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1318 |
* <p>The {@code forEachRemaining} method if invoked will never terminate. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1319 |
* The {@coe tryAdvance} method always returns true. |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1320 |
* |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1321 |
*/ |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1322 |
static abstract class InfiniteSupplyingSpliterator<T> implements Spliterator<T> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1323 |
long estimate; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1324 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1325 |
protected InfiniteSupplyingSpliterator(long estimate) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1326 |
this.estimate = estimate; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1327 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1328 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1329 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1330 |
public long estimateSize() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1331 |
return estimate; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1332 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1333 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1334 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1335 |
public int characteristics() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1336 |
return IMMUTABLE; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1337 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1338 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1339 |
static final class OfRef<T> extends InfiniteSupplyingSpliterator<T> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1340 |
final Supplier<T> s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1341 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1342 |
OfRef(long size, Supplier<T> s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1343 |
super(size); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1344 |
this.s = s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1345 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1346 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1347 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1348 |
public boolean tryAdvance(Consumer<? super T> action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1349 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1350 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1351 |
action.accept(s.get()); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1352 |
return true; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1353 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1354 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1355 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1356 |
public Spliterator<T> trySplit() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1357 |
if (estimate == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1358 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1359 |
return new InfiniteSupplyingSpliterator.OfRef<>(estimate >>>= 1, s); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1360 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1361 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1362 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1363 |
static final class OfInt extends InfiniteSupplyingSpliterator<Integer> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1364 |
implements Spliterator.OfInt { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1365 |
final IntSupplier s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1366 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1367 |
OfInt(long size, IntSupplier s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1368 |
super(size); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1369 |
this.s = s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1370 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1371 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1372 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1373 |
public boolean tryAdvance(IntConsumer action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1374 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1375 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1376 |
action.accept(s.getAsInt()); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1377 |
return true; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1378 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1379 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1380 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1381 |
public Spliterator.OfInt trySplit() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1382 |
if (estimate == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1383 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1384 |
return new InfiniteSupplyingSpliterator.OfInt(estimate = estimate >>> 1, s); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1385 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1386 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1387 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1388 |
static final class OfLong extends InfiniteSupplyingSpliterator<Long> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1389 |
implements Spliterator.OfLong { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1390 |
final LongSupplier s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1391 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1392 |
OfLong(long size, LongSupplier s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1393 |
super(size); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1394 |
this.s = s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1395 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1396 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1397 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1398 |
public boolean tryAdvance(LongConsumer action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1399 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1400 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1401 |
action.accept(s.getAsLong()); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1402 |
return true; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1403 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1404 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1405 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1406 |
public Spliterator.OfLong trySplit() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1407 |
if (estimate == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1408 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1409 |
return new InfiniteSupplyingSpliterator.OfLong(estimate = estimate >>> 1, s); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1410 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1411 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1412 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1413 |
static final class OfDouble extends InfiniteSupplyingSpliterator<Double> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1414 |
implements Spliterator.OfDouble { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1415 |
final DoubleSupplier s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1416 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1417 |
OfDouble(long size, DoubleSupplier s) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1418 |
super(size); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1419 |
this.s = s; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1420 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1421 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1422 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1423 |
public boolean tryAdvance(DoubleConsumer action) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1424 |
Objects.requireNonNull(action); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18572
diff
changeset
|
1425 |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1426 |
action.accept(s.getAsDouble()); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1427 |
return true; |
17182 | 1428 |
} |
1429 |
||
1430 |
@Override |
|
1431 |
public Spliterator.OfDouble trySplit() { |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1432 |
if (estimate == 0) |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1433 |
return null; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1434 |
return new InfiniteSupplyingSpliterator.OfDouble(estimate = estimate >>> 1, s); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1435 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1436 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1437 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1438 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1439 |
// @@@ Consolidate with Node.Builder |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1440 |
static abstract class ArrayBuffer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1441 |
int index; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1442 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1443 |
void reset() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1444 |
index = 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1445 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1446 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1447 |
static final class OfRef<T> extends ArrayBuffer implements Consumer<T> { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1448 |
final Object[] array; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1449 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1450 |
OfRef(int size) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1451 |
this.array = new Object[size]; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1452 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1453 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1454 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1455 |
public void accept(T t) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1456 |
array[index++] = t; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1457 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1458 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1459 |
public void forEach(Consumer<? super T> action, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1460 |
for (int i = 0; i < fence; i++) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1461 |
@SuppressWarnings("unchecked") |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1462 |
T t = (T) array[i]; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1463 |
action.accept(t); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1464 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1465 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1466 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1467 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1468 |
static abstract class OfPrimitive<T_CONS> extends ArrayBuffer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1469 |
int index; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1470 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1471 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1472 |
void reset() { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1473 |
index = 0; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1474 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1475 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1476 |
abstract void forEach(T_CONS action, long fence); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1477 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1478 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1479 |
static final class OfInt extends OfPrimitive<IntConsumer> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1480 |
implements IntConsumer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1481 |
final int[] array; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1482 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1483 |
OfInt(int size) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1484 |
this.array = new int[size]; |
17182 | 1485 |
} |
1486 |
||
1487 |
@Override |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1488 |
public void accept(int t) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1489 |
array[index++] = t; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1490 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1491 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1492 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1493 |
public void forEach(IntConsumer action, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1494 |
for (int i = 0; i < fence; i++) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1495 |
action.accept(array[i]); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1496 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1497 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1498 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1499 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1500 |
static final class OfLong extends OfPrimitive<LongConsumer> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1501 |
implements LongConsumer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1502 |
final long[] array; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1503 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1504 |
OfLong(int size) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1505 |
this.array = new long[size]; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1506 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1507 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1508 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1509 |
public void accept(long t) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1510 |
array[index++] = t; |
17182 | 1511 |
} |
1512 |
||
1513 |
@Override |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1514 |
public void forEach(LongConsumer action, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1515 |
for (int i = 0; i < fence; i++) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1516 |
action.accept(array[i]); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1517 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1518 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1519 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1520 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1521 |
static final class OfDouble extends OfPrimitive<DoubleConsumer> |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1522 |
implements DoubleConsumer { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1523 |
final double[] array; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1524 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1525 |
OfDouble(int size) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1526 |
this.array = new double[size]; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1527 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1528 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1529 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1530 |
public void accept(double t) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1531 |
array[index++] = t; |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1532 |
} |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1533 |
|
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1534 |
@Override |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1535 |
void forEach(DoubleConsumer action, long fence) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1536 |
for (int i = 0; i < fence; i++) { |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1537 |
action.accept(array[i]); |
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17182
diff
changeset
|
1538 |
} |
17182 | 1539 |
} |
1540 |
} |
|
1541 |
} |
|
19850 | 1542 |
} |
1543 |