2012-04-14 17:21:10 -07:00
|
|
|
type T = int;
|
2012-02-11 23:18:26 -08:00
|
|
|
|
2012-04-14 17:21:10 -07:00
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
const bits: T = 32 as T;
|
2011-12-13 16:25:51 -08:00
|
|
|
|
2012-04-14 17:21:10 -07:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
const bits: T = 64 as T;
|
2011-12-13 16:25:51 -08:00
|
|
|
|
2012-03-06 19:09:32 -08:00
|
|
|
#[doc = "Produce a uint suitable for use in a hash table"]
|
2012-06-12 16:16:47 -07:00
|
|
|
pure fn hash(&&x: int) -> uint { ret x as uint; }
|
2011-12-13 16:25:51 -08:00
|
|
|
|
2012-03-06 19:09:32 -08:00
|
|
|
#[doc = "Returns `base` raised to the power of `exponent`"]
|
2011-12-13 16:25:51 -08:00
|
|
|
fn pow(base: int, exponent: uint) -> int {
|
2012-06-29 16:26:56 -07:00
|
|
|
if exponent == 0u { ret 1; } //Not mathemtically true if ~[base == 0]
|
2011-12-13 16:25:51 -08:00
|
|
|
if base == 0 { ret 0; }
|
2012-03-06 20:48:40 -08:00
|
|
|
let mut my_pow = exponent;
|
|
|
|
let mut acc = 1;
|
|
|
|
let mut multiplier = base;
|
2011-12-13 16:25:51 -08:00
|
|
|
while(my_pow > 0u) {
|
|
|
|
if my_pow % 2u == 1u {
|
|
|
|
acc *= multiplier;
|
|
|
|
}
|
|
|
|
my_pow /= 2u;
|
|
|
|
multiplier *= multiplier;
|
|
|
|
}
|
|
|
|
ret acc;
|
|
|
|
}
|
2012-01-17 17:28:21 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pow() {
|
|
|
|
assert (pow(0, 0u) == 1);
|
|
|
|
assert (pow(0, 1u) == 0);
|
|
|
|
assert (pow(0, 2u) == 0);
|
|
|
|
assert (pow(-1, 0u) == 1);
|
|
|
|
assert (pow(1, 0u) == 1);
|
|
|
|
assert (pow(-3, 2u) == 9);
|
|
|
|
assert (pow(-3, 3u) == -27);
|
|
|
|
assert (pow(4, 9u) == 262144);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_overflows() {
|
|
|
|
assert (max_value > 0);
|
|
|
|
assert (min_value <= 0);
|
|
|
|
assert (min_value + max_value + 1 == 0);
|
|
|
|
}
|