This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix.go
426 lines (392 loc) · 9.77 KB
/
radix.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
419
420
421
422
423
424
425
426
// Radix sort adapted from https://github.com/shawnsmithdev/zermelo which comes
// with the following license:
//
// The MIT License (MIT)
//
// Copyright (c) 2014 Shawn Smith
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package goal
import (
"math"
"sort"
)
const radix uint = 8
const cachedLen = 256
func radixSortAI(ctx *Context, x *AI, min, max int64) *AI {
xlen := x.Len()
if min >= math.MinInt16 && max <= math.MaxInt16 {
var buf []int16
if xlen < cachedLen {
if ctx.sortBuf16 == nil {
ctx.sortBuf16 = make([]int16, cachedLen*2)
}
buf = ctx.sortBuf16
} else {
buf = make([]int16, xlen*2)
}
r := radixSortAIWithSize(x, buf, 16, math.MinInt16)
return r
}
if min >= math.MinInt32 && max <= math.MaxInt32 {
var buf []int32
if xlen < cachedLen {
if ctx.sortBuf32 == nil {
ctx.sortBuf32 = make([]int32, cachedLen*2)
}
buf = ctx.sortBuf32
} else {
buf = make([]int32, xlen*2)
}
r := radixSortAIWithSize(x, buf, 32, math.MinInt32)
return r
}
// NOTE: given that Go's stdlib interface-based sort isn't the fastest
// on integers, it would sometimes be better to use radix sort for
// 64bits too, but not always.
x = scloneAI(x)
sort.Sort(x)
return x
}
func radixSortAIWithSize[I signed](x *AI, buf []I, size uint, min I) *AI {
from := radixSortInt64sWithSize[I](x.elts, buf, size, min)
var dst []int64
reuse := x.reusable()
if reuse {
dst = x.elts
} else {
dst = make([]int64, x.Len())
}
for i, n := range from {
dst[i] = int64(n)
}
if reuse {
return x
}
return &AI{elts: dst}
}
func radixSortInt64sWithSize[I signed](x []int64, buf []I, size uint, min I) []I {
xlen := len(x)
from := buf[:xlen]
to := buf[xlen : xlen*2]
for i, xi := range x {
from[i] = I(xi)
}
radixSortWithBuffer[I](from, to, size, min)
return from
}
type signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
type unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
// radixSortWithBuffer sorts from using a radix sort. The to buffer slice
// should have same length as from, size should be the bitsize of T, and min
// should be the minimum possible value of type T.
func radixSortWithBuffer[I signed](from, to []I, size uint, min I) {
var keyOffset uint
for keyOffset = 0; keyOffset < size; keyOffset += radix {
var (
offset [256]int // Keep track of where room is made for byte groups in the buffer
prev I = min
key uint8
sorted = true
)
// Compute counts by byte type at current radix
for _, elem := range from {
key = uint8(elem >> keyOffset)
offset[key]++
if sorted {
sorted = elem >= prev
prev = elem
}
}
if sorted {
break
}
// Compute target bucket offsets from counts
var sum int
if keyOffset == size-radix {
// Negatives
for i := 128; i < len(offset); i++ {
count := offset[i]
offset[i] = sum
sum += count
}
// Positives
for i := 0; i < 128; i++ {
count := offset[i]
offset[i] = sum
sum += count
}
} else {
for i, count := range offset {
offset[i] = sum
sum += count
}
}
// Swap values between the buffers by radix
for _, elem := range from {
key = uint8(elem >> keyOffset)
to[offset[key]] = elem
offset[key]++
}
from, to = to, from
}
// copy from buffer if done during odd turn
if radix&keyOffset == radix {
copy(to, from)
}
}
func radixGradeSmallRange(ctx *Context, x *AI, min, max int64) V {
xlen := x.Len()
var buf []int8
if xlen < cachedLen {
if ctx.sortBuf8 == nil {
ctx.sortBuf8 = make([]int8, cachedLen)
}
buf = ctx.sortBuf8
} else {
buf = make([]int8, xlen)
}
from := buf[:xlen]
if min >= math.MinInt8 && max <= math.MaxInt8 {
for i, xi := range x.elts {
from[i] = int8(xi)
}
} else {
for i, xi := range x.elts {
from[i] = int8(xi - min - math.MinInt8)
}
}
if xlen < 256 {
p := make([]byte, xlen)
radixGradeInt8(from, p)
return NewAB(p)
}
var p []int64
if x.reusable() {
p = x.elts
} else {
p = make([]int64, xlen)
}
radixGradeInt8(from, p)
return NewAI(p)
}
func radixGradeAI(ctx *Context, x *AI, min, max int64) V {
xlen := x.Len()
if min >= math.MinInt16 && max <= math.MaxInt16 {
var buf []int16
if xlen < cachedLen {
if ctx.sortBuf16 == nil {
ctx.sortBuf16 = make([]int16, cachedLen*2)
}
buf = ctx.sortBuf16
} else {
buf = make([]int16, xlen*2)
}
if xlen < 256 {
r := radixGradeAIBytes[int16](x, buf, 16, math.MinInt16)
return NewAB(r)
}
r := radixGradeAIIs[int16](x, buf, 16, math.MinInt16)
return NewAI(r)
}
if min >= math.MinInt32 && max <= math.MaxInt32 {
var buf []int32
if xlen < cachedLen {
if ctx.sortBuf32 == nil {
ctx.sortBuf32 = make([]int32, cachedLen*2)
}
buf = ctx.sortBuf32
} else {
buf = make([]int32, xlen*2)
}
if xlen < 256 {
r := radixGradeAIBytes[int32](x, buf, 32, math.MinInt32)
return NewAB(r)
}
r := radixGradeAIIs[int32](x, buf, 32, math.MinInt32)
return NewAI(r)
}
if xlen < 256 {
p := &permutation[byte]{Perm: permRange[byte](xlen), X: x}
sort.Stable(p)
return NewAB(p.Perm)
}
p := &permutation[int64]{Perm: permRange[int64](xlen), X: x}
sort.Stable(p)
return NewAI(p.Perm)
}
func radixGradeAIIs[I signed](x *AI, buf []I, size uint, min I) []int64 {
xlen := x.Len()
from := buf[:xlen]
to := buf[xlen : xlen*2]
for i, xi := range x.elts {
from[i] = I(xi)
}
var fromp, top []int64
if x.reusable() {
fromp = x.elts
top = make([]int64, xlen)
} else {
bufp := make([]int64, xlen*2)
fromp = bufp[:xlen]
top = bufp[xlen : xlen*2]
}
for i := range fromp {
fromp[i] = int64(i)
}
radixGradeWithBuffer[I, int64](from, to, fromp, top, size, min)
return fromp
}
func radixGradeAIBytes[I signed](x *AI, buf []I, size uint, min I) []byte {
xlen := x.Len()
from := buf[:xlen]
to := buf[xlen : xlen*2]
for i, xi := range x.elts {
from[i] = I(xi)
}
bufp := make([]byte, xlen*2)
fromp := bufp[:xlen]
top := bufp[xlen : xlen*2]
for i := range fromp {
fromp[i] = byte(i)
}
radixGradeWithBuffer[I, byte](from, to, fromp, top, size, min)
return fromp
}
// radixGradeWithBuffer sorts from using a radix sort. The to buffer, fromp,
// top slices should have same length as from, size should be the bitsize of T,
// and min should be the minimum possible value of type T.
func radixGradeWithBuffer[J signed, I integer](from, to []J, fromp, top []I, size uint, min J) {
var keyOffset uint
for keyOffset = 0; keyOffset < size; keyOffset += radix {
var (
offset [256]I // Keep track of where room is made for byte groups in the buffer
prev J = min
key uint8
sorted = true
)
// Compute counts by byte type at current radix
for _, elem := range from {
key = uint8(elem >> keyOffset)
offset[key]++
if sorted {
sorted = elem >= prev
prev = elem
}
}
if sorted {
break
}
// Compute target bucket offsets from counts
var sum I
if keyOffset == size-radix {
// Negatives
for i := 128; i < len(offset); i++ {
count := offset[i]
offset[i] = sum
sum += count
}
// Positives
for i := 0; i < 128; i++ {
count := offset[i]
offset[i] = sum
sum += count
}
} else {
for i, count := range offset {
offset[i] = sum
sum += count
}
}
// Swap values between the buffers by radix
for i, elem := range from {
key = uint8(elem >> keyOffset)
j := offset[key]
offset[key]++
to[j] = elem
top[j] = fromp[i]
}
from, to = to, from
fromp, top = top, fromp
}
// copy from buffer if done during odd turn
if radix&keyOffset == radix {
copy(to, from)
copy(top, fromp)
}
}
// radixGradeInt8 sorts p by from, and puts sorted from into to.
func radixGradeInt8[I integer](from []int8, p []I) {
var (
offset [256]I // Keep track of where room is made for byte groups in the buffer
key uint8
)
// Compute counts by byte type at current radix
for _, elem := range from {
key = uint8(elem)
offset[key]++
}
// Compute target bucket offsets from counts
var sum I
// Negatives
for i := 128; i < len(offset); i++ {
count := offset[i]
offset[i] = sum
sum += count
}
// Positives
for i := 0; i < 128; i++ {
count := offset[i]
offset[i] = sum
sum += count
}
// Swap values between the buffers by radix
for i, elem := range from {
key = uint8(elem)
j := offset[key]
offset[key]++
p[j] = I(i)
}
}
// radixGradeUint8 sorts p by from.
func radixGradeUint8[I integer](from []uint8, p []I) {
var (
offset [256]I // Keep track of where room is made for byte groups in the buffer
)
// Compute counts by byte type at current radix
for _, elem := range from {
offset[elem]++
}
// Compute target bucket offsets from counts
var sum I
for i, count := range offset {
offset[i] = sum
sum += count
}
// Swap values between the buffers by radix
for i, elem := range from {
j := offset[elem]
offset[elem]++
p[j] = I(i)
}
}