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() {
|
|
230 |
return Spliterators.iteratorFromSpliterator(spliterator());
|
|
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() {
|
|
272 |
return new Spliterator<E>() {
|
|
273 |
// The current spine index
|
|
274 |
int splSpineIndex;
|
|
275 |
|
|
276 |
// The current element index into the current spine
|
|
277 |
int splElementIndex;
|
|
278 |
|
|
279 |
// When splSpineIndex >= spineIndex and splElementIndex >= elementIndex then
|
|
280 |
// this spliterator is fully traversed
|
|
281 |
// tryAdvance can set splSpineIndex > spineIndex if the last spine is full
|
|
282 |
|
|
283 |
// The current spine array
|
|
284 |
E[] splChunk = (spine == null) ? curChunk : spine[0];
|
|
285 |
|
|
286 |
@Override
|
|
287 |
public long estimateSize() {
|
|
288 |
return (spine == null)
|
|
289 |
? (elementIndex - splElementIndex)
|
|
290 |
: count() - (priorElementCount[splSpineIndex] + splElementIndex);
|
|
291 |
}
|
|
292 |
|
|
293 |
@Override
|
|
294 |
public int characteristics() {
|
|
295 |
return SPLITERATOR_CHARACTERISTICS;
|
|
296 |
}
|
|
297 |
|
|
298 |
@Override
|
|
299 |
public boolean tryAdvance(Consumer<? super E> consumer) {
|
|
300 |
if (splSpineIndex < spineIndex
|
|
301 |
|| (splSpineIndex == spineIndex && splElementIndex < elementIndex)) {
|
|
302 |
consumer.accept(splChunk[splElementIndex++]);
|
|
303 |
|
|
304 |
if (splElementIndex == splChunk.length) {
|
|
305 |
splElementIndex = 0;
|
|
306 |
++splSpineIndex;
|
|
307 |
if (spine != null && splSpineIndex < spine.length)
|
|
308 |
splChunk = spine[splSpineIndex];
|
|
309 |
}
|
|
310 |
return true;
|
|
311 |
}
|
|
312 |
return false;
|
|
313 |
}
|
|
314 |
|
|
315 |
@Override
|
|
316 |
public void forEachRemaining(Consumer<? super E> consumer) {
|
|
317 |
if (splSpineIndex < spineIndex
|
|
318 |
|| (splSpineIndex == spineIndex && splElementIndex < elementIndex)) {
|
|
319 |
int i = splElementIndex;
|
|
320 |
// completed chunks, if any
|
|
321 |
for (int sp = splSpineIndex; sp < spineIndex; sp++) {
|
|
322 |
E[] chunk = spine[sp];
|
|
323 |
for (; i < chunk.length; i++) {
|
|
324 |
consumer.accept(chunk[i]);
|
|
325 |
}
|
|
326 |
i = 0;
|
|
327 |
}
|
|
328 |
|
|
329 |
// current chunk
|
|
330 |
E[] chunk = curChunk;
|
|
331 |
int hElementIndex = elementIndex;
|
|
332 |
for (; i < hElementIndex; i++) {
|
|
333 |
consumer.accept(chunk[i]);
|
|
334 |
}
|
|
335 |
|
|
336 |
splSpineIndex = spineIndex;
|
|
337 |
splElementIndex = elementIndex;
|
|
338 |
}
|
|
339 |
}
|
|
340 |
|
|
341 |
@Override
|
|
342 |
public Spliterator<E> trySplit() {
|
|
343 |
if (splSpineIndex < spineIndex) {
|
|
344 |
Spliterator<E> ret = Arrays.spliterator(spine[splSpineIndex],
|
|
345 |
splElementIndex, spine[splSpineIndex].length);
|
|
346 |
splChunk = spine[++splSpineIndex];
|
|
347 |
splElementIndex = 0;
|
|
348 |
return ret;
|
|
349 |
}
|
|
350 |
else if (splSpineIndex == spineIndex) {
|
|
351 |
int t = (elementIndex - splElementIndex) / 2;
|
|
352 |
if (t == 0)
|
|
353 |
return null;
|
|
354 |
else {
|
|
355 |
Spliterator<E> ret = Arrays.spliterator(curChunk, splElementIndex, splElementIndex + t);
|
|
356 |
splElementIndex += t;
|
|
357 |
return ret;
|
|
358 |
}
|
|
359 |
}
|
|
360 |
else {
|
|
361 |
return null;
|
|
362 |
}
|
|
363 |
}
|
|
364 |
};
|
|
365 |
}
|
|
366 |
|
|
367 |
/**
|
|
368 |
* An ordered collection of primitive values. Elements can be added, but
|
|
369 |
* not removed. Goes through a building phase, during which elements can be
|
|
370 |
* added, and a traversal phase, during which elements can be traversed in
|
|
371 |
* order but no further modifications are possible.
|
|
372 |
*
|
|
373 |
* <p> One or more arrays are used to store elements. The use of a multiple
|
|
374 |
* arrays has better performance characteristics than a single array used by
|
|
375 |
* {@link ArrayList}, as when the capacity of the list needs to be increased
|
|
376 |
* no copying of elements is required. This is usually beneficial in the case
|
|
377 |
* where the results will be traversed a small number of times.
|
|
378 |
*
|
|
379 |
* @param <E> the wrapper type for this primitive type
|
|
380 |
* @param <T_ARR> the array type for this primitive type
|
|
381 |
* @param <T_CONS> the Consumer type for this primitive type
|
|
382 |
*/
|
|
383 |
abstract static class OfPrimitive<E, T_ARR, T_CONS>
|
|
384 |
extends AbstractSpinedBuffer implements Iterable<E> {
|
|
385 |
|
|
386 |
/*
|
|
387 |
* We optimistically hope that all the data will fit into the first chunk,
|
|
388 |
* so we try to avoid inflating the spine[] and priorElementCount[] arrays
|
|
389 |
* prematurely. So methods must be prepared to deal with these arrays being
|
|
390 |
* null. If spine is non-null, then spineIndex points to the current chunk
|
|
391 |
* within the spine, otherwise it is zero. The spine and priorElementCount
|
|
392 |
* arrays are always the same size, and for any i <= spineIndex,
|
|
393 |
* priorElementCount[i] is the sum of the sizes of all the prior chunks.
|
|
394 |
*
|
|
395 |
* The curChunk pointer is always valid. The elementIndex is the index of
|
|
396 |
* the next element to be written in curChunk; this may be past the end of
|
|
397 |
* curChunk so we have to check before writing. When we inflate the spine
|
|
398 |
* array, curChunk becomes the first element in it. When we clear the
|
|
399 |
* buffer, we discard all chunks except the first one, which we clear,
|
|
400 |
* restoring it to the initial single-chunk state.
|
|
401 |
*/
|
|
402 |
|
|
403 |
// The chunk we're currently writing into
|
|
404 |
T_ARR curChunk;
|
|
405 |
|
|
406 |
// All chunks, or null if there is only one chunk
|
|
407 |
T_ARR[] spine;
|
|
408 |
|
|
409 |
/**
|
|
410 |
* Constructs an empty list with the specified initial capacity.
|
|
411 |
*
|
|
412 |
* @param initialCapacity the initial capacity of the list
|
|
413 |
* @throws IllegalArgumentException if the specified initial capacity
|
|
414 |
* is negative
|
|
415 |
*/
|
|
416 |
OfPrimitive(int initialCapacity) {
|
|
417 |
super(initialCapacity);
|
|
418 |
curChunk = newArray(1 << initialChunkPower);
|
|
419 |
}
|
|
420 |
|
|
421 |
/**
|
|
422 |
* Constructs an empty list with an initial capacity of sixteen.
|
|
423 |
*/
|
|
424 |
OfPrimitive() {
|
|
425 |
super();
|
|
426 |
curChunk = newArray(1 << initialChunkPower);
|
|
427 |
}
|
|
428 |
|
|
429 |
@Override
|
|
430 |
public abstract Iterator<E> iterator();
|
|
431 |
|
|
432 |
@Override
|
|
433 |
public abstract void forEach(Consumer<? super E> consumer);
|
|
434 |
|
|
435 |
/** Create a new array-of-array of the proper type and size */
|
|
436 |
protected abstract T_ARR[] newArrayArray(int size);
|
|
437 |
|
|
438 |
/** Create a new array of the proper type and size */
|
|
439 |
protected abstract T_ARR newArray(int size);
|
|
440 |
|
|
441 |
/** Get the length of an array */
|
|
442 |
protected abstract int arrayLength(T_ARR array);
|
|
443 |
|
|
444 |
/** Iterate an array with the provided consumer */
|
|
445 |
protected abstract void arrayForEach(T_ARR array, int from, int to,
|
|
446 |
T_CONS consumer);
|
|
447 |
|
|
448 |
protected long capacity() {
|
|
449 |
return (spineIndex == 0)
|
|
450 |
? arrayLength(curChunk)
|
|
451 |
: priorElementCount[spineIndex] + arrayLength(spine[spineIndex]);
|
|
452 |
}
|
|
453 |
|
|
454 |
private void inflateSpine() {
|
|
455 |
if (spine == null) {
|
|
456 |
spine = newArrayArray(MIN_SPINE_SIZE);
|
|
457 |
priorElementCount = new long[MIN_SPINE_SIZE];
|
|
458 |
spine[0] = curChunk;
|
|
459 |
}
|
|
460 |
}
|
|
461 |
|
|
462 |
protected final void ensureCapacity(long targetSize) {
|
|
463 |
long capacity = capacity();
|
|
464 |
if (targetSize > capacity) {
|
|
465 |
inflateSpine();
|
|
466 |
for (int i=spineIndex+1; targetSize > capacity; i++) {
|
|
467 |
if (i >= spine.length) {
|
|
468 |
int newSpineSize = spine.length * 2;
|
|
469 |
spine = Arrays.copyOf(spine, newSpineSize);
|
|
470 |
priorElementCount = Arrays.copyOf(priorElementCount, newSpineSize);
|
|
471 |
}
|
|
472 |
int nextChunkSize = chunkSize(i);
|
|
473 |
spine[i] = newArray(nextChunkSize);
|
|
474 |
priorElementCount[i] = priorElementCount[i-1] + arrayLength(spine[i - 1]);
|
|
475 |
capacity += nextChunkSize;
|
|
476 |
}
|
|
477 |
}
|
|
478 |
}
|
|
479 |
|
|
480 |
protected void increaseCapacity() {
|
|
481 |
ensureCapacity(capacity() + 1);
|
|
482 |
}
|
|
483 |
|
|
484 |
protected int chunkFor(long index) {
|
|
485 |
if (spineIndex == 0) {
|
|
486 |
if (index < elementIndex)
|
|
487 |
return 0;
|
|
488 |
else
|
|
489 |
throw new IndexOutOfBoundsException(Long.toString(index));
|
|
490 |
}
|
|
491 |
|
|
492 |
if (index >= count())
|
|
493 |
throw new IndexOutOfBoundsException(Long.toString(index));
|
|
494 |
|
|
495 |
for (int j=0; j <= spineIndex; j++)
|
|
496 |
if (index < priorElementCount[j] + arrayLength(spine[j]))
|
|
497 |
return j;
|
|
498 |
|
|
499 |
throw new IndexOutOfBoundsException(Long.toString(index));
|
|
500 |
}
|
|
501 |
|
|
502 |
public void copyInto(T_ARR array, int offset) {
|
|
503 |
long finalOffset = offset + count();
|
|
504 |
if (finalOffset > arrayLength(array) || finalOffset < offset) {
|
|
505 |
throw new IndexOutOfBoundsException("does not fit");
|
|
506 |
}
|
|
507 |
|
|
508 |
if (spineIndex == 0)
|
|
509 |
System.arraycopy(curChunk, 0, array, offset, elementIndex);
|
|
510 |
else {
|
|
511 |
// full chunks
|
|
512 |
for (int i=0; i < spineIndex; i++) {
|
|
513 |
System.arraycopy(spine[i], 0, array, offset, arrayLength(spine[i]));
|
|
514 |
offset += arrayLength(spine[i]);
|
|
515 |
}
|
|
516 |
if (elementIndex > 0)
|
|
517 |
System.arraycopy(curChunk, 0, array, offset, elementIndex);
|
|
518 |
}
|
|
519 |
}
|
|
520 |
|
|
521 |
public T_ARR asPrimitiveArray() {
|
|
522 |
// @@@ will fail for size == MAX_VALUE
|
|
523 |
T_ARR result = newArray((int) count());
|
|
524 |
copyInto(result, 0);
|
|
525 |
return result;
|
|
526 |
}
|
|
527 |
|
|
528 |
protected void preAccept() {
|
|
529 |
if (elementIndex == arrayLength(curChunk)) {
|
|
530 |
inflateSpine();
|
|
531 |
if (spineIndex+1 >= spine.length || spine[spineIndex+1] == null)
|
|
532 |
increaseCapacity();
|
|
533 |
elementIndex = 0;
|
|
534 |
++spineIndex;
|
|
535 |
curChunk = spine[spineIndex];
|
|
536 |
}
|
|
537 |
}
|
|
538 |
|
|
539 |
public void clear() {
|
|
540 |
if (spine != null) {
|
|
541 |
curChunk = spine[0];
|
|
542 |
spine = null;
|
|
543 |
priorElementCount = null;
|
|
544 |
}
|
|
545 |
elementIndex = 0;
|
|
546 |
spineIndex = 0;
|
|
547 |
}
|
|
548 |
|
|
549 |
public void forEach(T_CONS consumer) {
|
|
550 |
// completed chunks, if any
|
|
551 |
for (int j = 0; j < spineIndex; j++)
|
|
552 |
arrayForEach(spine[j], 0, arrayLength(spine[j]), consumer);
|
|
553 |
|
|
554 |
// current chunk
|
|
555 |
arrayForEach(curChunk, 0, elementIndex, consumer);
|
|
556 |
}
|
|
557 |
|
|
558 |
abstract class BaseSpliterator<T_SPLITER extends Spliterator<E>>
|
|
559 |
implements Spliterator<E> {
|
|
560 |
// The current spine index
|
|
561 |
int splSpineIndex;
|
|
562 |
|
|
563 |
// The current element index into the current spine
|
|
564 |
int splElementIndex;
|
|
565 |
|
|
566 |
// When splSpineIndex >= spineIndex and splElementIndex >= elementIndex then
|
|
567 |
// this spliterator is fully traversed
|
|
568 |
// tryAdvance can set splSpineIndex > spineIndex if the last spine is full
|
|
569 |
|
|
570 |
// The current spine array
|
|
571 |
T_ARR splChunk = (spine == null) ? curChunk : spine[0];
|
|
572 |
|
|
573 |
abstract void arrayForOne(T_ARR array, int index, T_CONS consumer);
|
|
574 |
|
|
575 |
abstract T_SPLITER arraySpliterator(T_ARR array, int offset, int len);
|
|
576 |
|
|
577 |
@Override
|
|
578 |
public long estimateSize() {
|
|
579 |
return (spine == null)
|
|
580 |
? (elementIndex - splElementIndex)
|
|
581 |
: count() - (priorElementCount[splSpineIndex] + splElementIndex);
|
|
582 |
}
|
|
583 |
|
|
584 |
@Override
|
|
585 |
public int characteristics() {
|
|
586 |
return SPLITERATOR_CHARACTERISTICS;
|
|
587 |
}
|
|
588 |
|
|
589 |
public boolean tryAdvance(T_CONS consumer) {
|
|
590 |
if (splSpineIndex < spineIndex
|
|
591 |
|| (splSpineIndex == spineIndex && splElementIndex < elementIndex)) {
|
|
592 |
arrayForOne(splChunk, splElementIndex++, consumer);
|
|
593 |
|
|
594 |
if (splElementIndex == arrayLength(splChunk)) {
|
|
595 |
splElementIndex = 0;
|
|
596 |
++splSpineIndex;
|
|
597 |
if (spine != null && splSpineIndex < spine.length)
|
|
598 |
splChunk = spine[splSpineIndex];
|
|
599 |
}
|
|
600 |
return true;
|
|
601 |
}
|
|
602 |
return false;
|
|
603 |
}
|
|
604 |
|
|
605 |
public void forEachRemaining(T_CONS consumer) {
|
|
606 |
if (splSpineIndex < spineIndex
|
|
607 |
|| (splSpineIndex == spineIndex && splElementIndex < elementIndex)) {
|
|
608 |
int i = splElementIndex;
|
|
609 |
// completed chunks, if any
|
|
610 |
for (int sp = splSpineIndex; sp < spineIndex; sp++) {
|
|
611 |
T_ARR chunk = spine[sp];
|
|
612 |
arrayForEach(chunk, i, arrayLength(chunk), consumer);
|
|
613 |
i = 0;
|
|
614 |
}
|
|
615 |
|
|
616 |
arrayForEach(curChunk, i, elementIndex, consumer);
|
|
617 |
|
|
618 |
splSpineIndex = spineIndex;
|
|
619 |
splElementIndex = elementIndex;
|
|
620 |
}
|
|
621 |
}
|
|
622 |
|
|
623 |
@Override
|
|
624 |
public T_SPLITER trySplit() {
|
|
625 |
if (splSpineIndex < spineIndex) {
|
|
626 |
T_SPLITER ret = arraySpliterator(spine[splSpineIndex], splElementIndex,
|
|
627 |
arrayLength(spine[splSpineIndex]) - splElementIndex);
|
|
628 |
splChunk = spine[++splSpineIndex];
|
|
629 |
splElementIndex = 0;
|
|
630 |
return ret;
|
|
631 |
}
|
|
632 |
else if (splSpineIndex == spineIndex) {
|
|
633 |
int t = (elementIndex - splElementIndex) / 2;
|
|
634 |
if (t == 0)
|
|
635 |
return null;
|
|
636 |
else {
|
|
637 |
T_SPLITER ret = arraySpliterator(curChunk, splElementIndex, t);
|
|
638 |
splElementIndex += t;
|
|
639 |
return ret;
|
|
640 |
}
|
|
641 |
}
|
|
642 |
else {
|
|
643 |
return null;
|
|
644 |
}
|
|
645 |
}
|
|
646 |
}
|
|
647 |
}
|
|
648 |
|
|
649 |
/**
|
|
650 |
* An ordered collection of {@code int} values.
|
|
651 |
*/
|
|
652 |
static class OfInt extends SpinedBuffer.OfPrimitive<Integer, int[], IntConsumer>
|
|
653 |
implements IntConsumer {
|
|
654 |
OfInt() { }
|
|
655 |
|
|
656 |
OfInt(int initialCapacity) {
|
|
657 |
super(initialCapacity);
|
|
658 |
}
|
|
659 |
|
|
660 |
@Override
|
|
661 |
public void forEach(Consumer<? super Integer> consumer) {
|
|
662 |
if (consumer instanceof IntConsumer) {
|
|
663 |
forEach((IntConsumer) consumer);
|
|
664 |
}
|
|
665 |
else {
|
|
666 |
if (Tripwire.ENABLED)
|
|
667 |
Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfInt.forEach(Consumer)");
|
|
668 |
spliterator().forEachRemaining(consumer);
|
|
669 |
}
|
|
670 |
}
|
|
671 |
|
|
672 |
@Override
|
|
673 |
protected int[][] newArrayArray(int size) {
|
|
674 |
return new int[size][];
|
|
675 |
}
|
|
676 |
|
|
677 |
@Override
|
|
678 |
protected int[] newArray(int size) {
|
|
679 |
return new int[size];
|
|
680 |
}
|
|
681 |
|
|
682 |
@Override
|
|
683 |
protected int arrayLength(int[] array) {
|
|
684 |
return array.length;
|
|
685 |
}
|
|
686 |
|
|
687 |
@Override
|
|
688 |
protected void arrayForEach(int[] array,
|
|
689 |
int from, int to,
|
|
690 |
IntConsumer consumer) {
|
|
691 |
for (int i = from; i < to; i++)
|
|
692 |
consumer.accept(array[i]);
|
|
693 |
}
|
|
694 |
|
|
695 |
@Override
|
|
696 |
public void accept(int i) {
|
|
697 |
preAccept();
|
|
698 |
curChunk[elementIndex++] = i;
|
|
699 |
}
|
|
700 |
|
|
701 |
public int get(long index) {
|
|
702 |
int ch = chunkFor(index);
|
|
703 |
if (spineIndex == 0 && ch == 0)
|
|
704 |
return curChunk[(int) index];
|
|
705 |
else
|
|
706 |
return spine[ch][(int) (index-priorElementCount[ch])];
|
|
707 |
}
|
|
708 |
|
|
709 |
public int[] asIntArray() {
|
|
710 |
return asPrimitiveArray();
|
|
711 |
}
|
|
712 |
|
|
713 |
@Override
|
|
714 |
public PrimitiveIterator.OfInt iterator() {
|
|
715 |
return Spliterators.iteratorFromSpliterator(spliterator());
|
|
716 |
}
|
|
717 |
|
|
718 |
public Spliterator.OfInt spliterator() {
|
|
719 |
class Splitr extends BaseSpliterator<Spliterator.OfInt>
|
|
720 |
implements Spliterator.OfInt {
|
|
721 |
|
|
722 |
@Override
|
|
723 |
void arrayForOne(int[] array, int index, IntConsumer consumer) {
|
|
724 |
consumer.accept(array[index]);
|
|
725 |
}
|
|
726 |
|
|
727 |
@Override
|
|
728 |
Spliterator.OfInt arraySpliterator(int[] array, int offset, int len) {
|
|
729 |
return Arrays.spliterator(array, offset, offset+len);
|
|
730 |
}
|
|
731 |
};
|
|
732 |
return new Splitr();
|
|
733 |
}
|
|
734 |
|
|
735 |
@Override
|
|
736 |
public String toString() {
|
|
737 |
int[] array = asIntArray();
|
|
738 |
if (array.length < 200) {
|
|
739 |
return String.format("%s[length=%d, chunks=%d]%s",
|
|
740 |
getClass().getSimpleName(), array.length,
|
|
741 |
spineIndex, Arrays.toString(array));
|
|
742 |
}
|
|
743 |
else {
|
|
744 |
int[] array2 = Arrays.copyOf(array, 200);
|
|
745 |
return String.format("%s[length=%d, chunks=%d]%s...",
|
|
746 |
getClass().getSimpleName(), array.length,
|
|
747 |
spineIndex, Arrays.toString(array2));
|
|
748 |
}
|
|
749 |
}
|
|
750 |
}
|
|
751 |
|
|
752 |
/**
|
|
753 |
* An ordered collection of {@code long} values.
|
|
754 |
*/
|
|
755 |
static class OfLong extends SpinedBuffer.OfPrimitive<Long, long[], LongConsumer>
|
|
756 |
implements LongConsumer {
|
|
757 |
OfLong() { }
|
|
758 |
|
|
759 |
OfLong(int initialCapacity) {
|
|
760 |
super(initialCapacity);
|
|
761 |
}
|
|
762 |
|
|
763 |
@Override
|
|
764 |
public void forEach(Consumer<? super Long> consumer) {
|
|
765 |
if (consumer instanceof LongConsumer) {
|
|
766 |
forEach((LongConsumer) consumer);
|
|
767 |
}
|
|
768 |
else {
|
|
769 |
if (Tripwire.ENABLED)
|
|
770 |
Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfLong.forEach(Consumer)");
|
|
771 |
spliterator().forEachRemaining(consumer);
|
|
772 |
}
|
|
773 |
}
|
|
774 |
|
|
775 |
@Override
|
|
776 |
protected long[][] newArrayArray(int size) {
|
|
777 |
return new long[size][];
|
|
778 |
}
|
|
779 |
|
|
780 |
@Override
|
|
781 |
protected long[] newArray(int size) {
|
|
782 |
return new long[size];
|
|
783 |
}
|
|
784 |
|
|
785 |
@Override
|
|
786 |
protected int arrayLength(long[] array) {
|
|
787 |
return array.length;
|
|
788 |
}
|
|
789 |
|
|
790 |
@Override
|
|
791 |
protected void arrayForEach(long[] array,
|
|
792 |
int from, int to,
|
|
793 |
LongConsumer consumer) {
|
|
794 |
for (int i = from; i < to; i++)
|
|
795 |
consumer.accept(array[i]);
|
|
796 |
}
|
|
797 |
|
|
798 |
@Override
|
|
799 |
public void accept(long i) {
|
|
800 |
preAccept();
|
|
801 |
curChunk[elementIndex++] = i;
|
|
802 |
}
|
|
803 |
|
|
804 |
public long get(long index) {
|
|
805 |
int ch = chunkFor(index);
|
|
806 |
if (spineIndex == 0 && ch == 0)
|
|
807 |
return curChunk[(int) index];
|
|
808 |
else
|
|
809 |
return spine[ch][(int) (index-priorElementCount[ch])];
|
|
810 |
}
|
|
811 |
|
|
812 |
public long[] asLongArray() {
|
|
813 |
return asPrimitiveArray();
|
|
814 |
}
|
|
815 |
|
|
816 |
@Override
|
|
817 |
public PrimitiveIterator.OfLong iterator() {
|
|
818 |
return Spliterators.iteratorFromSpliterator(spliterator());
|
|
819 |
}
|
|
820 |
|
|
821 |
|
|
822 |
public Spliterator.OfLong spliterator() {
|
|
823 |
class Splitr extends BaseSpliterator<Spliterator.OfLong>
|
|
824 |
implements Spliterator.OfLong {
|
|
825 |
@Override
|
|
826 |
void arrayForOne(long[] array, int index, LongConsumer consumer) {
|
|
827 |
consumer.accept(array[index]);
|
|
828 |
}
|
|
829 |
|
|
830 |
@Override
|
|
831 |
Spliterator.OfLong arraySpliterator(long[] array, int offset, int len) {
|
|
832 |
return Arrays.spliterator(array, offset, offset+len);
|
|
833 |
}
|
|
834 |
};
|
|
835 |
return new Splitr();
|
|
836 |
}
|
|
837 |
|
|
838 |
@Override
|
|
839 |
public String toString() {
|
|
840 |
long[] array = asLongArray();
|
|
841 |
if (array.length < 200) {
|
|
842 |
return String.format("%s[length=%d, chunks=%d]%s",
|
|
843 |
getClass().getSimpleName(), array.length,
|
|
844 |
spineIndex, Arrays.toString(array));
|
|
845 |
}
|
|
846 |
else {
|
|
847 |
long[] array2 = Arrays.copyOf(array, 200);
|
|
848 |
return String.format("%s[length=%d, chunks=%d]%s...",
|
|
849 |
getClass().getSimpleName(), array.length,
|
|
850 |
spineIndex, Arrays.toString(array2));
|
|
851 |
}
|
|
852 |
}
|
|
853 |
}
|
|
854 |
|
|
855 |
/**
|
|
856 |
* An ordered collection of {@code double} values.
|
|
857 |
*/
|
|
858 |
static class OfDouble
|
|
859 |
extends SpinedBuffer.OfPrimitive<Double, double[], DoubleConsumer>
|
|
860 |
implements DoubleConsumer {
|
|
861 |
OfDouble() { }
|
|
862 |
|
|
863 |
OfDouble(int initialCapacity) {
|
|
864 |
super(initialCapacity);
|
|
865 |
}
|
|
866 |
|
|
867 |
@Override
|
|
868 |
public void forEach(Consumer<? super Double> consumer) {
|
|
869 |
if (consumer instanceof DoubleConsumer) {
|
|
870 |
forEach((DoubleConsumer) consumer);
|
|
871 |
}
|
|
872 |
else {
|
|
873 |
if (Tripwire.ENABLED)
|
|
874 |
Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfDouble.forEach(Consumer)");
|
|
875 |
spliterator().forEachRemaining(consumer);
|
|
876 |
}
|
|
877 |
}
|
|
878 |
|
|
879 |
@Override
|
|
880 |
protected double[][] newArrayArray(int size) {
|
|
881 |
return new double[size][];
|
|
882 |
}
|
|
883 |
|
|
884 |
@Override
|
|
885 |
protected double[] newArray(int size) {
|
|
886 |
return new double[size];
|
|
887 |
}
|
|
888 |
|
|
889 |
@Override
|
|
890 |
protected int arrayLength(double[] array) {
|
|
891 |
return array.length;
|
|
892 |
}
|
|
893 |
|
|
894 |
@Override
|
|
895 |
protected void arrayForEach(double[] array,
|
|
896 |
int from, int to,
|
|
897 |
DoubleConsumer consumer) {
|
|
898 |
for (int i = from; i < to; i++)
|
|
899 |
consumer.accept(array[i]);
|
|
900 |
}
|
|
901 |
|
|
902 |
@Override
|
|
903 |
public void accept(double i) {
|
|
904 |
preAccept();
|
|
905 |
curChunk[elementIndex++] = i;
|
|
906 |
}
|
|
907 |
|
|
908 |
public double get(long index) {
|
|
909 |
int ch = chunkFor(index);
|
|
910 |
if (spineIndex == 0 && ch == 0)
|
|
911 |
return curChunk[(int) index];
|
|
912 |
else
|
|
913 |
return spine[ch][(int) (index-priorElementCount[ch])];
|
|
914 |
}
|
|
915 |
|
|
916 |
public double[] asDoubleArray() {
|
|
917 |
return asPrimitiveArray();
|
|
918 |
}
|
|
919 |
|
|
920 |
@Override
|
|
921 |
public PrimitiveIterator.OfDouble iterator() {
|
|
922 |
return Spliterators.iteratorFromSpliterator(spliterator());
|
|
923 |
}
|
|
924 |
|
|
925 |
public Spliterator.OfDouble spliterator() {
|
|
926 |
class Splitr extends BaseSpliterator<Spliterator.OfDouble>
|
|
927 |
implements Spliterator.OfDouble {
|
|
928 |
@Override
|
|
929 |
void arrayForOne(double[] array, int index, DoubleConsumer consumer) {
|
|
930 |
consumer.accept(array[index]);
|
|
931 |
}
|
|
932 |
|
|
933 |
@Override
|
|
934 |
Spliterator.OfDouble arraySpliterator(double[] array, int offset, int len) {
|
|
935 |
return Arrays.spliterator(array, offset, offset+len);
|
|
936 |
}
|
|
937 |
}
|
|
938 |
return new Splitr();
|
|
939 |
}
|
|
940 |
|
|
941 |
@Override
|
|
942 |
public String toString() {
|
|
943 |
double[] array = asDoubleArray();
|
|
944 |
if (array.length < 200) {
|
|
945 |
return String.format("%s[length=%d, chunks=%d]%s",
|
|
946 |
getClass().getSimpleName(), array.length,
|
|
947 |
spineIndex, Arrays.toString(array));
|
|
948 |
}
|
|
949 |
else {
|
|
950 |
double[] array2 = Arrays.copyOf(array, 200);
|
|
951 |
return String.format("%s[length=%d, chunks=%d]%s...",
|
|
952 |
getClass().getSimpleName(), array.length,
|
|
953 |
spineIndex, Arrays.toString(array2));
|
|
954 |
}
|
|
955 |
}
|
|
956 |
}
|
|
957 |
}
|
|
958 |
|