]> git.lizzy.rs Git - rust.git/blob - src/lib/int.rs
Remove last uses of iterators from stdlib
[rust.git] / src / lib / int.rs
1 fn max_value() -> int {
2   ret min_value() - 1;
3 }
4
5 fn min_value() -> int {
6   ret (-1 << (sys::size_of::<int>()  * 8u as int - 1)) as int;
7 }
8
9
10 pure fn add(x: int, y: int) -> int { ret x + y; }
11
12 pure fn sub(x: int, y: int) -> int { ret x - y; }
13
14 pure fn mul(x: int, y: int) -> int { ret x * y; }
15
16 pure fn div(x: int, y: int) -> int { ret x / y; }
17
18 pure fn rem(x: int, y: int) -> int { ret x % y; }
19
20 pure fn lt(x: int, y: int) -> bool { ret x < y; }
21
22 pure fn le(x: int, y: int) -> bool { ret x <= y; }
23
24 pure fn eq(x: int, y: int) -> bool { ret x == y; }
25
26 pure fn ne(x: int, y: int) -> bool { ret x != y; }
27
28 pure fn ge(x: int, y: int) -> bool { ret x >= y; }
29
30 pure fn gt(x: int, y: int) -> bool { ret x > y; }
31
32 pure fn positive(x: int) -> bool { ret x > 0; }
33
34 pure fn negative(x: int) -> bool { ret x < 0; }
35
36 pure fn nonpositive(x: int) -> bool { ret x <= 0; }
37
38 pure fn nonnegative(x: int) -> bool { ret x >= 0; }
39
40
41 // FIXME: Make sure this works with negative integers.
42 fn hash(x: int) -> uint { ret x as uint; }
43
44 fn eq_alias(x: int, y: int) -> bool { ret x == y; }
45
46 fn range(lo: int, hi: int, it: block(int)) {
47     while lo < hi { it(lo); lo += 1; }
48 }
49
50 fn parse_buf(buf: [u8], radix: uint) -> int {
51     if vec::len::<u8>(buf) == 0u {
52         log_err "parse_buf(): buf is empty";
53         fail;
54     }
55     let i = vec::len::<u8>(buf) - 1u;
56     let power = 1;
57     if buf[0] == ('-' as u8) {
58         power = -1;
59         i -= 1u;
60     }
61     let n = 0;
62     while true {
63         n += (buf[i] - ('0' as u8) as int) * power;
64         power *= radix as int;
65         if i == 0u { ret n; }
66         i -= 1u;
67     }
68     fail;
69 }
70
71 fn from_str(s: str) -> int { parse_buf(str::bytes(s), 10u) }
72
73 fn to_str(n: int, radix: uint) -> str {
74     assert (0u < radix && radix <= 16u);
75     ret if n < 0 {
76             "-" + uint::to_str(-n as uint, radix)
77         } else { uint::to_str(n as uint, radix) };
78 }
79 fn str(i: int) -> str { ret to_str(i, 10u); }
80
81 fn pow(base: int, exponent: uint) -> int {
82     if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
83     if base     == 0  { ret 0; }
84     let my_pow  = exponent;
85     let acc     = 1;
86     let multiplier = base;
87     while(my_pow > 0u) {
88       if my_pow % 2u == 1u {
89          acc *= multiplier;
90       }
91       my_pow     /= 2u;
92       multiplier *= multiplier;
93     }
94     ret acc;
95 }
96 // Local Variables:
97 // mode: rust;
98 // fill-column: 78;
99 // indent-tabs-mode: nil
100 // c-basic-offset: 4
101 // buffer-file-coding-system: utf-8-unix
102 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
103 // End: