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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::FnMemo;
use chashmap::CHashMap;
use once_cell::sync::OnceCell;
use recur_fn::RecurFn;
use std::hash::Hash;
use std::sync::{Arc, RwLock};

/// The cache for synchronized memoization.
pub trait Cache {
    type Arg;
    type Output;

    /// Creates an empty cache.
    fn new() -> Self;
    /// Gets the cell of the `arg` in cache. Returns `None` if `arg` is not cached.
    /// This method should only acquire read lock if needed.
    fn get(&self, arg: &Self::Arg) -> Option<Arc<OnceCell<Self::Output>>>;
    /// Gets the cell of the `arg` in cache, creates if `arg` is not cached.
    /// This method can acquire write lock if needed.
    fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>>;
    /// Clears the cache.
    fn clear(&self);
}

/// Use `CHashMap` as `Cache`.
impl<Arg, Output> Cache for CHashMap<Arg, Arc<OnceCell<Output>>>
where
    Arg: PartialEq + 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();
    }
}

use std::collections::HashMap;
/// Use `HashMap` with `RwLock` as `Cache`.
impl<Arg, Output> Cache for RwLock<HashMap<Arg, Arc<OnceCell<Output>>>>
where
    Arg: Eq + Hash,
{
    type Arg = Arg;
    type Output = Output;

    fn new() -> Self {
        RwLock::new(HashMap::new())
    }

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

    fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>> {
        Arc::clone(
            self.write()
                .unwrap()
                .entry(arg)
                .or_insert_with(|| Arc::new(OnceCell::INIT)),
        )
    }

    fn clear(&self) {
        self.write().unwrap().clear()
    }
}

/// Use `Vec` with `RwLock` as `Cache` for sequences.
impl<Output> Cache for RwLock<Vec<Arc<OnceCell<Output>>>> {
    type Arg = usize;
    type Output = Output;

    fn new() -> Self {
        RwLock::new(Vec::new())
    }

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

    fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>> {
        let mut write = self.write().unwrap();

        if arg >= write.len() {
            let delta: usize = arg + 1 - write.len();
            write.reserve(delta);
            while write.len() <= arg {
                write.push(Arc::new(OnceCell::INIT));
            }
        }
        Arc::clone(&write[arg])
    }

    fn clear(&self) {
        self.write().unwrap().clear()
    }
}

/// The synchronized implementation of `FnMemo`.
pub struct Memo<C, F> {
    cache: C,
    f: F,
}

impl<C: Cache, F> Memo<C, F> {
    /// Constructs a `Memo` using `C` as cache, caching function `f`.
    pub fn new(f: F) -> Self {
        Memo { cache: C::new(), f }
    }
}

impl<C: Cache, F: RecurFn<C::Arg, C::Output>> FnMemo<C::Arg, C::Output> for Memo<C, F>
where
    C::Arg: Clone,
    C::Output: Clone,
{
    fn call(&self, arg: C::Arg) -> C::Output {
        self.cache
            .get(&arg)
            .unwrap_or_else(|| self.cache.get_or_new(arg.clone()))
            .get_or_init(|| self.f.body(|arg| self.call(arg), arg))
            .clone()
    }

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

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

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

/// Creates a synchronized memoization of the sequence `f`
/// using `Vec` with `RwLock` as cache.
pub fn memoize_rw_lock_seq<Output, F>(f: F) -> impl FnMemo<usize, Output>
where
    Output: Clone,
    F: RecurFn<usize, Output>,
{
    Memo::<RwLock<Vec<_>>, _>::new(f)
}