-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
1245 lines (937 loc) · 24.4 KB
/
slice.go
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
package parlo
import (
"runtime"
"sync"
"sync/atomic"
"github.com/mahdi-shojaee/parlo/internal/cmp"
"github.com/mahdi-shojaee/parlo/internal/constraints"
"github.com/mahdi-shojaee/parlo/internal/slices"
"github.com/mahdi-shojaee/parlo/internal/utils"
)
type isSortedChunkResult[S ~[]E, E any] struct {
isSorted bool
chunkStartIndex int
chunk S
}
type node[S ~[]E, E any] struct {
sortedChunk S
chunkIndex int
}
// Filter applies a predicate function to each element of the input slice
// and returns a new slice containing only the elements for which the predicate returns true.
func Filter[S ~[]E, E any](slice S, predicate func(item E, index int) bool) S {
result := make(S, 0)
for i := 0; i < len(slice); i++ {
if predicate(slice[i], i) {
result = append(result, slice[i])
}
}
return result
}
// ParFilter applies a predicate function to each element of the input slice in parallel
// and returns a new slice containing only the elements for which the predicate returns true.
func ParFilter[S ~[]E, E any](slice S, predicate func(item E, index int) bool) S {
chunkResults := Do(slice, 0, func(chunk S, _, chunkStartIndex int) S {
r := make(S, 0)
for i := 0; i < len(chunk); i++ {
if predicate(chunk[i], chunkStartIndex+i) {
r = append(r, chunk[i])
}
}
return r
})
size := 0
for _, chunkResult := range chunkResults {
size += len(chunkResult)
}
result := make(S, 0, size)
for _, chunkResult := range chunkResults {
result = append(result, chunkResult...)
}
return result
}
func parCopyChunks[S ~[]E, E any](dest S, chunks []S) {
destIndex := 0
var wg sync.WaitGroup
wg.Add(len(chunks))
for _, chunk := range chunks {
go func(destIndex int, chunk S) {
defer wg.Done()
copy(dest[destIndex:], chunk)
}(destIndex, chunk)
destIndex += len(chunk)
}
wg.Wait()
}
// Equal checks if two slices are equal.
func Equal[S ~[]E, E comparable](a S, b S) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
// ParEqual checks if two slices are equal in parallel.
func ParEqual[S ~[]E, E comparable](a S, b S) bool {
if len(a) != len(b) {
return false
}
result := Do(a, 0, func(chunk S, _, chunkStartIndex int) bool {
other := b[chunkStartIndex : chunkStartIndex+len(chunk)]
return Equal(chunk, other)
})
for _, r := range result {
if !r {
return false
}
}
return true
}
// EqualFunc checks if two slices are equal according to a comparison function.
func EqualFunc[S ~[]E, E any](a S, b S, eq func(a, b E) bool) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if !eq(a[i], b[i]) {
return false
}
}
return true
}
// ParEqualFunc checks if two slices are equal in parallel according to a comparison function.
func ParEqualFunc[S ~[]E, E any](a S, b S, eq func(a, b E) bool) bool {
if len(a) != len(b) {
return false
}
result := Do(a, 0, func(chunk S, _, chunkStartIndex int) bool {
other := b[chunkStartIndex : chunkStartIndex+len(chunk)]
return EqualFunc(chunk, other, eq)
})
for _, r := range result {
if !r {
return false
}
}
return true
}
func isSortedChunksSorted[S ~[]E, E constraints.Ordered](sortedChunks []S) bool {
chunks := Filter(sortedChunks, func(chunk S, index int) bool {
return len(chunk) > 0
})
if len(chunks) < 2 {
return true
}
for i := 1; i < len(chunks); i++ {
if chunks[i-1][len(chunks[i-1])-1] > chunks[i][0] {
return false
}
}
return true
}
func isSortedChunksSortedBy[S ~[]E, E any](sortedChunks []S, cmp func(a, b E) int) bool {
chunks := Filter(sortedChunks, func(chunk S, index int) bool {
return len(chunk) > 0
})
if len(chunks) < 2 {
return true
}
for i := 1; i < len(chunks); i++ {
if cmp(chunks[i-1][len(chunks[i-1])-1], chunks[i][0]) > 0 {
return false
}
}
return true
}
// IsSorted checks if the input slice is sorted in ascending order.
// It returns true if the slice is sorted, false otherwise.
func IsSorted[S ~[]E, E constraints.Ordered](slice S) bool {
if len(slice) <= 1 {
return true
}
item := slice[0]
for _, v := range slice[1:] {
if item > v {
return false
}
item = v
}
return true
}
// IsSortedFunc checks if the input slice is sorted according to the provided comparison function.
// The cmp function should return a negative integer if a is considered less than b,
// a positive integer if a is considered greater than b, and zero if a is considered equal to b.
// It returns true if the slice is sorted, false otherwise.
func IsSortedFunc[S ~[]E, E any](slice S, cmp func(a, b E) int) bool {
if len(slice) <= 1 {
return true
}
item := slice[0]
for _, v := range slice[1:] {
if cmp(item, v) > 0 {
return false
}
item = v
}
return true
}
// ParIsSorted checks if the input slice is sorted in ascending order in parallel.
// It returns true if the slice is sorted, false otherwise.
func ParIsSorted[S ~[]E, E constraints.Ordered](slice S) bool {
if len(slice) <= 1 {
return true
}
var end uint32 = 0
results := Do(slice, 0, func(chunk S, _, chunkStartIndex int) isSortedChunkResult[S, E] {
if len(chunk) <= 1 {
return isSortedChunkResult[S, E]{
isSorted: true,
chunkStartIndex: chunkStartIndex,
chunk: chunk,
}
}
prev := chunk[0]
isSorted := true
for _, v := range chunk[1:] {
if atomic.LoadUint32(&end) != 0 {
isSorted = false
break
}
if prev > v {
atomic.StoreUint32(&end, 1)
isSorted = false
break
}
prev = v
}
return isSortedChunkResult[S, E]{
isSorted: isSorted,
chunkStartIndex: chunkStartIndex,
chunk: chunk,
}
})
var prevChunkLastItem E
prevChunkLastItemIsSet := false
for _, r := range results {
if len(r.chunk) == 0 {
continue
}
if !r.isSorted {
return false
}
if prevChunkLastItemIsSet && prevChunkLastItem > r.chunk[0] {
return false
}
prevChunkLastItem = r.chunk[len(r.chunk)-1]
prevChunkLastItemIsSet = true
}
return true
}
// ParIsSortedFunc checks if the input slice is sorted according to the provided comparison function in parallel.
// The cmp function should return a negative integer if a is considered less than b,
// a positive integer if a is considered greater than b, and zero if a is considered equal to b.
// It returns true if the slice is sorted, false otherwise.
func ParIsSortedFunc[S ~[]E, E any](slice S, cmp func(a, b E) int) bool {
if len(slice) <= 1 {
return true
}
var end uint32 = 0
results := Do(slice, 0, func(chunk S, _, chunkStartIndex int) isSortedChunkResult[S, E] {
if len(chunk) <= 1 {
return isSortedChunkResult[S, E]{
isSorted: true,
chunkStartIndex: chunkStartIndex,
chunk: chunk,
}
}
prev := chunk[0]
isSorted := true
for _, v := range chunk[1:] {
if atomic.LoadUint32(&end) != 0 {
isSorted = false
break
}
if cmp(prev, v) > 0 {
atomic.StoreUint32(&end, 1)
isSorted = false
break
}
prev = v
}
return isSortedChunkResult[S, E]{
isSorted: isSorted,
chunkStartIndex: chunkStartIndex,
chunk: chunk,
}
})
var prevChunkLastItem E
prevChunkLastItemIsSet := false
for _, r := range results {
if len(r.chunk) == 0 {
continue
}
if !r.isSorted {
return false
}
if prevChunkLastItemIsSet && cmp(prevChunkLastItem, r.chunk[0]) > 0 {
return false
}
prevChunkLastItem = r.chunk[len(r.chunk)-1]
prevChunkLastItemIsSet = true
}
return true
}
// IsSortedDesc checks if the input slice is sorted in descending order.
// It returns true if the slice is sorted, false otherwise.
func IsSortedDesc[S ~[]E, E constraints.Ordered](slice S) bool {
if len(slice) <= 1 {
return true
}
item := slice[0]
for _, v := range slice[1:] {
if item < v {
return false
}
item = v
}
return true
}
// ParIsSortedDesc checks if the input slice is sorted in descending order in parallel.
// It returns true if the slice is sorted, false otherwise.
func ParIsSortedDesc[S ~[]E, E constraints.Ordered](slice S) bool {
if len(slice) <= 1 {
return true
}
var end uint32 = 0
results := Do(slice, 0, func(chunk S, _, chunkStartIndex int) isSortedChunkResult[S, E] {
if len(chunk) <= 1 {
return isSortedChunkResult[S, E]{
isSorted: true,
chunkStartIndex: chunkStartIndex,
chunk: chunk,
}
}
prev := chunk[0]
isSorted := true
for _, v := range chunk[1:] {
if atomic.LoadUint32(&end) != 0 {
isSorted = false
break
}
if prev < v {
atomic.StoreUint32(&end, 1)
isSorted = false
break
}
prev = v
}
return isSortedChunkResult[S, E]{
isSorted: isSorted,
chunkStartIndex: chunkStartIndex,
chunk: chunk,
}
})
var prevChunkLastItem E
prevChunkLastItemIsSet := false
for _, r := range results {
if len(r.chunk) == 0 {
continue
}
if !r.isSorted {
return false
}
if prevChunkLastItemIsSet && prevChunkLastItem < r.chunk[0] {
return false
}
prevChunkLastItem = r.chunk[len(r.chunk)-1]
prevChunkLastItemIsSet = true
}
return true
}
// Reverse reverses the elements of the input slice in place.
func Reverse[S ~[]E, E any](slice S) {
l := len(slice)
if l <= 1 {
return
}
end := l / 2
for i, j := 0, l-1; i < end; i, j = i+1, j-1 {
slice[i], slice[j] = slice[j], slice[i]
}
}
// ParReverse reverses the elements of the input slice in parallel.
func ParReverse[S ~[]E, E any](slice S) {
l := len(slice)
if l <= 1 {
return
}
// In practice, 2 is the best number of threads for this function.
numThreads := 2
chunkSize := l / numThreads / 2
var wg sync.WaitGroup
for i, startIndex := 0, 0; i < numThreads-1; i, startIndex = i+1, startIndex+chunkSize {
leftSlice := slice[startIndex : startIndex+chunkSize]
rightSlice := slice[l-startIndex-chunkSize : l-startIndex]
wg.Add(1)
go func() {
defer wg.Done()
for j, k := 0, len(rightSlice)-1; j < chunkSize; j, k = j+1, k-1 {
leftSlice[j], rightSlice[k] = rightSlice[k], leftSlice[j]
}
}()
}
Reverse(slice[(numThreads-1)*chunkSize : l-(numThreads-1)*chunkSize])
wg.Wait()
}
// Sort sorts the given slice in ascending order. The slice must contain elements that satisfy the constraints.Ordered interface.
func Sort[S ~[]E, E constraints.Ordered](slice S) {
slices.Sort(slice)
}
// SortFunc sorts the given slice using the provided comparison function.
// The comparison function should return a negative number when a < b,
// a positive number when a > b, and zero when a == b.
func SortFunc[S ~[]E, E any](slice S, cmp func(a, b E) int) {
slices.SortFunc(slice, cmp)
}
// SortStableFunc sorts the given slice using the provided comparison function.
// It maintains the relative order of equal elements, making it a stable sort.
// The comparison function should return a negative number when a < b,
// a positive number when a > b, and zero when a == b.
func SortStableFunc[S ~[]E, E any](slice S, cmp func(a, b E) int) {
slices.SortStableFunc(slice, cmp)
}
// ParSort performs a parallel sort on the given slice in ascending order.
// The slice must contain elements that satisfy the constraints.Ordered interface.
func ParSort[S ~[]E, E constraints.Ordered](slice S) {
isSortedDesc := ParIsSortedDesc(slice)
if isSortedDesc {
ParReverse(slice)
return
}
numThreads := runtime.GOMAXPROCS(0)
chunks := Do(slice, numThreads, func(chunk S, _, _ int) S {
if IsSorted(chunk) {
return chunk
}
if IsSortedDesc(chunk) {
Reverse(chunk)
return chunk
}
Sort(chunk)
return chunk
})
chunks = Filter(chunks, func(chunk S, index int) bool {
return len(chunk) > 0
})
if isSortedChunksSorted(chunks) {
return
}
chunksCopy := Do(chunks, len(chunks), func(chunk []S, _, _ int) S {
return slices.Clone(chunk[0])
})
slices.SortFunc(chunksCopy, func(a, b S) int {
return cmp.Compare(a[0], b[0])
})
if isSortedChunksSorted(chunksCopy) {
parCopyChunks(slice, chunksCopy)
return
}
parMergeByMerge(chunksCopy, slice, minHeapMerge[S, E])
}
// ParSortFunc performs a parallel sort on the given slice using a custom comparison function.
func ParSortFunc[S ~[]E, E any](slice S, cmp func(a, b E) int) {
isSortedDesc := ParIsSortedFunc(slice, func(a E, b E) int { return cmp(b, a) })
if isSortedDesc {
ParReverse(slice)
return
}
numThreads := runtime.GOMAXPROCS(0)
chunks := Do(slice, numThreads, func(chunk S, _, _ int) S {
if IsSortedFunc(chunk, cmp) {
return chunk
}
if IsSortedFunc(chunk, func(a E, b E) int { return cmp(b, a) }) {
Reverse(chunk)
return chunk
}
SortFunc(chunk, cmp)
return chunk
})
chunks = Filter(chunks, func(chunk S, index int) bool {
return len(chunk) > 0
})
if isSortedChunksSortedBy(chunks, cmp) {
return
}
chunksCopy := Do(chunks, len(chunks), func(chunk []S, _, _ int) S {
return slices.Clone(chunk[0])
})
slices.SortFunc(chunksCopy, func(a, b S) int {
return cmp(a[0], b[0])
})
if isSortedChunksSortedBy(chunksCopy, cmp) {
parCopyChunks(slice, chunksCopy)
return
}
parMergeByMergeFunc(chunksCopy, slice, minHeapMergeFunc[S, E], cmp)
}
// ParSortStableFunc performs a parallel stable sort on the given slice using a custom comparison function.
// It maintains the relative order of equal elements while sorting in parallel for improved performance.
func ParSortStableFunc[S ~[]E, E any](slice S, cmp func(a, b E) int) {
parSortStableByMerge(slice, minHeapMergeStableFunc[S, E], cmp)
}
func parSortStableByMerge[S ~[]E, E any](
slice S,
merge func(sortedChunks []S, dest S, cmp func(a, b E) int),
cmp func(a, b E) int,
) {
numThreads := utils.NumThreads(runtime.GOMAXPROCS(0))
chunks := Do(slice, numThreads, func(chunk S, _, _ int) S {
SortStableFunc(chunk, cmp)
return chunk
})
chunks = Filter(chunks, func(chunk S, index int) bool {
return len(chunk) > 0
})
if isSortedChunksSortedBy(chunks, cmp) {
return
}
chunksCopy := Do(chunks, len(chunks), func(chunk []S, _, _ int) S {
return slices.Clone(chunk[0])
})
// Cannot use parallel merge because it's not stable.
merge(chunksCopy, slice, cmp)
}
func minHeapMerge[S ~[]E, E constraints.Ordered](sortedChunks []S, dest S) {
if len(dest) == 0 {
return
}
h := slices.Clone(sortedChunks)
size := len(h)
destIndex := 0
// For initializing the min-heap, a sorting approach is used instead of heapify.
// While heapify is generally more efficient for building a heap, the small input size
// in this context (number of CPU cores) makes sorting a suitable and simpler option.
slices.SortFunc(h, func(a, b S) int {
if len(a) == 0 {
return 1
}
if len(b) == 0 {
return -1
}
return cmp.Compare(a[0], b[0])
})
less := func(i, j int) bool {
if len(h[i]) == 0 {
return false
}
if len(h[j]) == 0 {
return true
}
return h[i][0] < h[j][0]
}
sortedChunksLeft := size
var min *S
for {
min = &h[0]
dest[destIndex] = (*min)[0]
if destIndex++; destIndex == len(dest) {
break
}
*min = (*min)[1:]
if len(*min) == 0 {
sortedChunksLeft--
}
// down procedure
i := 0
for {
left := 2*i + 1
if left >= size {
break
}
min := left
if right := left + 1; right < size && less(right, left) {
min = right
}
if !less(min, i) {
break
}
h[min], h[i] = h[i], h[min]
i = min
}
if sortedChunksLeft == 1 {
break
}
}
copy(dest[destIndex:], *min)
}
func minHeapMergeFunc[S ~[]E, E any](sortedChunks []S, dest S, cmp func(a, b E) int) {
if len(dest) == 0 {
return
}
h := slices.Clone(sortedChunks)
size := len(h)
destIndex := 0
// For initializing the min-heap, a sorting approach is used instead of heapify.
// While heapify is generally more efficient for building a heap, the small input size
// in this context (number of CPU cores) makes sorting a suitable and simpler option.
slices.SortStableFunc(h, func(a, b S) int {
if len(a) == 0 {
return 1
}
if len(b) == 0 {
return -1
}
return cmp(a[0], b[0])
})
less := func(i, j int) bool {
if len(h[i]) == 0 {
return false
}
if len(h[j]) == 0 {
return true
}
return cmp(h[i][0], h[j][0]) < 0
}
sortedChunksLeft := size
var min *S
for {
min = &h[0]
dest[destIndex] = (*min)[0]
if destIndex++; destIndex == len(dest) {
break
}
*min = (*min)[1:]
if len(*min) == 0 {
sortedChunksLeft--
}
// down procedure
i := 0
for {
left := 2*i + 1
if left >= size {
break
}
min := left
if right := left + 1; right < size && less(right, left) {
min = right
}
if !less(min, i) {
break
}
h[min], h[i] = h[i], h[min]
i = min
}
if sortedChunksLeft == 1 {
break
}
}
copy(dest[destIndex:], *min)
}
func minHeapMergeStableFunc[S ~[]E, E any](sortedChunks []S, dest S, cmp func(a, b E) int) {
if len(dest) == 0 {
return
}
h := make([]node[S, E], len(sortedChunks))
for chunkIndex, sortedChunk := range sortedChunks {
h[chunkIndex] = node[S, E]{
sortedChunk: sortedChunk,
chunkIndex: chunkIndex,
}
}
size := len(h)
destIndex := 0
// For initializing the min-heap, a sorting approach is used instead of heapify.
// While heapify is generally more efficient for building a heap, the small input size
// in this context (number of CPU cores) makes sorting a suitable and simpler option.
slices.SortStableFunc(h, func(a, b node[S, E]) int {
if len(a.sortedChunk) == 0 {
return 1
}
if len(b.sortedChunk) == 0 {
return -1
}
return cmp(a.sortedChunk[0], b.sortedChunk[0])
})
less := func(i, j int) bool {
if len(h[i].sortedChunk) == 0 {
return false
}
if len(h[j].sortedChunk) == 0 {
return true
}
r := cmp(h[i].sortedChunk[0], h[j].sortedChunk[0])
return (r < 0) || (r == 0 && h[i].chunkIndex < h[j].chunkIndex)
}
sortedChunksLeft := size
var min *S
for {
min = &h[0].sortedChunk
dest[destIndex] = (*min)[0]
if destIndex++; destIndex == len(dest) {
break
}
*min = (*min)[1:]
if len(*min) == 0 {
sortedChunksLeft--
}
// down procedure
i := 0
for {
left := 2*i + 1
if left >= size {
break
}
min := left
if right := left + 1; right < size && less(right, left) {
min = right
}
if !less(min, i) {
break
}
h[min], h[i] = h[i], h[min]
i = min
}
if sortedChunksLeft == 1 {
break
}
}
copy(dest[destIndex:], *min)
}
// Is not stable
func parMergeByMerge[S ~[]E, E constraints.Ordered](
sortedChunks []S,
dest S,
merge func(sortedChunks []S, dest S),
) {
if len(sortedChunks) == 0 {
return
}
numThreads := utils.NumThreads(runtime.GOMAXPROCS(0))
if numThreads == 1 {
merge(sortedChunks, dest)
return
}
l := len(sortedChunks)
bigChunkIndex := 0
for i := 1; i < l; i++ {
if len(sortedChunks[i]) > len(sortedChunks[bigChunkIndex]) {
bigChunkIndex = i
}
}
bigChunk := sortedChunks[bigChunkIndex]
if len(bigChunk) == 0 {
return
}
otherChunks := slices.Concat(sortedChunks[0:bigChunkIndex], sortedChunks[bigChunkIndex+1:])
sortedChunks = slices.Concat([]S{sortedChunks[bigChunkIndex]}, otherChunks)
bigChunkIndex = 0
splitSize := len(bigChunk) / numThreads
lastIndexes := make([]int, l)
lastDestIndex := 0
var wg sync.WaitGroup
for i := 0; i < numThreads-1; i++ {
splitIndex := (i + 1) * splitSize
a := bigChunk[splitIndex]
indexes := make([]int, l)
indexes[0] = splitIndex
for chunkIndex, chunk := range otherChunks {
bSearchIndex, _ := slices.BinarySearch(chunk, a)
indexes[chunkIndex+1] = bSearchIndex
}
t := 0
chunks := make([]S, l)
for k, index := range indexes {
t += index
lastIndex := lastIndexes[k]
if i > 0 && k == 0 {
lastIndex++
}
if lastIndex < index {
chunks[k] = sortedChunks[k][lastIndex:index]
}