author | psandoz |
Thu, 16 Jan 2014 18:20:31 +0100 | |
changeset 22289 | bb9c71b84919 |
parent 21339 | 20e8b81964d5 |
child 22297 | 1c62c67d9dd2 |
permissions | -rw-r--r-- |
17182 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.util.stream; |
|
26 |
||
27 |
import java.util.ArrayList; |
|
28 |
import java.util.Arrays; |
|
29 |
import java.util.Iterator; |
|
30 |
import java.util.List; |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
31 |
import java.util.Objects; |
17182 | 32 |
import java.util.PrimitiveIterator; |
33 |
import java.util.Spliterator; |
|
34 |
import java.util.Spliterators; |
|
35 |
import java.util.function.Consumer; |
|
36 |
import java.util.function.DoubleConsumer; |
|
37 |
import java.util.function.IntConsumer; |
|
38 |
import java.util.function.IntFunction; |
|
39 |
import java.util.function.LongConsumer; |
|
40 |
||
41 |
/** |
|
42 |
* An ordered collection of elements. Elements can be added, but not removed. |
|
43 |
* Goes through a building phase, during which elements can be added, and a |
|
44 |
* traversal phase, during which elements can be traversed in order but no |
|
45 |
* further modifications are possible. |
|
46 |
* |
|
47 |
* <p> One or more arrays are used to store elements. The use of a multiple |
|
48 |
* arrays has better performance characteristics than a single array used by |
|
49 |
* {@link ArrayList}, as when the capacity of the list needs to be increased |
|
50 |
* no copying of elements is required. This is usually beneficial in the case |
|
51 |
* where the results will be traversed a small number of times. |
|
52 |
* |
|
53 |
* @param <E> the type of elements in this list |
|
54 |
* @since 1.8 |
|
55 |
*/ |
|
56 |
class SpinedBuffer<E> |
|
57 |
extends AbstractSpinedBuffer |
|
58 |
implements Consumer<E>, Iterable<E> { |
|
59 |
||
60 |
/* |
|
61 |
* We optimistically hope that all the data will fit into the first chunk, |
|
62 |
* so we try to avoid inflating the spine[] and priorElementCount[] arrays |
|
63 |
* prematurely. So methods must be prepared to deal with these arrays being |
|
64 |
* null. If spine is non-null, then spineIndex points to the current chunk |
|
65 |
* within the spine, otherwise it is zero. The spine and priorElementCount |
|
66 |
* arrays are always the same size, and for any i <= spineIndex, |
|
67 |
* priorElementCount[i] is the sum of the sizes of all the prior chunks. |
|
68 |
* |
|
69 |
* The curChunk pointer is always valid. The elementIndex is the index of |
|
70 |
* the next element to be written in curChunk; this may be past the end of |
|
71 |
* curChunk so we have to check before writing. When we inflate the spine |
|
72 |
* array, curChunk becomes the first element in it. When we clear the |
|
73 |
* buffer, we discard all chunks except the first one, which we clear, |
|
74 |
* restoring it to the initial single-chunk state. |
|
75 |
*/ |
|
76 |
||
77 |
/** |
|
78 |
* Chunk that we're currently writing into; may or may not be aliased with |
|
79 |
* the first element of the spine. |
|
80 |
*/ |
|
81 |
protected E[] curChunk; |
|
82 |
||
83 |
/** |
|
84 |
* All chunks, or null if there is only one chunk. |
|
85 |
*/ |
|
86 |
protected E[][] spine; |
|
87 |
||
88 |
/** |
|
89 |
* Constructs an empty list with the specified initial capacity. |
|
90 |
* |
|
91 |
* @param initialCapacity the initial capacity of the list |
|
92 |
* @throws IllegalArgumentException if the specified initial capacity |
|
93 |
* is negative |
|
94 |
*/ |
|
95 |
SpinedBuffer(int initialCapacity) { |
|
96 |
super(initialCapacity); |
|
97 |
curChunk = (E[]) new Object[1 << initialChunkPower]; |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Constructs an empty list with an initial capacity of sixteen. |
|
102 |
*/ |
|
103 |
SpinedBuffer() { |
|
104 |
super(); |
|
105 |
curChunk = (E[]) new Object[1 << initialChunkPower]; |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Returns the current capacity of the buffer |
|
110 |
*/ |
|
111 |
protected long capacity() { |
|
112 |
return (spineIndex == 0) |
|
113 |
? curChunk.length |
|
114 |
: priorElementCount[spineIndex] + spine[spineIndex].length; |
|
115 |
} |
|
116 |
||
117 |
private void inflateSpine() { |
|
118 |
if (spine == null) { |
|
119 |
spine = (E[][]) new Object[MIN_SPINE_SIZE][]; |
|
120 |
priorElementCount = new long[MIN_SPINE_SIZE]; |
|
121 |
spine[0] = curChunk; |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Ensure that the buffer has at least capacity to hold the target size |
|
127 |
*/ |
|
128 |
protected final void ensureCapacity(long targetSize) { |
|
129 |
long capacity = capacity(); |
|
130 |
if (targetSize > capacity) { |
|
131 |
inflateSpine(); |
|
132 |
for (int i=spineIndex+1; targetSize > capacity; i++) { |
|
133 |
if (i >= spine.length) { |
|
134 |
int newSpineSize = spine.length * 2; |
|
135 |
spine = Arrays.copyOf(spine, newSpineSize); |
|
136 |
priorElementCount = Arrays.copyOf(priorElementCount, newSpineSize); |
|
137 |
} |
|
138 |
int nextChunkSize = chunkSize(i); |
|
139 |
spine[i] = (E[]) new Object[nextChunkSize]; |
|
140 |
priorElementCount[i] = priorElementCount[i-1] + spine[i-1].length; |
|
141 |
capacity += nextChunkSize; |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Force the buffer to increase its capacity. |
|
148 |
*/ |
|
149 |
protected void increaseCapacity() { |
|
150 |
ensureCapacity(capacity() + 1); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* Retrieve the element at the specified index. |
|
155 |
*/ |
|
156 |
public E get(long index) { |
|
157 |
// @@@ can further optimize by caching last seen spineIndex, |
|
158 |
// which is going to be right most of the time |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
159 |
|
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
160 |
// Casts to int are safe since the spine array index is the index minus |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
161 |
// the prior element count from the current spine |
17182 | 162 |
if (spineIndex == 0) { |
163 |
if (index < elementIndex) |
|
164 |
return curChunk[((int) index)]; |
|
165 |
else |
|
166 |
throw new IndexOutOfBoundsException(Long.toString(index)); |
|
167 |
} |
|
168 |
||
169 |
if (index >= count()) |
|
170 |
throw new IndexOutOfBoundsException(Long.toString(index)); |
|
171 |
||
172 |
for (int j=0; j <= spineIndex; j++) |
|
173 |
if (index < priorElementCount[j] + spine[j].length) |
|
174 |
return spine[j][((int) (index - priorElementCount[j]))]; |
|
175 |
||
176 |
throw new IndexOutOfBoundsException(Long.toString(index)); |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* Copy the elements, starting at the specified offset, into the specified |
|
181 |
* array. |
|
182 |
*/ |
|
183 |
public void copyInto(E[] array, int offset) { |
|
184 |
long finalOffset = offset + count(); |
|
185 |
if (finalOffset > array.length || finalOffset < offset) { |
|
186 |
throw new IndexOutOfBoundsException("does not fit"); |
|
187 |
} |
|
188 |
||
189 |
if (spineIndex == 0) |
|
190 |
System.arraycopy(curChunk, 0, array, offset, elementIndex); |
|
191 |
else { |
|
192 |
// full chunks |
|
193 |
for (int i=0; i < spineIndex; i++) { |
|
194 |
System.arraycopy(spine[i], 0, array, offset, spine[i].length); |
|
195 |
offset += spine[i].length; |
|
196 |
} |
|
197 |
if (elementIndex > 0) |
|
198 |
System.arraycopy(curChunk, 0, array, offset, elementIndex); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Create a new array using the specified array factory, and copy the |
|
204 |
* elements into it. |
|
205 |
*/ |
|
206 |
public E[] asArray(IntFunction<E[]> arrayFactory) { |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
207 |
long size = count(); |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
208 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
209 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
210 |
E[] result = arrayFactory.apply((int) size); |
17182 | 211 |
copyInto(result, 0); |
212 |
return result; |
|
213 |
} |
|
214 |
||
215 |
@Override |
|
216 |
public void clear() { |
|
217 |
if (spine != null) { |
|
218 |
curChunk = spine[0]; |
|
219 |
for (int i=0; i<curChunk.length; i++) |
|
220 |
curChunk[i] = null; |
|
221 |
spine = null; |
|
222 |
priorElementCount = null; |
|
223 |
} |
|
224 |
else { |
|
225 |
for (int i=0; i<elementIndex; i++) |
|
226 |
curChunk[i] = null; |
|
227 |
} |
|
228 |
elementIndex = 0; |
|
229 |
spineIndex = 0; |
|
230 |
} |
|
231 |
||
232 |
@Override |
|
233 |
public Iterator<E> iterator() { |
|
18155
889970e5b728
8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents:
17182
diff
changeset
|
234 |
return Spliterators.iterator(spliterator()); |
17182 | 235 |
} |
236 |
||
237 |
@Override |
|
238 |
public void forEach(Consumer<? super E> consumer) { |
|
239 |
// completed chunks, if any |
|
240 |
for (int j = 0; j < spineIndex; j++) |
|
241 |
for (E t : spine[j]) |
|
242 |
consumer.accept(t); |
|
243 |
||
244 |
// current chunk |
|
245 |
for (int i=0; i<elementIndex; i++) |
|
246 |
consumer.accept(curChunk[i]); |
|
247 |
} |
|
248 |
||
249 |
@Override |
|
250 |
public void accept(E e) { |
|
251 |
if (elementIndex == curChunk.length) { |
|
252 |
inflateSpine(); |
|
253 |
if (spineIndex+1 >= spine.length || spine[spineIndex+1] == null) |
|
254 |
increaseCapacity(); |
|
255 |
elementIndex = 0; |
|
256 |
++spineIndex; |
|
257 |
curChunk = spine[spineIndex]; |
|
258 |
} |
|
259 |
curChunk[elementIndex++] = e; |
|
260 |
} |
|
261 |
||
262 |
@Override |
|
263 |
public String toString() { |
|
264 |
List<E> list = new ArrayList<>(); |
|
265 |
forEach(list::add); |
|
266 |
return "SpinedBuffer:" + list.toString(); |
|
267 |
} |
|
268 |
||
269 |
private static final int SPLITERATOR_CHARACTERISTICS |
|
270 |
= Spliterator.SIZED | Spliterator.ORDERED | Spliterator.SUBSIZED; |
|
271 |
||
272 |
/** |
|
273 |
* Return a {@link Spliterator} describing the contents of the buffer. |
|
274 |
*/ |
|
275 |
public Spliterator<E> spliterator() { |
|
18171 | 276 |
class Splitr implements Spliterator<E> { |
17182 | 277 |
// The current spine index |
278 |
int splSpineIndex; |
|
279 |
||
18171 | 280 |
// Last spine index |
281 |
final int lastSpineIndex; |
|
282 |
||
17182 | 283 |
// The current element index into the current spine |
284 |
int splElementIndex; |
|
285 |
||
18171 | 286 |
// Last spine's last element index + 1 |
287 |
final int lastSpineElementFence; |
|
288 |
||
289 |
// When splSpineIndex >= lastSpineIndex and |
|
290 |
// splElementIndex >= lastSpineElementFence then |
|
17182 | 291 |
// this spliterator is fully traversed |
292 |
// tryAdvance can set splSpineIndex > spineIndex if the last spine is full |
|
293 |
||
294 |
// The current spine array |
|
18171 | 295 |
E[] splChunk; |
296 |
||
297 |
Splitr(int firstSpineIndex, int lastSpineIndex, |
|
298 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
299 |
this.splSpineIndex = firstSpineIndex; |
|
300 |
this.lastSpineIndex = lastSpineIndex; |
|
301 |
this.splElementIndex = firstSpineElementIndex; |
|
302 |
this.lastSpineElementFence = lastSpineElementFence; |
|
303 |
assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0; |
|
304 |
splChunk = (spine == null) ? curChunk : spine[firstSpineIndex]; |
|
305 |
} |
|
17182 | 306 |
|
307 |
@Override |
|
308 |
public long estimateSize() { |
|
18171 | 309 |
return (splSpineIndex == lastSpineIndex) |
310 |
? (long) lastSpineElementFence - splElementIndex |
|
311 |
: // # of elements prior to end - |
|
312 |
priorElementCount[lastSpineIndex] + lastSpineElementFence - |
|
313 |
// # of elements prior to current |
|
314 |
priorElementCount[splSpineIndex] - splElementIndex; |
|
17182 | 315 |
} |
316 |
||
317 |
@Override |
|
318 |
public int characteristics() { |
|
319 |
return SPLITERATOR_CHARACTERISTICS; |
|
320 |
} |
|
321 |
||
322 |
@Override |
|
323 |
public boolean tryAdvance(Consumer<? super E> consumer) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
324 |
Objects.requireNonNull(consumer); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
325 |
|
18171 | 326 |
if (splSpineIndex < lastSpineIndex |
327 |
|| (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { |
|
17182 | 328 |
consumer.accept(splChunk[splElementIndex++]); |
329 |
||
330 |
if (splElementIndex == splChunk.length) { |
|
331 |
splElementIndex = 0; |
|
332 |
++splSpineIndex; |
|
18171 | 333 |
if (spine != null && splSpineIndex <= lastSpineIndex) |
17182 | 334 |
splChunk = spine[splSpineIndex]; |
335 |
} |
|
336 |
return true; |
|
337 |
} |
|
338 |
return false; |
|
339 |
} |
|
340 |
||
341 |
@Override |
|
342 |
public void forEachRemaining(Consumer<? super E> consumer) { |
|
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
343 |
Objects.requireNonNull(consumer); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
344 |
|
18171 | 345 |
if (splSpineIndex < lastSpineIndex |
346 |
|| (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { |
|
17182 | 347 |
int i = splElementIndex; |
348 |
// completed chunks, if any |
|
18171 | 349 |
for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) { |
17182 | 350 |
E[] chunk = spine[sp]; |
351 |
for (; i < chunk.length; i++) { |
|
352 |
consumer.accept(chunk[i]); |
|
353 |
} |
|
354 |
i = 0; |
|
355 |
} |
|
18171 | 356 |
// last (or current uncompleted) chunk |
357 |
E[] chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex]; |
|
358 |
int hElementIndex = lastSpineElementFence; |
|
17182 | 359 |
for (; i < hElementIndex; i++) { |
360 |
consumer.accept(chunk[i]); |
|
361 |
} |
|
18171 | 362 |
// mark consumed |
363 |
splSpineIndex = lastSpineIndex; |
|
364 |
splElementIndex = lastSpineElementFence; |
|
17182 | 365 |
} |
366 |
} |
|
367 |
||
368 |
@Override |
|
369 |
public Spliterator<E> trySplit() { |
|
18171 | 370 |
if (splSpineIndex < lastSpineIndex) { |
371 |
// split just before last chunk (if it is full this means 50:50 split) |
|
372 |
Spliterator<E> ret = new Splitr(splSpineIndex, lastSpineIndex - 1, |
|
373 |
splElementIndex, spine[lastSpineIndex-1].length); |
|
374 |
// position to start of last chunk |
|
375 |
splSpineIndex = lastSpineIndex; |
|
17182 | 376 |
splElementIndex = 0; |
18171 | 377 |
splChunk = spine[splSpineIndex]; |
17182 | 378 |
return ret; |
379 |
} |
|
18171 | 380 |
else if (splSpineIndex == lastSpineIndex) { |
381 |
int t = (lastSpineElementFence - splElementIndex) / 2; |
|
17182 | 382 |
if (t == 0) |
383 |
return null; |
|
384 |
else { |
|
18171 | 385 |
Spliterator<E> ret = Arrays.spliterator(splChunk, splElementIndex, splElementIndex + t); |
17182 | 386 |
splElementIndex += t; |
387 |
return ret; |
|
388 |
} |
|
389 |
} |
|
390 |
else { |
|
391 |
return null; |
|
392 |
} |
|
393 |
} |
|
18171 | 394 |
} |
395 |
return new Splitr(0, spineIndex, 0, elementIndex); |
|
17182 | 396 |
} |
397 |
||
398 |
/** |
|
399 |
* An ordered collection of primitive values. Elements can be added, but |
|
400 |
* not removed. Goes through a building phase, during which elements can be |
|
401 |
* added, and a traversal phase, during which elements can be traversed in |
|
402 |
* order but no further modifications are possible. |
|
403 |
* |
|
404 |
* <p> One or more arrays are used to store elements. The use of a multiple |
|
405 |
* arrays has better performance characteristics than a single array used by |
|
406 |
* {@link ArrayList}, as when the capacity of the list needs to be increased |
|
407 |
* no copying of elements is required. This is usually beneficial in the case |
|
408 |
* where the results will be traversed a small number of times. |
|
409 |
* |
|
410 |
* @param <E> the wrapper type for this primitive type |
|
411 |
* @param <T_ARR> the array type for this primitive type |
|
412 |
* @param <T_CONS> the Consumer type for this primitive type |
|
413 |
*/ |
|
414 |
abstract static class OfPrimitive<E, T_ARR, T_CONS> |
|
415 |
extends AbstractSpinedBuffer implements Iterable<E> { |
|
416 |
||
417 |
/* |
|
418 |
* We optimistically hope that all the data will fit into the first chunk, |
|
419 |
* so we try to avoid inflating the spine[] and priorElementCount[] arrays |
|
420 |
* prematurely. So methods must be prepared to deal with these arrays being |
|
421 |
* null. If spine is non-null, then spineIndex points to the current chunk |
|
422 |
* within the spine, otherwise it is zero. The spine and priorElementCount |
|
423 |
* arrays are always the same size, and for any i <= spineIndex, |
|
424 |
* priorElementCount[i] is the sum of the sizes of all the prior chunks. |
|
425 |
* |
|
426 |
* The curChunk pointer is always valid. The elementIndex is the index of |
|
427 |
* the next element to be written in curChunk; this may be past the end of |
|
428 |
* curChunk so we have to check before writing. When we inflate the spine |
|
429 |
* array, curChunk becomes the first element in it. When we clear the |
|
430 |
* buffer, we discard all chunks except the first one, which we clear, |
|
431 |
* restoring it to the initial single-chunk state. |
|
432 |
*/ |
|
433 |
||
434 |
// The chunk we're currently writing into |
|
435 |
T_ARR curChunk; |
|
436 |
||
437 |
// All chunks, or null if there is only one chunk |
|
438 |
T_ARR[] spine; |
|
439 |
||
440 |
/** |
|
441 |
* Constructs an empty list with the specified initial capacity. |
|
442 |
* |
|
443 |
* @param initialCapacity the initial capacity of the list |
|
444 |
* @throws IllegalArgumentException if the specified initial capacity |
|
445 |
* is negative |
|
446 |
*/ |
|
447 |
OfPrimitive(int initialCapacity) { |
|
448 |
super(initialCapacity); |
|
449 |
curChunk = newArray(1 << initialChunkPower); |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* Constructs an empty list with an initial capacity of sixteen. |
|
454 |
*/ |
|
455 |
OfPrimitive() { |
|
456 |
super(); |
|
457 |
curChunk = newArray(1 << initialChunkPower); |
|
458 |
} |
|
459 |
||
460 |
@Override |
|
461 |
public abstract Iterator<E> iterator(); |
|
462 |
||
463 |
@Override |
|
464 |
public abstract void forEach(Consumer<? super E> consumer); |
|
465 |
||
466 |
/** Create a new array-of-array of the proper type and size */ |
|
467 |
protected abstract T_ARR[] newArrayArray(int size); |
|
468 |
||
469 |
/** Create a new array of the proper type and size */ |
|
18171 | 470 |
public abstract T_ARR newArray(int size); |
17182 | 471 |
|
472 |
/** Get the length of an array */ |
|
473 |
protected abstract int arrayLength(T_ARR array); |
|
474 |
||
475 |
/** Iterate an array with the provided consumer */ |
|
476 |
protected abstract void arrayForEach(T_ARR array, int from, int to, |
|
477 |
T_CONS consumer); |
|
478 |
||
479 |
protected long capacity() { |
|
480 |
return (spineIndex == 0) |
|
481 |
? arrayLength(curChunk) |
|
482 |
: priorElementCount[spineIndex] + arrayLength(spine[spineIndex]); |
|
483 |
} |
|
484 |
||
485 |
private void inflateSpine() { |
|
486 |
if (spine == null) { |
|
487 |
spine = newArrayArray(MIN_SPINE_SIZE); |
|
488 |
priorElementCount = new long[MIN_SPINE_SIZE]; |
|
489 |
spine[0] = curChunk; |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
protected final void ensureCapacity(long targetSize) { |
|
494 |
long capacity = capacity(); |
|
495 |
if (targetSize > capacity) { |
|
496 |
inflateSpine(); |
|
497 |
for (int i=spineIndex+1; targetSize > capacity; i++) { |
|
498 |
if (i >= spine.length) { |
|
499 |
int newSpineSize = spine.length * 2; |
|
500 |
spine = Arrays.copyOf(spine, newSpineSize); |
|
501 |
priorElementCount = Arrays.copyOf(priorElementCount, newSpineSize); |
|
502 |
} |
|
503 |
int nextChunkSize = chunkSize(i); |
|
504 |
spine[i] = newArray(nextChunkSize); |
|
505 |
priorElementCount[i] = priorElementCount[i-1] + arrayLength(spine[i - 1]); |
|
506 |
capacity += nextChunkSize; |
|
507 |
} |
|
508 |
} |
|
509 |
} |
|
510 |
||
511 |
protected void increaseCapacity() { |
|
512 |
ensureCapacity(capacity() + 1); |
|
513 |
} |
|
514 |
||
515 |
protected int chunkFor(long index) { |
|
516 |
if (spineIndex == 0) { |
|
517 |
if (index < elementIndex) |
|
518 |
return 0; |
|
519 |
else |
|
520 |
throw new IndexOutOfBoundsException(Long.toString(index)); |
|
521 |
} |
|
522 |
||
523 |
if (index >= count()) |
|
524 |
throw new IndexOutOfBoundsException(Long.toString(index)); |
|
525 |
||
526 |
for (int j=0; j <= spineIndex; j++) |
|
527 |
if (index < priorElementCount[j] + arrayLength(spine[j])) |
|
528 |
return j; |
|
529 |
||
530 |
throw new IndexOutOfBoundsException(Long.toString(index)); |
|
531 |
} |
|
532 |
||
533 |
public void copyInto(T_ARR array, int offset) { |
|
534 |
long finalOffset = offset + count(); |
|
535 |
if (finalOffset > arrayLength(array) || finalOffset < offset) { |
|
536 |
throw new IndexOutOfBoundsException("does not fit"); |
|
537 |
} |
|
538 |
||
539 |
if (spineIndex == 0) |
|
540 |
System.arraycopy(curChunk, 0, array, offset, elementIndex); |
|
541 |
else { |
|
542 |
// full chunks |
|
543 |
for (int i=0; i < spineIndex; i++) { |
|
544 |
System.arraycopy(spine[i], 0, array, offset, arrayLength(spine[i])); |
|
545 |
offset += arrayLength(spine[i]); |
|
546 |
} |
|
547 |
if (elementIndex > 0) |
|
548 |
System.arraycopy(curChunk, 0, array, offset, elementIndex); |
|
549 |
} |
|
550 |
} |
|
551 |
||
552 |
public T_ARR asPrimitiveArray() { |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
553 |
long size = count(); |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
554 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
555 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
556 |
T_ARR result = newArray((int) size); |
17182 | 557 |
copyInto(result, 0); |
558 |
return result; |
|
559 |
} |
|
560 |
||
561 |
protected void preAccept() { |
|
562 |
if (elementIndex == arrayLength(curChunk)) { |
|
563 |
inflateSpine(); |
|
564 |
if (spineIndex+1 >= spine.length || spine[spineIndex+1] == null) |
|
565 |
increaseCapacity(); |
|
566 |
elementIndex = 0; |
|
567 |
++spineIndex; |
|
568 |
curChunk = spine[spineIndex]; |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
public void clear() { |
|
573 |
if (spine != null) { |
|
574 |
curChunk = spine[0]; |
|
575 |
spine = null; |
|
576 |
priorElementCount = null; |
|
577 |
} |
|
578 |
elementIndex = 0; |
|
579 |
spineIndex = 0; |
|
580 |
} |
|
581 |
||
21339 | 582 |
@SuppressWarnings("overloads") |
17182 | 583 |
public void forEach(T_CONS consumer) { |
584 |
// completed chunks, if any |
|
585 |
for (int j = 0; j < spineIndex; j++) |
|
586 |
arrayForEach(spine[j], 0, arrayLength(spine[j]), consumer); |
|
587 |
||
588 |
// current chunk |
|
589 |
arrayForEach(curChunk, 0, elementIndex, consumer); |
|
590 |
} |
|
591 |
||
18171 | 592 |
abstract class BaseSpliterator<T_SPLITR extends Spliterator.OfPrimitive<E, T_CONS, T_SPLITR>> |
593 |
implements Spliterator.OfPrimitive<E, T_CONS, T_SPLITR> { |
|
17182 | 594 |
// The current spine index |
595 |
int splSpineIndex; |
|
596 |
||
18171 | 597 |
// Last spine index |
598 |
final int lastSpineIndex; |
|
599 |
||
17182 | 600 |
// The current element index into the current spine |
601 |
int splElementIndex; |
|
602 |
||
18171 | 603 |
// Last spine's last element index + 1 |
604 |
final int lastSpineElementFence; |
|
605 |
||
606 |
// When splSpineIndex >= lastSpineIndex and |
|
607 |
// splElementIndex >= lastSpineElementFence then |
|
17182 | 608 |
// this spliterator is fully traversed |
609 |
// tryAdvance can set splSpineIndex > spineIndex if the last spine is full |
|
610 |
||
611 |
// The current spine array |
|
18171 | 612 |
T_ARR splChunk; |
613 |
||
614 |
BaseSpliterator(int firstSpineIndex, int lastSpineIndex, |
|
615 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
616 |
this.splSpineIndex = firstSpineIndex; |
|
617 |
this.lastSpineIndex = lastSpineIndex; |
|
618 |
this.splElementIndex = firstSpineElementIndex; |
|
619 |
this.lastSpineElementFence = lastSpineElementFence; |
|
620 |
assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0; |
|
621 |
splChunk = (spine == null) ? curChunk : spine[firstSpineIndex]; |
|
622 |
} |
|
623 |
||
624 |
abstract T_SPLITR newSpliterator(int firstSpineIndex, int lastSpineIndex, |
|
625 |
int firstSpineElementIndex, int lastSpineElementFence); |
|
17182 | 626 |
|
627 |
abstract void arrayForOne(T_ARR array, int index, T_CONS consumer); |
|
628 |
||
18171 | 629 |
abstract T_SPLITR arraySpliterator(T_ARR array, int offset, int len); |
17182 | 630 |
|
631 |
@Override |
|
632 |
public long estimateSize() { |
|
18171 | 633 |
return (splSpineIndex == lastSpineIndex) |
634 |
? (long) lastSpineElementFence - splElementIndex |
|
635 |
: // # of elements prior to end - |
|
636 |
priorElementCount[lastSpineIndex] + lastSpineElementFence - |
|
637 |
// # of elements prior to current |
|
638 |
priorElementCount[splSpineIndex] - splElementIndex; |
|
17182 | 639 |
} |
640 |
||
641 |
@Override |
|
642 |
public int characteristics() { |
|
643 |
return SPLITERATOR_CHARACTERISTICS; |
|
644 |
} |
|
645 |
||
18171 | 646 |
@Override |
17182 | 647 |
public boolean tryAdvance(T_CONS consumer) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
648 |
Objects.requireNonNull(consumer); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
649 |
|
18171 | 650 |
if (splSpineIndex < lastSpineIndex |
651 |
|| (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { |
|
17182 | 652 |
arrayForOne(splChunk, splElementIndex++, consumer); |
653 |
||
654 |
if (splElementIndex == arrayLength(splChunk)) { |
|
655 |
splElementIndex = 0; |
|
656 |
++splSpineIndex; |
|
18171 | 657 |
if (spine != null && splSpineIndex <= lastSpineIndex) |
17182 | 658 |
splChunk = spine[splSpineIndex]; |
659 |
} |
|
660 |
return true; |
|
661 |
} |
|
662 |
return false; |
|
663 |
} |
|
664 |
||
18171 | 665 |
@Override |
17182 | 666 |
public void forEachRemaining(T_CONS consumer) { |
19188
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
667 |
Objects.requireNonNull(consumer); |
bbf287c5cd92
8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents:
18171
diff
changeset
|
668 |
|
18171 | 669 |
if (splSpineIndex < lastSpineIndex |
670 |
|| (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { |
|
17182 | 671 |
int i = splElementIndex; |
672 |
// completed chunks, if any |
|
18171 | 673 |
for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) { |
17182 | 674 |
T_ARR chunk = spine[sp]; |
675 |
arrayForEach(chunk, i, arrayLength(chunk), consumer); |
|
676 |
i = 0; |
|
677 |
} |
|
18171 | 678 |
// last (or current uncompleted) chunk |
679 |
T_ARR chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex]; |
|
680 |
arrayForEach(chunk, i, lastSpineElementFence, consumer); |
|
681 |
// mark consumed |
|
682 |
splSpineIndex = lastSpineIndex; |
|
683 |
splElementIndex = lastSpineElementFence; |
|
17182 | 684 |
} |
685 |
} |
|
686 |
||
687 |
@Override |
|
18171 | 688 |
public T_SPLITR trySplit() { |
689 |
if (splSpineIndex < lastSpineIndex) { |
|
690 |
// split just before last chunk (if it is full this means 50:50 split) |
|
691 |
T_SPLITR ret = newSpliterator(splSpineIndex, lastSpineIndex - 1, |
|
692 |
splElementIndex, arrayLength(spine[lastSpineIndex - 1])); |
|
693 |
// position us to start of last chunk |
|
694 |
splSpineIndex = lastSpineIndex; |
|
17182 | 695 |
splElementIndex = 0; |
18171 | 696 |
splChunk = spine[splSpineIndex]; |
17182 | 697 |
return ret; |
698 |
} |
|
18171 | 699 |
else if (splSpineIndex == lastSpineIndex) { |
700 |
int t = (lastSpineElementFence - splElementIndex) / 2; |
|
17182 | 701 |
if (t == 0) |
702 |
return null; |
|
703 |
else { |
|
18171 | 704 |
T_SPLITR ret = arraySpliterator(splChunk, splElementIndex, t); |
17182 | 705 |
splElementIndex += t; |
706 |
return ret; |
|
707 |
} |
|
708 |
} |
|
709 |
else { |
|
710 |
return null; |
|
711 |
} |
|
712 |
} |
|
713 |
} |
|
714 |
} |
|
715 |
||
716 |
/** |
|
717 |
* An ordered collection of {@code int} values. |
|
718 |
*/ |
|
719 |
static class OfInt extends SpinedBuffer.OfPrimitive<Integer, int[], IntConsumer> |
|
720 |
implements IntConsumer { |
|
721 |
OfInt() { } |
|
722 |
||
723 |
OfInt(int initialCapacity) { |
|
724 |
super(initialCapacity); |
|
725 |
} |
|
726 |
||
727 |
@Override |
|
728 |
public void forEach(Consumer<? super Integer> consumer) { |
|
729 |
if (consumer instanceof IntConsumer) { |
|
730 |
forEach((IntConsumer) consumer); |
|
731 |
} |
|
732 |
else { |
|
733 |
if (Tripwire.ENABLED) |
|
734 |
Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfInt.forEach(Consumer)"); |
|
735 |
spliterator().forEachRemaining(consumer); |
|
736 |
} |
|
737 |
} |
|
738 |
||
739 |
@Override |
|
740 |
protected int[][] newArrayArray(int size) { |
|
741 |
return new int[size][]; |
|
742 |
} |
|
743 |
||
744 |
@Override |
|
18171 | 745 |
public int[] newArray(int size) { |
17182 | 746 |
return new int[size]; |
747 |
} |
|
748 |
||
749 |
@Override |
|
750 |
protected int arrayLength(int[] array) { |
|
751 |
return array.length; |
|
752 |
} |
|
753 |
||
754 |
@Override |
|
755 |
protected void arrayForEach(int[] array, |
|
756 |
int from, int to, |
|
757 |
IntConsumer consumer) { |
|
758 |
for (int i = from; i < to; i++) |
|
759 |
consumer.accept(array[i]); |
|
760 |
} |
|
761 |
||
762 |
@Override |
|
763 |
public void accept(int i) { |
|
764 |
preAccept(); |
|
765 |
curChunk[elementIndex++] = i; |
|
766 |
} |
|
767 |
||
768 |
public int get(long index) { |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
769 |
// Casts to int are safe since the spine array index is the index minus |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
770 |
// the prior element count from the current spine |
17182 | 771 |
int ch = chunkFor(index); |
772 |
if (spineIndex == 0 && ch == 0) |
|
773 |
return curChunk[(int) index]; |
|
774 |
else |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
775 |
return spine[ch][(int) (index - priorElementCount[ch])]; |
17182 | 776 |
} |
777 |
||
778 |
@Override |
|
779 |
public PrimitiveIterator.OfInt iterator() { |
|
18155
889970e5b728
8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents:
17182
diff
changeset
|
780 |
return Spliterators.iterator(spliterator()); |
17182 | 781 |
} |
782 |
||
783 |
public Spliterator.OfInt spliterator() { |
|
784 |
class Splitr extends BaseSpliterator<Spliterator.OfInt> |
|
785 |
implements Spliterator.OfInt { |
|
18171 | 786 |
Splitr(int firstSpineIndex, int lastSpineIndex, |
787 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
788 |
super(firstSpineIndex, lastSpineIndex, |
|
789 |
firstSpineElementIndex, lastSpineElementFence); |
|
790 |
} |
|
791 |
||
792 |
@Override |
|
793 |
Splitr newSpliterator(int firstSpineIndex, int lastSpineIndex, |
|
794 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
795 |
return new Splitr(firstSpineIndex, lastSpineIndex, |
|
796 |
firstSpineElementIndex, lastSpineElementFence); |
|
797 |
} |
|
17182 | 798 |
|
799 |
@Override |
|
800 |
void arrayForOne(int[] array, int index, IntConsumer consumer) { |
|
801 |
consumer.accept(array[index]); |
|
802 |
} |
|
803 |
||
804 |
@Override |
|
805 |
Spliterator.OfInt arraySpliterator(int[] array, int offset, int len) { |
|
806 |
return Arrays.spliterator(array, offset, offset+len); |
|
807 |
} |
|
18171 | 808 |
} |
809 |
return new Splitr(0, spineIndex, 0, elementIndex); |
|
17182 | 810 |
} |
811 |
||
812 |
@Override |
|
813 |
public String toString() { |
|
18171 | 814 |
int[] array = asPrimitiveArray(); |
17182 | 815 |
if (array.length < 200) { |
816 |
return String.format("%s[length=%d, chunks=%d]%s", |
|
817 |
getClass().getSimpleName(), array.length, |
|
818 |
spineIndex, Arrays.toString(array)); |
|
819 |
} |
|
820 |
else { |
|
821 |
int[] array2 = Arrays.copyOf(array, 200); |
|
822 |
return String.format("%s[length=%d, chunks=%d]%s...", |
|
823 |
getClass().getSimpleName(), array.length, |
|
824 |
spineIndex, Arrays.toString(array2)); |
|
825 |
} |
|
826 |
} |
|
827 |
} |
|
828 |
||
829 |
/** |
|
830 |
* An ordered collection of {@code long} values. |
|
831 |
*/ |
|
832 |
static class OfLong extends SpinedBuffer.OfPrimitive<Long, long[], LongConsumer> |
|
833 |
implements LongConsumer { |
|
834 |
OfLong() { } |
|
835 |
||
836 |
OfLong(int initialCapacity) { |
|
837 |
super(initialCapacity); |
|
838 |
} |
|
839 |
||
840 |
@Override |
|
841 |
public void forEach(Consumer<? super Long> consumer) { |
|
842 |
if (consumer instanceof LongConsumer) { |
|
843 |
forEach((LongConsumer) consumer); |
|
844 |
} |
|
845 |
else { |
|
846 |
if (Tripwire.ENABLED) |
|
847 |
Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfLong.forEach(Consumer)"); |
|
848 |
spliterator().forEachRemaining(consumer); |
|
849 |
} |
|
850 |
} |
|
851 |
||
852 |
@Override |
|
853 |
protected long[][] newArrayArray(int size) { |
|
854 |
return new long[size][]; |
|
855 |
} |
|
856 |
||
857 |
@Override |
|
18171 | 858 |
public long[] newArray(int size) { |
17182 | 859 |
return new long[size]; |
860 |
} |
|
861 |
||
862 |
@Override |
|
863 |
protected int arrayLength(long[] array) { |
|
864 |
return array.length; |
|
865 |
} |
|
866 |
||
867 |
@Override |
|
868 |
protected void arrayForEach(long[] array, |
|
869 |
int from, int to, |
|
870 |
LongConsumer consumer) { |
|
871 |
for (int i = from; i < to; i++) |
|
872 |
consumer.accept(array[i]); |
|
873 |
} |
|
874 |
||
875 |
@Override |
|
876 |
public void accept(long i) { |
|
877 |
preAccept(); |
|
878 |
curChunk[elementIndex++] = i; |
|
879 |
} |
|
880 |
||
881 |
public long get(long index) { |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
882 |
// Casts to int are safe since the spine array index is the index minus |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
883 |
// the prior element count from the current spine |
17182 | 884 |
int ch = chunkFor(index); |
885 |
if (spineIndex == 0 && ch == 0) |
|
886 |
return curChunk[(int) index]; |
|
887 |
else |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
888 |
return spine[ch][(int) (index - priorElementCount[ch])]; |
17182 | 889 |
} |
890 |
||
891 |
@Override |
|
892 |
public PrimitiveIterator.OfLong iterator() { |
|
18155
889970e5b728
8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents:
17182
diff
changeset
|
893 |
return Spliterators.iterator(spliterator()); |
17182 | 894 |
} |
895 |
||
896 |
||
897 |
public Spliterator.OfLong spliterator() { |
|
898 |
class Splitr extends BaseSpliterator<Spliterator.OfLong> |
|
899 |
implements Spliterator.OfLong { |
|
18171 | 900 |
Splitr(int firstSpineIndex, int lastSpineIndex, |
901 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
902 |
super(firstSpineIndex, lastSpineIndex, |
|
903 |
firstSpineElementIndex, lastSpineElementFence); |
|
904 |
} |
|
905 |
||
906 |
@Override |
|
907 |
Splitr newSpliterator(int firstSpineIndex, int lastSpineIndex, |
|
908 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
909 |
return new Splitr(firstSpineIndex, lastSpineIndex, |
|
910 |
firstSpineElementIndex, lastSpineElementFence); |
|
911 |
} |
|
912 |
||
17182 | 913 |
@Override |
914 |
void arrayForOne(long[] array, int index, LongConsumer consumer) { |
|
915 |
consumer.accept(array[index]); |
|
916 |
} |
|
917 |
||
918 |
@Override |
|
919 |
Spliterator.OfLong arraySpliterator(long[] array, int offset, int len) { |
|
920 |
return Arrays.spliterator(array, offset, offset+len); |
|
921 |
} |
|
18171 | 922 |
} |
923 |
return new Splitr(0, spineIndex, 0, elementIndex); |
|
17182 | 924 |
} |
925 |
||
926 |
@Override |
|
927 |
public String toString() { |
|
18171 | 928 |
long[] array = asPrimitiveArray(); |
17182 | 929 |
if (array.length < 200) { |
930 |
return String.format("%s[length=%d, chunks=%d]%s", |
|
931 |
getClass().getSimpleName(), array.length, |
|
932 |
spineIndex, Arrays.toString(array)); |
|
933 |
} |
|
934 |
else { |
|
935 |
long[] array2 = Arrays.copyOf(array, 200); |
|
936 |
return String.format("%s[length=%d, chunks=%d]%s...", |
|
937 |
getClass().getSimpleName(), array.length, |
|
938 |
spineIndex, Arrays.toString(array2)); |
|
939 |
} |
|
940 |
} |
|
941 |
} |
|
942 |
||
943 |
/** |
|
944 |
* An ordered collection of {@code double} values. |
|
945 |
*/ |
|
946 |
static class OfDouble |
|
947 |
extends SpinedBuffer.OfPrimitive<Double, double[], DoubleConsumer> |
|
948 |
implements DoubleConsumer { |
|
949 |
OfDouble() { } |
|
950 |
||
951 |
OfDouble(int initialCapacity) { |
|
952 |
super(initialCapacity); |
|
953 |
} |
|
954 |
||
955 |
@Override |
|
956 |
public void forEach(Consumer<? super Double> consumer) { |
|
957 |
if (consumer instanceof DoubleConsumer) { |
|
958 |
forEach((DoubleConsumer) consumer); |
|
959 |
} |
|
960 |
else { |
|
961 |
if (Tripwire.ENABLED) |
|
962 |
Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfDouble.forEach(Consumer)"); |
|
963 |
spliterator().forEachRemaining(consumer); |
|
964 |
} |
|
965 |
} |
|
966 |
||
967 |
@Override |
|
968 |
protected double[][] newArrayArray(int size) { |
|
969 |
return new double[size][]; |
|
970 |
} |
|
971 |
||
972 |
@Override |
|
18171 | 973 |
public double[] newArray(int size) { |
17182 | 974 |
return new double[size]; |
975 |
} |
|
976 |
||
977 |
@Override |
|
978 |
protected int arrayLength(double[] array) { |
|
979 |
return array.length; |
|
980 |
} |
|
981 |
||
982 |
@Override |
|
983 |
protected void arrayForEach(double[] array, |
|
984 |
int from, int to, |
|
985 |
DoubleConsumer consumer) { |
|
986 |
for (int i = from; i < to; i++) |
|
987 |
consumer.accept(array[i]); |
|
988 |
} |
|
989 |
||
990 |
@Override |
|
991 |
public void accept(double i) { |
|
992 |
preAccept(); |
|
993 |
curChunk[elementIndex++] = i; |
|
994 |
} |
|
995 |
||
996 |
public double get(long index) { |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
997 |
// Casts to int are safe since the spine array index is the index minus |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
998 |
// the prior element count from the current spine |
17182 | 999 |
int ch = chunkFor(index); |
1000 |
if (spineIndex == 0 && ch == 0) |
|
1001 |
return curChunk[(int) index]; |
|
1002 |
else |
|
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
19188
diff
changeset
|
1003 |
return spine[ch][(int) (index - priorElementCount[ch])]; |
17182 | 1004 |
} |
1005 |
||
1006 |
@Override |
|
1007 |
public PrimitiveIterator.OfDouble iterator() { |
|
18155
889970e5b728
8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents:
17182
diff
changeset
|
1008 |
return Spliterators.iterator(spliterator()); |
17182 | 1009 |
} |
1010 |
||
1011 |
public Spliterator.OfDouble spliterator() { |
|
1012 |
class Splitr extends BaseSpliterator<Spliterator.OfDouble> |
|
1013 |
implements Spliterator.OfDouble { |
|
18171 | 1014 |
Splitr(int firstSpineIndex, int lastSpineIndex, |
1015 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
1016 |
super(firstSpineIndex, lastSpineIndex, |
|
1017 |
firstSpineElementIndex, lastSpineElementFence); |
|
1018 |
} |
|
1019 |
||
1020 |
@Override |
|
1021 |
Splitr newSpliterator(int firstSpineIndex, int lastSpineIndex, |
|
1022 |
int firstSpineElementIndex, int lastSpineElementFence) { |
|
1023 |
return new Splitr(firstSpineIndex, lastSpineIndex, |
|
1024 |
firstSpineElementIndex, lastSpineElementFence); |
|
1025 |
} |
|
1026 |
||
17182 | 1027 |
@Override |
1028 |
void arrayForOne(double[] array, int index, DoubleConsumer consumer) { |
|
1029 |
consumer.accept(array[index]); |
|
1030 |
} |
|
1031 |
||
1032 |
@Override |
|
1033 |
Spliterator.OfDouble arraySpliterator(double[] array, int offset, int len) { |
|
1034 |
return Arrays.spliterator(array, offset, offset+len); |
|
1035 |
} |
|
1036 |
} |
|
18171 | 1037 |
return new Splitr(0, spineIndex, 0, elementIndex); |
17182 | 1038 |
} |
1039 |
||
1040 |
@Override |
|
1041 |
public String toString() { |
|
18171 | 1042 |
double[] array = asPrimitiveArray(); |
17182 | 1043 |
if (array.length < 200) { |
1044 |
return String.format("%s[length=%d, chunks=%d]%s", |
|
1045 |
getClass().getSimpleName(), array.length, |
|
1046 |
spineIndex, Arrays.toString(array)); |
|
1047 |
} |
|
1048 |
else { |
|
1049 |
double[] array2 = Arrays.copyOf(array, 200); |
|
1050 |
return String.format("%s[length=%d, chunks=%d]%s...", |
|
1051 |
getClass().getSimpleName(), array.length, |
|
1052 |
spineIndex, Arrays.toString(array2)); |
|
1053 |
} |
|
1054 |
} |
|
1055 |
} |
|
1056 |
} |
|
1057 |