1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use Traversal;
use std::ops::Add;

use num::traits::PrimInt;

/// An infinite iterator starting at `start` and advancing by `step` with each
/// iteration
#[derive(Copy, Clone)]
pub struct Counter<A> {
    /// The current state the counter is at (next value to be yielded)
    start: A,
    /// The amount that this iterator is stepping by
    step: A,
}

/// Creates a new counter with the specified start/step
#[inline]
pub fn count<A>(start: A, step: A) -> Counter<A> {
    Counter{ start: start, step: step }
}

impl<A: Add<Output=A> + Clone> Traversal for Counter<A> {
    type Item = A;

    #[inline]
    fn foreach<F>(self, mut f: F) where F: FnMut(A) -> bool {
        let mut i = self.start;
        loop {
            let old = i;
            // This is what std does, so I guess it's legit...
            i = old.clone() + self.step.clone();
            if f(old) { return; }
        }
    }
}

/// An iterator over the range [start, stop)
#[derive(Copy, Clone)]
pub struct Range<A> {
    start: A,
    stop: A,
}

/// Returns an iterator over the given range [start, stop) (that is, starting
/// at start (inclusive), and ending at stop (exclusive)).
#[inline]
pub fn range<A: PrimInt>(start: A, stop: A) -> Range<A> {
    Range { start: start, stop: stop }
}

// FIXME: rust-lang/rust#10414: Unfortunate type bound
impl<A: PrimInt> Traversal for Range<A> {
    type Item = A;

    #[inline]
    fn foreach<F>(self, mut f: F) where F: FnMut(A) -> bool {
        let mut i = self.start;
        let one = A::one();
        while i < self.stop {
            let old = i;
            i = old + one;
            if f(old) { return; }
        }
    }
}

/// An iterator over the range [start, stop]
#[derive(Copy, Clone)]
pub struct RangeInclusive<A> {
    start: A,
    stop: A,
}

/// Return an iterator over the range [start, stop]
#[inline]
pub fn range_inclusive<A: PrimInt>(start: A, stop: A) -> RangeInclusive<A> {
    RangeInclusive { start: start, stop: stop }
}

impl<A: PrimInt> Traversal for RangeInclusive<A> {
    type Item = A;

    #[inline]
    fn foreach<F>(self, mut f: F) where F: FnMut(A) -> bool {
        let mut i = self.start;
        let one = A::one();
        while i <= self.stop {
            let old = i;
            i = old + one;
            if f(old) { return; }
        }
    }
}

/// An iterator over the range [start, stop) by `step`. It handles overflow by stopping.
#[derive(Copy, Clone)]
pub struct RangeStep<A> {
    start: A,
    stop: A,
    step: A,
}

/// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping.
#[inline]
pub fn range_step<A: PrimInt>(start: A, stop: A, step: A) -> RangeStep<A> {
    RangeStep { start: start, stop: stop, step: step }
}

impl<A: PrimInt> Traversal for RangeStep<A> {
    type Item = A;

    #[inline]
    fn foreach<F>(self, mut f: F) where F: FnMut(A) -> bool {
        let mut i = self.start;
        // branch once and duplicate trivial logic for the perf
        if self.step > A::zero() {
            while i < self.stop {
                let old = i;
                let temp = i.checked_add(&self.step);
                if f(old) { return; }
                i = match temp { None => return, Some(x) => x }
            }
        } else {
            while i > self.stop {
                let old = i;
                let temp = i.checked_add(&self.step);
                if f(old) { return; }
                i = match temp { None => return, Some(x) => x }
            }
        }
    }
}

/// An iterator over the range [start, stop] by `step`. It handles overflow by stopping.
#[derive(Copy, Clone)]
pub struct RangeStepInclusive<A> {
    start: A,
    stop: A,
    step: A,
}

/// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping.
#[inline]
pub fn range_step_inclusive<A: PrimInt>(start: A, stop: A, step: A) -> RangeStepInclusive<A> {
    RangeStepInclusive { start: start, stop: stop, step: step }
}

impl<A: PrimInt> Traversal for RangeStepInclusive<A> {
    type Item = A;

    #[inline]
    fn foreach<F>(self, mut f: F) where F: FnMut(A) -> bool {
        let mut i = self.start;
        // branch once and duplicate trivial logic for the perf
        if self.step > A::zero() {
            while i <= self.stop {
                let old = i;
                let temp = i.checked_add(&self.step);
                if f(old) { return; }
                i = match temp { None => return, Some(x) => x }
            }
        } else {
            while i >= self.stop {
                let old = i;
                let temp = i.checked_add(&self.step);
                if f(old) { return; }
                i = match temp { None => return, Some(x) => x }
            }
        }
    }
}

