[][src]Struct memoizer::Memoizer

pub struct Memoizer<U, V, F> where
    U: Eq + Hash + Clone,
    V: Clone,
    F: Fn(U) -> V, 
{ /* fields omitted */ }

The eponymous struct. Can only memoize function that takes a single argument and returns a single value, if you need more than this, you can use vectors, arrays or structs of your own to pass in more than one value.

Methods

impl<U, V, F> Memoizer<U, V, F> where
    U: Eq + Hash + Clone,
    V: Clone,
    F: Fn(U) -> V, 
[src]

pub fn new(function: F) -> Memoizer<U, V, F>[src]

Creates a new Memoize given a function.

Examples

let mut add_two = Memoizer::new(|n| {
   	n + 2
   });
assert_eq!(4, add_two.value(2));

pub fn value(&mut self, arg: U) -> V[src]

Returns the value for the memoized function. If the function has already been called before, it will use the previous value. This means Memoizer should only be used for injective functions.

Examples


#[derive(Debug, Clone, Hash)]
    struct Dummy {
        pub id: usize,
        pub word: String,
    }

    /* PartialEq & Eq required for HashMap */
    impl PartialEq for Dummy {
        fn eq(&self, other: &Dummy) -> bool {
            self.id == other.id && self.word == other.word
        }
    }

   impl Eq for Dummy {}

let d = Dummy {
    id: 1,
    word: String::from("girls"),
};
let mut calc = Memoizer::new(|d: &Dummy| d.id + d.word.len());

 assert_eq!(6, calc.value(&d));
 assert_eq!(6, calc.value(&d));

Trait Implementations

impl<U: Debug, V: Debug, F: Debug> Debug for Memoizer<U, V, F> where
    U: Eq + Hash + Clone,
    V: Clone,
    F: Fn(U) -> V, 
[src]

Auto Trait Implementations

impl<U, V, F> RefUnwindSafe for Memoizer<U, V, F> where
    F: RefUnwindSafe,
    U: RefUnwindSafe,
    V: RefUnwindSafe

impl<U, V, F> Send for Memoizer<U, V, F> where
    F: Send,
    U: Send,
    V: Send

impl<U, V, F> Sync for Memoizer<U, V, F> where
    F: Sync,
    U: Sync,
    V: Sync

impl<U, V, F> Unpin for Memoizer<U, V, F> where
    F: Unpin,
    U: Unpin,
    V: Unpin

impl<U, V, F> UnwindSafe for Memoizer<U, V, F> where
    F: UnwindSafe,
    U: UnwindSafe,
    V: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.