zig/std/sort.zig

119 lines
3.1 KiB
Zig
Raw Normal View History

2016-11-02 15:10:44 -07:00
const assert = @import("debug.zig").assert;
2017-01-16 11:23:32 -08:00
const mem = @import("mem.zig");
const math = @import("math.zig");
pub const Cmp = math.Cmp;
2016-11-02 15:10:44 -07:00
pub fn sort(comptime T: type, array: []T, comptime cmp: fn(a: &const T, b: &const T)->Cmp) {
2016-11-02 15:10:44 -07:00
if (array.len > 0) {
quicksort(T, array, 0, array.len - 1, cmp);
2016-11-02 15:10:44 -07:00
}
}
fn quicksort(comptime T: type, array: []T, left: usize, right: usize, comptime cmp: fn(a: &const T, b: &const T)->Cmp) {
2016-11-02 15:10:44 -07:00
var i = left;
var j = right;
2017-01-16 11:23:32 -08:00
const p = (i + j) / 2;
2016-11-02 15:10:44 -07:00
while (i <= j) {
while (cmp(array[i], array[p]) == Cmp.Less) {
2016-11-02 15:10:44 -07:00
i += 1;
}
while (cmp(array[j], array[p]) == Cmp.Greater) {
2016-11-02 15:10:44 -07:00
j -= 1;
}
if (i <= j) {
const tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i += 1;
if (j > 0) j -= 1;
}
}
if (left < j) quicksort(T, array, left, j, cmp);
if (i < right) quicksort(T, array, i, right, cmp);
2016-11-02 15:10:44 -07:00
}
2017-01-16 11:23:32 -08:00
pub fn i32asc(a: &const i32, b: &const i32) -> Cmp {
return if (*a > *b) Cmp.Greater else if (*a < *b) Cmp.Less else Cmp.Equal
}
2017-01-16 11:23:32 -08:00
pub fn i32desc(a: &const i32, b: &const i32) -> Cmp {
reverse(i32asc(a, b))
}
2017-01-16 11:23:32 -08:00
pub fn u8asc(a: &const u8, b: &const u8) -> Cmp {
if (*a > *b) Cmp.Greater else if (*a < *b) Cmp.Less else Cmp.Equal
}
2017-01-16 11:23:32 -08:00
pub fn u8desc(a: &const u8, b: &const u8) -> Cmp {
reverse(u8asc(a, b))
}
fn reverse(was: Cmp) -> Cmp {
2017-01-16 11:23:32 -08:00
if (was == Cmp.Greater) Cmp.Less else if (was == Cmp.Less) Cmp.Greater else Cmp.Equal
}
// ---------------------------------------
// tests
2016-11-02 15:10:44 -07:00
fn testSort() {
2017-01-16 11:23:32 -08:00
@setFnTest(this);
2016-11-02 15:10:44 -07:00
const u8cases = [][][]u8 {
[][]u8{"", ""},
[][]u8{"a", "a"},
[][]u8{"az", "az"},
[][]u8{"za", "az"},
[][]u8{"asdf", "adfs"},
[][]u8{"one", "eno"},
};
for (u8cases) |case| {
2017-01-16 11:23:32 -08:00
var buf: [8]u8 = undefined;
const slice = buf[0...case[0].len];
mem.copy(u8, slice, case[0]);
sort(u8, slice, u8asc);
assert(mem.eql(slice, case[1]));
2016-11-02 15:10:44 -07:00
}
const i32cases = [][][]i32 {
[][]i32{[]i32{}, []i32{}},
[][]i32{[]i32{1}, []i32{1}},
[][]i32{[]i32{0, 1}, []i32{0, 1}},
[][]i32{[]i32{1, 0}, []i32{0, 1}},
[][]i32{[]i32{1, -1, 0}, []i32{-1, 0, 1}},
[][]i32{[]i32{2, 1, 3}, []i32{1, 2, 3}},
};
for (i32cases) |case| {
2017-01-16 11:23:32 -08:00
var buf: [8]i32 = undefined;
const slice = buf[0...case[0].len];
mem.copy(i32, slice, case[0]);
sort(i32, slice, i32asc);
assert(mem.eql(slice, case[1]));
2016-11-02 15:10:44 -07:00
}
}
fn testSortDesc() {
2017-01-16 11:23:32 -08:00
@setFnTest(this);
2017-01-16 11:23:32 -08:00
const rev_cases = [][][]i32 {
[][]i32{[]i32{}, []i32{}},
[][]i32{[]i32{1}, []i32{1}},
[][]i32{[]i32{0, 1}, []i32{1, 0}},
[][]i32{[]i32{1, 0}, []i32{1, 0}},
[][]i32{[]i32{1, -1, 0}, []i32{1, 0, -1}},
[][]i32{[]i32{2, 1, 3}, []i32{3, 2, 1}},
};
2017-01-16 11:23:32 -08:00
for (rev_cases) |case| {
var buf: [8]i32 = undefined;
const slice = buf[0...case[0].len];
mem.copy(i32, slice, case[0]);
sort(i32, slice, i32desc);
assert(mem.eql(slice, case[1]));
}
}