Merge branch 'master' into ir-merge

This commit is contained in:
Andrew Kelley 2017-01-16 14:23:32 -05:00
commit c715309bc5
5 changed files with 129 additions and 3 deletions

View File

@ -223,6 +223,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_libc.zig" DESTINATION "${ZIG_STD_DEST}")

View File

@ -384,18 +384,18 @@ the memory size given that you didn't actually hit your upper bound.
The allocated memory contents are undefined.
### @typeof(expression) -> type
### @typeOf(expression) -> type
This function returns a compile-time constant, which is the type of the
expression passed as an argument. The expression is *not evaluated*.
### @sizeof(inline T: type) -> (number literal)
### @sizeOf(inline T: type) -> (number literal)
This function returns the number of bytes it takes to store T in memory.
The result is a target-specific compile time constant.
### @alignof(inline T: type) -> (number literal)
### @alignOf(inline T: type) -> (number literal)
This function returns the number of bytes that this type should be aligned to
for the current target.

View File

@ -4,6 +4,7 @@ pub const os = @import("os.zig");
pub const math = @import("math.zig");
pub const str = @import("str.zig");
pub const cstr = @import("cstr.zig");
pub const sort = @import("sort.zig");
pub const net = @import("net.zig");
pub const list = @import("list.zig");
pub const hash_map = @import("hash_map.zig");

119
std/sort.zig Normal file
View File

@ -0,0 +1,119 @@
const assert = @import("debug.zig").assert;
const str = @import("str.zig");
const mem = @import("mem.zig");
const math = @import("math.zig");
pub const Cmp = math.Cmp;
pub fn sort(inline T: type, array: []T, inline cmp: fn(a: &const T, b: &const T)->Cmp) {
if (array.len > 0) {
quicksort(T, array, 0, array.len - 1, cmp);
}
}
fn quicksort(inline T: type, array: []T, left: usize, right: usize, inline cmp: fn(a: &const T, b: &const T)->Cmp) {
var i = left;
var j = right;
const p = (i + j) / 2;
while (i <= j) {
while (cmp(array[i], array[p]) == Cmp.Less) {
i += 1;
}
while (cmp(array[j], array[p]) == Cmp.Greater) {
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);
}
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
}
pub fn i32desc(a: &const i32, b: &const i32) -> Cmp {
reverse(i32asc(a, b))
}
pub fn u8asc(a: &const u8, b: &const u8) -> Cmp {
if (*a > *b) Cmp.Greater else if (*a < *b) Cmp.Less else Cmp.Equal
}
pub fn u8desc(a: &const u8, b: &const u8) -> Cmp {
reverse(u8asc(a, b))
}
fn reverse(was: Cmp) -> Cmp {
if (was == Cmp.Greater) Cmp.Less else if (was == Cmp.Less) Cmp.Greater else Cmp.Equal
}
// ---------------------------------------
// tests
fn testSort() {
@setFnTest(this);
const u8cases = [][][]u8 {
[][]u8{"", ""},
[][]u8{"a", "a"},
[][]u8{"az", "az"},
[][]u8{"za", "az"},
[][]u8{"asdf", "adfs"},
[][]u8{"one", "eno"},
};
for (u8cases) |case| {
var buf: [8]u8 = undefined;
const slice = buf[0...case[0].len];
mem.copy(u8, slice, case[0]);
sort(u8, slice, u8asc);
assert(str.eql(slice, case[1]));
}
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| {
var buf: [8]i32 = undefined;
const slice = buf[0...case[0].len];
mem.copy(i32, slice, case[0]);
sort(i32, slice, i32asc);
assert(str.sliceEql(i32, slice, case[1]));
}
}
fn testSortDesc() {
@setFnTest(this);
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}},
};
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(str.sliceEql(i32, slice, case[1]));
}
}

View File

@ -1590,6 +1590,11 @@ fn privateFunction() { }
)SOURCE");
}
add_compile_fail_case("container init with non-type", R"SOURCE(
const zero: i32 = 0;
const a = zero{1};
)SOURCE", 1, ".tmp_source.zig:3:11: error: expected type, found 'i32'");
}
//////////////////////////////////////////////////////////////////////////////