diff --git a/src/main.rs b/src/main.rs index 3e5d0fd..dbc31b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1483,39 +1483,79 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { } } -struct Cacher +// struct Cacher +// where +// T: Fn(u32) -> u32, +// { +// calculation: T, +// value: Option, +// } +// +// impl Cacher +// where +// T: Fn(u32) -> u32, +// { +// fn new(calculation: T) -> Cacher { +// Self { +// calculation, +// value: None, +// } +// } +// +// fn value(&mut self, args: u32) -> u32 { +// match self.value { +// Some(&v) => v, +// None => { +// let v = (self.calculation)(args); +// v +// } +// } +// } +// } + +struct Cacher where - T: Fn(&D) -> F, + T: Fn(&K) -> V, { calculation: T, - value: HashMap, + value: HashMap, } -impl Cacher +impl Cacher where - D: Hash + Eq, - F: Copy, - T: Fn(&D) -> F, + V: Copy, + K: Hash + Eq, + T: Fn(&K) -> V, { - fn new(calculation: T) -> Cacher { + fn new(calculation: T) -> Cacher { Self { calculation, - value: HashMap::::new(), + value: HashMap::::new(), } } - fn value(&mut self, args: D) -> F { - match self.value.get(&args) { + fn value(&mut self, args: K) -> V { + let value = &mut self.value; + + match value.get(&args) { Some(&v) => v, None => { - let v = (self.calculation)(&args); - let v1 = v.clone(); - - self.value.insert(args, v); - - v1 + let x = (self.calculation)(&args); + let x1 = x.clone(); + value.insert(args, x); + x1 } } + + // match self.value.get(&args) { + // Some(&v) => v, + // None => { + // let x = (self.calculation)(&args); + // let x1 = x.clone(); + // self.value.insert(args, x); + // x1 + // } + // } } } @@ -1617,6 +1657,7 @@ mod tests { }); let v = c.value("tetsss"); + let v = c.value("s"); assert_eq!(v, 7); }