1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use chashmap::CHashMap;
use once_cell::sync::OnceCell;
use recur_fn::RecurFn;
use std::hash::Hash;
use std::sync::Arc;

/// Use `CHashMap` as `Cache`.
impl<Arg, Output> super::Cache for CHashMap<Arg, Arc<OnceCell<Output>>>
where
    Arg: Eq + Hash,
{
    type Arg = Arg;
    type Output = Output;

    fn new() -> Self {
        CHashMap::new()
    }

    fn get(&self, arg: &Self::Arg) -> Option<Arc<OnceCell<Self::Output>>> {
        self.get(arg).map(|arc| Arc::clone(&arc))
    }

    fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>> {
        let cell = std::cell::UnsafeCell::new(unsafe { std::mem::uninitialized() });

        self.upsert(
            arg,
            || {
                let arc = Arc::new(OnceCell::INIT);
                unsafe { std::ptr::write(cell.get(), Arc::clone(&arc)) };
                arc
            },
            |arc| {
                unsafe { std::ptr::write(cell.get(), Arc::clone(arc)) };
            },
        );

        cell.into_inner()
    }

    fn clear(&self) {
        self.clear();
    }
}

/// Creates a synchronized memoization of `f` using `CHashMap` as cache.
pub fn memoize<Arg, Output, F>(f: F) -> impl crate::FnMemo<Arg, Output>
where
    Arg: Clone + Eq + Hash,
    Output: Clone,
    F: RecurFn<Arg, Output>,
{
    super::Memo::<chashmap::CHashMap<_, _>, _>::new(f)
}