authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-09 08:58:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-10 20:53:57-04:00
log9e92dbdd0889a68e32cde85c1a07767200a9ad7e
tree3011d68e950e0e1305c7bc8cc65d9b0be2b38647
parent100990b052be249c23743140d19ced37d463b51d

std: use parameter type inference on min and max functions


5 files changed, 12 insertions(+), 10 deletions(-)

src/analyze.cpp+1-2
...@@ -988,6 +988,7 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor...@@ -988,6 +988,7 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
988 case TypeTableEntryIdNullLit:988 case TypeTableEntryIdNullLit:
989 case TypeTableEntryIdNamespace:989 case TypeTableEntryIdNamespace:
990 case TypeTableEntryIdGenericFn:990 case TypeTableEntryIdGenericFn:
991 case TypeTableEntryIdVar:
991 fn_proto->skip = true;992 fn_proto->skip = true;
992 add_node_error(g, fn_proto->return_type,993 add_node_error(g, fn_proto->return_type,
993 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));994 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));
...@@ -1009,8 +1010,6 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor...@@ -1009,8 +1010,6 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
1009 case TypeTableEntryIdFn:1010 case TypeTableEntryIdFn:
1010 case TypeTableEntryIdTypeDecl:1011 case TypeTableEntryIdTypeDecl:
1011 break;1012 break;
1012 case TypeTableEntryIdVar:
1013 zig_panic("TODO var return type");
1014 }1013 }
10151014
10161015
std/hash_map.zig+3-4
...@@ -180,8 +180,8 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b...@@ -180,8 +180,8 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
180 if (entry.distance_from_start_index < distance_from_start_index) {180 if (entry.distance_from_start_index < distance_from_start_index) {
181 // robin hood to the rescue181 // robin hood to the rescue
182 const tmp = *entry;182 const tmp = *entry;
183 hm.max_distance_from_start_index = math.max(usize,183 hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index,
184 hm.max_distance_from_start_index, distance_from_start_index);184 distance_from_start_index);
185 *entry = Entry {185 *entry = Entry {
186 .used = true,186 .used = true,
187 .distance_from_start_index = distance_from_start_index,187 .distance_from_start_index = distance_from_start_index,
...@@ -201,8 +201,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b...@@ -201,8 +201,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
201 hm.size += 1;201 hm.size += 1;
202 }202 }
203203
204 hm.max_distance_from_start_index = math.max(usize, distance_from_start_index,204 hm.max_distance_from_start_index = math.max(distance_from_start_index, hm.max_distance_from_start_index);
205 hm.max_distance_from_start_index);
206 *entry = Entry {205 *entry = Entry {
207 .used = true,206 .used = true,
208 .distance_from_start_index = distance_from_start_index,207 .distance_from_start_index = distance_from_start_index,
std/io.zig+1-1
...@@ -79,7 +79,7 @@ pub struct OutStream {...@@ -79,7 +79,7 @@ pub struct OutStream {
79 const dest_space_left = os.buffer.len - os.index;79 const dest_space_left = os.buffer.len - os.index;
8080
81 while (src_bytes_left > 0) {81 while (src_bytes_left > 0) {
82 const copy_amt = math.min(usize, dest_space_left, src_bytes_left);82 const copy_amt = math.min(dest_space_left, src_bytes_left);
83 @memcpy(&os.buffer[os.index], &bytes[src_index], copy_amt);83 @memcpy(&os.buffer[os.index], &bytes[src_index], copy_amt);
84 os.index += copy_amt;84 os.index += copy_amt;
85 if (os.index == os.buffer.len) {85 if (os.index == os.buffer.len) {
std/math.zig+6-2
...@@ -4,11 +4,11 @@ pub enum Cmp {...@@ -4,11 +4,11 @@ pub enum Cmp {
4 Less,4 Less,
5}5}
66
7pub fn min(inline T: type, x: T, y: T) -> T {7pub fn min(x: var, y: var) -> @typeOf(x + y) {
8 if (x < y) x else y8 if (x < y) x else y
9}9}
1010
11pub fn max(inline T: type, x: T, y: T) -> T {11pub fn max(x: var, y: var) -> @typeOf(x + y) {
12 if (x > y) x else y12 if (x > y) x else y
13}13}
1414
...@@ -25,3 +25,7 @@ pub fn subOverflow(inline T: type, a: T, b: T) -> %T {...@@ -25,3 +25,7 @@ pub fn subOverflow(inline T: type, a: T, b: T) -> %T {
25 var answer: T = undefined;25 var answer: T = undefined;
26 if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer26 if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer
27}27}
28pub fn shlOverflow(inline T: type, a: T, b: T) -> %T {
29 var answer: T = undefined;
30 if (@shlWithOverflow(T, a, b, &answer)) error.Overflow else answer
31}
std/mem.zig+1-1
...@@ -50,7 +50,7 @@ pub fn copy(inline T: type, dest: []T, source: []const T) {...@@ -50,7 +50,7 @@ pub fn copy(inline T: type, dest: []T, source: []const T) {
50/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,50/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
51/// memory b, respectively.51/// memory b, respectively.
52pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {52pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
53 const n = math.min(usize, a.len, b.len);53 const n = math.min(a.len, b.len);
54 var i: usize = 0;54 var i: usize = 0;
55 while (i < n; i += 1) {55 while (i < n; i += 1) {
56 if (a[i] == b[i]) continue;56 if (a[i] == b[i]) continue;