From cf758968d957de0fa2f1bf39a42d633b4fe74616 Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Tue, 11 Nov 2025 08:02:20 +0100 Subject: feat: add backward sorting --- morphology/examples/test_sorting_directions.rs | 81 ++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 morphology/examples/test_sorting_directions.rs (limited to 'morphology/examples/test_sorting_directions.rs') diff --git a/morphology/examples/test_sorting_directions.rs b/morphology/examples/test_sorting_directions.rs new file mode 100644 index 0000000..30ea280 --- /dev/null +++ b/morphology/examples/test_sorting_directions.rs @@ -0,0 +1,81 @@ +// Test program to demonstrate Forward vs Backward sorting directions + +use meta_affine::{Sequence, Value}; +use meta_affine::sequence::{sort_sequences, SequenceOptions, SortingMethod, SortingDirection}; + +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 where Forward and Backward will produce different results + let sequences = vec![ + // Sequence A: (1,0), (0,1), (0,0) - differs at position 0 + Sequence::new(vec![Value::new(1, 0), Value::new(0, 1), Value::new(0, 0)]), + // Sequence B: (0,1), (1,0), (0,0) - differs at position 0 + Sequence::new(vec![Value::new(0, 1), Value::new(1, 0), Value::new(0, 0)]), + // Sequence C: (0,0) + Sequence::new(vec![Value::new(0, 0)]), + ]; + + print_sequences("Original sequences", &sequences); + + // Test Forward DepthFirst (default) + let mut forward_depth = sequences.clone(); + let opts = SequenceOptions::new() + .sorting_method(SortingMethod::DepthFirst) + .sorting_direction(SortingDirection::Forward); + sort_sequences(&mut forward_depth, opts); + + print_sequences("Forward DepthFirst (compares from first position)", &forward_depth); + println!(" Explanation: At position 0, compares (1,0) vs (0,1)"); + println!(" Outer values: 0 < 1, so B comes before A"); + + // Test Backward DepthFirst + let mut backward_depth = sequences.clone(); + let opts = SequenceOptions::new() + .sorting_method(SortingMethod::DepthFirst) + .sorting_direction(SortingDirection::Backward); + sort_sequences(&mut backward_depth, opts); + + print_sequences("Backward DepthFirst (compares from last position)", &backward_depth); + println!(" Explanation: Starting from last position (both end with (0,0))"); + println!(" Position -2 (second to last): compares (0,1) vs (1,0)"); + println!(" Outer values: 0 < 1, so A comes before B"); + + // Test Forward BreadthFirst + let mut forward_breadth = sequences.clone(); + let opts = SequenceOptions::new() + .sorting_method(SortingMethod::BreadthFirst) + .sorting_direction(SortingDirection::Forward); + sort_sequences(&mut forward_breadth, opts); + + print_sequences("Forward BreadthFirst", &forward_breadth); + println!(" Explanation: Compare all outer values [1,0,0] vs [0,1,0]"); + println!(" At position 0: 0 < 1, so B comes before A"); + + // Test Backward BreadthFirst + let mut backward_breadth = sequences.clone(); + let opts = SequenceOptions::new() + .sorting_method(SortingMethod::BreadthFirst) + .sorting_direction(SortingDirection::Backward); + sort_sequences(&mut backward_breadth, opts); + + print_sequences("Backward BreadthFirst", &backward_breadth); + println!(" Explanation: Compare all outer values reversed [0,0,1] vs [0,1,0]"); + println!(" At position 0 (from end): both are 0"); + println!(" At position 1 (from end): 0 < 1, so A comes before B"); + + println!("\n=== Summary ==="); + println!("Forward: Compares sequences starting from the first position"); + println!("Backward: Compares sequences starting from the last position"); + println!("This can produce different orderings depending on where differences occur."); +} -- cgit v1.2.3