diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-11 07:04:56 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-11 07:04:56 +0100 |
| commit | 1d0086a3d223f12201b4b1a7832cd283ff384daf (patch) | |
| tree | d6c500125025e48ead3cf92dea29edae396b35a9 /morphology/examples | |
| parent | cccaa346384c7fed2ebf2524a2a6cca1e1bec577 (diff) | |
feat: add breadth first sorting
Diffstat (limited to 'morphology/examples')
| -rw-r--r-- | morphology/examples/test_sorting_methods.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/morphology/examples/test_sorting_methods.rs b/morphology/examples/test_sorting_methods.rs new file mode 100644 index 0000000..b4ad18f --- /dev/null +++ b/morphology/examples/test_sorting_methods.rs @@ -0,0 +1,60 @@ +// Test program to demonstrate DepthFirst vs BreadthFirst sorting + +use meta_affine::{Sequence, Value}; +use meta_affine::sequence::{sort_sequences, SequenceOptions, SortingMethod}; + +fn print_sequences(label: &str, sequences: &[Sequence]) { + println!("\n{}:", label); + for (i, seq) in sequences.iter().enumerate() { + print!(" {}: [", i); + for (j, v) in seq.values().iter().enumerate() { + if j > 0 { print!(", "); } + print!("({},{})", v.outer, v.inner); + } + println!("]"); + } +} + +fn main() { + // Create test sequences that demonstrate the difference between DepthFirst and BreadthFirst + // These sequences have same outer values but different inner values at different positions + let sequences = vec![ + // Sequence A: (0,2), (0,1), (0,0) - outer: [0,0,0], inner: [2,1,0] + Sequence::new(vec![Value::new(0, 2), Value::new(0, 1), Value::new(0, 0)]), + // Sequence B: (0,1), (0,2), (0,0) - outer: [0,0,0], inner: [1,2,0] + Sequence::new(vec![Value::new(0, 1), Value::new(0, 2), Value::new(0, 0)]), + // Sequence C: (1,0), (0,0) - outer: [1,0], inner: [0,0] + Sequence::new(vec![Value::new(1, 0), Value::new(0, 0)]), + // Sequence D: (0,0) - outer: [0], inner: [0] + Sequence::new(vec![Value::new(0, 0)]), + ]; + + print_sequences("Original sequences", &sequences); + + // Test DepthFirst sorting (default) + let mut depth_first = sequences.clone(); + sort_sequences(&mut depth_first, SequenceOptions::new().sorting_method(SortingMethod::DepthFirst)); + + print_sequences("DepthFirst sorted (compares position-by-position)", &depth_first); + println!(" Explanation: At position 0, compares (0,2) vs (0,1)"); + println!(" Outer values equal (0==0), so compare inner: 1 < 2"); + println!(" Therefore sequence B comes before A"); + + // Test BreadthFirst sorting + let mut breadth_first = sequences.clone(); + sort_sequences(&mut breadth_first, SequenceOptions::new().sorting_method(SortingMethod::BreadthFirst)); + + print_sequences("BreadthFirst sorted (all outer values, then all inner values)", &breadth_first); + println!(" Explanation: First compares all outer values [0,0,0] vs [0,0,0] -> equal"); + println!(" Then compares all inner values [2,1,0] vs [1,2,0]"); + println!(" At position 0 of inner: 1 < 2"); + println!(" Therefore sequence B comes before A"); + + println!("\n=== Note ==="); + println!("In this example, both methods produce the same result because:"); + println!("- DepthFirst: Compares position 0 -> outer equal, inner 1<2"); + println!("- BreadthFirst: All outers equal, then inner position 0 -> 1<2"); + println!("\nBoth reach the same conclusion through different comparison paths."); + println!("The distinction becomes more apparent with more complex sequences"); + println!("where outer values differ at later positions."); +} |