forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodes.java
2545 lines (2124 loc) · 100 KB
/
Nodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util.stream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.CountedCompleter;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.LongConsumer;
import java.util.function.LongFunction;
/**
* Factory methods for constructing implementations of {@link Node} and
* {@link Node.Builder} and their primitive specializations. Fork/Join tasks
* for collecting output from a {@link PipelineHelper} to a {@link Node} and
* flattening {@link Node}s.
*
* @since 1.8
*/
// Node工厂,定义了六大类Node,应用于流水线的终端阶段
final class Nodes {
private Nodes() {
throw new Error("no instances");
}
/**
* The maximum size of an array that can be allocated.
*/
static final long MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
// IllegalArgumentException messages
static final String BAD_SIZE = "Stream size exceeds max array size";
@SuppressWarnings("rawtypes")
private static final Node EMPTY_NODE = new EmptyNode.OfRef();
private static final Node.OfInt EMPTY_INT_NODE = new EmptyNode.OfInt();
private static final Node.OfLong EMPTY_LONG_NODE = new EmptyNode.OfLong();
private static final Node.OfDouble EMPTY_DOUBLE_NODE = new EmptyNode.OfDouble();
/*▼ 构造第(1)类Node ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Produces an empty node whose count is zero, has no children and no content.
*
* @param <T> the type of elements of the created node
* @param shape the shape of the node to be created
* @return an empty node.
*/
@SuppressWarnings("unchecked")
static <T> Node<T> emptyNode(StreamShape shape) {
switch (shape) {
case REFERENCE: return (Node<T>) EMPTY_NODE;
case INT_VALUE: return (Node<T>) EMPTY_INT_NODE;
case LONG_VALUE: return (Node<T>) EMPTY_LONG_NODE;
case DOUBLE_VALUE: return (Node<T>) EMPTY_DOUBLE_NODE;
default:
throw new IllegalStateException("Unknown shape " + shape);
}
}
/*▲ 构造第(1)类Node ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 构造第(2)类Node ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Produces a {@link Node} describing an array.
*
* <p>The node will hold a reference to the array and will not make a copy.
*
* @param <T> the type of elements held by the node
* @param array the array
* @return a node holding an array
*/
// 返回ArrayNode
static <T> Node<T> node(T[] array) {
return new ArrayNode<>(array);
}
/**
* Produces a {@link Node.OfInt} describing an int[] array.
*
* <p>The node will hold a reference to the array and will not make a copy.
*
* @param array the array
* @return a node holding an array
*/
// 返回IntArrayNode
static Node.OfInt node(int[] array) {
return new IntArrayNode(array);
}
/**
* Produces a {@link Node.OfLong} describing a long[] array.
* <p>
* The node will hold a reference to the array and will not make a copy.
*
* @param array the array
* @return a node holding an array
*/
// 返回LongArrayNode
static Node.OfLong node(final long[] array) {
return new LongArrayNode(array);
}
/**
* Produces a {@link Node.OfDouble} describing a double[] array.
*
* <p>The node will hold a reference to the array and will not make a copy.
*
* @param array the array
* @return a node holding an array
*/
// 返回DoubleArrayNode
static Node.OfDouble node(final double[] array) {
return new DoubleArrayNode(array);
}
/*▲ 构造第(2)类Node ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 构造第(3)、(4)类Node ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Produces a {@link Node.Builder}.
*
* @param exactSizeIfKnown -1 if a variable size builder is requested,
* otherwise the exact capacity desired. A fixed capacity builder will
* fail if the wrong number of elements are added to the builder.
* @param generator the array factory
* @param <T> the type of elements of the node builder
* @return a {@code Node.Builder}
*/
// 返回Nodes.FixedNodeBuilder或Nodes.SpinedNodeBuilder
static <T> Node.Builder<T> builder(long exactSizeIfKnown, IntFunction<T[]> generator) {
return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
? new FixedNodeBuilder<>(exactSizeIfKnown, generator) // 新建FixedNodeBuilder,该Builder可用于创建固定长度的Node
: builder(); // 新建SpinedNodeBuilder,该Builder可用于创建可变长度的Node
}
/**
* Produces a variable size {@link Node.Builder}.
*
* @param <T> the type of elements of the node builder
* @return a {@code Node.Builder}
*/
// 返回Nodes.SpinedNodeBuilder
static <T> Node.Builder<T> builder() {
return new SpinedNodeBuilder<>();
}
/**
* Produces a {@link Node.Builder.OfInt}.
*
* @param exactSizeIfKnown -1 if a variable size builder is requested,
* otherwise the exact capacity desired. A fixed capacity builder will
* fail if the wrong number of elements are added to the builder.
* @return a {@code Node.Builder.OfInt}
*/
// 返回Nodes.IntNodeBuilder或Nodes.IntNodeBuilder
static Node.Builder.OfInt intBuilder(long exactSizeIfKnown) {
return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
? new IntFixedNodeBuilder(exactSizeIfKnown)
: intBuilder();
}
/**
* Produces a variable size @{link Node.Builder.OfInt}.
*
* @return a {@code Node.Builder.OfInt}
*/
// 返回Nodes.IntSpinedNodeBuilder
static Node.Builder.OfInt intBuilder() {
return new IntSpinedNodeBuilder();
}
/**
* Produces a {@link Node.Builder.OfLong}.
*
* @param exactSizeIfKnown -1 if a variable size builder is requested,
* otherwise the exact capacity desired. A fixed capacity builder will
* fail if the wrong number of elements are added to the builder.
* @return a {@code Node.Builder.OfLong}
*/
// 返回Nodes.LongFixedNodeBuilder或Nodes.LongSpinedNodeBuilder
static Node.Builder.OfLong longBuilder(long exactSizeIfKnown) {
return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
? new LongFixedNodeBuilder(exactSizeIfKnown)
: longBuilder();
}
/**
* Produces a variable size @{link Node.Builder.OfLong}.
*
* @return a {@code Node.Builder.OfLong}
*/
// 返回Nodes.LongSpinedNodeBuilder
static Node.Builder.OfLong longBuilder() {
return new LongSpinedNodeBuilder();
}
/**
* Produces a {@link Node.Builder.OfDouble}.
*
* @param exactSizeIfKnown -1 if a variable size builder is requested,
* otherwise the exact capacity desired. A fixed capacity builder will
* fail if the wrong number of elements are added to the builder.
* @return a {@code Node.Builder.OfDouble}
*/
// 返回Nodes.DoubleFixedNodeBuilder或Nodes.DoubleSpinedNodeBuilder
static Node.Builder.OfDouble doubleBuilder(long exactSizeIfKnown) {
return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
? new DoubleFixedNodeBuilder(exactSizeIfKnown)
: doubleBuilder();
}
/**
* Produces a variable size @{link Node.Builder.OfDouble}.
*
* @return a {@code Node.Builder.OfDouble}
*/
// 返回Nodes.DoubleSpinedNodeBuilder
static Node.Builder.OfDouble doubleBuilder() {
return new DoubleSpinedNodeBuilder();
}
/*▲ 构造第(3)、(4)类Node ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 构造第(5)类Node ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Produces a {@link Node} describing a {@link Collection}.
* <p>
* The node will hold a reference to the collection and will not make a copy.
*
* @param <T> the type of elements held by the node
* @param c the collection
* @return a node holding a collection
*/
static <T> Node<T> node(Collection<T> c) {
return new CollectionNode<>(c);
}
/*▲ 构造第(5)类Node ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 构造第(6)类Node ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Produces a concatenated {@link Node} that has two or more children.
* <p>The count of the concatenated node is equal to the sum of the count
* of each child. Traversal of the concatenated node traverses the content
* of each child in encounter order of the list of children. Splitting a
* spliterator obtained from the concatenated node preserves the encounter
* order of the list of children.
*
* <p>The result may be a concatenated node, the input sole node if the size
* of the list is 1, or an empty node.
*
* @param <T> the type of elements of the concatenated node
* @param shape the shape of the concatenated node to be created
* @param left the left input node
* @param right the right input node
* @return a {@code Node} covering the elements of the input nodes
* @throws IllegalStateException if all {@link Node} elements of the list
* are an not instance of type supported by this factory.
*/
// 将两个Node串联起来,并返回串联后的Node
@SuppressWarnings("unchecked")
static <T> Node<T> conc(StreamShape shape, Node<T> left, Node<T> right) {
switch (shape) {
case REFERENCE:
return new ConcNode<>(left, right);
case INT_VALUE:
return (Node<T>) new ConcNode.OfInt((Node.OfInt) left, (Node.OfInt) right);
case LONG_VALUE:
return (Node<T>) new ConcNode.OfLong((Node.OfLong) left, (Node.OfLong) right);
case DOUBLE_VALUE:
return (Node<T>) new ConcNode.OfDouble((Node.OfDouble) left, (Node.OfDouble) right);
default:
throw new IllegalStateException("Unknown shape " + shape);
}
}
/*▲ 构造第(6)类Node ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ flatten,并行地给Node降维 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Flatten, in parallel, a {@link Node}. A flattened node is one that has
* no children. If the node is already flat, it is simply returned.
*
* @implSpec
* If a new node is to be created, the generator is used to create an array
* whose length is {@link Node#count()}. Then the node tree is traversed
* and leaf node elements are placed in the array concurrently by leaf tasks
* at the correct offsets.
*
* @param <T> type of elements contained by the node
* @param node the node to flatten
* @param generator the array factory used to create array instances
* @return a flat {@code Node}
*/
// 将当前的Node降维成非树形Node
public static <T> Node<T> flatten(Node<T> node, IntFunction<T[]> generator) {
// 非线性Node(一般指树形Node)
if (node.getChildCount() > 0) {
long size = node.count();
if (size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成指定类型的数组以存储node中的数据
T[] array = generator.apply((int) size);
// 并行地将Node中的元素转存到线性数组中
new ToArrayTask.OfRef<>(node, array, 0).invoke();
// 返回线性Node
return node(array);
}
// 线性Node,包括存储结构为二维数组的Node
return node;
}
/**
* Flatten, in parallel, a {@link Node.OfInt}. A flattened node is one that
* has no children. If the node is already flat, it is simply returned.
*
* @implSpec
* If a new node is to be created, a new int[] array is created whose length
* is {@link Node#count()}. Then the node tree is traversed and leaf node
* elements are placed in the array concurrently by leaf tasks at the
* correct offsets.
*
* @param node the node to flatten
* @return a flat {@code Node.OfInt}
*/
// 将当前的Node降维成非树形Node
public static Node.OfInt flattenInt(Node.OfInt node) {
// 非线性Node(一般指树形Node)
if (node.getChildCount() > 0) {
long size = node.count();
if (size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成int类型的数组以存储node中的数据
int[] array = new int[(int) size];
// 并行地将Node中的元素转存到线性数组中
new ToArrayTask.OfInt(node, array, 0).invoke();
// 返回线性Node
return node(array);
}
// 线性Node,包括存储结构为二维数组的Node
return node;
}
/**
* Flatten, in parallel, a {@link Node.OfLong}. A flattened node is one that
* has no children. If the node is already flat, it is simply returned.
*
* @implSpec
* If a new node is to be created, a new long[] array is created whose length
* is {@link Node#count()}. Then the node tree is traversed and leaf node
* elements are placed in the array concurrently by leaf tasks at the
* correct offsets.
*
* @param node the node to flatten
* @return a flat {@code Node.OfLong}
*/
// 将当前的Node降维成非树形Node
public static Node.OfLong flattenLong(Node.OfLong node) {
// 非线性Node(一般指树形Node)
if (node.getChildCount() > 0) {
long size = node.count();
if (size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成long类型的数组以存储node中的数据
long[] array = new long[(int) size];
// 并行地将Node中的元素转存到线性数组中
new ToArrayTask.OfLong(node, array, 0).invoke();
// 返回线性Node
return node(array);
}
// 线性Node,包括存储结构为二维数组的Node
return node;
}
/**
* Flatten, in parallel, a {@link Node.OfDouble}. A flattened node is one that
* has no children. If the node is already flat, it is simply returned.
*
* @implSpec
* If a new node is to be created, a new double[] array is created whose length
* is {@link Node#count()}. Then the node tree is traversed and leaf node
* elements are placed in the array concurrently by leaf tasks at the
* correct offsets.
*
* @param node the node to flatten
* @return a flat {@code Node.OfDouble}
*/
// 将当前的Node降维成非树形Node
public static Node.OfDouble flattenDouble(Node.OfDouble node) {
// 非线性Node(一般指树形Node)
if (node.getChildCount() > 0) {
long size = node.count();
if (size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成double类型的数组以存储node中的数据
double[] array = new double[(int) size];
// 并行地将Node中的元素转存到线性数组中
new ToArrayTask.OfDouble(node, array, 0).invoke();
// 返回线性Node
return node(array);
}
// 线性Node,包括存储结构为二维数组的Node
return node;
}
/*▲ flatten,并行地给Node降维 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ collect,并行计算/收集元素 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Collect, in parallel, elements output from a pipeline and describe those elements with a {@link Node}.
*
* @param helper the pipeline helper describing the pipeline
* @param flattenTree whether a conc node should be flattened into a node
* describing an array before returning
* @param generator the array generator
*
* @return a {@link Node} describing the output elements
*
* @implSpec If the exact size of the output from the pipeline is known and the source
* {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
* then a flat {@link Node} will be returned whose content is an array,
* since the size is known the array can be constructed in advance and
* output elements can be placed into the array concurrently by leaf
* tasks at the correct offsets. If the exact size is not known, output
* elements are collected into a conc-node whose shape mirrors that
* of the computation. This conc-node can then be flattened in
* parallel to produce a flat {@code Node} if desired.
*/
public static <P_IN, P_OUT> Node<P_OUT> collect(PipelineHelper<P_OUT> helper, Spliterator<P_IN> spliterator, boolean flattenTree, IntFunction<P_OUT[]> generator) {
// 返回输出的元素数量,如果未知或无穷,则返回-1
long size = helper.exactOutputSizeIfKnown(spliterator);
// 处理元素总量一定,但是子结点数量不确定的Node
if(size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
P_OUT[] array = generator.apply((int) size);
new SizedCollectorTask.OfRef<>(spliterator, helper, array).invoke();
// 返回ArrayNode
return node(array);
}
// 处理给定的元素,将计算结果存入树形Node
Node<P_OUT> node = new CollectorTask.OfRef<>(helper, generator, spliterator).invoke();
// 返回包含计算结果的Node(视需求将Node降维)
return flattenTree ? flatten(node, generator) : node;
}
/**
* Collect, in parallel, elements output from an int-valued pipeline and
* describe those elements with a {@link Node.OfInt}.
*
* @param <P_IN> the type of elements from the source Spliterator
* @param helper the pipeline helper describing the pipeline
* @param flattenTree whether a conc node should be flattened into a node
* describing an array before returning
*
* @return a {@link Node.OfInt} describing the output elements
*
* @implSpec If the exact size of the output from the pipeline is known and the source
* {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
* then a flat {@link Node} will be returned whose content is an array,
* since the size is known the array can be constructed in advance and
* output elements can be placed into the array concurrently by leaf
* tasks at the correct offsets. If the exact size is not known, output
* elements are collected into a conc-node whose shape mirrors that
* of the computation. This conc-node can then be flattened in
* parallel to produce a flat {@code Node.OfInt} if desired.
*/
public static <P_IN> Node.OfInt collectInt(PipelineHelper<Integer> helper, Spliterator<P_IN> spliterator, boolean flattenTree) {
// 返回输出的元素数量,如果未知或无穷,则返回-1
long size = helper.exactOutputSizeIfKnown(spliterator);
// 处理元素总量一定,但是子结点数量不确定的Node
if(size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
int[] array = new int[(int) size];
new SizedCollectorTask.OfInt<>(spliterator, helper, array).invoke();
// 返回ArrayNode
return node(array);
}
// 处理给定的元素,将计算结果存入树形Node
Node.OfInt node = new CollectorTask.OfInt<>(helper, spliterator).invoke();
// 返回包含计算结果的Node(视需求将Node降维)
return flattenTree ? flattenInt(node) : node;
}
/**
* Collect, in parallel, elements output from a long-valued pipeline and
* describe those elements with a {@link Node.OfLong}.
*
* @param <P_IN> the type of elements from the source Spliterator
* @param helper the pipeline helper describing the pipeline
* @param flattenTree whether a conc node should be flattened into a node
* describing an array before returning
*
* @return a {@link Node.OfLong} describing the output elements
*
* @implSpec If the exact size of the output from the pipeline is known and the source
* {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
* then a flat {@link Node} will be returned whose content is an array,
* since the size is known the array can be constructed in advance and
* output elements can be placed into the array concurrently by leaf
* tasks at the correct offsets. If the exact size is not known, output
* elements are collected into a conc-node whose shape mirrors that
* of the computation. This conc-node can then be flattened in
* parallel to produce a flat {@code Node.OfLong} if desired.
*/
public static <P_IN> Node.OfLong collectLong(PipelineHelper<Long> helper, Spliterator<P_IN> spliterator, boolean flattenTree) {
// 返回输出的元素数量,如果未知或无穷,则返回-1
long size = helper.exactOutputSizeIfKnown(spliterator);
// 处理元素总量一定,但是子结点数量不确定的Node
if(size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
long[] array = new long[(int) size];
new SizedCollectorTask.OfLong<>(spliterator, helper, array).invoke();
// 返回ArrayNode
return node(array);
}
// 处理给定的元素,将计算结果存入树形Node
Node.OfLong node = new CollectorTask.OfLong<>(helper, spliterator).invoke();
// 返回包含计算结果的Node(视需求将Node降维)
return flattenTree ? flattenLong(node) : node;
}
/**
* Collect, in parallel, elements output from n double-valued pipeline and
* describe those elements with a {@link Node.OfDouble}.
*
* @param <P_IN> the type of elements from the source Spliterator
* @param helper the pipeline helper describing the pipeline
* @param flattenTree whether a conc node should be flattened into a node
* describing an array before returning
*
* @return a {@link Node.OfDouble} describing the output elements
*
* @implSpec If the exact size of the output from the pipeline is known and the source
* {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
* then a flat {@link Node} will be returned whose content is an array,
* since the size is known the array can be constructed in advance and
* output elements can be placed into the array concurrently by leaf
* tasks at the correct offsets. If the exact size is not known, output
* elements are collected into a conc-node whose shape mirrors that
* of the computation. This conc-node can then be flattened in
* parallel to produce a flat {@code Node.OfDouble} if desired.
*/
public static <P_IN> Node.OfDouble collectDouble(PipelineHelper<Double> helper, Spliterator<P_IN> spliterator, boolean flattenTree) {
// 返回输出的元素数量,如果未知或无穷,则返回-1
long size = helper.exactOutputSizeIfKnown(spliterator);
// 处理元素总量一定,但是子结点数量不确定的Node
if(size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
double[] array = new double[(int) size];
new SizedCollectorTask.OfDouble<>(spliterator, helper, array).invoke();
// 返回ArrayNode
return node(array);
}
// 处理给定的元素,将计算结果存入树形Node
Node.OfDouble node = new CollectorTask.OfDouble<>(helper, spliterator).invoke();
// 返回包含计算结果的Node(视需求将Node降维)
return flattenTree ? flattenDouble(node) : node;
}
/*▲ collect,并行计算/收集元素 ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* @return an array generator for an array whose elements are of type T.
*/
// 创建T[]类型的数组,数组长度由参数指定
@SuppressWarnings("unchecked")
static <T> IntFunction<T[]> castingArray() {
return size -> (T[]) new Object[size];
}
/*▼ 第(1)类Node 空 ████████████████████████████████████████████████████████████████████████████████┓ */
private static final int[] EMPTY_INT_ARRAY = new int[0];
private static final long[] EMPTY_LONG_ARRAY = new long[0];
private static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
// (1) 空Node,不包含有效数据
private abstract static class EmptyNode<T, T_ARR, T_CONS>
implements Node<T> {
EmptyNode() { }
@Override
public T[] asArray(IntFunction<T[]> generator) {
return generator.apply(0);
}
public void copyInto(T_ARR array, int offset) {
}
@Override
public long count() {
return 0;
}
public void forEach(T_CONS consumer) {
}
private static class OfRef<T> extends EmptyNode<T, T[], Consumer<? super T>> {
private OfRef() {
super();
}
@Override
public Spliterator<T> spliterator() {
return Spliterators.emptySpliterator();
}
}
private static final class OfInt
extends EmptyNode<Integer, int[], IntConsumer>
implements Node.OfInt {
OfInt() {
// Avoid creation of special accessor
}
@Override
public Spliterator.OfInt spliterator() {
return Spliterators.emptyIntSpliterator();
}
// 返回长度为0的int数组
@Override
public int[] asPrimitiveArray() {
return EMPTY_INT_ARRAY;
}
}
private static final class OfLong
extends EmptyNode<Long, long[], LongConsumer>
implements Node.OfLong {
OfLong() {
// Avoid creation of special accessor
}
@Override
public Spliterator.OfLong spliterator() {
return Spliterators.emptyLongSpliterator();
}
// 返回长度为0的long数组
@Override
public long[] asPrimitiveArray() {
return EMPTY_LONG_ARRAY;
}
}
private static final class OfDouble
extends EmptyNode<Double, double[], DoubleConsumer>
implements Node.OfDouble {
OfDouble() {
// Avoid creation of special accessor
}
@Override
public Spliterator.OfDouble spliterator() {
return Spliterators.emptyDoubleSpliterator();
}
// 返回长度为0的double数组
@Override
public double[] asPrimitiveArray() {
return EMPTY_DOUBLE_ARRAY;
}
}
}
/*▲ 第(1)类Node 空 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 第(2)类Node 封装了数组的Node ████████████████████████████████████████████████████████████████████████████████┓ */
/** Node class for a reference array */
// 【Node子类】封装了数组的Node
private static class ArrayNode<T> implements Node<T> {
final T[] array;
int curSize; // 数组元素数量
// 新建ArrayNode,内部包含一个长度为size的空数组
@SuppressWarnings("unchecked")
ArrayNode(long size, IntFunction<T[]> generator) {
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
this.array = generator.apply((int) size);
this.curSize = 0;
}
// 从已有的数组新建ArrayNode
ArrayNode(T[] array) {
this.array = array;
this.curSize = array.length;
}
// 返回描述此ArrayNode中元素的Spliterator(这里直接将内部数组包装到Spliterator后返回)
@Override
public Spliterator<T> spliterator() {
return Arrays.spliterator(array, 0, curSize);
}
// 将ArrayNode的内容复制到数组dest中(这里直接进行数组拷贝)
@Override
public void copyInto(T[] dest, int destOffset) {
System.arraycopy(array, 0, dest, destOffset, curSize);
}
// 返回ArrayNode内部数据的数组视图(这里直接返回内部数组)
@Override
public T[] asArray(IntFunction<T[]> generator) {
if(array.length == curSize) {
return array;
} else {
throw new IllegalStateException();
}
}
// 返回ArrayNode中包含的元素数量(这里直接返回数组中元素的个数)
@Override
public long count() {
return curSize;
}
// 遍历ArrayNode中的元素,并在其上执行Consumer操作
@Override
public void forEach(Consumer<? super T> consumer) {
for(int i = 0; i < curSize; i++) {
consumer.accept(array[i]);
}
}
@Override
public String toString() {
return String.format("ArrayNode[%d][%s]", array.length - curSize, Arrays.toString(array));
}
}
// 【Node子类】封装了int[]的Node
private static class IntArrayNode implements Node.OfInt {
final int[] array;
int curSize;
// 新建Node,内部包含一个长度为size的空数组
IntArrayNode(long size) {
if (size >= MAX_ARRAY_SIZE)
throw new IllegalArgumentException(BAD_SIZE);
this.array = new int[(int) size];
this.curSize = 0;
}
// 从已有的数组新建Node
IntArrayNode(int[] array) {
this.array = array;
this.curSize = array.length;
}
// 返回描述此Node中元素的Spliterator(这里是IntArraySpliterator)
@Override
public Spliterator.OfInt spliterator() {
return Arrays.spliterator(array, 0, curSize);
}
// 将Node中的元素存入int数组后返回
@Override
public int[] asPrimitiveArray() {
if (array.length == curSize) {
return array;
} else {
return Arrays.copyOf(array, curSize);
}
}
// 将Node的内容复制到数组dest中(这里直接进行数组拷贝)
@Override
public void copyInto(int[] dest, int destOffset) {
System.arraycopy(array, 0, dest, destOffset, curSize);
}
// 返回Node中包含的元素数量(这里直接返回数组中元素的个数)
@Override
public long count() {
return curSize;
}
// 遍历Node中的元素,并在其上执行consumer操作
@Override
public void forEach(IntConsumer consumer) {
for (int i = 0; i < curSize; i++) {
consumer.accept(array[i]);
}
}
@Override
public String toString() {
return String.format("IntArrayNode[%d][%s]",
array.length - curSize, Arrays.toString(array));
}
}
// 为【Node子类】封装了long[]的Node
private static class LongArrayNode implements Node.OfLong {
final long[] array;
int curSize;
// 新建Node,内部包含一个长度为size的空数组
LongArrayNode(long size) {
if (size >= MAX_ARRAY_SIZE)
throw new IllegalArgumentException(BAD_SIZE);
this.array = new long[(int) size];
this.curSize = 0;
}
LongArrayNode(long[] array) {
this.array = array;
this.curSize = array.length;
}
// 返回描述此Node中元素的Spliterator(这里是LongArraySpliterator)
@Override
public Spliterator.OfLong spliterator() {
return Arrays.spliterator(array, 0, curSize);
}
// 将Node中的元素存入long数组后返回
@Override
public long[] asPrimitiveArray() {
if (array.length == curSize) {
return array;
} else {
return Arrays.copyOf(array, curSize);
}
}
// 将Node的内容复制到数组dest中(这里直接进行数组拷贝)
@Override
public void copyInto(long[] dest, int destOffset) {
System.arraycopy(array, 0, dest, destOffset, curSize);
}
// 返回Node中包含的元素数量(这里直接返回数组中元素的个数)
@Override
public long count() {
return curSize;
}
// 遍历Node中的元素,并在其上执行consumer操作
@Override
public void forEach(LongConsumer consumer) {
for (int i = 0; i < curSize; i++) {
consumer.accept(array[i]);
}
}
@Override
public String toString() {
return String.format("LongArrayNode[%d][%s]",
array.length - curSize, Arrays.toString(array));
}
}
// 【Node子类】封装了double[]的Node
private static class DoubleArrayNode implements Node.OfDouble {
final double[] array;
int curSize;
// 新建Node,内部包含一个长度为size的空数组
DoubleArrayNode(long size) {
if (size >= MAX_ARRAY_SIZE)
throw new IllegalArgumentException(BAD_SIZE);
this.array = new double[(int) size];
this.curSize = 0;
}
DoubleArrayNode(double[] array) {
this.array = array;
this.curSize = array.length;
}
// 返回描述此Node中元素的Spliterator(这里是DoubleArraySpliterator)
@Override
public Spliterator.OfDouble spliterator() {
return Arrays.spliterator(array, 0, curSize);
}
// 将Node中的元素存入double数组后返回
@Override
public double[] asPrimitiveArray() {
if (array.length == curSize) {
return array;
} else {
return Arrays.copyOf(array, curSize);