-
Notifications
You must be signed in to change notification settings - Fork 6
/
sortyU8.go
375 lines (330 loc) · 7 KB
/
sortyU8.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
/* 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"
)
// isSortedU8 returns 0 if ar is sorted in ascending
// order, otherwise it returns i > 0 with ar[i] < ar[i-1], inlined
func isSortedU8(ar []uint64) int {
for i := len(ar) - 1; i > 0; i-- {
if ar[i] < ar[i-1] {
return i
}
}
return 0
}
// insertion sort, inlined
func insertionU8(slc []uint64) {
for h := 1; h < len(slc); h++ {
l, val := h, slc[h]
var pre uint64
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
}
}
// pivotU8 selects n equidistant samples from slc that minimizes max distance
// to non-selected members, then calculates median-of-n pivot from samples.
// Assumes even n, nsConc ≥ n ≥ 2, len(slc) ≥ 2n. Returns pivot for partitioning.
//
//go:nosplit
func pivotU8(slc []uint64, n uint) uint64 {
first, step, _ := minMaxSample(uint(len(slc)), n)
var sample [nsConc]uint64
for i := int(n - 1); i >= 0; i-- {
sample[i] = slc[first]
first += step
}
insertionU8(sample[:n]) // sort n samples
n >>= 1 // return mean of middle two samples
return sixb.MeanU8(sample[n-1], sample[n])
}
// 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 partOneU8(slc []uint64, pv uint64) 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 partTwoU8(slc []uint64, l, h int, pv uint64) 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 gPartOneU8(ar []uint64, pv uint64, ch chan int) {
ch <- partOneU8(ar, pv)
}
// partition slc in two goroutines, returns k with slc[:k] ≤ pivot ≤ slc[k:]
//
//go:nosplit
func partConU8(slc []uint64, ch chan int) int {
pv := pivotU8(slc, nsConc) // median-of-n pivot
mid := len(slc) >> 1
l, h := mid>>1, sixb.MeanI(mid, len(slc))
go gPartOneU8(slc[l:h:h], pv, ch) // mid half range
r := partTwoU8(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 shortU8(ar []uint64) {
start:
first, step := minMaxFour(uint32(len(ar)))
a, b, c, d := ar[first], ar[first+step], ar[first+2*step], ar[first+3*step]
if d < b {
d, b = b, d
}
if c < a {
c, a = a, c
}
if d < c {
c = d
}
if b < a {
b = a
}
pv := sixb.MeanU8(b, c) // median-of-4 pivot
k := partOneU8(ar, pv)
var aq []uint64
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 {
shortU8(aq) // recurse on the shorter range
goto start
}
isort:
insertionU8(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 gLongU8(ar []uint64, sv *syncVar) {
longU8(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 longU8(ar []uint64, sv *syncVar) {
start:
pv := pivotU8(ar, nsLong) // median-of-n pivot
k := partOneU8(ar, pv)
var aq []uint64
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 {
shortU8(aq)
} else {
insertionU8(aq)
}
if len(ar) > MaxLenRec { // two not-long ranges?
goto start
}
shortU8(ar) // we know len(ar) > MaxLenIns
return
}
// max goroutines? not atomic but good enough
if sv == nil || gorFull(sv) {
longU8(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 gLongU8(ar, sv)
ar = aq
goto start
}
// sortU8 concurrently sorts ar in ascending order.
//
//go:nosplit
func sortU8(ar []uint64) {
if len(ar) < 2*(MaxLenRec+1) || MaxGor <= 1 {
if len(ar) > MaxLenRec { // single-goroutine sorting
longU8(ar, nil)
} else if len(ar) > MaxLenIns {
shortU8(ar)
} else {
insertionU8(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 := partConU8(ar, sv.done)
var aq []uint64
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 gLongU8(aq, &sv)
} else if len(aq) > MaxLenIns {
shortU8(aq)
} else {
insertionU8(aq)
}
// longer range big enough? max goroutines?
if len(ar) < 2*(MaxLenRec+1) || gorFull(&sv) {
break
}
// dual partition longer range
}
longU8(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
}
}