essential_constraint_vm/
cached.rs

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
use std::{collections::HashSet, sync::OnceLock};

use essential_types::{solution::SolutionData, Hash};

use crate::access::init_predicate_exists;

#[derive(Default, Debug, PartialEq)]
/// Lazily cache expensive to compute values.
pub struct LazyCache {
    /// Decision variables and addresses set of hashes.
    /// See [`PredicateExists`][essential_constraint_asm::Op] for more details.
    pub dec_var_hashes: OnceLock<HashSet<Hash>>,
}

impl LazyCache {
    /// Create a new empty `LazyCache`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the decision variable hashes.
    ///
    /// The first time this is called, it will compute the hashes.
    pub fn get_dec_var_hashes(&self, data: &[SolutionData]) -> &HashSet<Hash> {
        self.dec_var_hashes
            .get_or_init(|| init_predicate_exists(data).collect())
    }
}