Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We may simplify the implement of Fibonacci sequence in this way. #85

Open
kingwingfly opened this issue Mar 27, 2023 · 0 comments
Open

Comments

@kingwingfly
Copy link

We may simplify the implement of Fibonacci sequence in this way.

struct Fib {
    mem: [u64; 2],
    pos: usize,
}

impl Iterator for Fib {
    type Item = u64;

    fn next(&mut self) -> Option<Self::Item> {
        let result = if self.pos < 2 {
            self.mem[self.pos]
        } else {
            let new = {
                let a = &self.mem;
                let n = self.mem.len();
                a[n - 2] + a[n - 1]
            };
            let mut temp = new.clone();
            for i in (0..2).rev() {
                std::mem::swap(&mut self.mem[i], &mut temp);
            }
            new
        };
        self.pos += 1;
        Some(result)
    }
}

So maybe we do not need to create a struct and impl Index trait for it.

Apart from that, in the count_exprs, I feel the first matcher may never be reached for it's meaningless if there's no initial element .

Here's the code and test:

#[macro_export]
macro_rules! count_exprs {
    // () => {0}; // I feel this line can never be reached
    ($e:expr) => {1};
    ($e:expr, $($e1:expr),+) => { 1 + count_exprs!($($e1),+) }
}

#[macro_export]
macro_rules! recurrence {
    (
        $seq: ident [$ind: ident]: $type_: ty = $($inits:expr),+ ;...; $recur:expr
        // seq is short for sequence; ind is short for index
    ) => {{
        use $crate::count_exprs;

        const MEM_SIZE: usize = count_exprs!($($inits),+);

        struct Fib {
            mem: [$type_; MEM_SIZE],
            pos: usize,
        }

        impl Iterator for Fib {
            type Item = $type_;

            fn next(&mut self) -> Option<Self::Item> {
                let result = if self.pos < MEM_SIZE {
                    self.mem[self.pos]
                } else {
                    let new = {
                        let $seq = &self.mem;
                        let $ind = MEM_SIZE;
                        $recur
                    };
                    let mut temp = new.clone();
                    for i in (0..MEM_SIZE).rev() {
                        std::mem::swap(&mut self.mem[i], &mut temp);
                    }
                    new
                };
                self.pos += 1;
                Some(result)
            }
        }

        Fib {
            mem: [$($inits),+],
            pos: 0,
        }
    }};
}

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

    #[test]
    fn fib_test() {
        let mut fib = Fib {
            mem: [0, 1],
            pos: 0,
        };
        assert_eq!(fib.next().unwrap(), 0);
        assert_eq!(fib.next().unwrap(), 1);
        assert_eq!(fib.next().unwrap(), 1);
        assert_eq!(fib.next().unwrap(), 2);
        assert_eq!(fib.next().unwrap(), 3);
        assert_eq!(fib.next().unwrap(), 5);
    }

    #[test]
    fn macro_test() {
        let mut fib = recurrence![a[n]: u64 = 0, 1;...; a[n-1] + a[n-2]];
        assert_eq!(fib.next().unwrap(), 0);
        assert_eq!(fib.next().unwrap(), 1);
        assert_eq!(fib.next().unwrap(), 1);
        assert_eq!(fib.next().unwrap(), 2);
        assert_eq!(fib.next().unwrap(), 3);
        assert_eq!(fib.next().unwrap(), 5);
        for _ in 0..10 {
            fib.next();
        }
        assert_eq!(fib.next().unwrap(), 987);
    }

    #[test]
    fn macro_test2() {
        let mut fib = recurrence!(f[i]: i32 = 1, 1, 3;...; f[i-3]+f[i-2]+f[i-1]);
        assert_eq!(fib.next().unwrap(), 1);
        assert_eq!(fib.next().unwrap(), 1);
        assert_eq!(fib.next().unwrap(), 3);
        assert_eq!(fib.next().unwrap(), 5);
        assert_eq!(fib.next().unwrap(), 9);
        for _ in 0..10 {
            fib.next();
        }
        assert_eq!(fib.next().unwrap(), 7473);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant