Skip to content

Commit 6e3d5c6

Browse files
blake2-ppcthestinger
authored andcommitted
std::num: Remove range_step for each numeric type
Replaced by `std::iter::range_step`
1 parent ad74fde commit 6e3d5c6

File tree

2 files changed

+0
-296
lines changed

2 files changed

+0
-296
lines changed

src/libstd/num/int_macros.rs

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -41,101 +41,6 @@ impl CheckedDiv for $T {
4141
}
4242
}
4343

44-
enum Range { Closed, HalfOpen }
45-
46-
#[inline]
47-
///
48-
/// Iterate through a range with a given step value.
49-
///
50-
/// Let `term` denote the closed interval `[stop-step,stop]` if `r` is Closed;
51-
/// otherwise `term` denotes the half-open interval `[stop-step,stop)`.
52-
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
53-
/// `x_j == start + step*j`, and `x_n` lies in the interval `term`.
54-
///
55-
/// If no such nonnegative integer `n` exists, then the iteration range
56-
/// is empty.
57-
///
58-
fn range_step_core(start: $T, stop: $T, step: $T, r: Range, it: &fn($T) -> bool) -> bool {
59-
let mut i = start;
60-
if step == 0 {
61-
fail!(~"range_step called with step == 0");
62-
} else if step == (1 as $T) { // elide bounds check to tighten loop
63-
while i < stop {
64-
if !it(i) { return false; }
65-
// no need for overflow check;
66-
// cannot have i + 1 > max_value because i < stop <= max_value
67-
i += (1 as $T);
68-
}
69-
} else if step == (-1 as $T) { // elide bounds check to tighten loop
70-
while i > stop {
71-
if !it(i) { return false; }
72-
// no need for underflow check;
73-
// cannot have i - 1 < min_value because i > stop >= min_value
74-
i -= (1 as $T);
75-
}
76-
} else if step > 0 { // ascending
77-
while i < stop {
78-
if !it(i) { return false; }
79-
// avoiding overflow. break if i + step > max_value
80-
if i > max_value - step { return true; }
81-
i += step;
82-
}
83-
} else { // descending
84-
while i > stop {
85-
if !it(i) { return false; }
86-
// avoiding underflow. break if i + step < min_value
87-
if i < min_value - step { return true; }
88-
i += step;
89-
}
90-
}
91-
match r {
92-
HalfOpen => return true,
93-
Closed => return (i != stop || it(i))
94-
}
95-
}
96-
97-
#[inline]
98-
///
99-
/// Iterate through the range [`start`..`stop`) with a given step value.
100-
///
101-
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
102-
/// * `x_i == start + step*i`, and
103-
/// * `n` is the greatest nonnegative integer such that `x_n < stop`
104-
///
105-
/// (If no such `n` exists, then the iteration range is empty.)
106-
///
107-
/// # Arguments
108-
///
109-
/// * `start` - lower bound, inclusive
110-
/// * `stop` - higher bound, exclusive
111-
///
112-
/// # Examples
113-
/// ~~~
114-
/// let mut sum = 0;
115-
/// for int::range(1, 5) |i| {
116-
/// sum += i;
117-
/// }
118-
/// assert!(sum == 10);
119-
/// ~~~
120-
///
121-
pub fn range_step(start: $T, stop: $T, step: $T, it: &fn($T) -> bool) -> bool {
122-
range_step_core(start, stop, step, HalfOpen, it)
123-
}
124-
125-
#[inline]
126-
///
127-
/// Iterate through a range with a given step value.
128-
///
129-
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
130-
/// `x_i == start + step*i` and `x_n <= last < step + x_n`.
131-
///
132-
/// (If no such nonnegative integer `n` exists, then the iteration
133-
/// range is empty.)
134-
///
135-
pub fn range_step_inclusive(start: $T, last: $T, step: $T, it: &fn($T) -> bool) -> bool {
136-
range_step_core(start, last, step, Closed, it)
137-
}
138-
13944
impl Num for $T {}
14045

14146
#[cfg(not(test))]
@@ -878,56 +783,6 @@ mod tests {
878783
assert!(i64::from_str("-9223372036854775809").is_none());
879784
}
880785

881-
#[test]
882-
fn test_ranges() {
883-
let mut l = ~[];
884-
885-
do range_step(20,26,2) |i| {
886-
l.push(i);
887-
true
888-
};
889-
do range_step(36,30,-2) |i| {
890-
l.push(i);
891-
true
892-
};
893-
do range_step(max_value - 2, max_value, 2) |i| {
894-
l.push(i);
895-
true
896-
};
897-
do range_step(max_value - 3, max_value, 2) |i| {
898-
l.push(i);
899-
true
900-
};
901-
do range_step(min_value + 2, min_value, -2) |i| {
902-
l.push(i);
903-
true
904-
};
905-
do range_step(min_value + 3, min_value, -2) |i| {
906-
l.push(i);
907-
true
908-
};
909-
assert_eq!(l, ~[20,22,24,
910-
36,34,32,
911-
max_value-2,
912-
max_value-3,max_value-1,
913-
min_value+2,
914-
min_value+3,min_value+1]);
915-
916-
// None of the `fail`s should execute.
917-
do range_step(10,0,1) |_i| {
918-
fail!(~"unreachable");
919-
};
920-
do range_step(0,10,-1) |_i| {
921-
fail!(~"unreachable");
922-
};
923-
}
924-
925-
#[test]
926-
#[should_fail]
927-
fn test_range_step_zero_step() {
928-
do range_step(0,10,0) |_i| { true };
929-
}
930-
931786
#[test]
932787
fn test_signed_checked_div() {
933788
assert_eq!(10i.checked_div(&2), Some(5));

src/libstd/num/uint_macros.rs

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -42,101 +42,6 @@ impl CheckedDiv for $T {
4242
}
4343
}
4444

