diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-05-02 18:33:18 -0400 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-05-10 02:46:18 -0400 |
| commit | 28256052a4b141350dc0fe4e2e5357137bb49706 (patch) | |
| tree | 6d5660a849451c8a39c75cebe6a4ce148c31ab49 /src/libcore/num | |
| parent | 0cde8ba684e738fa71ce62d691ba2886776e49f9 (diff) | |
| download | rust-28256052a4b141350dc0fe4e2e5357137bb49706.tar.gz rust-28256052a4b141350dc0fe4e2e5357137bb49706.zip | |
core: Use the new `for` protocol
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/int-template.rs | 35 | ||||
| -rw-r--r-- | src/libcore/num/uint-template.rs | 42 | ||||
| -rw-r--r-- | src/libcore/num/uint-template/uint.rs | 24 |
3 files changed, 88 insertions, 13 deletions
diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs index 9ee5ba4753d..f2bba6a4639 100644 --- a/src/libcore/num/int-template.rs +++ b/src/libcore/num/int-template.rs @@ -86,38 +86,63 @@ pub fn gt(x: T, y: T) -> bool { x > y } /// #[inline(always)] /// Iterate over the range [`start`,`start`+`step`..`stop`) -pub fn range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) { +pub fn _range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) -> bool { let mut i = start; if step == 0 { fail!(~"range_step called with step == 0"); } else if step > 0 { // ascending while i < stop { - if !it(i) { break } + if !it(i) { return false; } // avoiding overflow. break if i + step > max_value - if i > max_value - step { break; } + if i > max_value - step { return true; } i += step; } } else { // descending while i > stop { - if !it(i) { break } + if !it(i) { return false; } // avoiding underflow. break if i + step < min_value - if i < min_value - step { break; } + if i < min_value - step { return true; } i += step; } } + return true; +} + +#[cfg(stage0)] +pub fn range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) { + _range_step(start, stop, step, it); +} +#[cfg(not(stage0))] +pub fn range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) -> bool { + _range_step(start, stop, step, it) } #[inline(always)] +#[cfg(stage0)] /// Iterate over the range [`lo`..`hi`) pub fn range(lo: T, hi: T, it: &fn(T) -> bool) { range_step(lo, hi, 1 as T, it); } #[inline(always)] +#[cfg(not(stage0))] +/// Iterate over the range [`lo`..`hi`) +pub fn range(lo: T, hi: T, it: &fn(T) -> bool) -> bool { + range_step(lo, hi, 1 as T, it) +} + +#[inline(always)] +#[cfg(stage0)] /// Iterate over the range [`hi`..`lo`) pub fn range_rev(hi: T, lo: T, it: &fn(T) -> bool) { range_step(hi, lo, -1 as T, it); } +#[inline(always)] +#[cfg(not(stage0))] +/// Iterate over the range [`hi`..`lo`) +pub fn range_rev(hi: T, lo: T, it: &fn(T) -> bool) -> bool { + range_step(hi, lo, -1 as T, it) +} /// Computes the bitwise complement #[inline(always)] diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs index dcb0865cb9b..1c115ee5072 100644 --- a/src/libcore/num/uint-template.rs +++ b/src/libcore/num/uint-template.rs @@ -51,43 +51,69 @@ pub fn gt(x: T, y: T) -> bool { x > y } /// /// Iterate over the range [`start`,`start`+`step`..`stop`) /// -pub fn range_step(start: T, - stop: T, - step: T_SIGNED, - it: &fn(T) -> bool) { +pub fn _range_step(start: T, + stop: T, + step: T_SIGNED, + it: &fn(T) -> bool) -> bool { let mut i = start; if step == 0 { fail!(~"range_step called with step == 0"); } if step >= 0 { while i < stop { - if !it(i) { break } + if !it(i) { return false; } // avoiding overflow. break if i + step > max_value - if i > max_value - (step as T) { break; } + if i > max_value - (step as T) { return true; } i += step as T; } } else { while i > stop { - if !it(i) { break } + if !it(i) { return false; } // avoiding underflow. break if i + step < min_value - if i < min_value + ((-step) as T) { break; } + if i < min_value + ((-step) as T) { return true; } i -= -step as T; } } + return true; +} + +#[cfg(stage0)] +pub fn range_step(start: T, stop: T, step: T_SIGNED, it: &fn(T) -> bool) { + _range_step(start, stop, step, it); +} +#[cfg(not(stage0))] +pub fn range_step(start: T, stop: T, step: T_SIGNED, it: &fn(T) -> bool) -> bool { + _range_step(start, stop, step, it) } #[inline(always)] +#[cfg(stage0)] /// Iterate over the range [`lo`..`hi`) pub fn range(lo: T, hi: T, it: &fn(T) -> bool) { range_step(lo, hi, 1 as T_SIGNED, it); } #[inline(always)] +#[cfg(not(stage0))] +/// Iterate over the range [`lo`..`hi`) +pub fn range(lo: T, hi: T, it: &fn(T) -> bool) -> bool { + range_step(lo, hi, 1 as T_SIGNED, it) +} + +#[inline(always)] +#[cfg(stage0)] /// Iterate over the range [`hi`..`lo`) pub fn range_rev(hi: T, lo: T, it: &fn(T) -> bool) { range_step(hi, lo, -1 as T_SIGNED, it); } +#[inline(always)] +#[cfg(not(stage0))] +/// Iterate over the range [`hi`..`lo`) +pub fn range_rev(hi: T, lo: T, it: &fn(T) -> bool) -> bool { + range_step(hi, lo, -1 as T_SIGNED, it) +} + /// Computes the bitwise complement #[inline(always)] pub fn compl(i: T) -> T { diff --git a/src/libcore/num/uint-template/uint.rs b/src/libcore/num/uint-template/uint.rs index de882f1ee7a..d8a4ec19304 100644 --- a/src/libcore/num/uint-template/uint.rs +++ b/src/libcore/num/uint-template/uint.rs @@ -154,6 +154,7 @@ pub mod inst { return true; } + #[cfg(stage0)] impl iter::Times for uint { #[inline(always)] /// @@ -175,6 +176,29 @@ pub mod inst { } } + #[cfg(not(stage0))] + impl iter::Times for uint { + #[inline(always)] + /// + /// A convenience form for basic iteration. Given a uint `x`, + /// `for x.times { ... }` executes the given block x times. + /// + /// Equivalent to `for uint::range(0, x) |_| { ... }`. + /// + /// Not defined on all integer types to permit unambiguous + /// use with integer literals of inferred integer-type as + /// the self-value (eg. `for 100.times { ... }`). + /// + fn times(&self, it: &fn() -> bool) -> bool { + let mut i = *self; + while i > 0 { + if !it() { return false; } + i -= 1; + } + return true; + } + } + /// Returns the smallest power of 2 greater than or equal to `n` #[inline(always)] pub fn next_power_of_two(n: uint) -> uint { |
