-
Notifications
You must be signed in to change notification settings - Fork 6
/
sortyLsw.go
192 lines (169 loc) · 4.03 KB
/
sortyLsw.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
/* 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"
// IsSorted3 checks if underlying collection of length n is sorted as per less().
// An existing lesswap() can be used like:
// IsSorted3(n, func(i, k int) bool { return lesswap(i, k, 0, 0) })
func IsSorted3(n int, less func(i, k int) bool) bool {
for i := n - 1; i > 0; i-- {
if less(i, i-1) {
return false
}
}
return true
}
// insertion sort
func insertion3(lsw func(i, k, r, s int) bool, lo, hi int) {
for l, h := mid(lo, hi+1)-2, hi; l >= lo; l, h = l-1, h-1 {
lsw(h, l, h, l)
}
for h := lo + 1; h <= hi; h++ {
for l := h; lsw(l, l-1, l, l-1); {
l--
if l <= lo {
break
}
}
}
}
// set such that ar[l,l+1] <= ar[m] = pivot <= ar[h-1,h]
func pivot3(lsw func(i, k, r, s int) bool, l, h int) (a, b, c int) {
m := mid(l, h)
lsw(h, l, h, l)
_ = lsw(h, m, h, m) || lsw(m, l, m, l)
// ar[l] <= ar[m] <= ar[h]
k, h := h, h-1
if lsw(h, m, h, m) {
k--
lsw(m, l, m, l)
}
l++
if lsw(m, l, m, l) {
if k > h && lsw(h, h+1, 0, 0) {
k = h
}
lsw(k, m, k, m)
}
return l + 1, h - 1, m
}
// partition ar into two groups: >= and <= pivot
func partition3(lsw func(i, k, r, s int) bool, l, h int) (int, int) {
l, h, pv := pivot3(lsw, l, h)
out:
for ; l < h; l, h = l+1, h-1 {
if lsw(h, pv, 0, 0) { // avoid unnecessary comparisons
for {
if lsw(pv, l, h, l) {
break
}
l++
if l >= h {
break out
}
}
} else if lsw(pv, l, 0, 0) { // extend ranges in balance
for {
h--
if l >= h {
break out
}
if lsw(h, pv, h, l) {
break
}
}
}
}
if l == h {
if lsw(pv, l, 0, 0) { // classify mid element
h--
} else {
l++
}
}
return l, h
}
// Sort3 concurrently sorts underlying collection of length n via
// lesswap() which must be equivalent to:
// if less(i, k) { // must be a strict ordering like < or >
// if r != s {
// swap(r, s)
// }
// return true
// }
// return false
// Once for each non-trivial type you want to sort in a certain way, you
// can implement a custom sorting routine (for a slice for example) as:
// func SortObjAsc(c []Obj) {
// lsw := func(i, k, r, s int) bool {
// if c[i].Key < c[k].Key { // or your custom comparator
// if r != s {
// c[r], c[s] = c[s], c[r]
// }
// return true
// }
// return false
// }
// sorty.Sort3(len(c), lsw)
// }
func Sort3(n int, lesswap func(i, k, r, s int) bool) {
var (
mli = Mli >> 1
ngr uint32 // number of sorting goroutines including this
done chan bool // end signal
srt, gsrt func(int, int) // recursive & new-goroutine sort functions
)
gsrt = func(lo, hi int) {
srt(lo, hi)
if atomic.AddUint32(&ngr, ^uint32(0)) == 0 { // decrease goroutine counter
done <- false // we are the last, all done
}
}
srt = func(lo, hi int) { // assumes hi-lo >= mli
start:
l, h := partition3(lesswap, lo, hi)
if h-lo < hi-l {
h, hi = hi, h // [lo,h] is the longer range
l, lo = lo, l
}
// branches below are optimally laid out for fewer jumps
// at least one short range?
if hi-l < mli {
insertion3(lesswap, l, hi)
if h-lo < mli { // two short ranges?
insertion3(lesswap, lo, h)
return
}
hi = h
goto start
}
// range not long enough for new goroutine? max goroutines?
// not atomic but good enough
if hi-l < Mlr || ngr >= Mxg {
srt(l, hi) // start a recursive sort on the shorter range
hi = h
goto start
}
if atomic.AddUint32(&ngr, 1) == 0 { // increase goroutine counter
panic("Sort3: counter overflow")
}
go gsrt(lo, h) // start a new-goroutine sort on the longer range
lo = l
goto start
}
n--
if n > 2*Mlr {
ngr, done = 1, make(chan bool, 1)
gsrt(0, n) // start master sort
<-done
return
}
if n >= mli {
srt(0, n) // single goroutine
return
}
insertion3(lesswap, 0, n)
}