45-
enum Range { Closed, HalfOpen }
46-
47-
#[inline]
48-
///
49-
/// Iterate through a range with a given step value.
50-
///
51-
/// Let `term` denote the closed interval `[stop-step,stop]` if `r` is Closed;
52-
/// otherwise `term` denotes the half-open interval `[stop-step,stop)`.
53-
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
54-
/// `x_j == start + step*j`, and `x_n` lies in the interval `term`.
55-
///
56-
/// If no such nonnegative integer `n` exists, then the iteration range
57-
/// is empty.
58-
///
59-
fn range_step_core(start: $T, stop: $T, step: $T_SIGNED, r: Range, it: &fn($T) -> bool) -> bool {
60-
let mut i = start;
61-
if step == 0 {
62-
fail!("range_step called with step == 0");
63-
} else if step == (1 as $T_SIGNED) { // elide bounds check to tighten loop
64-
while i < stop {
65-
if !it(i) { return false; }
66-
// no need for overflow check;
67-
// cannot have i + 1 > max_value because i < stop <= max_value
68-
i += (1 as $T);
69-
}
70-
} else if step == (-1 as $T_SIGNED) { // elide bounds check to tighten loop
71-
while i > stop {
72-
if !it(i) { return false; }
73-
// no need for underflow check;
74-
// cannot have i - 1 < min_value because i > stop >= min_value
75-
i -= (1 as $T);
76-
}
77-
} else if step > 0 { // ascending
78-
while i < stop {
79-
if !it(i) { return false; }
80-
// avoiding overflow. break if i + step > max_value
81-
if i > max_value - (step as $T) { return true; }
82-
i += step as $T;
83-
}
84-
} else { // descending
85-
while i > stop {
86-
if !it(i) { return false; }
87-
// avoiding underflow. break if i + step < min_value
88-
if i < min_value + ((-step) as $T) { return true; }
89-
i -= -step as $T;
90-
}
91-
}
92-
match r {
93-
HalfOpen => return true,
94-
Closed => return (i != stop || it(i))
95-
}
96-
}
97-
98-
#[inline]
99-
///
100-
/// Iterate through the range [`start`..`stop`) with a given step value.
101-
///
102-
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
103-
/// - `x_i == start + step*i`, and
104-
/// - `n` is the greatest nonnegative integer such that `x_n < stop`
105-
///
106-
/// (If no such `n` exists, then the iteration range is empty.)
107-
///
108-
/// # Arguments
109-
///
110-
/// * `start` - lower bound, inclusive
111-
/// * `stop` - higher bound, exclusive
112-
///
113-
/// # Examples
114-
/// ~~~ {.rust}
115-
/// let nums = [1,2,3,4,5,6,7];
116-
///
117-
/// for uint::range_step(0, nums.len() - 1, 2) |i| {
118-
/// printfln!("%d & %d", nums[i], nums[i+1]);
119-
/// }
120-
/// ~~~
121-
///
122-
pub fn range_step(start: $T, stop: $T, step: $T_SIGNED, it: &fn($T) -> bool) -> bool {
123-
range_step_core(start, stop, step, HalfOpen, it)
124-
}
125-
126-
#[inline]
127-
///
128-
/// Iterate through a range with a given step value.
129-
///
130-
/// Iterates through the range `[x_0, x_1, ..., x_n]` where
131-
/// `x_i == start + step*i` and `x_n <= last < step + x_n`.
132-
///
133-
/// (If no such nonnegative integer `n` exists, then the iteration
134-
/// range is empty.)
135-
///
136-
pub fn range_step_inclusive(start: $T, last: $T, step: $T_SIGNED, it: &fn($T) -> bool) -> bool {
137-
range_step_core(start, last, step, Closed, it)
138-
}
139-
14045
impl Num for $T {}
14146

14247
#[cfg(not(test))]
@@ -653,62 +558,6 @@ mod tests {
653558
100u.to_str_radix(37u);
654559
}
655560

656-
#[test]
657-
pub fn test_ranges() {
658-
let mut l = ~[];
659-
660-
do range_step(20,26,2) |i| {
661-
l.push(i);
662-
true
663-
};
664-
do range_step(36,30,-2) |i| {
665-
l.push(i);
666-
true
667-
};
668-
do range_step(max_value - 2, max_value, 2) |i| {
669-
l.push(i);
670-
true
671-
};
672-
do range_step(max_value - 3, max_value, 2) |i| {
673-
l.push(i);
674-
true
675-
};
676-
do range_step(min_value + 2, min_value, -2) |i| {
677-
l.push(i);
678-
true
679-
};
680-
do range_step(min_value + 3, min_value, -2) |i| {
681-
l.push(i);
682-
true
683-
};
684-
685-
assert_eq!(l, ~[20,22,24,
686-
36,34,32,
687-
max_value-2,
688-
max_value-3,max_value-1,
689-
min_value+2,
690-
min_value+3,min_value+1]);
691-
692-
// None of the `fail`s should execute.
693-
do range_step(10,0,1) |_i| {
694-
fail!("unreachable");
695-
};
696-
do range_step(0,1,-10) |_i| {
697-
fail!("unreachable");
698-
};
699-
}
700-
701-
#[test]
702-
#[should_fail]
703-
fn test_range_step_zero_step_up() {
704-
do range_step(0,10,0) |_i| { true };
705-
}
706-
#[test]
707-
#[should_fail]
708-
fn test_range_step_zero_step_down() {
709-
do range_step(0,-10,0) |_i| { true };
710-
}
711-
712561
#[test]
713562
fn test_unsigned_checked_div() {
714563
assert_eq!(10u.checked_div(&2), Some(5));

0 commit comments

Comments
 (0)