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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
mod cache;
|
||||
pub use cache::Cache;
|
||||
mod substack;
|
||||
pub use cache::Cache;
|
||||
pub use substack::Substack;
|
||||
|
||||
pub fn as_modpath(path: &Vec<String>) -> String {
|
||||
path.join("::")
|
||||
}
|
||||
43
src/utils/substack.rs
Normal file
43
src/utils/substack.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
/// Implement a FILO stack that lives on the regular call stack as a linked list.
|
||||
/// Mainly useful to detect loops in recursive algorithms where the recursion isn't
|
||||
/// deep enough to warrant a heap-allocated set
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Substack<'a, T> {
|
||||
pub item: T,
|
||||
pub prev: Option<&'a Self>
|
||||
}
|
||||
|
||||
impl<'a, T> Substack<'a, T> {
|
||||
pub fn item(&self) -> &T { &self.item }
|
||||
pub fn prev(&self) -> Option<&'a Substack<'a, T>> { self.prev }
|
||||
|
||||
pub fn new(item: T) -> Self {
|
||||
Self {
|
||||
item,
|
||||
prev: None
|
||||
}
|
||||
}
|
||||
pub fn push(&'a self, item: T) -> Self {
|
||||
Self {
|
||||
item,
|
||||
prev: Some(self)
|
||||
}
|
||||
}
|
||||
pub fn iter(&'a self) -> SubstackIterator<'a, T> {
|
||||
SubstackIterator { curr: Some(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SubstackIterator<'a, T> {
|
||||
curr: Option<&'a Substack<'a, T>>
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for SubstackIterator<'a, T> {
|
||||
type Item = &'a T;
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
let Substack{ item, prev } = self.curr?;
|
||||
self.curr = *prev;
|
||||
Some(item)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user