diff options
| -rw-r--r-- | morphology/Cargo.toml | 1 | ||||
| -rw-r--r-- | morphology/src/sequence.rs | 14 | ||||
| -rw-r--r-- | morphology/src/set.rs | 34 |
3 files changed, 26 insertions, 23 deletions
diff --git a/morphology/Cargo.toml b/morphology/Cargo.toml index 1380a3b..efe01af 100644 --- a/morphology/Cargo.toml +++ b/morphology/Cargo.toml @@ -9,6 +9,7 @@ name = "meta_affine" path = "src/lib.rs" [dependencies] +rayon = "1.8" [[example]] name = "demo" diff --git a/morphology/src/sequence.rs b/morphology/src/sequence.rs index 784567d..2203e83 100644 --- a/morphology/src/sequence.rs +++ b/morphology/src/sequence.rs @@ -11,6 +11,7 @@ use crate::value::Value; use crate::set::ValidSet; use std::collections::{HashMap, HashSet}; +use rayon::prelude::*; /// Represents an ordered sequence of meta-affine values. /// @@ -393,14 +394,11 @@ pub fn generate_all_sequences(length: usize) -> Vec<Sequence> { let max_set_size = length; // Heuristic: set size can't exceed sequence length let all_sets = generate_valid_sets(max_set_size); - let mut all_sequences = Vec::new(); - - for set in all_sets { - let sequences = generate_sequences(&set, length); - all_sequences.extend(sequences); - } - - all_sequences + // Process sets in parallel + all_sets + .par_iter() + .flat_map(|set| generate_sequences(set, length)) + .collect() } /// Counts the number of valid sequences for a given length without generating them. diff --git a/morphology/src/set.rs b/morphology/src/set.rs index b3adbbc..cb0d015 100644 --- a/morphology/src/set.rs +++ b/morphology/src/set.rs @@ -12,6 +12,7 @@ use crate::value::{Value, get_minimum_derivation_path}; use std::collections::HashSet; use std::hash::{Hash, Hasher}; use std::collections::hash_map::DefaultHasher; +use rayon::prelude::*; /// Represents a valid set in meta-affine space. #[derive(Debug, Clone, PartialEq, Eq)] @@ -281,22 +282,25 @@ pub fn generate_valid_sets(max_size: usize) -> Vec<ValidSet> { // Generate sets of increasing size for size in 2..=max_size { - let mut new_sets = HashSet::new(); - - // For each set of size-1 - for prev_set in &sets_by_size[size - 1] { - // Try adding each candidate value - let candidates = get_candidate_values(prev_set, max_size as i32); - - for candidate in candidates { - let new_set = prev_set.add_value_with_dependencies(candidate); + // Process sets in parallel + let new_sets_vec: Vec<ValidSet> = sets_by_size[size - 1] + .par_iter() + .flat_map(|prev_set| { + let candidates = get_candidate_values(prev_set, max_size as i32); + + candidates.into_iter().filter_map(|candidate| { + let new_set = prev_set.add_value_with_dependencies(candidate); + if new_set.size() == size { + Some(new_set) + } else { + None + } + }).collect::<Vec<_>>() + }) + .collect(); - // Only keep if size matches (HashSet automatically deduplicates) - if new_set.size() == size { - new_sets.insert(new_set); - } - } - } + // Deduplicate using HashSet + let new_sets: HashSet<ValidSet> = new_sets_vec.into_iter().collect(); // Add all new sets of this size for set in new_sets { |