2025-06-16 11:54:01 -05:00
|
|
|
use std::collections::HashMap;
|
2025-06-16 11:32:05 -05:00
|
|
|
use std::collections::HashSet;
|
2025-06-16 11:54:01 -05:00
|
|
|
use std::sync::{LazyLock, Mutex};
|
|
|
|
|
|
2025-06-16 12:24:18 -05:00
|
|
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
|
|
|
|
|
pub enum Op {
|
|
|
|
|
Multiply,
|
|
|
|
|
Add,
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// type OperatorCache = HashMap<usize, HashSet<Vec<Op>>>;
|
2025-06-16 11:54:01 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> =
|
|
|
|
|
// LazyLock::new(|| Mutex::new(HashMap::new()));
|
2025-06-16 11:32:05 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// fn check_operator_set_cache(cache_key: usize) -> Option<HashSet<Vec<Op>>> {
|
|
|
|
|
// OPERATOR_SET_CACHE.lock().unwrap().get(&cache_key).cloned()
|
|
|
|
|
// }
|
2025-06-16 12:07:23 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet<Vec<Op>>) {
|
|
|
|
|
// OPERATOR_SET_CACHE
|
|
|
|
|
// .lock()
|
|
|
|
|
// .unwrap()
|
|
|
|
|
// .insert(cache_key, cache_value);
|
|
|
|
|
// }
|
2025-06-16 12:07:23 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// fn create_operator_set_recurse(
|
|
|
|
|
// operators_permutation_set: &mut HashSet<Vec<Op>>,
|
|
|
|
|
// operators: Vec<Op>,
|
|
|
|
|
// ) {
|
|
|
|
|
// operators_permutation_set.insert(operators.clone());
|
|
|
|
|
// if !operators.contains(&Op::Add) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
2025-06-16 11:32:05 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// for i in 0..operators.len() {
|
|
|
|
|
// if operators[i] == Op::Multiply {
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
// let mut new_operators = operators.clone();
|
|
|
|
|
// new_operators[i] = Op::Multiply;
|
|
|
|
|
// create_operator_set_recurse(operators_permutation_set, new_operators);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2025-06-16 11:32:05 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// pub fn create_operator_set(num_of_operators: usize) -> HashSet<Vec<Op>> {
|
|
|
|
|
// // Check cache
|
|
|
|
|
// if let Some(cached_value) = check_operator_set_cache(num_of_operators) {
|
|
|
|
|
// return cached_value.clone();
|
|
|
|
|
// }
|
2025-06-16 12:07:23 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// let mut operator_sets: HashSet<Vec<Op>> = HashSet::new();
|
|
|
|
|
// create_operator_set_recurse(&mut operator_sets, vec![Op::Add; num_of_operators]);
|
2025-06-16 12:07:23 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// // Store value in cache
|
|
|
|
|
// store_operator_set_in_cache(num_of_operators, operator_sets.clone());
|
2025-06-16 12:07:23 -05:00
|
|
|
|
2025-06-16 12:59:46 -05:00
|
|
|
// operator_sets
|
|
|
|
|
// }
|
2025-06-16 12:58:38 -05:00
|
|
|
|
|
|
|
|
fn create_permutations_recurse(
|
|
|
|
|
values: &HashSet<Op>,
|
|
|
|
|
len: usize,
|
|
|
|
|
permutations: &mut HashSet<Vec<Op>>,
|
|
|
|
|
curr_permutation: Vec<Op>,
|
|
|
|
|
) {
|
|
|
|
|
if curr_permutation.len() == len {
|
|
|
|
|
permutations.insert(curr_permutation);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for v in values {
|
|
|
|
|
let mut next_permutation = curr_permutation.clone();
|
|
|
|
|
next_permutation.push(*v);
|
|
|
|
|
create_permutations_recurse(values, len, permutations, next_permutation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_permutations(set: HashSet<Op>, len: usize) -> HashSet<Vec<Op>> {
|
|
|
|
|
let mut permutations = HashSet::new();
|
|
|
|
|
create_permutations_recurse(&set, len, &mut permutations, vec![]);
|
|
|
|
|
permutations
|
|
|
|
|
}
|