From d0e3dc212ea7d855bf5339a5f9904e681e7b363a Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Tue, 11 Nov 2025 08:12:01 +0100 Subject: feat: add sort with segregated sequence lengths --- morphology/README.md | 9 +++- morphology/examples/test_all_sorting_options.rs | 69 +++++++++++++++++++++++++ morphology/examples/test_length_sorting.rs | 64 +++++++++++++++++++++++ morphology/src/sequence.rs | 40 +++++++++++++- 4 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 morphology/examples/test_all_sorting_options.rs create mode 100644 morphology/examples/test_length_sorting.rs (limited to 'morphology') diff --git a/morphology/README.md b/morphology/README.md index dee8c3e..31c1e16 100644 --- a/morphology/README.md +++ b/morphology/README.md @@ -88,6 +88,9 @@ SortingMethod::BreadthFirst // Compare all outer values first, then all inner SortingDirection::Forward // Compare sequences from first to last position SortingDirection::Backward // Compare sequences from last to first position +LengthSorting::InterpolateLength // Sequences of different lengths sorted together by content +LengthSorting::SegregateLength // Sequences grouped by length, shorter sequences first + sort_sequences(&mut sequences, outer_order, inner_order) ``` @@ -168,7 +171,7 @@ let all_sets = generate_valid_sets(5); ```rust use meta_affine::{ValidSet, Value, Sequence}; use meta_affine::sequence::{generate_sequences, generate_all_sequences, sort_sequences}; -use meta_affine::sequence::{SequenceOptions, PrecedenceOrder, SortingMethod, SortingDirection}; +use meta_affine::sequence::{SequenceOptions, PrecedenceOrder, SortingMethod, SortingDirection, LengthSorting}; // Create a valid set let set = ValidSet::new() @@ -177,11 +180,12 @@ let set = ValidSet::new() // Generate all valid sequences of length 3 from this set let mut sequences = generate_sequences(&set, 3); -// Sort with defaults (Forward, DepthFirst, IncreasingPositiveFirst) +// Sort with defaults (InterpolateLength, Forward, DepthFirst, IncreasingPositiveFirst) sort_sequences(&mut sequences, None); // Sort with custom options using builder pattern let opts = SequenceOptions::new() + .length_sorting(LengthSorting::SegregateLength) .sorting_direction(SortingDirection::Backward) .sorting_method(SortingMethod::BreadthFirst) .outer_order(PrecedenceOrder::IncreasingNegativeFirst) @@ -190,6 +194,7 @@ sort_sequences(&mut sequences, opts); // Generate all sequences from all valid sets with sorting options let opts = SequenceOptions::new() + .length_sorting(LengthSorting::SegregateLength) .sorting_method(SortingMethod::BreadthFirst) .sorting_direction(SortingDirection::Backward); let all_sequences = generate_all_sequences(3, opts); diff --git a/morphology/examples/test_all_sorting_options.rs b/morphology/examples/test_all_sorting_options.rs new file mode 100644 index 0000000..2b60b3f --- /dev/null +++ b/morphology/examples/test_all_sorting_options.rs @@ -0,0 +1,69 @@ +// Test program demonstrating all sorting options working together + +use meta_affine::{Sequence, Value}; +use meta_affine::sequence::{ + sort_sequences, SequenceOptions, + PrecedenceOrder, SortingMethod, SortingDirection, LengthSorting +}; + +fn print_sequences(label: &str, sequences: &[Sequence]) { + println!("\n{}:", label); + for (i, seq) in sequences.iter().enumerate() { + print!(" {}: len={} [", i, seq.length()); + for (j, v) in seq.values().iter().enumerate() { + if j > 0 { print!(", "); } + print!("({},{})", v.outer, v.inner); + } + println!("]"); + } +} + +fn main() { + // Create diverse sequences to showcase all options + let sequences = vec![ + Sequence::new(vec![Value::new(1, 0), Value::new(0, 0)]), + Sequence::new(vec![Value::new(0, 0)]), + Sequence::new(vec![Value::new(0, 1), Value::new(1, 0), Value::new(0, 0)]), + Sequence::new(vec![Value::new(1, 0), Value::new(0, 1), Value::new(0, 0)]), + Sequence::new(vec![Value::new(0, 1), Value::new(0, 0)]), + ]; + + print_sequences("Original sequences", &sequences); + + // Example 1: All defaults + let mut test1 = sequences.clone(); + sort_sequences(&mut test1, None); + print_sequences("Defaults (InterpolateLength, Forward, DepthFirst, IncreasingPositiveFirst)", &test1); + + // Example 2: SegregateLength only + let mut test2 = sequences.clone(); + let opts = SequenceOptions::new() + .length_sorting(LengthSorting::SegregateLength); + sort_sequences(&mut test2, opts); + print_sequences("SegregateLength + defaults", &test2); + + // Example 3: BreadthFirst + Backward + let mut test3 = sequences.clone(); + let opts = SequenceOptions::new() + .sorting_method(SortingMethod::BreadthFirst) + .sorting_direction(SortingDirection::Backward); + sort_sequences(&mut test3, opts); + print_sequences("BreadthFirst + Backward + InterpolateLength", &test3); + + // Example 4: All custom options combined + let mut test4 = sequences.clone(); + let opts = SequenceOptions::new() + .length_sorting(LengthSorting::SegregateLength) + .sorting_direction(SortingDirection::Backward) + .sorting_method(SortingMethod::BreadthFirst) + .outer_order(PrecedenceOrder::IncreasingNegativeFirst) + .inner_order(PrecedenceOrder::IncreasingPositiveFirst); + sort_sequences(&mut test4, opts); + print_sequences("All custom: SegregateLength + Backward + BreadthFirst + custom precedence", &test4); + + println!("\n=== Complete Sorting Options ==="); + println!("LengthSorting: InterpolateLength (default) | SegregateLength"); + println!("SortingDirection: Forward (default) | Backward"); + println!("SortingMethod: DepthFirst (default) | BreadthFirst"); + println!("PrecedenceOrder: IncreasingPositiveFirst (default) | IncreasingNegativeFirst"); +} diff --git a/morphology/examples/test_length_sorting.rs b/morphology/examples/test_length_sorting.rs new file mode 100644 index 0000000..2163e50 --- /dev/null +++ b/morphology/examples/test_length_sorting.rs @@ -0,0 +1,64 @@ +// Test program to demonstrate InterpolateLength vs SegregateLength sorting + +use meta_affine::{Sequence, Value}; +use meta_affine::sequence::{sort_sequences, SequenceOptions, LengthSorting, 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!("] (length {})", seq.length()); + } +} + +fn main() { + // Create sequences of different lengths + let sequences = vec![ + // Length 3 + Sequence::new(vec![Value::new(1, 0), Value::new(0, 1), Value::new(0, 0)]), + // Length 1 + Sequence::new(vec![Value::new(0, 0)]), + // Length 2 + Sequence::new(vec![Value::new(1, 0), Value::new(0, 0)]), + // Length 3 + Sequence::new(vec![Value::new(0, 1), Value::new(1, 0), Value::new(0, 0)]), + // Length 2 + Sequence::new(vec![Value::new(0, 1), Value::new(0, 0)]), + // Length 4 + Sequence::new(vec![Value::new(1, 0), Value::new(0, 1), Value::new(1, 0), Value::new(0, 0)]), + ]; + + print_sequences("Original sequences", &sequences); + + // Test InterpolateLength (default) - sequences sorted by content, length mixed in + let mut interpolate = sequences.clone(); + let opts = SequenceOptions::new() + .length_sorting(LengthSorting::InterpolateLength) + .sorting_method(SortingMethod::DepthFirst); + sort_sequences(&mut interpolate, opts); + + print_sequences("InterpolateLength (sorted by content, lengths mixed)", &interpolate); + println!(" Explanation: Sequences are sorted by their content values"); + println!(" Length is only considered when content is equal"); + + // Test SegregateLength - sequences grouped by length, then sorted by content within each group + let mut segregate = sequences.clone(); + let opts = SequenceOptions::new() + .length_sorting(LengthSorting::SegregateLength) + .sorting_method(SortingMethod::DepthFirst); + sort_sequences(&mut segregate, opts); + + print_sequences("SegregateLength (grouped by length, then by content)", &segregate); + println!(" Explanation: All length-1 sequences come first (sorted by content)"); + println!(" Then all length-2 sequences (sorted by content)"); + println!(" Then all length-3 sequences (sorted by content)"); + println!(" Then all length-4 sequences (sorted by content)"); + + println!("\n=== Summary ==="); + println!("InterpolateLength: Length is only a tiebreaker when content is equal"); + println!("SegregateLength: Sequences are grouped by length first, shorter always before longer"); +} diff --git a/morphology/src/sequence.rs b/morphology/src/sequence.rs index bb93418..4fcf70e 100644 --- a/morphology/src/sequence.rs +++ b/morphology/src/sequence.rs @@ -464,6 +464,16 @@ pub enum SortingDirection { Backward, } +/// Defines how sequences of different lengths are sorted. +#[derive(Debug, Clone, Copy, Default)] +pub enum LengthSorting { + /// Sequences of different lengths are sorted together by content + #[default] + InterpolateLength, + /// Sequences are grouped by length, shorter sequences first + SegregateLength, +} + /// Configuration options for sequence generation and sorting. /// /// Use the builder pattern to specify only the options you need. @@ -491,6 +501,7 @@ pub struct SequenceOptions { inner_precedence_order: Option, sorting_method: Option, sorting_direction: Option, + length_sorting: Option, } impl SequenceOptions { @@ -523,6 +534,12 @@ impl SequenceOptions { self } + /// Sets the length sorting mode. + pub fn length_sorting(mut self, length_sorting: LengthSorting) -> Self { + self.length_sorting = Some(length_sorting); + self + } + /// Gets the outer precedence order, or default if not set. pub(crate) fn get_outer_order(&self) -> PrecedenceOrder { self.outer_precedence_order.unwrap_or_default() @@ -542,6 +559,11 @@ impl SequenceOptions { pub(crate) fn get_sorting_direction(&self) -> SortingDirection { self.sorting_direction.unwrap_or_default() } + + /// Gets the length sorting mode, or default if not set. + pub(crate) fn get_length_sorting(&self) -> LengthSorting { + self.length_sorting.unwrap_or_default() + } } /// Sorts sequences using specified precedence orders for outer and inner values. @@ -587,6 +609,7 @@ pub fn sort_sequences( let inner_order = opts.get_inner_order(); let sorting_method = opts.get_sorting_method(); let sorting_direction = opts.get_sorting_direction(); + let length_sorting = opts.get_length_sorting(); // Determine the length of the longest sequence let max_length = sequences.iter() @@ -600,7 +623,7 @@ pub fn sort_sequences( // Sort using the generated precedence lists sequences.sort_by(|a, b| { - compare_sequences_with_precedence(a, b, &outer_precedence, &inner_precedence, sorting_method, sorting_direction) + compare_sequences_with_precedence(a, b, &outer_precedence, &inner_precedence, sorting_method, sorting_direction, length_sorting) }); } @@ -638,9 +661,24 @@ fn compare_sequences_with_precedence( inner_precedence: &[i32], sorting_method: SortingMethod, sorting_direction: SortingDirection, + length_sorting: LengthSorting, ) -> std::cmp::Ordering { use std::cmp::Ordering; + // Handle length-based segregation + match length_sorting { + LengthSorting::SegregateLength => { + let len_cmp = a.values.len().cmp(&b.values.len()); + if len_cmp != Ordering::Equal { + return len_cmp; // Early return: shorter sequences always come first + } + // If lengths are equal, fall through to content comparison + } + LengthSorting::InterpolateLength => { + // Do nothing, proceed to normal content comparison + } + } + match sorting_direction { SortingDirection::Forward => { match sorting_method { -- cgit v1.2.3