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