Crates.io | memor |
lib.rs | memor |
version | 0.1.0 |
source | src |
created_at | 2020-06-28 07:38:31.254356 |
updated_at | 2020-06-28 07:38:31.254356 |
description | Simple memoization macro for rust |
homepage | https://github.com/tamuhey/memor |
repository | https://github.com/tamuhey/memor |
max_upload_size | |
id | 259014 |
size | 7,786 |
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);