]> git.lizzy.rs Git - rust.git/blob - src/lib/int.rs
Remove std::int::eq_alias
[rust.git] / src / lib / int.rs
1 /*
2 Module: int
3 */
4
5 /*
6 Function: max_value
7
8 The maximum value of an integer
9 */
10 fn max_value() -> int {
11   ret min_value() - 1;
12 }
13
14 /*
15 Function: min_value
16
17 The minumum value of an integer
18 */
19 fn min_value() -> int {
20   ret (-1 << (sys::size_of::<int>()  * 8u as int - 1)) as int;
21 }
22
23 /* Function: add */
24 pure fn add(x: int, y: int) -> int { ret x + y; }
25
26 /* Function: sub */
27 pure fn sub(x: int, y: int) -> int { ret x - y; }
28
29 /* Function: mul */
30 pure fn mul(x: int, y: int) -> int { ret x * y; }
31
32 /* Function: div */
33 pure fn div(x: int, y: int) -> int { ret x / y; }
34
35 /* Function: rem */
36 pure fn rem(x: int, y: int) -> int { ret x % y; }
37
38 /* Predicate: lt */
39 pure fn lt(x: int, y: int) -> bool { ret x < y; }
40
41 /* Predicate: le */
42 pure fn le(x: int, y: int) -> bool { ret x <= y; }
43
44 /* Predicate: eq */
45 pure fn eq(x: int, y: int) -> bool { ret x == y; }
46
47 /* Predicate: ne */
48 pure fn ne(x: int, y: int) -> bool { ret x != y; }
49
50 /* Predicate: ge */
51 pure fn ge(x: int, y: int) -> bool { ret x >= y; }
52
53 /* Predicate: gt */
54 pure fn gt(x: int, y: int) -> bool { ret x > y; }
55
56 /* Predicate: positive */
57 pure fn positive(x: int) -> bool { ret x > 0; }
58
59 /* Predicate: negative */
60 pure fn negative(x: int) -> bool { ret x < 0; }
61
62 /* Predicate: nonpositive */
63 pure fn nonpositive(x: int) -> bool { ret x <= 0; }
64
65 /* Predicate: nonnegative */
66 pure fn nonnegative(x: int) -> bool { ret x >= 0; }
67
68
69 // FIXME: Make sure this works with negative integers.
70 /*
71 Function: hash
72
73 Produce a uint suitable for use in a hash table
74 */
75 fn hash(x: int) -> uint { ret x as uint; }
76
77 /*
78 Function: range
79
80 Iterate over the range [`lo`..`hi`)
81 */
82 fn range(lo: int, hi: int, it: block(int)) {
83     while lo < hi { it(lo); lo += 1; }
84 }
85
86 /*
87 Function: parse_buf
88
89 Parse a buffer of bytes
90
91 Parameters:
92
93 buf - A byte buffer
94 radix - The base of the number
95
96 Failure:
97
98 buf must not be empty
99 */
100 fn parse_buf(buf: [u8], radix: uint) -> int {
101     if vec::len::<u8>(buf) == 0u {
102         log_err "parse_buf(): buf is empty";
103         fail;
104     }
105     let i = vec::len::<u8>(buf) - 1u;
106     let power = 1;
107     if buf[0] == ('-' as u8) {
108         power = -1;
109         i -= 1u;
110     }
111     let n = 0;
112     while true {
113         n += (buf[i] - ('0' as u8) as int) * power;
114         power *= radix as int;
115         if i == 0u { ret n; }
116         i -= 1u;
117     }
118     fail;
119 }
120
121 /*
122 Function: from_str
123
124 Parse a string to an int
125
126 Failure:
127
128 s must not be empty
129 */
130 fn from_str(s: str) -> int { parse_buf(str::bytes(s), 10u) }
131
132 /*
133 Function: to_str
134
135 Convert to a string in a given base
136 */
137 fn to_str(n: int, radix: uint) -> str {
138     assert (0u < radix && radix <= 16u);
139     ret if n < 0 {
140             "-" + uint::to_str(-n as uint, radix)
141         } else { uint::to_str(n as uint, radix) };
142 }
143
144 /*
145 Function: str
146
147 Convert to a string
148 */
149 fn str(i: int) -> str { ret to_str(i, 10u); }
150
151 /*
152 Function: pow
153
154 Returns `base` raised to the power of `exponent`
155 */
156 fn pow(base: int, exponent: uint) -> int {
157     if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
158     if base     == 0  { ret 0; }
159     let my_pow  = exponent;
160     let acc     = 1;
161     let multiplier = base;
162     while(my_pow > 0u) {
163       if my_pow % 2u == 1u {
164          acc *= multiplier;
165       }
166       my_pow     /= 2u;
167       multiplier *= multiplier;
168     }
169     ret acc;
170 }
171 // Local Variables:
172 // mode: rust;
173 // fill-column: 78;
174 // indent-tabs-mode: nil
175 // c-basic-offset: 4
176 // buffer-file-coding-system: utf-8-unix
177 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
178 // End: