/
sortyF4.go
392 lines (344 loc) · 7.64 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
/* Copyright (c) 2019, 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"
// IsSortedF4 returns 0 if ar is sorted in ascending order,
// otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedF4(ar []float32) int {
for i := len(ar) - 1; i > 0; i-- {
if ar[i] < ar[i-1] {
return i
}
}
return 0
}
// insertion sort, assumes len(ar) >= 2
func insertionF4(ar []float32) {
hi := len(ar) - 1
for l, h := (hi-3)>>1, hi; l >= 0; {
if ar[h] < ar[l] {
ar[l], ar[h] = ar[h], ar[l]
}
l--
h--
}
for h := 0; ; {
l := h
h++
v := ar[h]
if v < ar[l] {
for {
ar[l+1] = ar[l]
l--
if l < 0 || v >= ar[l] {
break
}
}
ar[l+1] = v
}
if h >= hi {
break
}
}
}
// pivotF4 divides ar into 2n+1 equal intervals, sorts mid-points of them
// to find median-of-2n+1 pivot. ensures lo/hi ranges have at least n elements by
// moving 2n of mid-points to n positions at lo/hi ends.
// assumes n > 0, len(ar) > 4n+2. returns remaining slice,pivot for partitioning.
func pivotF4(ar []float32, n int) ([]float32, float32) {
m := len(ar) >> 1
s := len(ar) / (2*n + 1) // step > 1
l, h := m-n*s, m+n*s
for q, k := h, m-2*s; k >= l; { // insertion sort ar[m+i*s], i=-n..n
if ar[q] < ar[k] {
ar[k], ar[q] = ar[q], ar[k]
}
q -= s
k -= s
}
for q := l; ; {
k := q
q += s
v := ar[q]
if v < ar[k] {
for {
ar[k+s] = ar[k]
k -= s
if k < l || v >= ar[k] {
break
}
}
ar[k+s] = v
}
if q >= h {
break
}
}
lo, hi := 0, len(ar)
// move lo/hi mid-points to lo/hi ends
for {
hi--
ar[l], ar[lo] = ar[lo], ar[l]
ar[h], ar[hi] = ar[hi], ar[h]
l += s
h -= s
lo++
if h <= m {
break
}
}
return ar[lo:hi:hi], ar[m] // lo <= m-s+1, m+s-1 < hi
}
// partition ar into <= and >= pivot, assumes len(ar) >= 2
// returns k with ar[:k] <= pivot, ar[k:] >= pivot
func partition1F4(ar []float32, pv float32) int {
l, h := 0, len(ar)-1
for {
if ar[h] < pv { // avoid unnecessary comparisons
for {
if pv < ar[l] {
ar[l], ar[h] = ar[h], ar[l]
break
}
l++
if l >= h {
return l + 1
}
}
} else if pv < ar[l] { // extend ranges in balance
for {
h--
if l >= h {
return l
}
if ar[h] < pv {
ar[l], ar[h] = ar[h], ar[l]
break
}
}
}
l++
h--
if l >= h {
break
}
}
if l == h && ar[h] < pv { // classify mid element
l++
}
return l
}
// rearrange ar[:a] and ar[b:] into <= and >= pivot, assumes 0 < a < b < len(ar)
// gap (a,b) expands until one of the intervals is fully consumed
func partition2F4(ar []float32, a, b int, pv float32) (int, int) {
a--
for {
if ar[b] < pv { // avoid unnecessary comparisons
for {
if pv < ar[a] {
ar[a], ar[b] = ar[b], ar[a]
break
}
a--
if a < 0 {
return a, b
}
}
} else if pv < ar[a] { // extend ranges in balance
for {
b++
if b >= len(ar) {
return a, b
}
if ar[b] < pv {
ar[a], ar[b] = ar[b], ar[a]
break
}
}
}
a--
b++
if a < 0 || b >= len(ar) {
return a, b
}
}
}
// new-goroutine partition
func gpart1F4(ar []float32, pv float32, ch chan int) {
ch <- partition1F4(ar, pv)
}
// concurrent dual partitioning of ar
// returns k with ar[:k] <= pivot, ar[k:] >= pivot
func cdualparF4(ar []float32, ch chan int) int {
aq, pv := pivotF4(ar, 4) // median-of-9
k := len(aq) >> 1
a, b := k>>1, mid(k, len(aq))
go gpart1F4(aq[a:b:b], pv, ch) // mid half range
t := a
a, b = partition2F4(aq, a, b, pv) // left/right quarter ranges
k = <-ch
k += t // convert k indice to aq
// only one gap is possible
for ; 0 <= a; a-- { // gap left in low range?
if pv < aq[a] {
k--
aq[a], aq[k] = aq[k], aq[a]
}
}
for ; b < len(aq); b++ { // gap left in high range?
if aq[b] < pv {
aq[b], aq[k] = aq[k], aq[b]
k++
}
}
return k + 4 // convert k indice to ar
}
// short range sort function, assumes Mli < len(ar) <= Mlr
func shortF4(ar []float32) {
start:
aq, pv := pivotF4(ar, 2)
k := partition1F4(aq, pv) // median-of-5 partitioning
k += 2 // convert k indice from aq to ar
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) > Mli {
shortF4(aq) // recurse on the shorter range
goto start
}
insertionF4(aq) // at least one insertion range
if len(ar) > Mli {
goto start
}
insertionF4(ar) // two insertion ranges
}
// long range sort function (single goroutine), assumes len(ar) > Mlr
func slongF4(ar []float32) {
start:
aq, pv := pivotF4(ar, 3)
k := partition1F4(aq, pv) // median-of-7 partitioning
k += 3 // convert k indice from aq to ar
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) > Mlr { // at least one not-long range?
slongF4(aq) // recurse on the shorter range
goto start
}
if len(aq) > Mli {
shortF4(aq)
} else {
insertionF4(aq)
}
if len(ar) > Mlr { // two not-long ranges?
goto start
}
shortF4(ar) // we know len(ar) > Mli
}
// new-goroutine sort function
func glongF4(ar []float32, sv *syncVar) {
longF4(ar, sv)
if atomic.AddUint32(&sv.ngr, ^uint32(0)) == 0 { // decrease goroutine counter
sv.done <- 0 // we are the last, all done
}
}
// long range sort function, assumes len(ar) > Mlr
func longF4(ar []float32, sv *syncVar) {
start:
aq, pv := pivotF4(ar, 3)
k := partition1F4(aq, pv) // median-of-7 partitioning
k += 3 // convert k indice from aq to ar
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) <= Mlr { // at least one not-long range?
if len(aq) > Mli {
shortF4(aq)
} else {
insertionF4(aq)
}
if len(ar) > Mlr { // two not-long ranges?
goto start
}
shortF4(ar) // we know len(ar) > Mli
return
}
// max goroutines? not atomic but good enough
if sv.ngr >= Mxg {
longF4(aq, sv) // recurse on the shorter range
goto start
}
if atomic.AddUint32(&sv.ngr, 1) == 0 { // increase goroutine counter
panic("sorty: longF4: counter overflow")
}
// new-goroutine sort on the longer range only when
// both ranges are big and max goroutines is not exceeded
go glongF4(ar, sv)
ar = aq
goto start
}
// SortF4 concurrently sorts ar in ascending order.
func SortF4(ar []float32) {
if len(ar) < 2*(Mlr+1) || Mxg <= 1 {
// single-goroutine sorting
if len(ar) > Mlr {
slongF4(ar)
} else if len(ar) > Mli {
shortF4(ar)
} else if len(ar) > 1 {
insertionF4(ar)
}
return
}
// create channel only when concurrent partitioning & sorting
sv := syncVar{1, // number of goroutines including this
make(chan int)} // end signal
for {
// median-of-9 concurrent dual partitioning with done
k := cdualparF4(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) > Mlr {
if atomic.AddUint32(&sv.ngr, 1) == 0 { // increase goroutine counter
panic("sorty: SortF4: counter overflow")
}
go glongF4(aq, &sv)
} else if len(aq) > Mli {
shortF4(aq)
} else {
insertionF4(aq)
}
// longer range big enough? max goroutines?
if len(ar) < 2*(Mlr+1) || sv.ngr >= Mxg {
break
}
// dual partition longer range
}
longF4(ar, &sv) // we know len(ar) > Mlr
if atomic.AddUint32(&sv.ngr, ^uint32(0)) != 0 { // decrease goroutine counter
<-sv.done // we are not the last, wait
}
}