/// Create a new iterator that endlessly repeats the element `elt`.
#[inline]
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
    Repeat{ element: elt }
}

/// An iterator that repeats an element endlessly
#[derive(Copy, Clone)]
pub struct Repeat<A> {
    element: A
}

impl<A: Clone> Traversal for Repeat<A> {
    type Item = A;

    #[inline]
    fn foreach<F>(self, mut f: F) where F: FnMut(A) -> bool {
        loop {
            if f(self.element.clone()) { return; }
        }
    }
}

/// An iterator that repeatedly applies a given function, starting
/// from a given seed value.
#[derive(Copy, Clone)]
pub struct Iterate<T, F> {
    seed: T,
    iter: F,
}

/// Create a new iterator that produces an infinite sequence of
/// repeated applications of the given function `f`.
#[inline]
pub fn iterate<T, F>(seed: T, f: F) -> Iterate<T, F> where
    T: Clone,
    F: FnMut(T) -> T
{
    Iterate { seed: seed, iter: f }
}

impl<A, I> Traversal for Iterate<A, I> where
    A: Clone,
    I: FnMut(A) -> A {
    type Item = A;

    #[inline]
    fn foreach<F>(mut self, mut f: F) where F: FnMut(A) -> bool {
        if !f(self.seed.clone()) {
            let mut cur = self.seed;
            loop {
                let next = (self.iter)(cur);
                if f(next.clone()) { return; }
                cur = next;
            }
        }
    }
}



#[cfg(test)]
mod test {
    use super::*;
    use Traversal;

    #[test]
    fn test_range() {
        assert_eq!(range(0, 5).collect::<Vec<i32>>(), vec![0, 1, 2, 3, 4]);
        assert_eq!(range(-10, -1).collect::<Vec<i32>>(),
            vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]);
        assert_eq!(range(200i32, -5).count(), 0);
        assert_eq!(range(200i32, 200).count(), 0);
    }

    #[test]
    fn test_range_inclusive() {
        assert_eq!(range_inclusive(0, 5).collect::<Vec<i32>>(), vec![0, 1, 2, 3, 4, 5]);
        assert_eq!(range_inclusive(200i32, -5).count(), 0);
        assert_eq!(range_inclusive(200, 200).collect::<Vec<i32>>(), vec![200]);
    }

    #[test]
    fn test_range_step() {
        assert_eq!(range_step(0, 20, 5).collect::<Vec<i32>>(), vec![0, 5, 10, 15]);
        assert_eq!(range_step(20, 0, -5).collect::<Vec<i32>>(), vec![20, 15, 10, 5]);
        assert_eq!(range_step(20, 0, -6).collect::<Vec<i32>>(), vec![20, 14, 8, 2]);
        assert_eq!(range_step(200u8, 255, 50).collect::<Vec<u8>>(), vec![200u8, 250]);
        assert_eq!(range_step(200, -5, 1).collect::<Vec<i32>>(), vec![]);
        assert_eq!(range_step(200, 200, 1).collect::<Vec<i32>>(), vec![]);
    }

    #[test]
    fn test_range_step_inclusive() {
        assert_eq!(range_step_inclusive(0, 20, 5).collect::<Vec<i32>>(), vec![0, 5, 10, 15, 20]);
        assert_eq!(range_step_inclusive(20, 0, -5).collect::<Vec<i32>>(), vec![20, 15, 10, 5, 0]);
        assert_eq!(range_step_inclusive(20, 0, -6).collect::<Vec<i32>>(), vec![20, 14, 8, 2]);
        assert_eq!(range_step_inclusive(200, 255, 50).collect::<Vec<u8>>(), vec![200, 250]);
        assert_eq!(range_step_inclusive(200, -5, 1).collect::<Vec<i32>>(), vec![]);
        assert_eq!(range_step_inclusive(200, 200, 1).collect::<Vec<i32>>(), vec![200]);
    }


    #[test]
    fn test_iterate() {
        assert_eq!(iterate(1, |x| x * 2).take(5).collect::<Vec<i32>>(), vec![1, 2, 4, 8, 16]);
    }

    #[test]
    fn test_repeat() {
        assert_eq!(repeat(42).take(5).collect::<Vec<i32>>(), vec![42, 42, 42, 42, 42]);
    }
}