forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nodes.java
3197 lines (2642 loc) · 134 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的实现
*
* 主要包含以下6类Node:
* [1] "空"Node
* [2] 普通"数组"Node
* [3] 增强"数组"Node
* [4] "弹性缓冲区"Node
* [5] Collection-Node
* [6] "树状"Node
*
* 还提供了一些辅助类,包括:
* [1] "树状"Node的Spliterator
* [2] "并行复制"任务
* [3] 线性"并行择取"任务
* [4] 树状"并行择取"任务
*
* 在方法层面,除了提供构造各类Node的工厂方法,还提供了flatten操作和collect操作的实现。
*/
final class Nodes {
/**
* 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";
private Nodes() {
throw new Error("no instances");
}
/*▼ (1) 构造"空"Node ████████████████████████████████████████████████████████████████████████████████┓ */
@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();
/**
* 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.
*/
// 构造"空"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
*/
// 构造普通"数组"Node(引用类型版本)
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
*/
// 构造普通"数组"Node(int类型版本)
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
*/
// 构造普通"数组"Node(long类型版本)
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
*/
// 构造普通"数组"Node(double类型版本)
static Node.OfDouble node(final double[] array) {
return new DoubleArrayNode(array);
}
/*▲ (2) 构造普通"数组"Node ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ (3)(4) 构造增强"数组"Node或"弹性缓冲区"Node ████████████████████████████████████████████████████████████████████████████████┓ */
/*
* 无论是增强"数组"Node还是"弹性缓冲区"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}
*/
// 构造增强"数组"Node或"弹性缓冲区"Node(引用类型版本)
static <T> Node.Builder<T> builder(long exactSizeIfKnown, IntFunction<T[]> generator) {
// 长度已知且固定
if(exactSizeIfKnown >= 0 && exactSizeIfKnown<MAX_ARRAY_SIZE) {
// 构造增强"数组"Node(引用类型版本)
return new FixedNodeBuilder<>(exactSizeIfKnown, generator);
}
// 构造"弹性缓冲区"Node(引用类型版本)
return builder();
}
/**
* 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}
*/
// 构造增强"数组"Node或"弹性缓冲区"Node(int类型版本)
static Node.Builder.OfInt intBuilder(long exactSizeIfKnown) {
// 长度已知且固定
if(exactSizeIfKnown >= 0 && exactSizeIfKnown<MAX_ARRAY_SIZE) {
// 构造增强"数组"Node(int类型版本)
return new IntFixedNodeBuilder(exactSizeIfKnown);
}
// 构造"弹性缓冲区"Node(int类型版本)
return intBuilder();
}
/**
* 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}
*/
// 构造增强"数组"Node或"弹性缓冲区"Node(long类型版本)
static Node.Builder.OfLong longBuilder(long exactSizeIfKnown) {
// 长度已知且固定
if(exactSizeIfKnown >= 0 && exactSizeIfKnown<MAX_ARRAY_SIZE) {
// 构造增强"数组"Node(long类型版本)
return new LongFixedNodeBuilder(exactSizeIfKnown);
}
// 构造"弹性缓冲区"Node(long类型版本)
return longBuilder();
}
/**
* 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}
*/
// 构造增强"数组"Node或"弹性缓冲区"Node(double类型版本)
static Node.Builder.OfDouble doubleBuilder(long exactSizeIfKnown) {
// 长度已知且固定
if(exactSizeIfKnown >= 0 && exactSizeIfKnown<MAX_ARRAY_SIZE) {
// 构造增强"数组"Node(double类型版本)
return new DoubleFixedNodeBuilder(exactSizeIfKnown);
}
// 构造"弹性缓冲区"Node(double类型版本)
return doubleBuilder();
}
/**
* Produces a variable size {@link Node.Builder}.
*
* @param <T> the type of elements of the node builder
*
* @return a {@code Node.Builder}
*/
// 构造"弹性缓冲区"Node(引用类型版本)
static <T> Node.Builder<T> builder() {
return new SpinedNodeBuilder<>();
}
/**
* Produces a variable size @{link Node.Builder.OfInt}.
*
* @return a {@code Node.Builder.OfInt}
*/
// 构造"弹性缓冲区"Node(int类型版本)
static Node.Builder.OfInt intBuilder() {
return new IntSpinedNodeBuilder();
}
/**
* Produces a variable size @{link Node.Builder.OfLong}.
*
* @return a {@code Node.Builder.OfLong}
*/
// 构造"弹性缓冲区"Node(long类型版本)
static Node.Builder.OfLong longBuilder() {
return new LongSpinedNodeBuilder();
}
/**
* Produces a variable size @{link Node.Builder.OfDouble}.
*
* @return a {@code Node.Builder.OfDouble}
*/
// 构造"弹性缓冲区"Node(double类型版本)
static Node.Builder.OfDouble doubleBuilder() {
return new DoubleSpinedNodeBuilder();
}
/*▲ (3)(4) 构造增强"数组"Node或"弹性缓冲区"Node ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ (5) 构造Collection-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 collection the collection
*
* @return a node holding a collection
*/
// 构造Collection-Node(引用类型版本)
static <T> Node<T> node(Collection<T> collection) {
return new CollectionNode<>(collection);
}
/*▲ (5) 构造Collection-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按树形链接起来,并返回链接后的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转换为普通"数组"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.
*
* @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}
*
* @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.
*/
/*
* 尝试将树状Node转换为普通"数组"Node;如果node不是树状node,那直接就返回了。
* 将树状Node中的元素并行地复制到generator生成的数组中,然后再将该数组封装成普通"数组"Node后返回(引用类型版本)。
*/
public static <T> Node<T> flatten(Node<T> node, IntFunction<T[]> generator) {
// 如果给定的node已经没有子Node,则直接返回
if(node.getChildCount()<=0) {
return node;
}
long size = node.count();
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成指定类型的数组以存储node中的数据
T[] array = generator.apply((int) size);
// 构造"并行复制"任务:将树状node中的元素并行地复制到数组array中
ToArrayTask.OfRef<T> task = new ToArrayTask.OfRef<>(node, array, 0);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
}
/**
* 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;如果node不是树状node,那直接就返回了。
* 将树状Node中的元素并行地复制到int数组中,然后再将该数组封装成普通"数组"Node后返回(int类型版本)。
*/
public static Node.OfInt flattenInt(Node.OfInt node) {
// 如果给定的node已经没有子Node,则直接返回
if(node.getChildCount()<=0) {
return node;
}
long size = node.count();
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成int类型的数组以存储node中的数据
int[] array = new int[(int) size];
// 构造"并行复制"任务:将树状node中的元素并行地复制到数组array中
ToArrayTask.OfInt task = new ToArrayTask.OfInt(node, array, 0);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
}
/**
* 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;如果node不是树状node,那直接就返回了。
* 将树状Node中的元素并行地复制到long数组中,然后再将该数组封装成普通"数组"Node后返回(long类型版本)。
*/
public static Node.OfLong flattenLong(Node.OfLong node) {
// 如果给定的node已经没有子Node,则直接返回
if(node.getChildCount()<=0) {
return node;
}
long size = node.count();
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成long类型的数组以存储node中的数据
long[] array = new long[(int) size];
// 构造"并行复制"任务:将树状node中的元素并行地复制到数组array中
ToArrayTask.OfLong task = new ToArrayTask.OfLong(node, array, 0);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
}
/**
* 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;如果node不是树状node,那直接就返回了。
* 将树状Node中的元素并行地复制到double数组中,然后再将该数组封装成普通"数组"Node后返回(double类型版本)。
*/
public static Node.OfDouble flattenDouble(Node.OfDouble node) {
// 如果给定的node已经没有子Node,则直接返回
if(node.getChildCount()<=0) {
return node;
}
long size = node.count();
if(size >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成double类型的数组以存储node中的数据
double[] array = new double[(int) size];
// 构造"并行复制"任务:将树状node中的元素并行地复制到数组array中
ToArrayTask.OfDouble task = new ToArrayTask.OfDouble(node, array, 0);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
}
/*▲ flatten操作:将树状Node转换为普通"数组"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.
*/
/*
* 并行搜集元素,中间依然会经过sink的择取操作(引用类型版本)。
* 将spliterator中的元素并行地收集到generator生成的数组中,然后将该数组封装到Node中返回。
*/
public static <P_IN, P_OUT> Node<P_OUT> collect(PipelineHelper<P_OUT> helper, Spliterator<P_IN> spliterator, boolean flattenTree, IntFunction<P_OUT[]> generator) {
/*
* 初始时,尝试返回spliterator中的元素总量。如果无法获取精确值,则返回-1。
* 当访问过spliterator中的元素后,此处的返回值可能是元素总量,也可能是剩余未访问的元素数量,依实现而定。
*
* 注:通常在流拥有SIZED参数(相当于spliterator有SIZED参数)时可以获取到一个精确值。
*/
long sizeIfKnown = helper.exactOutputSizeIfKnown(spliterator);
// 如果可以获取到一个精确的元素量
if(sizeIfKnown >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(sizeIfKnown >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
// 生成指定容量的数组
P_OUT[] array = generator.apply((int) sizeIfKnown);
// 构造线性"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的数组中
SizedCollectorTask.OfRef<P_IN, P_OUT> task = new SizedCollectorTask.OfRef<>(spliterator, helper, array);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
// 如果spliterator中的数据量未知,则需要使用树状"并行择取"任务
} else {
// 构造树状"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的Node(数组)中
CollectorTask.OfRef<P_IN, P_OUT> task = new CollectorTask.OfRef<>(helper, generator, spliterator);
// 提交树状"并行择取"任务到线程池,并将任务执行结果存入Node后返回
Node<P_OUT> node = task.invoke();
// 返回包含计算结果的Node(视需求将Node降维)
if(flattenTree) {
// 将树状Node中的元素并行地复制到generator生成的数组中,然后再将该数组封装成普通"数组"Node后返回(引用类型版本)
return flatten(node, generator);
}
return 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.
*/
/*
* 并行搜集元素,中间依然会经过sink的择取操作(int类型版本)。
* 将spliterator中的元素并行地收集到int数组中,然后将该数组封装到Node中返回。
*/
public static <P_IN> Node.OfInt collectInt(PipelineHelper<Integer> helper, Spliterator<P_IN> spliterator, boolean flattenTree) {
/*
* 初始时,尝试返回spliterator中的元素总量。如果无法获取精确值,则返回-1。
* 当访问过spliterator中的元素后,此处的返回值可能是元素总量,也可能是剩余未访问的元素数量,依实现而定。
*
* 注:通常在流拥有SIZED参数(相当于spliterator有SIZED参数)时可以获取到一个精确值。
*/
long sizeIfKnown = helper.exactOutputSizeIfKnown(spliterator);
// 处理元素总量一定,但是子结点数量不确定的Node
if(sizeIfKnown >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(sizeIfKnown >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
int[] array = new int[(int) sizeIfKnown];
// 构造线性"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的数组中
SizedCollectorTask.OfInt<P_IN> task = new SizedCollectorTask.OfInt<>(spliterator, helper, array);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
// 如果spliterator中的数据量未知,则需要使用树状"并行择取"任务
} else {
// 构造树状"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的Node(数组)中
CollectorTask.OfInt<P_IN> task = new CollectorTask.OfInt<>(helper, spliterator);
// 提交树状"并行择取"任务到线程池,并将任务执行结果存入Node后返回
Node.OfInt node = task.invoke();
/*
* 如果返回的node是树状node,且要求对返回的node进行降维,
* 则这里需要将树状node中的内容复制到非树状node后返回。
*/
if(flattenTree) {
return flattenInt(node);
}
return 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.
*/
/*
* 并行搜集元素,中间依然会经过sink的择取操作(long类型版本)。
* 将spliterator中的元素并行地收集到long数组中,然后将该数组封装到Node中返回。
*/
public static <P_IN> Node.OfLong collectLong(PipelineHelper<Long> helper, Spliterator<P_IN> spliterator, boolean flattenTree) {
/*
* 初始时,尝试返回spliterator中的元素总量。如果无法获取精确值,则返回-1。
* 当访问过spliterator中的元素后,此处的返回值可能是元素总量,也可能是剩余未访问的元素数量,依实现而定。
*
* 注:通常在流拥有SIZED参数(相当于spliterator有SIZED参数)时可以获取到一个精确值。
*/
long sizeIfKnown = helper.exactOutputSizeIfKnown(spliterator);
// 处理元素总量一定,但是子结点数量不确定的Node
if(sizeIfKnown >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(sizeIfKnown >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
long[] array = new long[(int) sizeIfKnown];
// 构造线性"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的数组中
SizedCollectorTask.OfLong<P_IN> task = new SizedCollectorTask.OfLong<>(spliterator, helper, array);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
// 如果spliterator中的数据量未知,则需要使用树状"并行择取"任务
} else {
// 构造树状"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的Node(数组)中
CollectorTask.OfLong<P_IN> task = new CollectorTask.OfLong<>(helper, spliterator);
// 提交树状"并行择取"任务到线程池,并将任务执行结果存入Node后返回
Node.OfLong node = task.invoke();
/*
* 如果返回的node是树状node,且要求对返回的node进行降维,
* 则这里需要将树状node中的内容复制到非树状node后返回。
*/
if(flattenTree) {
return flattenLong(node);
}
return 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.
*/
/*
* 并行搜集元素,中间依然会经过sink的择取操作(double类型版本)。
* 将spliterator中的元素并行地收集到double数组中,然后将该数组封装到Node中返回。
*/
public static <P_IN> Node.OfDouble collectDouble(PipelineHelper<Double> helper, Spliterator<P_IN> spliterator, boolean flattenTree) {
/*
* 初始时,尝试返回spliterator中的元素总量。如果无法获取精确值,则返回-1。
* 当访问过spliterator中的元素后,此处的返回值可能是元素总量,也可能是剩余未访问的元素数量,依实现而定。
*
* 注:通常在流拥有SIZED参数(相当于spliterator有SIZED参数)时可以获取到一个精确值。
*/
long sizeIfKnown = helper.exactOutputSizeIfKnown(spliterator);
// 处理元素总量一定,但是子结点数量不确定的Node
if(sizeIfKnown >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
if(sizeIfKnown >= MAX_ARRAY_SIZE) {
throw new IllegalArgumentException(BAD_SIZE);
}
double[] array = new double[(int) sizeIfKnown];
// 构造线性"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的数组中
SizedCollectorTask.OfDouble<P_IN> task = new SizedCollectorTask.OfDouble<>(spliterator, helper, array);
// 提交"并行复制"任务到线程池
task.invoke();
// 由给定的数组构造普通"数组"Node(引用类型版本)
return node(array);
// 如果spliterator中的数据量未知,则需要使用树状"并行择取"任务
} else {
// 构造树状"并行择取"任务:将spliterator中的元素并行地择取/筛选到指定的Node(数组)中
CollectorTask.OfDouble<P_IN> task = new CollectorTask.OfDouble<>(helper, spliterator);
// 提交树状"并行择取"任务到线程池,并将任务执行结果存入Node后返回
Node.OfDouble node = task.invoke();
/*
* 如果返回的node是树状node,且要求对返回的node进行降维,
* 则这里需要将树状node中的内容复制到非树状node后返回。
*/
if(flattenTree) {
return flattenDouble(node);
}
return 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 ████████████████████████████████████████████████████████████████████████████████┓ */
/*
* "空"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];
// "空"Node的抽象实现
private abstract static class EmptyNode<T, T_ARR, T_CONS> implements Node<T> {
EmptyNode() {
}
public void forEach(T_CONS consumer) {
}
@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;
}
// "空"Node(引用类型版本)
private static class OfRef<T> extends EmptyNode<T, T[], Consumer<? super T>> {
private OfRef() {
super();
}
@Override
public Spliterator<T> spliterator() {
return Spliterators.emptySpliterator();
}
}
// "空"Node(int类型版本)
private static final class OfInt extends EmptyNode<Integer, int[], IntConsumer> implements Node.OfInt {
OfInt() {
}
@Override
public Spliterator.OfInt spliterator() {
return Spliterators.emptyIntSpliterator();
}
// 返回长度为0的int数组
@Override
public int[] asPrimitiveArray() {
return EMPTY_INT_ARRAY;
}
}
// "空"Node(long类型版本)
private static final class OfLong extends EmptyNode<Long, long[], LongConsumer> implements Node.OfLong {
OfLong() {
}
@Override
public Spliterator.OfLong spliterator() {
return Spliterators.emptyLongSpliterator();
}
// 返回长度为0的long数组
@Override
public long[] asPrimitiveArray() {
return EMPTY_LONG_ARRAY;
}
}
// "空"Node(double类型版本)
private static final class OfDouble extends EmptyNode<Double, double[], DoubleConsumer> implements Node.OfDouble {
OfDouble() {
}
@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(引用类型版本)
private static class ArrayNode<T> implements Node<T> {
final T[] array;
int curSize; // 数组元素数量
// 从已有的数组新建ArrayNode
ArrayNode(T[] array) {
this.array = array;
this.curSize = array.length;
}
// 新建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;
}
// 返回当前Node的流迭代器