-
Notifications
You must be signed in to change notification settings - Fork 6
/
sortyF4.go
418 lines (372 loc) · 7.71 KB
/
sortyF4.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
/* 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"
"github.com/jfcg/sixb"
)
// isSortedF4 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 isSortedF4(slc []float32) 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
}
}
}
for i := h; i > l; i-- {
if !(slc[i] >= slc[i-1]) {
return i
}
}
return 0
}
// insertion sort, inlined
func insertionF4(slc []float32) {
for h := 1; h < len(slc); h++ {
l, val := h, slc[h]
var pre float32
goto start
loop:
slc[l] = pre
l--
if l == 0 {
goto last
}
start:
pre = slc[l-1]
if val < pre {
goto loop
}
if l == h {
continue
}
last:
slc[l] = val
}
}
// pivotF4 selects n equidistant samples from slc that minimizes max distance
// to non-selected members, then calculates median-of-n pivot from samples.
// Assumes odd n, nsConc > n ≥ 3, len(slc) ≥ 2n. Returns pivot for partitioning.
//
//go:nosplit
func pivotF4(slc []float32, n uint) float32 {
first, step, _ := minMaxSample(uint(len(slc)), n)
var sample [nsConc - 1]float32
for i := int(n - 1); i >= 0; i-- {
sample[i] = slc[first]
first += step
}
insertionF4(sample[:n]) // sort n samples
return sample[n>>1] // return middle sample
}
// partition slc, returns k with slc[:k] ≤ pivot ≤ slc[k:]
// swap: slc[h] < pv ≤ slc[l]
// swap: slc[h] ≤ pv < slc[l]
// next: slc[l] ≤ pv ≤ slc[h]
//
//go:nosplit
func partOneF4(slc []float32, pv float32) int {
l, h := 0, len(slc)-1
goto start
second:
for {
h--
if h <= l {
return l
}
if slc[h] <= pv {
break
}
}
swap:
slc[l], slc[h] = slc[h], slc[l]
next:
l++
h--
start:
if h <= l {
goto last
}
if pv <= slc[h] { // avoid unnecessary comparisons
if pv < slc[l] { // extend ranges in balance
goto second
}
goto next
}
for {
if pv <= slc[l] {
goto swap
}
l++
if h <= l {
return l + 1
}
}
last:
if l == h && slc[h] < pv { // classify mid element
l++
}
return l
}
// swaps elements to get slc[:l] ≤ pivot ≤ slc[h:]
// Gap (l,h) expands until one of the intervals is fully consumed.
// swap: slc[h] < pv ≤ slc[l]
// swap: slc[h] ≤ pv < slc[l]
// next: slc[l] ≤ pv ≤ slc[h]
//
//go:nosplit
func partTwoF4(slc []float32, l, h int, pv float32) int {
l--
if h <= l {
return -1 // will not run
}
goto start
second:
for {
h++
if h >= len(slc) {
return l
}
if slc[h] <= pv {
break
}
}
swap:
slc[l], slc[h] = slc[h], slc[l]
next:
l--
h++
start:
if l < 0 {
return h
}
if h >= len(slc) {
return l
}
if pv <= slc[h] { // avoid unnecessary comparisons
if pv < slc[l] { // extend ranges in balance
goto second
}
goto next
}
for {
if pv <= slc[l] {
goto swap
}
l--
if l < 0 {
return h
}
}
}
// new-goroutine partition
//
//go:nosplit
func gPartOneF4(ar []float32, pv float32, ch chan int) {
ch <- partOneF4(ar, pv)
}
// partition slc in two goroutines, returns k with slc[:k] ≤ pivot ≤ slc[k:]
//
//go:nosplit
func partConF4(slc []float32, ch chan int) int {
pv := pivotF4(slc, nsConc-1) // median-of-n pivot
mid := len(slc) >> 1
l, h := mid>>1, sixb.MeanI(mid, len(slc))
go gPartOneF4(slc[l:h:h], pv, ch) // mid half range
r := partTwoF4(slc, l, h, pv) // left/right quarter ranges
k := l + <-ch // convert returned index to slc
// only one gap is possible
if r < mid {
for ; 0 <= r; r-- { // gap left in low range?
if pv < slc[r] {
k--
slc[r], slc[k] = slc[k], slc[r]
}
}
} else {
for ; r < len(slc); r++ { // gap left in high range?
if slc[r] < pv {
slc[r], slc[k] = slc[k], slc[r]
k++
}
}
}
return k
}
// short range sort function, assumes MaxLenIns < len(ar) <= MaxLenRec, recursive
func shortF4(ar []float32) {
start:
first, step, last := minMaxSample(uint(len(ar)), 3)
f, pv, l := ar[first], ar[first+step], ar[last]
if pv < f {
pv, f = f, pv
}
if l < pv {
if l < f {
pv = f
} else {
pv = l // median-of-3 pivot
}
}
k := partOneF4(ar, pv)
var aq []float32
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 {
shortF4(aq) // recurse on the shorter range
goto start
}
isort:
insertionF4(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 gLongF4(ar []float32, sv *syncVar) {
longF4(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 longF4(ar []float32, sv *syncVar) {
start:
pv := pivotF4(ar, nsLong-1) // median-of-n pivot
k := partOneF4(ar, pv)
var aq []float32
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 {
shortF4(aq)
} else {
insertionF4(aq)
}
if len(ar) > MaxLenRec { // two not-long ranges?
goto start
}
shortF4(ar) // we know len(ar) > MaxLenIns
return
}
// max goroutines? not atomic but good enough
if sv == nil || gorFull(sv) {
longF4(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 gLongF4(ar, sv)
ar = aq
goto start
}
// sortF4 concurrently sorts ar in ascending order.
//
//go:nosplit
func sortF4(ar []float32) {
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
longF4(ar, nil)
} else if len(ar) > MaxLenIns {
shortF4(ar)
} else {
insertionF4(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
k := partConF4(ar, sv.done)
var aq []float32
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 gLongF4(aq, &sv)
} else if len(aq) > MaxLenIns {
shortF4(aq)
} else {
insertionF4(aq)
}
// longer range big enough? max goroutines?
if len(ar) < 2*(MaxLenRec+1) || gorFull(&sv) {
break
}
// dual partition longer range
}
longF4(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
}
}