@@ -42,101 +42,6 @@ impl CheckedDiv for $T {
42
42
}
43
43
}
44
44
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
-
140
45
impl Num for $T { }
141
46
142
47
#[ cfg( not( test) ) ]
@@ -653,62 +558,6 @@ mod tests {
653
558
100 u. to_str_radix( 37 u) ;
654
559
}
655
560
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
-
712
561
#[ test]
713
562
fn test_unsigned_checked_div( ) {
714
563
assert_eq!( 10 u. checked_div( & 2 ) , Some ( 5 ) ) ;
0 commit comments