aboutsummaryrefslogtreecommitdiff
path: root/core/slice/sort_private.odin
blob: efa9c596b995e94a0ca9c256a28e4d869845f21e (plain)
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
#+private
package slice

import "base:builtin"
import "base:intrinsics"
_ :: intrinsics

ORD :: intrinsics.type_is_ordered

Sort_Kind :: enum {
	Ordered,
	Less,
	Cmp,
}

_stable_sort_general :: proc(data: $T/[]$E, call: $P, $KIND: Sort_Kind) where (ORD(E) && KIND == .Ordered) || (KIND != .Ordered) #no_bounds_check {
	less :: #force_inline proc(a, b: E, call: P) -> bool {
		when KIND == .Ordered {
			return a < b
		} else when KIND == .Less {
			return call(a, b)
		} else when KIND == .Cmp {
			return call(a, b) == .Less
		} else {
			#panic("unhandled Sort_Kind")
		}
	}
	
	// insertion sort
	// TODO(bill): use a different algorithm as insertion sort is O(n^2)
	n := len(data)
	for i in 1..<n {
		for j := i; j > 0 && less(data[j], data[j-1], call); j -= 1 {
			swap(data, j, j-1)
		}
	}
}

@(private)
_smoothsort :: proc(base: [^]byte, nel: uint, width: uint, cmp: Generic_Cmp, arg: rawptr) {
	pntz :: proc "contextless" (p: [2]uint) -> int {
		r := intrinsics.count_trailing_zeros(p[0] - 1)
		if r != 0 {
			return int(r)
		}
		r = (8*size_of(uint) + intrinsics.count_trailing_zeros(p[1]))
		if r != 8*size_of(uint) {
			return int(r)
		}
		return 0
	}

	shl :: proc "contextless" (p: []uint, n: int) {
		n := n
		if n >= 8*size_of(uint) {
			n -= 8*size_of(uint)
			p[1] = p[0]
			p[0] = 0
		}
		p[1] <<= uint(n)
		p[0] |= p[0] >> uint(8*size_of(uint) - n)
		p[0] <<= uint(n)
	}
	shr :: proc "contextless" (p: []uint, n: int) {
		n := n
		if n >= 8*size_of(uint) {
			n -= 8*size_of(uint)
			p[0] = p[1]
			p[1] = 0
		}
		p[0] >>= uint(n)
		p[0] |= p[1] << uint(8*size_of(uint) - n)
		p[1] >>= uint(n)
	}

	cycle :: proc "contextless" (width: uint, data: [][^]byte, n: int) {
		if len(data) < 2 {
			return
		}
		buf: [256]u8 = ---
		data[n] = raw_data(buf[:])
		width := width
		for width != 0 {
			l := builtin.min(size_of(buf), int(width))
			copy(data[n][:l], data[0][:l])
			for i in 0..<n {
				copy(data[i][:l], data[i+1][:l])
				data[i] = data[i][l:]
			}
			width -= uint(l)
		}
	}

	sift :: proc(head: [^]byte, width: uint, cmp: Generic_Cmp, arg: rawptr, pshift: int, lp: []uint) {
		head := head
		buf: [14*size_of(uint)+1][^]byte = ---
		buf[0] = head
		i := 1
		pshift := pshift
		for pshift > 1 {
			rt := head[-width:]
			lf := head[-width:][-lp[pshift - 2]:]
			if cmp(buf[0], lf, arg) >= .Equal && cmp(buf[0], rt, arg) >= .Equal {
				break
			}
			if cmp(lf, rt, arg) >= .Equal {
				buf[i], head = lf, lf
				pshift -= 1
			} else {
				buf[i], head = rt, rt
				pshift -= 2
			}
			i += 1
		}
		cycle(width, buf[:], i)
	}

	trinkle :: proc(head: [^]byte, width: uint, cmp: Generic_Cmp, arg: rawptr, pp: []uint, pshift: int, trusty: bool, lp: []uint) {
		head := head

		p := [2]uint{pp[0], pp[1]}

		buf: [14*size_of(uint)+1][^]byte = ---
		buf[0] = head

		i := 1
		trail := 0
		pshift := pshift
		trusty := trusty
		for p[0] != 1 || p[1] != 0 {
			stepson := head[-lp[pshift]:]
			if cmp(stepson, buf[0], arg) <= .Equal {
				break
			}
			if !trusty && pshift > 1 {
				rt := head[-width:]
				lf := head[-width:][-lp[pshift-2]:]
				if cmp(rt, stepson, arg) >= .Equal || cmp(lf, stepson, arg) >= .Equal {
					break
				}
			}
			buf[i] = stepson
			head = stepson
			trail = pntz(p)
			shr(p[:], trail)
			pshift += trail
			trusty = false
			i += 1
		}
		if trusty {
			return
		}
		cycle(width, buf[:], i)
		sift(head, width, cmp, arg, pshift, lp)
	}

	size := nel * width
	if size == 0 {
		return
	}

	lp: [12*size_of(uint)]uint = ---
	lp[1] = width
	lp[0] = lp[1]
	for i := 2; true; i += 1 {
		lp[i] = lp[i-2] + lp[i-1] + width
		if lp[i] >= size {
			break
		}
	}

	head := base
	high := head[size - width:]
	p := [2]uint{1, 0}
	pshift := 1
	for head < high {
		if (p[0] & 3) == 3 {
			sift(head, width, cmp, arg, pshift, lp[:])
			shr(p[:], 2)
			pshift += 2
		} else {
			if lp[pshift - 1] >= uint(uintptr(high) - uintptr(head)) {
				trinkle(head, width, cmp, arg, p[:], pshift, false, lp[:])
			} else {
				sift(head, width, cmp, arg, pshift, lp[:])
			}
			if pshift == 1 {
				shl(p[:], 1)
				pshift = 0
			} else {
				shl(p[:], pshift - 1)
				pshift = 1
			}
		}
		p[0] |= 1
		head = head[width:]
	}
	trinkle(head, width, cmp, arg, p[:], pshift, false, lp[:])
	for pshift != 1 || p[0] != 1 || p[1] != 0 {
		if pshift <= 1 {
			trail := pntz(p)
			shr(p[:], trail)
			pshift += trail
		} else {
			shl(p[:], 2)
			pshift -= 2
			p[0] ~= 7
			shr(p[:], 1)
			trinkle(head[-width:][-lp[pshift]:], width, cmp, arg, p[:], pshift + 1, true, lp[:])
			shl(p[:], 1)
			p[0] |= 1
			trinkle(head[-width:], width, cmp, arg, p[:], pshift, true, lp[:])
		}
		head = head[-width:]
	}
}