diff options
| author | David Rajchenbach-Teller <dteller@mozilla.com> | 2011-11-05 11:12:42 +0100 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2011-11-05 13:04:34 -0700 |
| commit | b17847b2323167b557b84b088f0c865724829c30 (patch) | |
| tree | aa22ab7f85b8ebe4f0b719176be1792a124f2dd6 /src/lib | |
| parent | 57425b575c08ad2fb53c7a08dc29c224f0f4502b (diff) | |
| download | rust-b17847b2323167b557b84b088f0c865724829c30.tar.gz rust-b17847b2323167b557b84b088f0c865724829c30.zip | |
[Docfixes + feature] lib/uint.rs: Applied review suggesions, took the opportunity to add function loop
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/uint.rs | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/src/lib/uint.rs b/src/lib/uint.rs index 561acc66d66..6d904d33d7a 100644 --- a/src/lib/uint.rs +++ b/src/lib/uint.rs @@ -34,27 +34,54 @@ pure fn mul(x: uint, y: uint) -> uint { ret x * y; } /* Function: div */ pure fn div(x: uint, y: uint) -> uint { ret x / y; } -/** - * Divide two numbers, return the result, rounded up. - */ +/* Function: div_ceil + + Divide two numbers, return the result, rounded up. + + Parameters: + x - an integer + y - an integer distinct from 0u + + Return: + The smallest integer `q` such that `x/y <= q`. +*/ pure fn div_ceil(x: uint, y: uint) -> uint { let div = div(x, y); if x % y == 0u { ret div;} else { ret div + 1u; } } -/** - * Divide two numbers, return the result, rounded to the closest integer. - */ +/* Function: div_ceil + + Divide two numbers, return the result, rounded to the closest integer. + + Parameters: + x - an integer + y - an integer distinct from 0u + + Return: + The integer `q` closest to `x/y`. +*/ pure fn div_round(x: uint, y: uint) -> uint { let div = div(x, y); if x % y * 2u < y { ret div;} else { ret div + 1u; } } -/** - * Divide two numbers, return the result, rounded down. - */ +/* Function: div_ceil + + Divide two numbers, return the result, rounded down. + + Parameters: + x - an integer + y - an integer distinct from 0u + + Note: This is the same function as `div`. + + Return: + The smallest integer `q` such that `x/y <= q`. This + is either `x/y` or `x/y + 1`. +*/ pure fn div_floor(x: uint, y: uint) -> uint { ret x / y; } /* Function: rem */ @@ -89,6 +116,31 @@ fn range(lo: uint, hi: uint, it: block(uint)) { } /* +Function: loop + +Iterate over the range [`lo`..`hi`), or stop when requested + +Parameters: +lo - The integer at which to start the loop (included) +hi - The integer at which to stop the loop (excluded) +it - A block to execute with each consecutive integer of the range. +Return `true` to continue, `false` to stop. + +Returns: + +`true` If execution proceeded correctly, `false` if it was interrupted, +that is if `it` returned `false` at any point. +*/ +fn loop(lo: uint, hi: uint, it: block(uint) -> bool) -> bool { + let i = lo; + while i < hi { + if (!it(i)) { ret false; } + i += 1u; + } + ret true; +} + +/* Function: next_power_of_two Returns the smallest power of 2 greater than or equal to `n` |
