-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsortyF.go
More file actions
213 lines (185 loc) · 4.41 KB
/
Copy pathsortyF.go
File metadata and controls
213 lines (185 loc) · 4.41 KB
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
/* Copyright (c) 2019-present, Serhat Şevki Dinçer.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package sorty
import (
"sync/atomic"
sb "github.com/jfcg/sixb/v3"
)
// isSortedF returns 0 if slc is sorted in ascending order, otherwise it returns i > 0
// with slc[i] < slc[i-1] or either one is a NaN. NaNoption is taken into account.
func isSortedF[S ~[]T, T sb.Float](slc S) int {
l, h := 0, len(slc)-1
if NaNoption == NaNlarge { // ignore NaNs at the end
for ; l <= h; h-- {
if x := slc[h]; x == x {
break
}
}
} else if NaNoption == NaNsmall { // ignore NaNs at the start
for ; l <= h; l++ {
if x := slc[l]; x == x {
break
}
}
}
return isSortedO(slc[l : h+1])
}
// short range sort function, assumes MaxLenIns < len(ar) <= MaxLenRec, recursive
func shortF[S ~[]T, T sb.Float](ar S) {
start:
first, step, last := minMaxSample(uint(len(ar)), 3)
pv := sb.Median3(ar[first], ar[first+step], ar[last])
k := partOneO(ar, pv)
var aq S
if k < len(ar)-k {
aq = ar[:k:k]
ar = ar[k:] // ar is the longer range
} else {
aq = ar[k:]
ar = ar[:k:k]
}
if len(aq) > MaxLenIns {
shortF(aq) // recurse on the shorter range
goto start
}
isort:
insertionO(aq) // at least one insertion range
if len(ar) > MaxLenIns {
goto start
}
if &ar[0] != &aq[0] {
aq = ar
goto isort // two insertion ranges
}
}
// new-goroutine sort function
//
//go:nosplit
func gLongF[S ~[]T, T sb.Float](ar S, sv *syncVar) {
longF(ar, sv)
if atomic.AddUint64(&sv.nGor, ^uint64(0)) == 0 { // decrease goroutine counter
sv.done <- 0 // we are the last, all done
}
}
// long range sort function, assumes len(ar) > MaxLenRec, recursive
func longF[S ~[]T, T sb.Float](ar S, sv *syncVar) {
start:
_, pv := pivotO(ar, nsLong-1) // median-of-n pivot
k := partOneO(ar, pv)
var aq S
if k < len(ar)-k {
aq = ar[:k:k]
ar = ar[k:] // ar is the longer range
} else {
aq = ar[k:]
ar = ar[:k:k]
}
// branches below are optimal for fewer total jumps
if len(aq) <= MaxLenRec { // at least one not-long range?
if len(aq) > MaxLenIns {
shortF(aq)
} else {
insertionO(aq)
}
if len(ar) > MaxLenRec { // two not-long ranges?
goto start
}
shortF(ar) // we know len(ar) > MaxLenIns
return
}
// max goroutines? not atomic but good enough
if sv == nil || gorFull(sv) {
longF(aq, sv) // recurse on the shorter range
goto start
}
// new-goroutine sort on the longer range only when
// both ranges are big and max goroutines is not exceeded
atomic.AddUint64(&sv.nGor, 1) // increase goroutine counter
go gLongF(ar, sv)
ar = aq
goto start
}
// sortF concurrently sorts ar in ascending order.
//
//go:nosplit
func sortF[S ~[]T, T sb.Float](ar S) {
l, h := 0, len(ar)-1
if NaNoption == NaNlarge { // move NaNs to the end
for l <= h {
x := ar[h]
if x != x {
h--
continue
}
y := ar[l]
if y != y {
ar[l], ar[h] = x, y
h--
}
l++
}
ar = ar[:h+1]
} else if NaNoption == NaNsmall { // move NaNs to the start
for l <= h {
y := ar[l]
if y != y {
l++
continue
}
x := ar[h]
if x != x {
ar[l], ar[h] = x, y
l++
}
h--
}
ar = ar[l:]
}
if len(ar) < 2*(MaxLenRec+1) || MaxGor <= 1 {
if len(ar) > MaxLenRec { // single-goroutine sorting
longF(ar, nil)
} else if len(ar) > MaxLenIns {
shortF(ar)
} else {
insertionO(ar)
}
return
}
// create channel only when concurrent partitioning & sorting
sv := syncVar{1, // number of goroutines including this
make(chan int)} // end signal
for {
// concurrent dual partitioning with done
_, pv := pivotO(ar, nsConc-1) // median-of-n pivot
k := partConO(ar, pv, sv.done)
var aq S
if k < len(ar)-k {
aq = ar[:k:k]
ar = ar[k:] // ar is the longer range
} else {
aq = ar[k:]
ar = ar[:k:k]
}
// handle shorter range
if len(aq) > MaxLenRec {
atomic.AddUint64(&sv.nGor, 1) // increase goroutine counter
go gLongF(aq, &sv)
} else if len(aq) > MaxLenIns {
shortF(aq)
} else {
insertionO(aq)
}
// longer range big enough? max goroutines?
if len(ar) < 2*(MaxLenRec+1) || gorFull(&sv) {
break
}
// dual partition longer range
}
longF(ar, &sv) // we know len(ar) > MaxLenRec
if atomic.AddUint64(&sv.nGor, ^uint64(0)) != 0 { // decrease goroutine counter
<-sv.done // we are not the last, wait
}
}