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
#[cfg_attr(not(feature = "num-traits"), no_std)]

#[cfg(feature = "num-traits")]
extern crate num;

#[cfg(test)]
mod test;

/// An Iterator that returns numbers from the Kolakoski sequence
///
/// Information about the Kolakoski sequence and the first 108 numbers
/// can be found at https://oeis.org/A000002. The source of the numbers
/// used for testing in `test.rs` is also https://oeis.org/A000002.
///
/// This sequence is self-refrential, and the implementation is recursive.
/// Attempts to get a large number of items from the sequence may overflow
/// the stack, depending on the machine. Use with caution!
#[cfg(all(feature = "default", not(feature = "num-traits")))]
pub struct Kolakoski
{
    run: usize,
    run_length: u8,
    is_one: bool,
}

#[cfg(feature = "num-traits")]
pub struct Kolakoski<N>
{
    run: usize,
    run_length: N,
    is_one: bool,
}

#[cfg(all(feature = "default", not(feature = "num-traits")))]
impl Kolakoski
{
    /// Creates a new Kolakoski iterator
    pub fn new() -> Kolakoski
    {
        Kolakoski
        {
            run: 0,
            run_length: 1,
            is_one: true,
        }
    }
}

#[cfg(feature = "num-traits")]
impl<N: Num> Kolakoski<N>
{
    /// Creates a new Kolakoski iterator
    pub fn new() -> Kolakoski<N>
    {
        Kolakoski
        {
            run: 0,
            run_length: N::one(),
            is_one: true,
        }
    }
}

#[cfg(all(feature = "default", not(feature = "num-traits")))]
impl Iterator for Kolakoski
{
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item>
    {
        if self.run_length == 0
        {
            self.is_one = !self.is_one;
            self.run += 1;

            if self.run == 1
            {
                self.run_length = 2;
            }
            else
            {
                // store current status (don't bother with run_length, because we will chage it)
                let run = self.run;
                let is_one = self.is_one;

                // reset the status of the Iterator
                self.run = 0;
                self.is_one = true;
                self.run_length = 1;

                // get the run length for the current run
                // If you want too many items from the sequence, you will overflow the stack!
                let run_length = self.nth(run).unwrap();

                // re-set (not reset!) the status, including the new run_length value
                self.run = run;
                self.run_length = run_length;
                self.is_one = is_one;
            }
        }

        self.run_length -= 1;

        if self.is_one
        {
            Some(1)
        }
        else
        {
            Some(2)
        }
    }
}

#[cfg(feature = "num-traits")]
use num::traits::Num;

#[cfg(feature = "num-traits")]
impl<N: Num + Clone> Iterator for Kolakoski<N>
{
    type Item = N;

    fn next(&mut self) -> Option<Self::Item>
    {
        if self.run_length == N::zero()
        {
            self.is_one = !self.is_one;
            self.run += 1;

            if self.run == 1
            {
                self.run_length = N::one() + N::one()
            }
            else
            {
                // store current status (don't bother with run_length, because we will chage it)
                let run = self.run;
                let is_one = self.is_one;

                // reset the status of the Iterator
                self.run = 0;
                self.is_one = true;
                self.run_length = N::one();

                // get the run length for the current run
                // If you want too many items from the sequence, you will overflow the stack!
                let run_length = self.nth(run).unwrap();

                // re-set (not reset!) the status, including the new run_length value
                self.run = run;
                self.run_length = run_length;
                self.is_one = is_one;
            }
        }

        self.run_length = self.run_length.clone() - N::one();

        if self.is_one
        {
            Some(N::one())
        }
        else
        {
            Some(N::add(N::one(), N::one()))
        }
    }
}