diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-11 08:02:20 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-11 08:02:20 +0100 |
| commit | cf758968d957de0fa2f1bf39a42d633b4fe74616 (patch) | |
| tree | b7a8a3d181055422a9de046732d3804786549a24 /morphology/src/sequence.rs | |
| parent | 1d0086a3d223f12201b4b1a7832cd283ff384daf (diff) | |
feat: add backward sorting
Diffstat (limited to 'morphology/src/sequence.rs')
| -rw-r--r-- | morphology/src/sequence.rs | 118 |
1 files changed, 89 insertions, 29 deletions
diff --git a/morphology/src/sequence.rs b/morphology/src/sequence.rs index 3005cdf..bb93418 100644 --- a/morphology/src/sequence.rs +++ b/morphology/src/sequence.rs @@ -454,6 +454,16 @@ pub enum SortingMethod { BreadthFirst, } +/// Defines the sorting direction for sequences. +#[derive(Debug, Clone, Copy, Default)] +pub enum SortingDirection { + /// Compare sequences from first to last position + #[default] + Forward, + /// Compare sequences from last to first position + Backward, +} + /// Configuration options for sequence generation and sorting. /// /// Use the builder pattern to specify only the options you need. @@ -480,6 +490,7 @@ pub struct SequenceOptions { outer_precedence_order: Option<PrecedenceOrder>, inner_precedence_order: Option<PrecedenceOrder>, sorting_method: Option<SortingMethod>, + sorting_direction: Option<SortingDirection>, } impl SequenceOptions { @@ -506,6 +517,12 @@ impl SequenceOptions { self } + /// Sets the sorting direction. + pub fn sorting_direction(mut self, direction: SortingDirection) -> Self { + self.sorting_direction = Some(direction); + 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() @@ -520,6 +537,11 @@ impl SequenceOptions { pub(crate) fn get_sorting_method(&self) -> SortingMethod { self.sorting_method.unwrap_or_default() } + + /// Gets the sorting direction, or default if not set. + pub(crate) fn get_sorting_direction(&self) -> SortingDirection { + self.sorting_direction.unwrap_or_default() + } } /// Sorts sequences using specified precedence orders for outer and inner values. @@ -564,6 +586,7 @@ pub fn sort_sequences( let outer_order = opts.get_outer_order(); let inner_order = opts.get_inner_order(); let sorting_method = opts.get_sorting_method(); + let sorting_direction = opts.get_sorting_direction(); // Determine the length of the longest sequence let max_length = sequences.iter() @@ -577,7 +600,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) + compare_sequences_with_precedence(a, b, &outer_precedence, &inner_precedence, sorting_method, sorting_direction) }); } @@ -614,43 +637,80 @@ fn compare_sequences_with_precedence( outer_precedence: &[i32], inner_precedence: &[i32], sorting_method: SortingMethod, + sorting_direction: SortingDirection, ) -> std::cmp::Ordering { use std::cmp::Ordering; - match sorting_method { - SortingMethod::DepthFirst => { - // Current behavior: compare position-by-position - for (val_a, val_b) in a.values.iter().zip(b.values.iter()) { - let cmp = compare_values_with_precedence(val_a, val_b, outer_precedence, inner_precedence); - if cmp != Ordering::Equal { - return cmp; + match sorting_direction { + SortingDirection::Forward => { + match sorting_method { + SortingMethod::DepthFirst => { + // Compare position-by-position: outer then inner at each position + for (val_a, val_b) in a.values.iter().zip(b.values.iter()) { + let cmp = compare_values_with_precedence(val_a, val_b, outer_precedence, inner_precedence); + if cmp != Ordering::Equal { + return cmp; + } + } + a.values.len().cmp(&b.values.len()) + } + SortingMethod::BreadthFirst => { + // Compare all outer values first, then all inner values + + // First, compare all outer values position-by-position + for (val_a, val_b) in a.values.iter().zip(b.values.iter()) { + let outer_cmp = compare_with_precedence(val_a.outer, val_b.outer, outer_precedence); + if outer_cmp != Ordering::Equal { + return outer_cmp; + } + } + + // If all outer values are equal, compare all inner values + for (val_a, val_b) in a.values.iter().zip(b.values.iter()) { + let inner_cmp = compare_with_precedence(val_a.inner, val_b.inner, inner_precedence); + if inner_cmp != Ordering::Equal { + return inner_cmp; + } + } + + a.values.len().cmp(&b.values.len()) } } - - // If everything is equal, shorter sequence comes first - a.values.len().cmp(&b.values.len()) } - SortingMethod::BreadthFirst => { - // New behavior: compare all outer values, then all inner values - - // First, compare all outer values position-by-position - for (val_a, val_b) in a.values.iter().zip(b.values.iter()) { - let outer_cmp = compare_with_precedence(val_a.outer, val_b.outer, outer_precedence); - if outer_cmp != Ordering::Equal { - return outer_cmp; + SortingDirection::Backward => { + match sorting_method { + SortingMethod::DepthFirst => { + // Compare position-by-position from last to first + for (val_a, val_b) in a.values.iter().rev().zip(b.values.iter().rev()) { + let cmp = compare_values_with_precedence(val_a, val_b, outer_precedence, inner_precedence); + if cmp != Ordering::Equal { + return cmp; + } + } + a.values.len().cmp(&b.values.len()) } - } - - // If all outer values are equal, compare all inner values - for (val_a, val_b) in a.values.iter().zip(b.values.iter()) { - let inner_cmp = compare_with_precedence(val_a.inner, val_b.inner, inner_precedence); - if inner_cmp != Ordering::Equal { - return inner_cmp; + SortingMethod::BreadthFirst => { + // Compare all outer values from last to first, then all inner values + + // First, compare all outer values position-by-position (reversed) + for (val_a, val_b) in a.values.iter().rev().zip(b.values.iter().rev()) { + let outer_cmp = compare_with_precedence(val_a.outer, val_b.outer, outer_precedence); + if outer_cmp != Ordering::Equal { + return outer_cmp; + } + } + + // If all outer values are equal, compare all inner values (reversed) + for (val_a, val_b) in a.values.iter().rev().zip(b.values.iter().rev()) { + let inner_cmp = compare_with_precedence(val_a.inner, val_b.inner, inner_precedence); + if inner_cmp != Ordering::Equal { + return inner_cmp; + } + } + + a.values.len().cmp(&b.values.len()) } } - - // If everything is equal, shorter sequence comes first - a.values.len().cmp(&b.values.len()) } } } |