Add caching back in
This commit is contained in:
@@ -55,7 +55,7 @@ impl Equation {
|
||||
match op {
|
||||
Op::Multiply => accumulator *= self.components[i + 1],
|
||||
Op::Add => accumulator += self.components[i + 1],
|
||||
Op::CONCAT => {
|
||||
Op::Concat => {
|
||||
let accumulator_string = format!("{}", accumulator);
|
||||
let component_string = format!("{}", self.components[i + 1]);
|
||||
accumulator = (accumulator_string + &component_string).parse().unwrap();
|
||||
@@ -82,7 +82,7 @@ impl Equation {
|
||||
|
||||
pub fn solvable_multiply_add_concat(&self) -> bool {
|
||||
let op_combinations = create_permutations(
|
||||
HashSet::from([Op::Add, Op::Multiply, Op::CONCAT]),
|
||||
HashSet::from([Op::Add, Op::Multiply, Op::Concat]),
|
||||
self.components.len() - 1,
|
||||
);
|
||||
|
||||
|
||||
@@ -4,27 +4,42 @@ use std::collections::HashSet;
|
||||
pub enum Op {
|
||||
Multiply,
|
||||
Add,
|
||||
CONCAT,
|
||||
Concat,
|
||||
}
|
||||
|
||||
// use std::collections::HashMap;
|
||||
// use std::sync::{LazyLock, Mutex};
|
||||
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
||||
struct PermutationCacheKey {
|
||||
size: usize,
|
||||
set: Vec<Op>,
|
||||
}
|
||||
|
||||
// type OperatorCache = HashMap<usize, HashSet<Vec<Op>>>;
|
||||
impl PermutationCacheKey {
|
||||
fn new(size: usize, set: &HashSet<Op>) -> PermutationCacheKey {
|
||||
PermutationCacheKey {
|
||||
size,
|
||||
set: set.iter().cloned().collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> =
|
||||
// LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
|
||||
// fn check_operator_set_cache(cache_key: usize) -> Option<HashSet<Vec<Op>>> {
|
||||
// OPERATOR_SET_CACHE.lock().unwrap().get(&cache_key).cloned()
|
||||
// }
|
||||
type OperatorCache = HashMap<PermutationCacheKey, HashSet<Vec<Op>>>;
|
||||
|
||||
// fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet<Vec<Op>>) {
|
||||
// OPERATOR_SET_CACHE
|
||||
// .lock()
|
||||
// .unwrap()
|
||||
// .insert(cache_key, cache_value);
|
||||
// }
|
||||
static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
|
||||
fn check_operator_set_cache(cache_key: &PermutationCacheKey) -> Option<HashSet<Vec<Op>>> {
|
||||
OPERATOR_SET_CACHE.lock().unwrap().get(cache_key).cloned()
|
||||
}
|
||||
|
||||
fn store_operator_set_in_cache(cache_key: PermutationCacheKey, cache_value: HashSet<Vec<Op>>) {
|
||||
OPERATOR_SET_CACHE
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(cache_key, cache_value);
|
||||
}
|
||||
|
||||
// fn create_operator_set_recurse(
|
||||
// operators_permutation_set: &mut HashSet<Vec<Op>>,
|
||||
@@ -79,7 +94,11 @@ fn create_permutations_recurse(
|
||||
}
|
||||
|
||||
pub fn create_permutations(set: HashSet<Op>, len: usize) -> HashSet<Vec<Op>> {
|
||||
if let Some(cached_value) = check_operator_set_cache(&PermutationCacheKey::new(len, &set)) {
|
||||
return cached_value;
|
||||
}
|
||||
let mut permutations = HashSet::new();
|
||||
create_permutations_recurse(&set, len, &mut permutations, vec![]);
|
||||
store_operator_set_in_cache(PermutationCacheKey::new(len, &set), permutations.clone());
|
||||
permutations
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user