Skip to content

Commit a8999b7

Browse files
David Rajchenbach-Tellerbrson
David Rajchenbach-Teller
authored andcommitted
---
yaml --- r: 6148 b: refs/heads/master c: b17847b h: refs/heads/master v: v3
1 parent c6bf174 commit a8999b7

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 57425b575c08ad2fb53c7a08dc29c224f0f4502b
2+
refs/heads/master: b17847b2323167b557b84b088f0c865724829c30

trunk/src/lib/uint.rs

+61-9
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,54 @@ pure fn mul(x: uint, y: uint) -> uint { ret x * y; }
3434
/* Function: div */
3535
pure fn div(x: uint, y: uint) -> uint { ret x / y; }
3636

37-
/**
38-
* Divide two numbers, return the result, rounded up.
39-
*/
37+
/* Function: div_ceil
38+
39+
Divide two numbers, return the result, rounded up.
40+
41+
Parameters:
42+
x - an integer
43+
y - an integer distinct from 0u
44+
45+
Return:
46+
The smallest integer `q` such that `x/y <= q`.
47+
*/
4048
pure fn div_ceil(x: uint, y: uint) -> uint {
4149
let div = div(x, y);
4250
if x % y == 0u { ret div;}
4351
else { ret div + 1u; }
4452
}
4553

46-
/**
47-
* Divide two numbers, return the result, rounded to the closest integer.
48-
*/
54+
/* Function: div_ceil
55+
56+
Divide two numbers, return the result, rounded to the closest integer.
57+
58+
Parameters:
59+
x - an integer
60+
y - an integer distinct from 0u
61+
62+
Return:
63+
The integer `q` closest to `x/y`.
64+
*/
4965
pure fn div_round(x: uint, y: uint) -> uint {
5066
let div = div(x, y);
5167
if x % y * 2u < y { ret div;}
5268
else { ret div + 1u; }
5369
}
5470

55-
/**
56-
* Divide two numbers, return the result, rounded down.
57-
*/
71+
/* Function: div_ceil
72+
73+
Divide two numbers, return the result, rounded down.
74+
75+
Parameters:
76+
x - an integer
77+
y - an integer distinct from 0u
78+
79+
Note: This is the same function as `div`.
80+
81+
Return:
82+
The smallest integer `q` such that `x/y <= q`. This
83+
is either `x/y` or `x/y + 1`.
84+
*/
5885
pure fn div_floor(x: uint, y: uint) -> uint { ret x / y; }
5986

6087
/* Function: rem */
@@ -88,6 +115,31 @@ fn range(lo: uint, hi: uint, it: block(uint)) {
88115
while i < hi { it(i); i += 1u; }
89116
}
90117

118+
/*
119+
Function: loop
120+
121+
Iterate over the range [`lo`..`hi`), or stop when requested
122+
123+
Parameters:
124+
lo - The integer at which to start the loop (included)
125+
hi - The integer at which to stop the loop (excluded)
126+
it - A block to execute with each consecutive integer of the range.
127+
Return `true` to continue, `false` to stop.
128+
129+
Returns:
130+
131+
`true` If execution proceeded correctly, `false` if it was interrupted,
132+
that is if `it` returned `false` at any point.
133+
*/
134+
fn loop(lo: uint, hi: uint, it: block(uint) -> bool) -> bool {
135+
let i = lo;
136+
while i < hi {
137+
if (!it(i)) { ret false; }
138+
i += 1u;
139+
}
140+
ret true;
141+
}
142+
91143
/*
92144
Function: next_power_of_two
93145

0 commit comments

Comments
 (0)