| author | |
| committer | |
| log | c715309bc5de874d3d418c80095d50f7efc17001 |
| tree | c795a49970383a38f5d2715e09bff99f68371d91 |
| parent | 4cbeb87e83135e0a1ecbfb90170761ad6e5adb7a |
| parent | 1e0b493276bd645e0162fb5edac03bbc8911d0b1 |
5 files changed, 129 insertions(+), 3 deletions(-)
CMakeLists.txt+1| ... | @@ -223,6 +223,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}") | ... | @@ -223,6 +223,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}") |
| 223 | install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}") | 223 | install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}") |
| 224 | install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}") | 224 | install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}") |
| 225 | install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}") | 225 | install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}") |
| 226 | install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}") | ||
| 226 | install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}") | 227 | install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}") |
| 227 | install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner.zig" DESTINATION "${ZIG_STD_DEST}") | 228 | install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner.zig" DESTINATION "${ZIG_STD_DEST}") |
| 228 | install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_libc.zig" DESTINATION "${ZIG_STD_DEST}") | 229 | install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_libc.zig" DESTINATION "${ZIG_STD_DEST}") |
doc/langref.md+3-3| ... | @@ -384,18 +384,18 @@ the memory size given that you didn't actually hit your upper bound. | ... | @@ -384,18 +384,18 @@ the memory size given that you didn't actually hit your upper bound. |
| 384 | 384 | ||
| 385 | The allocated memory contents are undefined. | 385 | The allocated memory contents are undefined. |
| 386 | 386 | ||
| 387 | ### @typeof(expression) -> type | 387 | ### @typeOf(expression) -> type |
| 388 | 388 | ||
| 389 | This function returns a compile-time constant, which is the type of the | 389 | This function returns a compile-time constant, which is the type of the |
| 390 | expression passed as an argument. The expression is *not evaluated*. | 390 | expression passed as an argument. The expression is *not evaluated*. |
| 391 | 391 | ||
| 392 | ### @sizeof(inline T: type) -> (number literal) | 392 | ### @sizeOf(inline T: type) -> (number literal) |
| 393 | 393 | ||
| 394 | This function returns the number of bytes it takes to store T in memory. | 394 | This function returns the number of bytes it takes to store T in memory. |
| 395 | 395 | ||
| 396 | The result is a target-specific compile time constant. | 396 | The result is a target-specific compile time constant. |
| 397 | 397 | ||
| 398 | ### @alignof(inline T: type) -> (number literal) | 398 | ### @alignOf(inline T: type) -> (number literal) |
| 399 | 399 | ||
| 400 | This function returns the number of bytes that this type should be aligned to | 400 | This function returns the number of bytes that this type should be aligned to |
| 401 | for the current target. | 401 | for the current target. |
std/index.zig+1| ... | @@ -4,6 +4,7 @@ pub const os = @import("os.zig"); | ... | @@ -4,6 +4,7 @@ pub const os = @import("os.zig"); |
| 4 | pub const math = @import("math.zig"); | 4 | pub const math = @import("math.zig"); |
| 5 | pub const str = @import("str.zig"); | 5 | pub const str = @import("str.zig"); |
| 6 | pub const cstr = @import("cstr.zig"); | 6 | pub const cstr = @import("cstr.zig"); |
| 7 | pub const sort = @import("sort.zig"); | ||
| 7 | pub const net = @import("net.zig"); | 8 | pub const net = @import("net.zig"); |
| 8 | pub const list = @import("list.zig"); | 9 | pub const list = @import("list.zig"); |
| 9 | pub const hash_map = @import("hash_map.zig"); | 10 | pub const hash_map = @import("hash_map.zig"); |
std/sort.zig created+119| ... | @@ -0,0 +1,119 @@ | ||
| 1 | const assert = @import("debug.zig").assert; | ||
| 2 | const str = @import("str.zig"); | ||
| 3 | const mem = @import("mem.zig"); | ||
| 4 | const math = @import("math.zig"); | ||
| 5 | |||
| 6 | pub const Cmp = math.Cmp; | ||
| 7 | |||
| 8 | pub fn sort(inline T: type, array: []T, inline cmp: fn(a: &const T, b: &const T)->Cmp) { | ||
| 9 | if (array.len > 0) { | ||
| 10 | quicksort(T, array, 0, array.len - 1, cmp); | ||
| 11 | } | ||
| 12 | } | ||
| 13 | |||
| 14 | fn quicksort(inline T: type, array: []T, left: usize, right: usize, inline cmp: fn(a: &const T, b: &const T)->Cmp) { | ||
| 15 | var i = left; | ||
| 16 | var j = right; | ||
| 17 | const p = (i + j) / 2; | ||
| 18 | |||
| 19 | while (i <= j) { | ||
| 20 | while (cmp(array[i], array[p]) == Cmp.Less) { | ||
| 21 | i += 1; | ||
| 22 | } | ||
| 23 | while (cmp(array[j], array[p]) == Cmp.Greater) { | ||
| 24 | j -= 1; | ||
| 25 | } | ||
| 26 | if (i <= j) { | ||
| 27 | const tmp = array[i]; | ||
| 28 | array[i] = array[j]; | ||
| 29 | array[j] = tmp; | ||
| 30 | i += 1; | ||
| 31 | if (j > 0) j -= 1; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | if (left < j) quicksort(T, array, left, j, cmp); | ||
| 36 | if (i < right) quicksort(T, array, i, right, cmp); | ||
| 37 | } | ||
| 38 | |||
| 39 | pub fn i32asc(a: &const i32, b: &const i32) -> Cmp { | ||
| 40 | return if (*a > *b) Cmp.Greater else if (*a < *b) Cmp.Less else Cmp.Equal | ||
| 41 | } | ||
| 42 | |||
| 43 | pub fn i32desc(a: &const i32, b: &const i32) -> Cmp { | ||
| 44 | reverse(i32asc(a, b)) | ||
| 45 | } | ||
| 46 | |||
| 47 | pub fn u8asc(a: &const u8, b: &const u8) -> Cmp { | ||
| 48 | if (*a > *b) Cmp.Greater else if (*a < *b) Cmp.Less else Cmp.Equal | ||
| 49 | } | ||
| 50 | |||
| 51 | pub fn u8desc(a: &const u8, b: &const u8) -> Cmp { | ||
| 52 | reverse(u8asc(a, b)) | ||
| 53 | } | ||
| 54 | |||
| 55 | fn reverse(was: Cmp) -> Cmp { | ||
| 56 | if (was == Cmp.Greater) Cmp.Less else if (was == Cmp.Less) Cmp.Greater else Cmp.Equal | ||
| 57 | } | ||
| 58 | |||
| 59 | // --------------------------------------- | ||
| 60 | // tests | ||
| 61 | |||
| 62 | fn testSort() { | ||
| 63 | @setFnTest(this); | ||
| 64 | |||
| 65 | const u8cases = [][][]u8 { | ||
| 66 | [][]u8{"", ""}, | ||
| 67 | [][]u8{"a", "a"}, | ||
| 68 | [][]u8{"az", "az"}, | ||
| 69 | [][]u8{"za", "az"}, | ||
| 70 | [][]u8{"asdf", "adfs"}, | ||
| 71 | [][]u8{"one", "eno"}, | ||
| 72 | }; | ||
| 73 | |||
| 74 | for (u8cases) |case| { | ||
| 75 | var buf: [8]u8 = undefined; | ||
| 76 | const slice = buf[0...case[0].len]; | ||
| 77 | mem.copy(u8, slice, case[0]); | ||
| 78 | sort(u8, slice, u8asc); | ||
| 79 | assert(str.eql(slice, case[1])); | ||
| 80 | } | ||
| 81 | |||
| 82 | const i32cases = [][][]i32 { | ||
| 83 | [][]i32{[]i32{}, []i32{}}, | ||
| 84 | [][]i32{[]i32{1}, []i32{1}}, | ||
| 85 | [][]i32{[]i32{0, 1}, []i32{0, 1}}, | ||
| 86 | [][]i32{[]i32{1, 0}, []i32{0, 1}}, | ||
| 87 | [][]i32{[]i32{1, -1, 0}, []i32{-1, 0, 1}}, | ||
| 88 | [][]i32{[]i32{2, 1, 3}, []i32{1, 2, 3}}, | ||
| 89 | }; | ||
| 90 | |||
| 91 | for (i32cases) |case| { | ||
| 92 | var buf: [8]i32 = undefined; | ||
| 93 | const slice = buf[0...case[0].len]; | ||
| 94 | mem.copy(i32, slice, case[0]); | ||
| 95 | sort(i32, slice, i32asc); | ||
| 96 | assert(str.sliceEql(i32, slice, case[1])); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | fn testSortDesc() { | ||
| 101 | @setFnTest(this); | ||
| 102 | |||
| 103 | const rev_cases = [][][]i32 { | ||
| 104 | [][]i32{[]i32{}, []i32{}}, | ||
| 105 | [][]i32{[]i32{1}, []i32{1}}, | ||
| 106 | [][]i32{[]i32{0, 1}, []i32{1, 0}}, | ||
| 107 | [][]i32{[]i32{1, 0}, []i32{1, 0}}, | ||
| 108 | [][]i32{[]i32{1, -1, 0}, []i32{1, 0, -1}}, | ||
| 109 | [][]i32{[]i32{2, 1, 3}, []i32{3, 2, 1}}, | ||
| 110 | }; | ||
| 111 | |||
| 112 | for (rev_cases) |case| { | ||
| 113 | var buf: [8]i32 = undefined; | ||
| 114 | const slice = buf[0...case[0].len]; | ||
| 115 | mem.copy(i32, slice, case[0]); | ||
| 116 | sort(i32, slice, i32desc); | ||
| 117 | assert(str.sliceEql(i32, slice, case[1])); | ||
| 118 | } | ||
| 119 | } | ||
test/run_tests.cpp+5| ... | @@ -1590,6 +1590,11 @@ fn privateFunction() { } | ... | @@ -1590,6 +1590,11 @@ fn privateFunction() { } |
| 1590 | )SOURCE"); | 1590 | )SOURCE"); |
| 1591 | } | 1591 | } |
| 1592 | 1592 | ||
| 1593 | add_compile_fail_case("container init with non-type", R"SOURCE( | ||
| 1594 | const zero: i32 = 0; | ||
| 1595 | const a = zero{1}; | ||
| 1596 | )SOURCE", 1, ".tmp_source.zig:3:11: error: expected type, found 'i32'"); | ||
| 1597 | |||
| 1593 | } | 1598 | } |
| 1594 | 1599 | ||
| 1595 | ////////////////////////////////////////////////////////////////////////////// | 1600 | ////////////////////////////////////////////////////////////////////////////// |