Repository navigation
Expand file tree
/
Copy pathmodule.ae
More file actions
396 lines (373 loc) · 10.8 KB
/
Copy pathmodule.ae
File metadata and controls
396 lines (373 loc) · 10.8 KB
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
// std.sort — in-place ascending sort and binary search over slices:
// `int[]`, `long[]`, `float[]` and `string[]`.
//
// Every entry point takes a slice, so the length travels with the elements
// and no call takes a separate count: a whole array sorts as `sort.ints(a)`,
// a prefix as `sort.ints(a[..n])`, any run as `sort.ints(a[lo..hi])`. The
// packed-array handles give their elements as a slice (`intarr.array(h)`,
// `longarr.array(h)`, `floatarr.array(h)`, `strarr.array(h)`), as do a
// `T[N]` array, a `make([]T, n)` buffer and a bounded view of C memory
// (`(p as int[])[0..n]`). A view with no bound (`p as int[]`, `.len` -1)
// has no length to sort and panics.
//
// Scoped to concrete types by design (see docs/cross-references/
// c3-stdlib-gaps.md §2.3): C3's `std::sort` is generics-first, which
// Aether lowers differently; here each element type gets a direct sort so
// there is no generic machinery to reason about. The default order is
// ascending. Sorts are IN PLACE and stable is not guaranteed (shell sort).
// The slice's bound is checked once, on entry; the loops index an unchecked
// view of the same elements, so a sort costs what it did over the raw
// buffer.
//
// The algorithm choice (shell sort with the Ciura gap sequence) and the
// binary-search contract follow C3's `std::sort` at the capability level
// (MIT, Copyright (c) 2022-2026 Christoffer Lernö and contributors),
// re-expressed for Aether's concrete array types.
//
// Ordering: numbers by `<`; strings by std.string.compare, lexicographic
// BYTE order, binary-safe (embedded NULs take part), the default C, Go and
// Zig use. Locale-aware collation is a different question and stays in
// contrib/i18n.
exports(
ints, longs, floats, strings,
int_search, long_search, float_search, string_search,
ints_by, longs_by, floats_by, strings_by
)
import std.string
import std.mem
// Ciura's gap sequence, extended by *2.25 for larger n. A module-level
// const array of the fixed prefix; larger gaps are computed on the fly.
const CIURA[] = [1, 4, 10, 23, 57, 132, 301, 701]
// The largest starting gap <= n/2: Ciura's, then *2.25 growth.
fn first_gap(n: int) -> int {
gap = 1
idx = 0
while idx < 8 && CIURA[idx] < n {
gap = CIURA[idx]
idx = idx + 1
}
while gap < n / 2 {
gap = (gap * 9) / 4 // ~*2.25
}
return gap
}
// The next gap down the chain; 0 after the final gap-1 pass.
fn next_gap(gap: int) -> int {
if gap == 1 {
return 0
}
next = (gap * 4) / 9
if next < 1 {
return 1
}
return next
}
// A view with no bound (`p as T[]`) has no length to sort or search.
fn unbounded(fname: string) {
panic("sort.${fname}: the slice has no bound (.len -1); give it one, as `(p as T[])[0..n]`")
}
// The comparator forms (`*_by`): `cmp` must be a strict weak ordering. An
// inconsistent comparator gives an unspecified permutation, not a crash, but
// not a sorted slice either. They are separate names rather than an optional
// argument so the default path keeps a direct comparison instead of an
// indirect call per element: these sorts are in place and hot, and the
// natural order is what almost every caller wants. Not stable, like every
// sort in this module.
// ---- int[] -------------------------------------------------------------------
// Sort `xs` ascending, in place.
ints(xs: int[]) {
n = xs.len
if n < 0 {
unbounded("ints")
}
if n < 2 {
return
}
v = xs as ptr as int[] // bound checked above; the loops index unchecked
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
tmp = v[i]
j = i
while j >= gap && v[j - gap] > tmp {
v[j] = v[j - gap]
j = j - gap
}
v[j] = tmp
i = i + 1
}
gap = next_gap(gap)
}
}
// Binary search a SORTED `xs` for `x`: the index of the first element not
// ordered before `x`, which is `x`'s index when present and otherwise where
// it would be inserted to keep `xs` sorted (`xs.len` when every element
// sorts before it). C3's binarysearch contract.
int_search(xs: int[], x: int) -> int {
if xs.len < 0 {
unbounded("int_search")
}
v = xs as ptr as int[]
lo = 0
hi = xs.len
while lo < hi {
mid = lo + (hi - lo) / 2
if v[mid] < x {
lo = mid + 1
} else {
hi = mid
}
}
return lo
}
// Sort `xs` in place by `cmp`, which returns <0 when its first argument
// sorts before its second, 0 when they tie, >0 when it sorts after.
ints_by(xs: int[], cmp: fn(int, int) -> int) {
n = xs.len
if n < 0 {
unbounded("ints_by")
}
if n < 2 {
return
}
v = xs as ptr as int[]
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
tmp = v[i]
j = i
while j >= gap && cmp(v[j - gap], tmp) > 0 {
v[j] = v[j - gap]
j = j - gap
}
v[j] = tmp
i = i + 1
}
gap = next_gap(gap)
}
}
// ---- long[] ------------------------------------------------------------------
// Sort `xs` ascending, in place.
longs(xs: long[]) {
n = xs.len
if n < 0 {
unbounded("longs")
}
if n < 2 {
return
}
v = xs as ptr as long[] // bound checked above; the loops index unchecked
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
tmp = v[i]
j = i
while j >= gap && v[j - gap] > tmp {
v[j] = v[j - gap]
j = j - gap
}
v[j] = tmp
i = i + 1
}
gap = next_gap(gap)
}
}
// Binary search a SORTED `xs` for `x`: the index of the first element not
// ordered before `x`, which is `x`'s index when present and otherwise where
// it would be inserted to keep `xs` sorted (`xs.len` when every element
// sorts before it). C3's binarysearch contract.
long_search(xs: long[], x: long) -> int {
if xs.len < 0 {
unbounded("long_search")
}
v = xs as ptr as long[]
lo = 0
hi = xs.len
while lo < hi {
mid = lo + (hi - lo) / 2
if v[mid] < x {
lo = mid + 1
} else {
hi = mid
}
}
return lo
}
// Sort `xs` in place by `cmp`, which returns <0 when its first argument
// sorts before its second, 0 when they tie, >0 when it sorts after.
longs_by(xs: long[], cmp: fn(long, long) -> int) {
n = xs.len
if n < 0 {
unbounded("longs_by")
}
if n < 2 {
return
}
v = xs as ptr as long[]
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
tmp = v[i]
j = i
while j >= gap && cmp(v[j - gap], tmp) > 0 {
v[j] = v[j - gap]
j = j - gap
}
v[j] = tmp
i = i + 1
}
gap = next_gap(gap)
}
}
// ---- float[] -----------------------------------------------------------------
// Sort `xs` ascending, in place.
floats(xs: float[]) {
n = xs.len
if n < 0 {
unbounded("floats")
}
if n < 2 {
return
}
v = xs as ptr as float[] // bound checked above; the loops index unchecked
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
tmp = v[i]
j = i
while j >= gap && v[j - gap] > tmp {
v[j] = v[j - gap]
j = j - gap
}
v[j] = tmp
i = i + 1
}
gap = next_gap(gap)
}
}
// Binary search a SORTED `xs` for `x`: the index of the first element not
// ordered before `x`, which is `x`'s index when present and otherwise where
// it would be inserted to keep `xs` sorted (`xs.len` when every element
// sorts before it). C3's binarysearch contract.
float_search(xs: float[], x: float) -> int {
if xs.len < 0 {
unbounded("float_search")
}
v = xs as ptr as float[]
lo = 0
hi = xs.len
while lo < hi {
mid = lo + (hi - lo) / 2
if v[mid] < x {
lo = mid + 1
} else {
hi = mid
}
}
return lo
}
// Sort `xs` in place by `cmp`, which returns <0 when its first argument
// sorts before its second, 0 when they tie, >0 when it sorts after.
floats_by(xs: float[], cmp: fn(float, float) -> int) {
n = xs.len
if n < 0 {
unbounded("floats_by")
}
if n < 2 {
return
}
v = xs as ptr as float[]
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
tmp = v[i]
j = i
while j >= gap && cmp(v[j - gap], tmp) > 0 {
v[j] = v[j - gap]
j = j - gap
}
v[j] = tmp
i = i + 1
}
gap = next_gap(gap)
}
}
// ---- string[] ----------------------------------------------------------------
// Sort `xs` ascending, in place.
strings(xs: string[]) {
n = xs.len
if n < 0 {
unbounded("strings")
}
if n < 2 {
return
}
// A permutation: the elements' pointers are exchanged, taking and
// releasing nothing, so the array may be Aether's or C's (a strarr).
base = xs as ptr
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
j = i
while j >= gap && string.compare(xs[j - gap], xs[j]) > 0 {
mem.swap_ptrs(base, j - gap, j)
j = j - gap
}
i = i + 1
}
gap = next_gap(gap)
}
}
// Binary search a SORTED `xs` for `x`: the index of the first element not
// ordered before `x`, which is `x`'s index when present and otherwise where
// it would be inserted to keep `xs` sorted (`xs.len` when every element
// sorts before it). C3's binarysearch contract.
string_search(xs: string[], x: string) -> int {
if xs.len < 0 {
unbounded("string_search")
}
v = xs as ptr as string[]
lo = 0
hi = xs.len
while lo < hi {
mid = lo + (hi - lo) / 2
if string.compare(v[mid], x) < 0 {
lo = mid + 1
} else {
hi = mid
}
}
return lo
}
// Sort `xs` in place by `cmp`, which returns <0 when its first argument
// sorts before its second, 0 when they tie, >0 when it sorts after.
strings_by(xs: string[], cmp: fn(string, string) -> int) {
n = xs.len
if n < 0 {
unbounded("strings_by")
}
if n < 2 {
return
}
base = xs as ptr
gap = first_gap(n)
while gap >= 1 {
i = gap
while i < n {
j = i
while j >= gap && cmp(xs[j - gap], xs[j]) > 0 {
mem.swap_ptrs(base, j - gap, j)
j = j - gap
}
i = i + 1
}
gap = next_gap(gap)
}
}