From 3b7646c04ea3b37cebb3a92d5aa2db3d53129551 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Mon, 21 Feb 2022 18:27:46 -0500 Subject: [PATCH] Cleanup and add comments --- src/sort.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/sort.rs b/src/sort.rs index ebef49f..e8ae900 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -35,6 +35,7 @@ struct SortableSelection<'a> { subselections: Vec<&'a str>, } +/// Gets a Vec of sortable selections with a given list of subselections and descriptions fn get_sortable_selections_subselections<'a, 'b, 'tmp, S: AsRef + std::fmt::Debug + 'a>( sort_options: &'b SortOptions, selections: &'a [S], @@ -65,6 +66,8 @@ fn get_sortable_selections_subselections<'a, 'b, 'tmp, S: AsRef + std::fmt: subselections.sort_by(|(_, ssd_a), (_, ssd_b)| ssd_a.cmp(ssd_b)); + // For each selection, check if they contain any subselections + // If so, add them to the subselections vector // TODO: This is O(n^2), but can be made more efficient since subselections is sorted for (s, s_desc) in &mut sortable_selections { for i in &subselections { @@ -75,19 +78,22 @@ fn get_sortable_selections_subselections<'a, 'b, 'tmp, S: AsRef + std::fmt: } sortable_selections.sort_by(|(a, _), (b, _)| { + // First, check if there are any subselection comparisons to be made + // If one has more subselections than the other, stop comparing for (a_subsel, b_subsel) in a.subselections.iter().zip(b.subselections.iter()) { match a_subsel.cmp(b_subsel) { + // These subselecitons are equal, so we can't do anything Ordering::Equal => continue, + // We found a difference, so return the comparison o => return o, } } + // No subselections mismatched, so compare the (possibly trimmed) content a.content_comparison.cmp(b.content_comparison) }); Ok(sortable_selections.into_iter().map(|(s, _)| s).collect()) - - // todo!(); } fn to_sortable_selection<'a, 'b>(