authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-16 14:23:32-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-16 14:23:32-05:00
logc715309bc5de874d3d418c80095d50f7efc17001
treec795a49970383a38f5d2715e09bff99f68371d91
parent4cbeb87e83135e0a1ecbfb90170761ad6e5adb7a
parent1e0b493276bd645e0162fb5edac03bbc8911d0b1

Merge branch 'master' into ir-merge


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}")
223223install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")
224224install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")
225225install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}")
226install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}")
226227install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")
227228install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner.zig" DESTINATION "${ZIG_STD_DEST}")
228229install(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.
384384
385385The allocated memory contents are undefined.
386386
387### @typeof(expression) -> type
387### @typeOf(expression) -> type
388388
389389This function returns a compile-time constant, which is the type of the
390390expression passed as an argument. The expression is *not evaluated*.
391391
392### @sizeof(inline T: type) -> (number literal)
392### @sizeOf(inline T: type) -> (number literal)
393393
394394This function returns the number of bytes it takes to store T in memory.
395395
396396The result is a target-specific compile time constant.
397397
398### @alignof(inline T: type) -> (number literal)
398### @alignOf(inline T: type) -> (number literal)
399399
400400This function returns the number of bytes that this type should be aligned to
401401for the current target.
std/index.zig+1
......@@ -4,6 +4,7 @@ pub const os = @import("os.zig");
44pub const math = @import("math.zig");
55pub const str = @import("str.zig");
66pub const cstr = @import("cstr.zig");
7pub const sort = @import("sort.zig");
78pub const net = @import("net.zig");
89pub const list = @import("list.zig");
910pub const hash_map = @import("hash_map.zig");
std/sort.zig created+119
......@@ -0,0 +1,119 @@
1const assert = @import("debug.zig").assert;
2const str = @import("str.zig");
3const mem = @import("mem.zig");
4const math = @import("math.zig");
5
6pub const Cmp = math.Cmp;
7
8pub 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
14fn 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
39pub 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
43pub fn i32desc(a: &const i32, b: &const i32) -> Cmp {
44 reverse(i32asc(a, b))
45}
46
47pub 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
51pub fn u8desc(a: &const u8, b: &const u8) -> Cmp {
52 reverse(u8asc(a, b))
53}
54
55fn 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
62fn 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
100fn 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() { }
15901590 )SOURCE");
15911591 }
15921592
1593 add_compile_fail_case("container init with non-type", R"SOURCE(
1594const zero: i32 = 0;
1595const a = zero{1};
1596 )SOURCE", 1, ".tmp_source.zig:3:11: error: expected type, found 'i32'");
1597
15931598}
15941599
15951600//////////////////////////////////////////////////////////////////////////////