forked from Orchid/orchid
Changed most everything
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use hashbrown::HashMap;
|
||||
|
||||
/// Cache the return values of an effectless closure in a hashmap
|
||||
/// Inspired by the closure_cacher crate.
|
||||
@@ -8,27 +8,64 @@ pub struct Cache<I, O, F> {
|
||||
closure: F
|
||||
}
|
||||
|
||||
impl<I, O, F> Cache<I, O, F> where
|
||||
impl<I: 'static, O, F> Cache<I, O, F> where
|
||||
I: Eq + Hash,
|
||||
F: FnMut(I) -> O
|
||||
{
|
||||
pub fn new(closure: F) -> Self {
|
||||
Self { store: HashMap::new(), closure }
|
||||
}
|
||||
pub fn by_copy(&mut self, i: I) -> &O where I: Copy {
|
||||
/// Produce and cache a result by copying I if necessary
|
||||
pub fn by_copy(&mut self, i: &I) -> &O where I: Copy {
|
||||
let closure = &mut self.closure;
|
||||
self.store.entry(i).or_insert_with(|| closure(i))
|
||||
self.store.raw_entry_mut().from_key(i)
|
||||
.or_insert_with(|| (*i, closure(*i))).1
|
||||
}
|
||||
pub fn by_clone(&mut self, i: I) -> &O where I: Clone {
|
||||
/// Produce and cache a result by cloning I if necessary
|
||||
pub fn by_clone(&mut self, i: &I) -> &O where I: Clone {
|
||||
let closure = &mut self.closure;
|
||||
// Make sure we only clone if necessary
|
||||
self.store.entry(i).or_insert_with_key(|k| closure(k.clone()))
|
||||
self.store.raw_entry_mut().from_key(i)
|
||||
.or_insert_with(|| (i.clone(), closure(i.clone()))).1
|
||||
}
|
||||
/// Return the result if it has already been computed
|
||||
pub fn known(&self, i: &I) -> Option<&O> {
|
||||
self.store.get(i)
|
||||
}
|
||||
/// Forget the output for the given input
|
||||
pub fn drop(&mut self, i: &I) -> bool {
|
||||
self.store.remove(i).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: 'static, O, E, F> Cache<I, Result<O, E>, F> where
|
||||
I: Eq + Hash,
|
||||
E: Clone,
|
||||
F: FnMut(I) -> Result<O, E>
|
||||
{
|
||||
/// Sink the ref from a Result into the Ok value, such that copying only occurs on the sad path
|
||||
/// but the return value can be short-circuited
|
||||
pub fn by_copy_fallible(&mut self, i: &I) -> Result<&O, E> where I: Copy {
|
||||
self.by_clone(i).as_ref().map_err(|e| e.clone())
|
||||
}
|
||||
/// Sink the ref from a Result into the Ok value, such that cloning only occurs on the sad path
|
||||
/// but the return value can be short-circuited
|
||||
pub fn by_clone_fallible(&mut self, i: &I) -> Result<&O, E> where I: Clone {
|
||||
self.by_clone(i).as_ref().map_err(|e| e.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: 'static, O, F> Cache<I, Option<O>, F> where
|
||||
I: Eq + Hash,
|
||||
F: FnMut(I) -> Option<O>
|
||||
{
|
||||
/// Sink the ref from an Option into the Some value such that the return value can be
|
||||
/// short-circuited
|
||||
pub fn by_copy_fallible(&mut self, i: &I) -> Option<&O> where I: Copy {
|
||||
self.by_copy(i).as_ref()
|
||||
}
|
||||
/// Sink the ref from an Option into the Some value such that the return value can be
|
||||
/// short-circuited
|
||||
pub fn by_clone_fallible(&mut self, i: &I) -> Option<&O> where I: Clone {
|
||||
self.by_clone(i).as_ref()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user