memor

Crates.iomemor
lib.rsmemor
version0.1.0
sourcesrc
created_at2020-06-28 07:38:31.254356
updated_at2020-06-28 07:38:31.254356
descriptionSimple memoization macro for rust
homepagehttps://github.com/tamuhey/memor
repositoryhttps://github.com/tamuhey/memor
max_upload_size
id259014
size7,786
Yohei Tamura (tamuhey)

documentation

https://docs.rs/memor

README

memor: Simple memoization macro for rust

Usage

Just add #[memo] to your function.

use memor::memo;
#[memo]
fn fib(n: i64) -> i64 {
    if n == 0 || n == 1 {
        n
    } else {
        fib(n - 1) + fib(n - 2)
    }
}

assert_eq!(12586269025, fib(50));

Various functions can be memoized. Because the arguments are saved as the keys of std::collections::HashMap internally, this macro can be applied to functions all of whose arguments implements Eq + Hash.

use memor::memo;
#[derive(Hash, Eq, PartialEq)]
struct Foo {
    a: usize,
    b: usize,
}

#[memo]
fn foo(Foo { a, b }: Foo, c: usize) -> usize {
    if a == 0 || b == 0 || c == 0 {
        1
    } else {
        foo(Foo { a, b: b - 1 }, c)
            .wrapping_add(foo(Foo { a: a - 1, b }, c))
            .wrapping_add(foo(Foo { a, b }, c - 1))
    }
}

assert_eq!(foo(Foo { a: 50, b: 50 }, 50), 6753084261833197057);
Commit count: 9

cargo fmt