Use Arc to not clone cached permutations
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
mod operator_set;
|
mod operator_set;
|
||||||
|
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
use operator_set::{Op, create_permutations};
|
use operator_set::{Op, create_permutations};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -69,8 +71,8 @@ impl Equation {
|
|||||||
let op_combinations =
|
let op_combinations =
|
||||||
create_permutations(&Equation::MULTIPLY_ADD_SET, self.components.len() - 1);
|
create_permutations(&Equation::MULTIPLY_ADD_SET, self.components.len() - 1);
|
||||||
|
|
||||||
for combination in op_combinations {
|
for combination in op_combinations.deref() {
|
||||||
if self.answer == self.compute_answer(&combination) {
|
if self.answer == self.compute_answer(combination) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,8 +86,8 @@ impl Equation {
|
|||||||
self.components.len() - 1,
|
self.components.len() - 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
for combination in op_combinations {
|
for combination in op_combinations.deref() {
|
||||||
if self.answer == self.compute_answer(&combination) {
|
if self.answer == self.compute_answer(combination) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,17 +22,21 @@ impl PermutationCacheKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Permutation = Vec<Op>;
|
type Permutation = Vec<Op>;
|
||||||
|
type OperatorCache = HashMap<PermutationCacheKey, Arc<HashSet<Permutation>>>;
|
||||||
type OperatorCache = HashMap<PermutationCacheKey, HashSet<Permutation>>;
|
|
||||||
|
|
||||||
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: &PermutationCacheKey) -> Option<HashSet<Permutation>> {
|
fn check_operator_set_cache(cache_key: &PermutationCacheKey) -> Option<Arc<HashSet<Permutation>>> {
|
||||||
OPERATOR_SET_CACHE.lock().unwrap().get(cache_key).cloned()
|
let lock = OPERATOR_SET_CACHE.lock().unwrap();
|
||||||
|
let cache_value = lock.get(cache_key);
|
||||||
|
cache_value.cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn store_operator_set_in_cache(cache_key: PermutationCacheKey, cache_value: HashSet<Permutation>) {
|
fn store_operator_set_in_cache(
|
||||||
|
cache_key: PermutationCacheKey,
|
||||||
|
cache_value: Arc<HashSet<Permutation>>,
|
||||||
|
) {
|
||||||
OPERATOR_SET_CACHE
|
OPERATOR_SET_CACHE
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -57,13 +61,19 @@ fn create_permutations_recurse(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_permutations(set: &[Op], len: usize) -> HashSet<Permutation> {
|
pub fn create_permutations(set: &[Op], len: usize) -> Arc<HashSet<Permutation>> {
|
||||||
|
// Check cache
|
||||||
let cache_key = PermutationCacheKey::new(len, set.to_vec());
|
let cache_key = PermutationCacheKey::new(len, set.to_vec());
|
||||||
if let Some(cached_value) = check_operator_set_cache(&cache_key) {
|
if let Some(cached_value) = check_operator_set_cache(&cache_key) {
|
||||||
return cached_value;
|
return cached_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create permutations
|
||||||
let mut permutations = HashSet::new();
|
let mut permutations = HashSet::new();
|
||||||
create_permutations_recurse(set, len, &mut permutations, vec![]);
|
create_permutations_recurse(set, len, &mut permutations, vec![]);
|
||||||
|
|
||||||
|
// Cache and return
|
||||||
|
let permutations = Arc::new(permutations);
|
||||||
store_operator_set_in_cache(cache_key, permutations.clone());
|
store_operator_set_in_cache(cache_key, permutations.clone());
|
||||||
permutations
|
permutations
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user