]> git.lizzy.rs Git - rust.git/blob - src/lib/int.rs
Begin documenting std and add doc generation using naturaldocs
[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 // FIXME: This is redundant
78 fn eq_alias(x: int, y: int) -> bool { ret x == y; }
79
80 /*
81 Function: range
82
83 Iterate over the range [`lo`..`hi`)
84 */
85 fn range(lo: int, hi: int, it: block(int)) {
86     while lo < hi { it(lo); lo += 1; }
87 }
88
89 /*
90 Function: parse_buf
91
92 Parse a buffer of bytes
93
94 Parameters:
95
96 buf - A byte buffer
97 radix - The base of the number
98
99 Failure:
100
101 buf must not be empty
102 */
103 fn parse_buf(buf: [u8], radix: uint) -> int {
104     if vec::len::<u8>(buf) == 0u {
105         log_err "parse_buf(): buf is empty";
106         fail;
107     }
108     let i = vec::len::<u8>(buf) - 1u;
109     let power = 1;
110     if buf[0] == ('-' as u8) {
111         power = -1;
112         i -= 1u;
113     }
114     let n = 0;
115     while true {
116         n += (buf[i] - ('0' as u8) as int) * power;
117         power *= radix as int;
118         if i == 0u { ret n; }
119         i -= 1u;
120     }
121     fail;
122 }
123
124 /*
125 Function: from_str
126
127 Parse a string to an int
128
129 Failure:
130
131 s must not be empty
132 */
133 fn from_str(s: str) -> int { parse_buf(str::bytes(s), 10u) }
134
135 /*
136 Function: to_str
137
138 Convert to a string in a given base
139 */
140 fn to_str(n: int, radix: uint) -> str {
141     assert (0u < radix && radix <= 16u);
142     ret if n < 0 {
143             "-" + uint::to_str(-n as uint, radix)
144         } else { uint::to_str(n as uint, radix) };
145 }
146
147 /*
148 Function: str
149
150 Convert to a string
151 */
152 fn str(i: int) -> str { ret to_str(i, 10u); }
153
154 /*
155 Function: pow
156
157 Returns `base` raised to the power of `exponent`
158 */
159 fn pow(base: int, exponent: uint) -> int {
160     if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
161     if base     == 0  { ret 0; }
162     let my_pow  = exponent;
163     let acc     = 1;
164     let multiplier = base;
165     while(my_pow > 0u) {
166       if my_pow % 2u == 1u {
167          acc *= multiplier;
168       }
169       my_pow     /= 2u;
170       multiplier *= multiplier;
171     }
172     ret acc;
173 }
174 // Local Variables:
175 // mode: rust;
176 // fill-column: 78;
177 // indent-tabs-mode: nil
178 // c-basic-offset: 4
179 // buffer-file-coding-system: utf-8-unix
180 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
181 // End: