memor 0.1.0

Simple memoization macro for rust
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 2 items with examples
  • Size
  • Source code size: 7.8 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 124.5 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • tamuhey/memor
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • tamuhey

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);