diff --git a/CMakeLists.txt b/CMakeLists.txt index 535263c3df..0e5d58ecc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}") diff --git a/doc/langref.md b/doc/langref.md index 0e6465098a..dc1a998eae 100644 --- a/doc/langref.md +++ b/doc/langref.md @@ -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. diff --git a/std/index.zig b/std/index.zig index 0f6fe974ef..c4a18cbbe8 100644 --- a/std/index.zig +++ b/std/index.zig @@ -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"); diff --git a/std/sort.zig b/std/sort.zig new file mode 100644 index 0000000000..562864ca89 --- /dev/null +++ b/std/sort.zig @@ -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])); + } +} diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 91f9e74db1..7ecc1ca36c 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -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'"); + } //////////////////////////////////////////////////////////////////////////////