mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 07:44:10 +00:00
std::num: Remove range_step
for each numeric type
Replaced by `std::iter::range_step`
This commit is contained in:
parent
ad74fde62f
commit
6e3d5c62e7
@ -41,101 +41,6 @@ impl CheckedDiv for $T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Range { Closed, HalfOpen }
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
///
|
|
||||||
/// Iterate through a range with a given step value.
|
|
||||||
///
|
|
||||||
/// Let `term` denote the closed interval `[stop-step,stop]` if `r` is Closed;
|
|
||||||
/// otherwise `term` denotes the half-open interval `[stop-step,stop)`.
|
|
||||||
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
|
|
||||||
/// `x_j == start + step*j`, and `x_n` lies in the interval `term`.
|
|
||||||
///
|
|
||||||
/// If no such nonnegative integer `n` exists, then the iteration range
|
|
||||||
/// is empty.
|
|
||||||
///
|
|
||||||
fn range_step_core(start: $T, stop: $T, step: $T, r: Range, it: &fn($T) -> bool) -> bool {
|
|
||||||
let mut i = start;
|
|
||||||
if step == 0 {
|
|
||||||
fail!(~"range_step called with step == 0");
|
|
||||||
} else if step == (1 as $T) { // elide bounds check to tighten loop
|
|
||||||
while i < stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// no need for overflow check;
|
|
||||||
// cannot have i + 1 > max_value because i < stop <= max_value
|
|
||||||
i += (1 as $T);
|
|
||||||
}
|
|
||||||
} else if step == (-1 as $T) { // elide bounds check to tighten loop
|
|
||||||
while i > stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// no need for underflow check;
|
|
||||||
// cannot have i - 1 < min_value because i > stop >= min_value
|
|
||||||
i -= (1 as $T);
|
|
||||||
}
|
|
||||||
} else if step > 0 { // ascending
|
|
||||||
while i < stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// avoiding overflow. break if i + step > max_value
|
|
||||||
if i > max_value - step { return true; }
|
|
||||||
i += step;
|
|
||||||
}
|
|
||||||
} else { // descending
|
|
||||||
while i > stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// avoiding underflow. break if i + step < min_value
|
|
||||||
if i < min_value - step { return true; }
|
|
||||||
i += step;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match r {
|
|
||||||
HalfOpen => return true,
|
|
||||||
Closed => return (i != stop || it(i))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
///
|
|
||||||
/// Iterate through the range [`start`..`stop`) with a given step value.
|
|
||||||
///
|
|
||||||
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
|
|
||||||
/// * `x_i == start + step*i`, and
|
|
||||||
/// * `n` is the greatest nonnegative integer such that `x_n < stop`
|
|
||||||
///
|
|
||||||
/// (If no such `n` exists, then the iteration range is empty.)
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `start` - lower bound, inclusive
|
|
||||||
/// * `stop` - higher bound, exclusive
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ~~~
|
|
||||||
/// let mut sum = 0;
|
|
||||||
/// for int::range(1, 5) |i| {
|
|
||||||
/// sum += i;
|
|
||||||
/// }
|
|
||||||
/// assert!(sum == 10);
|
|
||||||
/// ~~~
|
|
||||||
///
|
|
||||||
pub fn range_step(start: $T, stop: $T, step: $T, it: &fn($T) -> bool) -> bool {
|
|
||||||
range_step_core(start, stop, step, HalfOpen, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
///
|
|
||||||
/// Iterate through a range with a given step value.
|
|
||||||
///
|
|
||||||
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
|
|
||||||
/// `x_i == start + step*i` and `x_n <= last < step + x_n`.
|
|
||||||
///
|
|
||||||
/// (If no such nonnegative integer `n` exists, then the iteration
|
|
||||||
/// range is empty.)
|
|
||||||
///
|
|
||||||
pub fn range_step_inclusive(start: $T, last: $T, step: $T, it: &fn($T) -> bool) -> bool {
|
|
||||||
range_step_core(start, last, step, Closed, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Num for $T {}
|
impl Num for $T {}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
@ -878,56 +783,6 @@ mod tests {
|
|||||||
assert!(i64::from_str("-9223372036854775809").is_none());
|
assert!(i64::from_str("-9223372036854775809").is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_ranges() {
|
|
||||||
let mut l = ~[];
|
|
||||||
|
|
||||||
do range_step(20,26,2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(36,30,-2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(max_value - 2, max_value, 2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(max_value - 3, max_value, 2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(min_value + 2, min_value, -2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(min_value + 3, min_value, -2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
assert_eq!(l, ~[20,22,24,
|
|
||||||
36,34,32,
|
|
||||||
max_value-2,
|
|
||||||
max_value-3,max_value-1,
|
|
||||||
min_value+2,
|
|
||||||
min_value+3,min_value+1]);
|
|
||||||
|
|
||||||
// None of the `fail`s should execute.
|
|
||||||
do range_step(10,0,1) |_i| {
|
|
||||||
fail!(~"unreachable");
|
|
||||||
};
|
|
||||||
do range_step(0,10,-1) |_i| {
|
|
||||||
fail!(~"unreachable");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_fail]
|
|
||||||
fn test_range_step_zero_step() {
|
|
||||||
do range_step(0,10,0) |_i| { true };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_signed_checked_div() {
|
fn test_signed_checked_div() {
|
||||||
assert_eq!(10i.checked_div(&2), Some(5));
|
assert_eq!(10i.checked_div(&2), Some(5));
|
||||||
|
@ -42,101 +42,6 @@ impl CheckedDiv for $T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Range { Closed, HalfOpen }
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
///
|
|
||||||
/// Iterate through a range with a given step value.
|
|
||||||
///
|
|
||||||
/// Let `term` denote the closed interval `[stop-step,stop]` if `r` is Closed;
|
|
||||||
/// otherwise `term` denotes the half-open interval `[stop-step,stop)`.
|
|
||||||
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
|
|
||||||
/// `x_j == start + step*j`, and `x_n` lies in the interval `term`.
|
|
||||||
///
|
|
||||||
/// If no such nonnegative integer `n` exists, then the iteration range
|
|
||||||
/// is empty.
|
|
||||||
///
|
|
||||||
fn range_step_core(start: $T, stop: $T, step: $T_SIGNED, r: Range, it: &fn($T) -> bool) -> bool {
|
|
||||||
let mut i = start;
|
|
||||||
if step == 0 {
|
|
||||||
fail!("range_step called with step == 0");
|
|
||||||
} else if step == (1 as $T_SIGNED) { // elide bounds check to tighten loop
|
|
||||||
while i < stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// no need for overflow check;
|
|
||||||
// cannot have i + 1 > max_value because i < stop <= max_value
|
|
||||||
i += (1 as $T);
|
|
||||||
}
|
|
||||||
} else if step == (-1 as $T_SIGNED) { // elide bounds check to tighten loop
|
|
||||||
while i > stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// no need for underflow check;
|
|
||||||
// cannot have i - 1 < min_value because i > stop >= min_value
|
|
||||||
i -= (1 as $T);
|
|
||||||
}
|
|
||||||
} else if step > 0 { // ascending
|
|
||||||
while i < stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// avoiding overflow. break if i + step > max_value
|
|
||||||
if i > max_value - (step as $T) { return true; }
|
|
||||||
i += step as $T;
|
|
||||||
}
|
|
||||||
} else { // descending
|
|
||||||
while i > stop {
|
|
||||||
if !it(i) { return false; }
|
|
||||||
// avoiding underflow. break if i + step < min_value
|
|
||||||
if i < min_value + ((-step) as $T) { return true; }
|
|
||||||
i -= -step as $T;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match r {
|
|
||||||
HalfOpen => return true,
|
|
||||||
Closed => return (i != stop || it(i))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
///
|
|
||||||
/// Iterate through the range [`start`..`stop`) with a given step value.
|
|
||||||
///
|
|
||||||
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
|
|
||||||
/// - `x_i == start + step*i`, and
|
|
||||||
/// - `n` is the greatest nonnegative integer such that `x_n < stop`
|
|
||||||
///
|
|
||||||
/// (If no such `n` exists, then the iteration range is empty.)
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `start` - lower bound, inclusive
|
|
||||||
/// * `stop` - higher bound, exclusive
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ~~~ {.rust}
|
|
||||||
/// let nums = [1,2,3,4,5,6,7];
|
|
||||||
///
|
|
||||||
/// for uint::range_step(0, nums.len() - 1, 2) |i| {
|
|
||||||
/// printfln!("%d & %d", nums[i], nums[i+1]);
|
|
||||||
/// }
|
|
||||||
/// ~~~
|
|
||||||
///
|
|
||||||
pub fn range_step(start: $T, stop: $T, step: $T_SIGNED, it: &fn($T) -> bool) -> bool {
|
|
||||||
range_step_core(start, stop, step, HalfOpen, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
///
|
|
||||||
/// Iterate through a range with a given step value.
|
|
||||||
///
|
|
||||||
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
|
|
||||||
/// `x_i == start + step*i` and `x_n <= last < step + x_n`.
|
|
||||||
///
|
|
||||||
/// (If no such nonnegative integer `n` exists, then the iteration
|
|
||||||
/// range is empty.)
|
|
||||||
///
|
|
||||||
pub fn range_step_inclusive(start: $T, last: $T, step: $T_SIGNED, it: &fn($T) -> bool) -> bool {
|
|
||||||
range_step_core(start, last, step, Closed, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Num for $T {}
|
impl Num for $T {}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
@ -653,62 +558,6 @@ mod tests {
|
|||||||
100u.to_str_radix(37u);
|
100u.to_str_radix(37u);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn test_ranges() {
|
|
||||||
let mut l = ~[];
|
|
||||||
|
|
||||||
do range_step(20,26,2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(36,30,-2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(max_value - 2, max_value, 2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(max_value - 3, max_value, 2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(min_value + 2, min_value, -2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
do range_step(min_value + 3, min_value, -2) |i| {
|
|
||||||
l.push(i);
|
|
||||||
true
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(l, ~[20,22,24,
|
|
||||||
36,34,32,
|
|
||||||
max_value-2,
|
|
||||||
max_value-3,max_value-1,
|
|
||||||
min_value+2,
|
|
||||||
min_value+3,min_value+1]);
|
|
||||||
|
|
||||||
// None of the `fail`s should execute.
|
|
||||||
do range_step(10,0,1) |_i| {
|
|
||||||
fail!("unreachable");
|
|
||||||
};
|
|
||||||
do range_step(0,1,-10) |_i| {
|
|
||||||
fail!("unreachable");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_fail]
|
|
||||||
fn test_range_step_zero_step_up() {
|
|
||||||
do range_step(0,10,0) |_i| { true };
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
#[should_fail]
|
|
||||||
fn test_range_step_zero_step_down() {
|
|
||||||
do range_step(0,-10,0) |_i| { true };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unsigned_checked_div() {
|
fn test_unsigned_checked_div() {
|
||||||
assert_eq!(10u.checked_div(&2), Some(5));
|
assert_eq!(10u.checked_div(&2), Some(5));
|
||||||
|
Loading…
Reference in New Issue
Block a user