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");
|
2017-06-07 19:56:57 -07:00
|
|
|
const math = @import("math/index.zig");
|
2016-11-02 15:52:00 -07:00
|
|
|
|
|
|
|
pub const Cmp = math.Cmp;
|
2016-11-02 15:10:44 -07:00
|
|
|
|
2017-12-01 15:11:39 -08:00
|
|
|
/// Stable sort using O(1) space. Currently implemented as insertion sort.
|
|
|
|
pub fn sort_stable(comptime T: type, array: []T, comptime cmp: fn(a: &const T, b: &const T)->Cmp) {
|
|
|
|
{var i: usize = 1; while (i < array.len) : (i += 1) {
|
|
|
|
const x = array[i];
|
|
|
|
var j: usize = i;
|
|
|
|
while (j > 0 and cmp(array[j - 1], x) == Cmp.Greater) : (j -= 1) {
|
|
|
|
array[j] = array[j - 1];
|
|
|
|
}
|
|
|
|
array[j] = x;
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2017-12-02 19:31:42 -08:00
|
|
|
/// Unstable sort using O(n) stack space. Currently implemented as quicksort.
|
2017-01-22 16:51:37 -08: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) {
|
2016-11-02 21:10:33 -07:00
|
|
|
quicksort(T, array, 0, array.len - 1, cmp);
|
2016-11-02 15:10:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08: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) {
|
2016-11-02 21:10:33 -07:00
|
|
|
while (cmp(array[i], array[p]) == Cmp.Less) {
|
2016-11-02 15:10:44 -07:00
|
|
|
i += 1;
|
|
|
|
}
|
2016-11-02 21:10:33 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 21:10:33 -07:00
|
|
|
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
|
2016-11-02 21:10:33 -07:00
|
|
|
}
|
2016-11-02 15:52:00 -07:00
|
|
|
|
2017-01-16 11:23:32 -08:00
|
|
|
pub fn i32desc(a: &const i32, b: &const i32) -> Cmp {
|
|
|
|
reverse(i32asc(a, b))
|
2016-11-02 15:52:00 -07:00
|
|
|
}
|
|
|
|
|
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
|
2016-11-02 21:10:33 -07:00
|
|
|
}
|
2016-11-02 15:52:00 -07:00
|
|
|
|
2017-01-16 11:23:32 -08:00
|
|
|
pub fn u8desc(a: &const u8, b: &const u8) -> Cmp {
|
|
|
|
reverse(u8asc(a, b))
|
2016-11-02 21:10:33 -07:00
|
|
|
}
|
2016-11-02 15:52:00 -07:00
|
|
|
|
2016-11-02 21:10:33 -07:00
|
|
|
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
|
2016-11-02 15:52:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
// tests
|
|
|
|
|
2017-12-01 15:11:39 -08:00
|
|
|
test "stable sort" {
|
|
|
|
testStableSort();
|
2017-12-01 18:54:01 -08:00
|
|
|
// TODO: uncomment this after https://github.com/zig-lang/zig/issues/639
|
|
|
|
//comptime testStableSort();
|
2017-12-01 15:11:39 -08:00
|
|
|
}
|
|
|
|
fn testStableSort() {
|
|
|
|
var expected = []IdAndValue {
|
|
|
|
IdAndValue{.id = 0, .value = 0},
|
|
|
|
IdAndValue{.id = 1, .value = 0},
|
|
|
|
IdAndValue{.id = 2, .value = 0},
|
|
|
|
IdAndValue{.id = 0, .value = 1},
|
|
|
|
IdAndValue{.id = 1, .value = 1},
|
|
|
|
IdAndValue{.id = 2, .value = 1},
|
|
|
|
IdAndValue{.id = 0, .value = 2},
|
|
|
|
IdAndValue{.id = 1, .value = 2},
|
|
|
|
IdAndValue{.id = 2, .value = 2},
|
|
|
|
};
|
|
|
|
var cases = [][9]IdAndValue {
|
|
|
|
[]IdAndValue {
|
|
|
|
IdAndValue{.id = 0, .value = 0},
|
|
|
|
IdAndValue{.id = 0, .value = 1},
|
|
|
|
IdAndValue{.id = 0, .value = 2},
|
|
|
|
IdAndValue{.id = 1, .value = 0},
|
|
|
|
IdAndValue{.id = 1, .value = 1},
|
|
|
|
IdAndValue{.id = 1, .value = 2},
|
|
|
|
IdAndValue{.id = 2, .value = 0},
|
|
|
|
IdAndValue{.id = 2, .value = 1},
|
|
|
|
IdAndValue{.id = 2, .value = 2},
|
|
|
|
},
|
|
|
|
[]IdAndValue {
|
|
|
|
IdAndValue{.id = 0, .value = 2},
|
|
|
|
IdAndValue{.id = 0, .value = 1},
|
|
|
|
IdAndValue{.id = 0, .value = 0},
|
|
|
|
IdAndValue{.id = 1, .value = 2},
|
|
|
|
IdAndValue{.id = 1, .value = 1},
|
|
|
|
IdAndValue{.id = 1, .value = 0},
|
|
|
|
IdAndValue{.id = 2, .value = 2},
|
|
|
|
IdAndValue{.id = 2, .value = 1},
|
|
|
|
IdAndValue{.id = 2, .value = 0},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
for (cases) |*case| {
|
|
|
|
sort_stable(IdAndValue, (*case)[0..], cmpByValue);
|
|
|
|
for (*case) |item, i| {
|
|
|
|
assert(item.id == expected[i].id);
|
|
|
|
assert(item.value == expected[i].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const IdAndValue = struct {
|
|
|
|
id: i32,
|
|
|
|
value: i32,
|
|
|
|
};
|
|
|
|
fn cmpByValue(a: &const IdAndValue, b: &const IdAndValue) -> Cmp {
|
|
|
|
return i32asc(a.value, b.value);
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "testSort" {
|
2017-02-12 14:22:35 -08:00
|
|
|
const u8cases = [][]const []const u8 {
|
|
|
|
[][]const u8{"", ""},
|
|
|
|
[][]const u8{"a", "a"},
|
|
|
|
[][]const u8{"az", "az"},
|
|
|
|
[][]const u8{"za", "az"},
|
|
|
|
[][]const u8{"asdf", "adfs"},
|
|
|
|
[][]const u8{"one", "eno"},
|
2016-11-02 15:10:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
for (u8cases) |case| {
|
2017-01-16 11:23:32 -08:00
|
|
|
var buf: [8]u8 = undefined;
|
2017-05-19 07:39:59 -07:00
|
|
|
const slice = buf[0..case[0].len];
|
2017-01-16 11:23:32 -08:00
|
|
|
mem.copy(u8, slice, case[0]);
|
|
|
|
sort(u8, slice, u8asc);
|
2017-02-12 14:22:35 -08:00
|
|
|
assert(mem.eql(u8, slice, case[1]));
|
2016-11-02 15:10:44 -07:00
|
|
|
}
|
|
|
|
|
2017-02-12 14:22:35 -08:00
|
|
|
const i32cases = [][]const []const i32 {
|
|
|
|
[][]const i32{[]i32{}, []i32{}},
|
|
|
|
[][]const i32{[]i32{1}, []i32{1}},
|
|
|
|
[][]const i32{[]i32{0, 1}, []i32{0, 1}},
|
|
|
|
[][]const i32{[]i32{1, 0}, []i32{0, 1}},
|
|
|
|
[][]const i32{[]i32{1, -1, 0}, []i32{-1, 0, 1}},
|
|
|
|
[][]const i32{[]i32{2, 1, 3}, []i32{1, 2, 3}},
|
2016-11-02 15:10:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
for (i32cases) |case| {
|
2017-01-16 11:23:32 -08:00
|
|
|
var buf: [8]i32 = undefined;
|
2017-05-19 07:39:59 -07:00
|
|
|
const slice = buf[0..case[0].len];
|
2017-01-16 11:23:32 -08:00
|
|
|
mem.copy(i32, slice, case[0]);
|
|
|
|
sort(i32, slice, i32asc);
|
2017-02-12 14:22:35 -08:00
|
|
|
assert(mem.eql(i32, slice, case[1]));
|
2016-11-02 15:10:44 -07:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 15:52:00 -07:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "testSortDesc" {
|
2017-02-12 14:22:35 -08:00
|
|
|
const rev_cases = [][]const []const i32 {
|
|
|
|
[][]const i32{[]i32{}, []i32{}},
|
|
|
|
[][]const i32{[]i32{1}, []i32{1}},
|
|
|
|
[][]const i32{[]i32{0, 1}, []i32{1, 0}},
|
|
|
|
[][]const i32{[]i32{1, 0}, []i32{1, 0}},
|
|
|
|
[][]const i32{[]i32{1, -1, 0}, []i32{1, 0, -1}},
|
|
|
|
[][]const i32{[]i32{2, 1, 3}, []i32{3, 2, 1}},
|
2016-11-02 15:52:00 -07:00
|
|
|
};
|
|
|
|
|
2017-01-16 11:23:32 -08:00
|
|
|
for (rev_cases) |case| {
|
|
|
|
var buf: [8]i32 = undefined;
|
2017-05-19 07:39:59 -07:00
|
|
|
const slice = buf[0..case[0].len];
|
2017-01-16 11:23:32 -08:00
|
|
|
mem.copy(i32, slice, case[0]);
|
|
|
|
sort(i32, slice, i32desc);
|
2017-02-12 14:22:35 -08:00
|
|
|
assert(mem.eql(i32, slice, case[1]));
|
2016-11-02 15:52:00 -07:00
|
|
|
}
|
|
|
|
}
|