Skip to content

Memoize

A function whose results are remembered: calling it again with the same argument returns the stored result instead of computing it again.

The wrapped function receives the argument by reference and must be deterministic (and free of side effects you rely on), since it runs only once per distinct argument. There is no size limit: every distinct argument keeps its result until clear is called, so do not feed it unbounded input.

use helpers4::function::Memoize;

Cargo feature function (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features function

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["function"] }
pub struct Memoize<A, R, F> { /* private fields */ }
use helpers4::function::Memoize;

let mut square = Memoize::new(|n: &u64| n * n);
assert_eq!(square.call(12), 144);
assert_eq!(square.call(12), 144); // served from memory
assert_eq!(square.len(), 1);
pub fn new(func: F) -> Self

Wraps func.

Parameters

ParameterTypeDescription
funcFThe function whose results to remember.

Returns

Self — A memoized version of func with an empty memory.

pub fn call(&mut self, arg: A) -> R

Calls the function with arg, or returns the remembered result for that argument.

Parameters

ParameterTypeDescription
argAThe argument to call the function with.

Returns

R — The result for arg, computed at most once until clear.

pub fn len(&self) -> usize

The number of distinct arguments whose result is remembered.

Returns

usize — The number of stored results.

pub fn is_empty(&self) -> bool

Whether no result is remembered yet.

Returns

booltrue when nothing is stored.

pub fn clear(&mut self)

Forgets every remembered result.

Returns

()

src/function/memoize.rs