Encapsulate cache locking code
This commit is contained in:
@@ -7,6 +7,17 @@ type OperatorCache = HashMap<usize, HashSet<Vec<bool>>>;
|
|||||||
static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> =
|
static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> =
|
||||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||||
|
|
||||||
|
fn check_operator_set_cache(cache_key: usize) -> Option<HashSet<Vec<bool>>> {
|
||||||
|
OPERATOR_SET_CACHE.lock().unwrap().get(&cache_key).cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet<Vec<bool>>) {
|
||||||
|
OPERATOR_SET_CACHE
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(cache_key, cache_value);
|
||||||
|
}
|
||||||
|
|
||||||
fn create_operator_set_recurse(
|
fn create_operator_set_recurse(
|
||||||
operators_permutation_set: &mut HashSet<Vec<bool>>,
|
operators_permutation_set: &mut HashSet<Vec<bool>>,
|
||||||
operators: Vec<bool>,
|
operators: Vec<bool>,
|
||||||
@@ -27,20 +38,16 @@ fn create_operator_set_recurse(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_operator_set(num_of_operators: usize) -> HashSet<Vec<bool>> {
|
pub fn create_operator_set(num_of_operators: usize) -> HashSet<Vec<bool>> {
|
||||||
{
|
|
||||||
// Check cache
|
// Check cache
|
||||||
if let Some(cached_value) = OPERATOR_SET_CACHE.lock().unwrap().get(&num_of_operators) {
|
if let Some(cached_value) = check_operator_set_cache(num_of_operators) {
|
||||||
return cached_value.clone();
|
return cached_value.clone();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
let mut operator_sets: HashSet<Vec<bool>> = HashSet::new();
|
let mut operator_sets: HashSet<Vec<bool>> = HashSet::new();
|
||||||
create_operator_set_recurse(&mut operator_sets, vec![false; num_of_operators]);
|
create_operator_set_recurse(&mut operator_sets, vec![false; num_of_operators]);
|
||||||
{
|
|
||||||
// Store value in cache
|
// Store value in cache
|
||||||
OPERATOR_SET_CACHE
|
store_operator_set_in_cache(num_of_operators, operator_sets.clone());
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.insert(num_of_operators, operator_sets.clone());
|
|
||||||
}
|
|
||||||
operator_sets
|
operator_sets
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user