essential_constraint_vm/
cached.rs

1use std::{collections::HashSet, sync::OnceLock};
2
3use essential_types::{solution::SolutionData, Hash};
4
5use crate::access::init_predicate_exists;
6
7#[derive(Default, Debug, PartialEq)]
8/// Lazily cache expensive to compute values.
9pub struct LazyCache {
10    /// Decision variables and addresses set of hashes.
11    /// See [`PredicateExists`][essential_constraint_asm::Op] for more details.
12    pub dec_var_hashes: OnceLock<HashSet<Hash>>,
13}
14
15impl LazyCache {
16    /// Create a new empty `LazyCache`.
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Get the decision variable hashes.
22    ///
23    /// The first time this is called, it will compute the hashes.
24    pub fn get_dec_var_hashes(&self, data: &[SolutionData]) -> &HashSet<Hash> {
25        self.dec_var_hashes
26            .get_or_init(|| init_predicate_exists(data).collect())
27    }
28}