summaryrefslogtreecommitdiff
path: root/morphology/src/sequence.rs
diff options
context:
space:
mode:
Diffstat (limited to 'morphology/src/sequence.rs')
-rw-r--r--morphology/src/sequence.rs40
1 files changed, 39 insertions, 1 deletions
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<PrecedenceOrder>,
sorting_method: Option<SortingMethod>,
sorting_direction: Option<SortingDirection>,
+ length_sorting: Option<LengthSorting>,
}
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 {