2016-12-18 22:24:52 +00:00
|
|
|
pub const Cmp = enum {
|
2016-08-12 05:25:13 +00:00
|
|
|
Equal,
|
|
|
|
Greater,
|
|
|
|
Less,
|
2016-12-18 22:24:52 +00:00
|
|
|
};
|
2016-08-12 05:25:13 +00:00
|
|
|
|
2016-09-09 12:58:39 +00:00
|
|
|
pub fn min(x: var, y: var) -> @typeOf(x + y) {
|
2016-02-02 09:43:33 +00:00
|
|
|
if (x < y) x else y
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:58:39 +00:00
|
|
|
pub fn max(x: var, y: var) -> @typeOf(x + y) {
|
2016-05-08 23:05:41 +00:00
|
|
|
if (x > y) x else y
|
|
|
|
}
|
2016-07-25 01:35:50 +00:00
|
|
|
|
2016-12-18 05:25:26 +00:00
|
|
|
error Overflow;
|
2017-01-23 00:51:37 +00:00
|
|
|
pub fn mulOverflow(comptime T: type, a: T, b: T) -> %T {
|
2016-07-25 01:35:50 +00:00
|
|
|
var answer: T = undefined;
|
2016-08-17 05:42:50 +00:00
|
|
|
if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer
|
2016-07-25 01:35:50 +00:00
|
|
|
}
|
2017-01-23 00:51:37 +00:00
|
|
|
pub fn addOverflow(comptime T: type, a: T, b: T) -> %T {
|
2016-07-25 01:35:50 +00:00
|
|
|
var answer: T = undefined;
|
2016-08-17 05:42:50 +00:00
|
|
|
if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer
|
2016-07-25 01:35:50 +00:00
|
|
|
}
|
2017-01-23 00:51:37 +00:00
|
|
|
pub fn subOverflow(comptime T: type, a: T, b: T) -> %T {
|
2016-07-25 01:35:50 +00:00
|
|
|
var answer: T = undefined;
|
2016-08-17 05:42:50 +00:00
|
|
|
if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer
|
2016-07-25 01:35:50 +00:00
|
|
|
}
|
2017-01-23 00:51:37 +00:00
|
|
|
pub fn shlOverflow(comptime T: type, a: T, b: T) -> %T {
|
2016-09-09 12:58:39 +00:00
|
|
|
var answer: T = undefined;
|
|
|
|
if (@shlWithOverflow(T, a, b, &answer)) error.Overflow else answer
|
|
|
|
}
|