inc_complete/computation/
hashmapped.rs1use std::collections::HashMap;
2
3use crate::{Cell, DbHandle};
4use std::hash::Hash;
5
6use super::Computation;
7
8#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
10pub struct HashMapStorage<T>(T);
11
12impl<T> HashMapStorage<T> {
13 pub fn new(value: T) -> Self {
14 Self(value)
15 }
16}
17
18impl<T> Computation for HashMapStorage<T>
19where
20 T: Computation + Eq + Hash,
21{
22 type Output = <T as Computation>::Output;
23 type Storage = (
24 HashMap<Self, Cell>,
25 HashMap<Cell, (Self, Option<Self::Output>)>,
26 );
27
28 fn run(&self, handle: &mut DbHandle<impl Computation>) -> Self::Output {
29 self.0.run(handle)
30 }
31
32 fn input_to_cell(input: &Self, (self_to_cell, _): &Self::Storage) -> Option<Cell> {
33 self_to_cell.get(input).copied()
34 }
35
36 fn get_function_and_output(
37 cell: Cell,
38 (_, cell_to_output): &Self::Storage,
39 ) -> (&Self, Option<&Self::Output>) {
40 let (this, output) = &cell_to_output[&cell];
41 (this, output.as_ref())
42 }
43
44 fn set_output(cell: Cell, new_output: Self::Output, (_, cell_to_output): &mut Self::Storage) {
45 cell_to_output.entry(cell).and_modify(|(_, output)| {
46 *output = Some(new_output);
47 });
48 }
49
50 fn insert_new_cell(cell: Cell, function: Self, storage: &mut Self::Storage) {
51 storage.0.insert(function.clone(), cell);
52 storage.1.insert(cell, (function, None));
53 }
54}