-
-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathArray.java
1588 lines (1432 loc) · 50.8 KB
/
Array.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 2021 Vavr, https://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vavr.collection;
import io.vavr.*;
import io.vavr.collection.ArrayModule.Combinations;
import io.vavr.collection.JavaConverters.ListView;
import io.vavr.control.Option;
import java.io.Serializable;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import static io.vavr.collection.JavaConverters.ChangePolicy.IMMUTABLE;
import static io.vavr.collection.JavaConverters.ChangePolicy.MUTABLE;
import static java.util.Arrays.copyOf;
import static java.util.Arrays.sort;
/**
* Array is a Traversable wrapper for {@code Object[]} containing elements of type {@code T}.
*
* @param <T> Component type
*/
public final class Array<T> implements IndexedSeq<T>, Serializable {
private static final long serialVersionUID = 1L;
private static final Array<?> EMPTY = new Array<>(new Object[0]);
private final Object[] delegate;
private Array(Object[] delegate) {
this.delegate = delegate;
}
static <T> Array<T> wrap(Object[] array) {
return (array.length == 0) ? empty() : new Array<>(array);
}
/**
* Returns a {@link java.util.stream.Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link Array}.
*
* @param <T> Component type of the Array.
* @return a {@code Collector} which collects all the input elements into a
* {@link io.vavr.collection.Array}, in encounter order
*/
public static <T> Collector<T, ArrayList<T>, Array<T>> collector() {
return Collections.toListAndThen(Array::ofAll);
}
@SuppressWarnings("unchecked")
public static <T> Array<T> empty() {
return (Array<T>) EMPTY;
}
/**
* Narrows a widened {@code Array<? extends T>} to {@code Array<T>}
* by performing a type-safe cast. This is eligible because immutable/read-only
* collections are covariant.
*
* <pre>{@code
* Array<Double> doubles = Array.of(1.0d, 2.0d);
* Array<Number> numbers = Array.narrow(doubles); // = Array(1.0d, 2.0d)
* }</pre>
*
* @param array An {@code Array}.
* @param <T> Component type of the {@code Array}.
* @return the given {@code array} instance as narrowed type {@code Array<T>}.
*/
@SuppressWarnings("unchecked")
public static <T> Array<T> narrow(Array<? extends T> array) {
return (Array<T>) array;
}
/**
* Returns a singleton {@code Array}, i.e. a {@code Array} of one element.
*
* <pre>{@code
* Array<Double> doubles = Array.of(1.0d); // = Array(1.0d)
* Array<Integer> ints = Array.of(10); // = Array(10)
* }</pre>
*
* @param element An element.
* @param <T> The component type
* @return A new Array instance containing the given element
*/
public static <T> Array<T> of(T element) {
return wrap(new Object[] { element });
}
/**
* Creates an Array of the given elements.
*
* <pre>{@code
* Array<Integer> ints = Array.of(1, 2, 3); // = Array(1, 2, 3)
* Array<Double> doubles = Array.of(1.0d, 2.0d, 3.0d); // = Array(1.0, 2.0, 3.0)
* }</pre>
*
* @param <T> Component type of the Array.
* @param elements Zero or more elements.
* @return An Array containing the given elements in the same order.
* @throws NullPointerException if {@code elements} is null
*/
@SuppressWarnings("varargs")
@SafeVarargs
public static <T> Array<T> of(T... elements) {
Objects.requireNonNull(elements, "elements is null");
return wrap(copyOf(elements, elements.length));
}
/**
* Creates an Array of the given elements.
* <p>
* The resulting Array has the same iteration order as the given iterable of elements
* if the iteration order of the elements is stable.
*
* <pre>{@code
* List<Integer> integers = List.of(1, 2, 3);
* Array<Integer> ints = Array.ofAll(integers); // = Array(1, 2, 3)
* }</pre>
*
* @param <T> Component type of the Array.
* @param elements An Iterable of elements.
* @return An Array containing the given elements in the same order.
* @throws NullPointerException if {@code elements} is null
*/
@SuppressWarnings("unchecked")
public static <T> Array<T> ofAll(Iterable<? extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
if (elements instanceof Array) {
return (Array<T>) elements;
}
if (elements instanceof ListView
&& ((ListView<T, ?>) elements).getDelegate() instanceof Array) {
return (Array<T>) ((ListView<T, ?>) elements).getDelegate();
}
return wrap(toArray(elements));
}
/**
* Creates an Array that contains the elements of the given {@link java.util.stream.Stream}.
*
* <pre>{@code
* List<Integer> integers = List.of(1, 2, 3);
* Array<Integer> ints = Array.ofAll(integers.toJavaStream()); // = Array(1, 2, 3)
* }</pre>
*
* @param javaStream A {@link java.util.stream.Stream}
* @param <T> Component type of the Stream.
* @return An Array containing the given elements in the same order.
*/
public static <T> Array<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
Objects.requireNonNull(javaStream, "javaStream is null");
return wrap(javaStream.toArray());
}
/**
* Creates an Array from boolean values.
*
* <pre>{@code
* Array<Boolean> booleans = Array.ofAll(true, false); // = Array(true, false)
* }</pre>
*
* @param elements boolean values
* @return A new Array of Boolean values
* @throws NullPointerException if elements is null
*/
public static Array<Boolean> ofAll(boolean... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates an Array from byte values.
*
* <pre>{@code
* Array<Byte> bytes = Array.ofAll((byte)1, (byte)2); // = Array(1, 2)
* }</pre>
*
* @param elements byte values
* @return A new Array of Byte values
* @throws NullPointerException if elements is null
*/
public static Array<Byte> ofAll(byte... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates an Array from char values.
*
* <pre>{@code
* Array<Character> characters = Array.ofAll('a', 'e', 'i', 'o', 'u'); // = Array(a, e, i, o, u)
* }</pre>
*
* @param elements char values
* @return A new Array of Character values
* @throws NullPointerException if elements is null
*/
public static Array<Character> ofAll(char... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates an Array from double values.
*
* <pre>{@code
* Array<Double> doubles = Array.ofAll(1d, 2d); // = Array(1.0, 2.0)
* }</pre>
* @param elements double values
* @return A new Array of Double values
* @throws NullPointerException if elements is null
*/
public static Array<Double> ofAll(double... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates an Array from float values.
*
* <pre>{@code
* Array<Float> floats = Array.ofAll(1.1f, 2.2f); // = Array(1.1, 2.2)
* }</pre>
*
* @param elements float values
* @return A new Array of Float values
* @throws NullPointerException if elements is null
*/
public static Array<Float> ofAll(float... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates an Array from int values.
*
* <pre>{@code
* Array<Integer> ints = Array.ofAll(1, 2); // = Array(1, 2)
* }</pre>
*
* @param elements int values
* @return A new Array of Integer values
* @throws NullPointerException if elements is null
*/
public static Array<Integer> ofAll(int... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates an Array from long values.
*
* <pre>{@code
* Array<Long> longValues = Array.ofAll(1L, 2L); // = Array(1, 2)
* }</pre>
*
* @param elements long values
* @return A new Array of Long values
* @throws NullPointerException if elements is null
*/
public static Array<Long> ofAll(long... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Creates an Array from short values.
*
* <pre>{@code
* Array<Short> shortValues = Array.ofAll((short)1, (short)2); // = Array(1, 2)
* }</pre>
*
* @param elements short values
* @return A new Array of Short values
* @throws NullPointerException if elements is null
*/
public static Array<Short> ofAll(short... elements) {
Objects.requireNonNull(elements, "elements is null");
return ofAll(Iterator.ofAll(elements));
}
/**
* Returns an Array containing {@code n} values of a given Function {@code f}
* over a range of integer values from 0 to {@code n - 1}.
*
* <pre>{@code
* Array<Integer> values = Array.tabulate(5, i -> 2 * i); // = Array(0, 2, 4, 6, 8)
* }</pre>
*
* @param <T> Component type of the Array
* @param n The number of elements in the Array
* @param f The Function computing element values
* @return An Array consisting of elements {@code f(0),f(1), ..., f(n - 1)}
* @throws NullPointerException if {@code f} is null
*/
public static <T> Array<T> tabulate(int n, Function<? super Integer, ? extends T> f) {
Objects.requireNonNull(f, "f is null");
return io.vavr.collection.Collections.tabulate(n, f, empty(), Array::of);
}
/**
* Returns an Array containing {@code n} values supplied by a given Supplier {@code s}.
*
* @param <T> Component type of the Array
* @param n The number of elements in the Array
* @param s The Supplier computing element values
* @return An Array of size {@code n}, where each element contains the result supplied by {@code s}.
* @throws NullPointerException if {@code s} is null
*/
public static <T> Array<T> fill(int n, Supplier<? extends T> s) {
Objects.requireNonNull(s, "s is null");
return io.vavr.collection.Collections.fill(n, s, empty(), Array::of);
}
/**
* Returns an Array containing {@code n} times the given {@code element}
*
* <pre>{@code
* Array<Integer> values = Array.fill(5, 20); // = Array(20, 20, 20, 20, 20)
* }</pre>
*
* @param <T> Component type of the Array
* @param n The number of elements in the Array
* @param element The element
* @return An Array of size {@code n}, where each element is the given {@code element}.
*/
public static <T> Array<T> fill(int n, T element) {
return io.vavr.collection.Collections.fillObject(n, element, empty(), Array::of);
}
public static Array<Character> range(char from, char toExclusive) {
return ofAll(Iterator.range(from, toExclusive));
}
public static Array<Character> rangeBy(char from, char toExclusive, int step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
public static Array<Double> rangeBy(double from, double toExclusive, double step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
/**
* Creates an Array of int numbers starting from {@code from}, extending to {@code toExclusive - 1}.
* <p>
* Examples:
* <pre>
* <code>
* Array.range(0, 0) // = Array()
* Array.range(2, 0) // = Array()
* Array.range(-2, 2) // = Array(-2, -1, 0, 1)
* </code>
* </pre>
*
* @param from the first number
* @param toExclusive the last number + 1
* @return a range of int values as specified or the empty range if {@code from >= toExclusive}
*/
public static Array<Integer> range(int from, int toExclusive) {
return ofAll(Iterator.range(from, toExclusive));
}
/**
* Creates an Array of int numbers starting from {@code from}, extending to {@code toExclusive - 1},
* with {@code step}.
* <p>
* Examples:
* <pre>
* <code>
* Array.rangeBy(1, 3, 1) // = Array(1, 2)
* Array.rangeBy(1, 4, 2) // = Array(1, 3)
* Array.rangeBy(4, 1, -2) // = Array(4, 2)
* Array.rangeBy(4, 1, 2) // = Array()
* </code>
* </pre>
*
* @param from the first number
* @param toExclusive the last number + 1
* @param step the step
* @return a range of long values as specified or the empty range if<br>
* {@code from >= toInclusive} and {@code step > 0} or<br>
* {@code from <= toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
public static Array<Integer> rangeBy(int from, int toExclusive, int step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
/**
* Creates an Array of long numbers starting from {@code from}, extending to {@code toExclusive - 1}.
* <p>
* Examples:
* <pre>
* <code>
* Array.range(0L, 0L) // = Array()
* Array.range(2L, 0L) // = Array()
* Array.range(-2L, 2L) // = Array(-2L, -1L, 0L, 1L)
* </code>
* </pre>
*
* @param from the first number
* @param toExclusive the last number + 1
* @return a range of long values as specified or the empty range if {@code from >= toExclusive}
*/
public static Array<Long> range(long from, long toExclusive) {
return ofAll(Iterator.range(from, toExclusive));
}
/**
* Creates an Array of long numbers starting from {@code from}, extending to {@code toExclusive - 1},
* with {@code step}.
* <p>
* Examples:
* <pre>
* <code>
* Array.rangeBy(1L, 3L, 1L) // = Array(1L, 2L)
* Array.rangeBy(1L, 4L, 2L) // = Array(1L, 3L)
* Array.rangeBy(4L, 1L, -2L) // = Array(4L, 2L)
* Array.rangeBy(4L, 1L, 2L) // = Array()
* </code>
* </pre>
*
* @param from the first number
* @param toExclusive the last number + 1
* @param step the step
* @return a range of long values as specified or the empty range if<br>
* {@code from >= toInclusive} and {@code step > 0} or<br>
* {@code from <= toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
public static Array<Long> rangeBy(long from, long toExclusive, long step) {
return ofAll(Iterator.rangeBy(from, toExclusive, step));
}
public static Array<Character> rangeClosed(char from, char toInclusive) {
return ofAll(Iterator.rangeClosed(from, toInclusive));
}
public static Array<Character> rangeClosedBy(char from, char toInclusive, int step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
public static Array<Double> rangeClosedBy(double from, double toInclusive, double step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
/**
* Creates an Array of int numbers starting from {@code from}, extending to {@code toInclusive}.
* <p>
* Examples:
* <pre>
* <code>
* Array.rangeClosed(0, 0) // = Array(0)
* Array.rangeClosed(2, 0) // = Array()
* Array.rangeClosed(-2, 2) // = Array(-2, -1, 0, 1, 2)
* </code>
* </pre>
*
* @param from the first number
* @param toInclusive the last number
* @return a range of int values as specified or the empty range if {@code from > toInclusive}
*/
public static Array<Integer> rangeClosed(int from, int toInclusive) {
return ofAll(Iterator.rangeClosed(from, toInclusive));
}
/**
* Creates an Array of int numbers starting from {@code from}, extending to {@code toInclusive},
* with {@code step}.
* <p>
* Examples:
* <pre>
* <code>
* Array.rangeClosedBy(1, 3, 1) // = Array(1, 2, 3)
* Array.rangeClosedBy(1, 4, 2) // = Array(1, 3)
* Array.rangeClosedBy(4, 1, -2) // = Array(4, 2)
* Array.rangeClosedBy(4, 1, 2) // = Array()
* </code>
* </pre>
*
* @param from the first number
* @param toInclusive the last number
* @param step the step
* @return a range of int values as specified or the empty range if<br>
* {@code from > toInclusive} and {@code step > 0} or<br>
* {@code from < toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
public static Array<Integer> rangeClosedBy(int from, int toInclusive, int step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
/**
* Creates an Array of long numbers starting from {@code from}, extending to {@code toInclusive}.
* <p>
* Examples:
* <pre>
* <code>
* Array.rangeClosed(0L, 0L) // = Array(0L)
* Array.rangeClosed(2L, 0L) // = Array()
* Array.rangeClosed(-2L, 2L) // = Array(-2L, -1L, 0L, 1L, 2L)
* </code>
* </pre>
*
* @param from the first number
* @param toInclusive the last number
* @return a range of long values as specified or the empty range if {@code from > toInclusive}
*/
public static Array<Long> rangeClosed(long from, long toInclusive) {
return ofAll(Iterator.rangeClosed(from, toInclusive));
}
/**
* Creates an Array of long numbers starting from {@code from}, extending to {@code toInclusive},
* with {@code step}.
* <p>
* Examples:
* <pre>
* <code>
* Array.rangeClosedBy(1L, 3L, 1L) // = Array(1L, 2L, 3L)
* Array.rangeClosedBy(1L, 4L, 2L) // = Array(1L, 3L)
* Array.rangeClosedBy(4L, 1L, -2L) // = Array(4L, 2L)
* Array.rangeClosedBy(4L, 1L, 2L) // = Array()
* </code>
* </pre>
*
* @param from the first number
* @param toInclusive the last number
* @param step the step
* @return a range of int values as specified or the empty range if<br>
* {@code from > toInclusive} and {@code step > 0} or<br>
* {@code from < toInclusive} and {@code step < 0}
* @throws IllegalArgumentException if {@code step} is zero
*/
public static Array<Long> rangeClosedBy(long from, long toInclusive, long step) {
return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
}
/**
* Transposes the rows and columns of an {@link Array} matrix.
*
* @param matrix to be transposed.
* @return a transposed {@link Array} matrix.
* @throws IllegalArgumentException if the row lengths of {@code matrix} differ.
* <p>
* ex: {@code
* Array.transpose(Array(Array(1,2,3), Array(4,5,6))) → Array(Array(1,4), Array(2,5), Array(3,6))
* }
*/
static <T> Array<Array<T>> transpose(Array<Array<T>> matrix) {
return io.vavr.collection.Collections.transpose(matrix, Array::ofAll, Array::of);
}
/**
* Creates an Array from a seed value and a function.
* The function takes the seed at first.
* The function should return {@code None} when it's
* done generating the Array, otherwise {@code Some} {@code Tuple}
* of the element for the next call and the value to add to the
* resulting Array.
* <p>
* Example:
* <pre>
* <code>
* Array.unfoldRight(10, x -> x == 0
* ? Option.none()
* : Option.of(new Tuple2<gt;(x, x-1)));
* // Array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1))
* </code>
* </pre>
*
* @param <T> type of seeds
* @param <U> type of unfolded values
* @param seed the start value for the iteration
* @param f the function to get the next step of the iteration
* @return an Array with the values built up by the iteration
* @throws NullPointerException if {@code f} is null
*/
public static <T, U> Array<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f) {
return Iterator.unfoldRight(seed, f).toArray();
}
/**
* Creates an Array from a seed value and a function.
* The function takes the seed at first.
* The function should return {@code None} when it's
* done generating the list, otherwise {@code Some} {@code Tuple}
* of the value to add to the resulting list and
* the element for the next call.
* <p>
* Example:
* <pre>
* <code>
* Array.unfoldLeft(10, x -> x == 0
* ? Option.none()
* : Option.of(new Tuple2<gt;(x-1, x)));
* // Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
* </code>
* </pre>
*
* @param <T> type of seeds
* @param <U> type of unfolded values
* @param seed the start value for the iteration
* @param f the function to get the next step of the iteration
* @return an Array with the values built up by the iteration
* @throws NullPointerException if {@code f} is null
*/
public static <T, U> Array<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
return Iterator.unfoldLeft(seed, f).toArray();
}
/**
* Creates an Array from a seed value and a function.
* The function takes the seed at first.
* The function should return {@code None} when it's
* done generating the list, otherwise {@code Some} {@code Tuple}
* of the value to add to the resulting list and
* the element for the next call.
* <p>
* Example:
* <pre>
* <code>
* Array.unfold(10, x -> x == 0
* ? Option.none()
* : Option.of(new Tuple2<gt;(x-1, x)));
* // Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
* </code>
* </pre>
*
* @param <T> type of seeds and unfolded values
* @param seed the start value for the iteration
* @param f the function to get the next step of the iteration
* @return an Array with the values built up by the iteration
* @throws NullPointerException if {@code f} is null
*/
public static <T> Array<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
return Iterator.unfold(seed, f).toArray();
}
@Override
public Array<T> append(T element) {
final Object[] copy = copyOf(delegate, delegate.length + 1);
copy[delegate.length] = element;
return wrap(copy);
}
@Override
public Array<T> appendAll(Iterable<? extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
if (isEmpty() && elements instanceof Array) {
@SuppressWarnings("unchecked")
final Array<T> array = (Array<T>) elements;
return array;
}
final Object[] source = toArray(elements);
if (source.length == 0) {
return this;
} else {
final Object[] arr = copyOf(delegate, delegate.length + source.length);
System.arraycopy(source, 0, arr, delegate.length, source.length);
return wrap(arr);
}
}
@Override
public java.util.List<T> asJava() {
return JavaConverters.asJava(this, IMMUTABLE);
}
@Override
public Array<T> asJava(Consumer<? super java.util.List<T>> action) {
return Collections.asJava(this, action, IMMUTABLE);
}
@Override
public java.util.List<T> asJavaMutable() {
return JavaConverters.asJava(this, MUTABLE);
}
@Override
public Array<T> asJavaMutable(Consumer<? super java.util.List<T>> action) {
return Collections.asJava(this, action, MUTABLE);
}
@Override
public <R> Array<R> collect(PartialFunction<? super T, ? extends R> partialFunction) {
return ofAll(iterator().<R> collect(partialFunction));
}
@Override
public boolean hasDefiniteSize() {
return true;
}
/**
* An {@code Array} is computed synchronously.
*
* @return false
*/
@Override
public boolean isAsync() {
return false;
}
/**
* An {@code Array} is computed eagerly.
*
* @return false
*/
@Override
public boolean isLazy() {
return false;
}
@Override
public boolean isTraversableAgain() {
return true;
}
@SuppressWarnings("unchecked")
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < delegate.length;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return (T) delegate[index++];
}
};
}
@Override
public Array<Array<T>> combinations() {
return rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity());
}
@Override
public Array<Array<T>> combinations(int k) {
return Combinations.apply(this, Math.max(k, 0));
}
@Override
public Iterator<Array<T>> crossProduct(int power) {
return io.vavr.collection.Collections.crossProduct(empty(), this, power);
}
@SuppressWarnings("unchecked")
@Override
public T apply(Integer index) {
return (T) delegate[index];
}
@Override
public Array<T> distinct() {
return distinctBy(Function.identity());
}
@Override
public Array<T> distinctBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator, "comparator is null");
final java.util.Set<T> seen = new java.util.TreeSet<>(comparator);
return filter(seen::add);
}
@Override
public <U> Array<T> distinctBy(Function<? super T, ? extends U> keyExtractor) {
Objects.requireNonNull(keyExtractor, "keyExtractor is null");
final java.util.Set<U> seen = new java.util.HashSet<>();
return filter(t -> seen.add(keyExtractor.apply(t)));
}
@Override
public Array<T> drop(int n) {
if (n <= 0) {
return this;
} else if (n >= length()) {
return empty();
} else {
final Object[] arr = new Object[delegate.length - n];
System.arraycopy(delegate, n, arr, 0, arr.length);
return wrap(arr);
}
}
@Override
public Array<T> dropUntil(Predicate<? super T> predicate) {
return io.vavr.collection.Collections.dropUntil(this, predicate);
}
@Override
public Array<T> dropWhile(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return dropUntil(predicate.negate());
}
@Override
public Array<T> dropRight(int n) {
if (n <= 0) {
return this;
} else if (n >= length()) {
return empty();
} else {
return wrap(copyOf(delegate, delegate.length - n));
}
}
@Override
public Array<T> dropRightUntil(Predicate<? super T> predicate) {
return io.vavr.collection.Collections.dropRightUntil(this, predicate);
}
@Override
public Array<T> dropRightWhile(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return dropRightUntil(predicate.negate());
}
@Override
public Array<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
final java.util.List<T> list = new ArrayList<>();
for (T t : this) {
if (predicate.test(t)) {
list.add(t);
}
}
if (list.isEmpty()) {
return empty();
} else if (list.size() == size()) {
return this;
} else {
return wrap(list.toArray());
}
}
@Override
public Array<T> filterNot(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return Collections.filterNot(this, predicate);
}
@Override
public <U> Array<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
if (isEmpty()) {
return empty();
} else {
final java.util.List<U> list = new ArrayList<>();
for (T t : this) {
for (U u : mapper.apply(t)) {
list.add(u);
}
}
return wrap(list.toArray());
}
}
@Override
public <C> Map<C, Array<T>> groupBy(Function<? super T, ? extends C> classifier) {
return io.vavr.collection.Collections.groupBy(this, classifier, Array::ofAll);
}
@Override
public Iterator<Array<T>> grouped(int size) {
return sliding(size, size);
}
@SuppressWarnings("unchecked")
@Override
public T head() {
if (isEmpty()) {
throw new NoSuchElementException("head on empty Array");
} else {
return (T) delegate[0];
}
}
@Override
public int indexOf(T element, int from) {
for (int i = from; i < length(); i++) {
if (Objects.equals(get(i), element)) {
return i;
}
}
return -1;
}
@Override
public Array<T> init() {
if (isEmpty()) {
throw new UnsupportedOperationException("init of empty Array");
}
return dropRight(1);
}
@Override
public Option<Array<T>> initOption() {
return isEmpty() ? Option.none() : Option.some(init());
}
@Override
public boolean isEmpty() {
return delegate.length == 0;
}
private Object readResolve() {
return isEmpty() ? EMPTY : this;
}
@Override
public Array<T> insert(int index, T element) {
if (index < 0 || index > length()) {
throw new IndexOutOfBoundsException("insert(" + index + ", e) on Array of length " + length());
}
final Object[] arr = new Object[delegate.length + 1];
System.arraycopy(delegate, 0, arr, 0, index);
arr[index] = element;
System.arraycopy(delegate, index, arr, index + 1, delegate.length - index);
return wrap(arr);
}
@Override
public Array<T> insertAll(int index, Iterable<? extends T> elements) {
if (index < 0 || index > length()) {
throw new IndexOutOfBoundsException("insert(" + index + ", e) on Array of length " + length());
}
if (isEmpty() && elements instanceof Array) {
@SuppressWarnings("unchecked")
final Array<T> array = (Array<T>) elements;
return array;
}
final Object[] list = toArray(elements);
if (list.length == 0) {
return this;
} else {
final Object[] arr = new Object[delegate.length + list.length];
System.arraycopy(delegate, 0, arr, 0, index);
System.arraycopy(list, 0, arr, index, list.length);
System.arraycopy(delegate, index, arr, index + list.length, delegate.length - index);
return wrap(arr);
}
}
@Override
public Array<T> intersperse(T element) {
if (delegate.length <= 1) {
return this;
} else {
final Object[] arr = new Object[delegate.length * 2 - 1];
for (int i = 0; i < delegate.length; i++) {
arr[i * 2] = delegate[i];
if (i > 0) {
arr[i * 2 - 1] = element;
}
}
return wrap(arr);
}
}
@Override
public int lastIndexOf(T element, int end) {
for (int i = Math.min(end, length() - 1); i >= 0; i--) {
if (Objects.equals(get(i), element)) {
return i;
}
}
return -1;
}
@Override
public int length() {
return delegate.length;