-
Notifications
You must be signed in to change notification settings - Fork 1
/
uint64.go
150 lines (130 loc) · 2.77 KB
/
uint64.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
package radixsort
import "math"
// Uint64 implements radix sort using secondary buffer.
//
// buf must be larger than arr.
func Uint64(arr, buf []uint64) {
if len(arr) > len(buf) {
panic("len(arr) > len(buf)")
}
buf = buf[:len(arr)]
if len(arr) <= 1 {
return
}
if int64(len(arr)) >= math.MaxUint32 {
uint64_large(arr, buf)
return
}
prev := arr[0]
sorted := true
var count [8][256]uint32
for _, v := range arr {
count[0][byte(v>>(0*8))]++
count[1][byte(v>>(1*8))]++
count[2][byte(v>>(2*8))]++
count[3][byte(v>>(3*8))]++
count[4][byte(v>>(4*8))]++
count[5][byte(v>>(5*8))]++
count[6][byte(v>>(6*8))]++
count[7][byte(v>>(7*8))]++
sorted = sorted && prev <= v
prev = v
}
if sorted {
return
}
var offset [8][256]uint32
for k := 1; k < 256; k++ {
offset[0][k] = offset[0][k-1] + count[0][k-1]
offset[1][k] = offset[1][k-1] + count[1][k-1]
offset[2][k] = offset[2][k-1] + count[2][k-1]
offset[3][k] = offset[3][k-1] + count[3][k-1]
offset[4][k] = offset[4][k-1] + count[4][k-1]
offset[5][k] = offset[5][k-1] + count[5][k-1]
offset[6][k] = offset[6][k-1] + count[6][k-1]
offset[7][k] = offset[7][k-1] + count[7][k-1]
}
swaps := 0
src, dst := arr, buf
for k := uint8(0); k < 8; k++ {
nz := 0
cnt := &count[k]
for i := range cnt {
if cnt[i] != 0 {
nz++
}
}
if nz == 1 {
continue
}
swaps++
off := &offset[k]
p := k * 8
for _, v := range src {
key := uint8(v >> p)
dst[off[key]] = v
off[key]++
}
dst, src = src, dst
}
if swaps&1 == 1 {
copy(arr, src)
}
}
func uint64_large(arr, buf []uint64) {
prev := arr[0]
sorted := true
var count [8][256]uint64
for _, v := range arr {
count[0][byte(v>>(0*8))]++
count[1][byte(v>>(1*8))]++
count[2][byte(v>>(2*8))]++
count[3][byte(v>>(3*8))]++
count[4][byte(v>>(4*8))]++
count[5][byte(v>>(5*8))]++
count[6][byte(v>>(6*8))]++
count[7][byte(v>>(7*8))]++
sorted = sorted && prev <= v
prev = v
}
if sorted {
return
}
var offset [8][256]uint64
for k := 1; k < 256; k++ {
offset[0][k] = offset[0][k-1] + count[0][k-1]
offset[1][k] = offset[1][k-1] + count[1][k-1]
offset[2][k] = offset[2][k-1] + count[2][k-1]
offset[3][k] = offset[3][k-1] + count[3][k-1]
offset[4][k] = offset[4][k-1] + count[4][k-1]
offset[5][k] = offset[5][k-1] + count[5][k-1]
offset[6][k] = offset[6][k-1] + count[6][k-1]
offset[7][k] = offset[7][k-1] + count[7][k-1]
}
swaps := 0
src, dst := arr, buf
for k := uint8(0); k < 8; k++ {
nz := 0
cnt := &count[k]
for i := range cnt {
if cnt[i] != 0 {
nz++
}
}
if nz == 1 {
continue
}
swaps++
off := &offset[k]
p := k * 8
for _, v := range src {
key := uint8(v >> p)
dst[off[key]] = v
off[key]++
}
dst, src = src, dst
}
if swaps&1 == 1 {
copy(arr, src)
}
}