Cache operator sets
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
|
||||
type OperatorCache = HashMap<usize, HashSet<Vec<bool>>>;
|
||||
|
||||
static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
|
||||
fn create_operator_set_recurse(
|
||||
operators_permutation_set: &mut HashSet<Vec<bool>>,
|
||||
@@ -20,7 +27,20 @@ fn create_operator_set_recurse(
|
||||
}
|
||||
|
||||
pub fn create_operator_set(num_of_operators: usize) -> HashSet<Vec<bool>> {
|
||||
{
|
||||
// Check cache
|
||||
if let Some(cached_value) = OPERATOR_SET_CACHE.lock().unwrap().get(&num_of_operators) {
|
||||
return cached_value.clone();
|
||||
}
|
||||
}
|
||||
let mut operator_sets: HashSet<Vec<bool>> = HashSet::new();
|
||||
create_operator_set_recurse(&mut operator_sets, vec![false; num_of_operators]);
|
||||
{
|
||||
// Store value in cache
|
||||
OPERATOR_SET_CACHE
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(num_of_operators, operator_sets.clone());
|
||||
}
|
||||
operator_sets
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user