From 64f95b2b8aa0fcdce23a226a159588ee2c0fd52e Mon Sep 17 00:00:00 2001 From: Chris West Date: Sat, 18 May 2024 00:19:27 -0600 Subject: [PATCH 1/3] add method to Atom that preserves the index of the passed value in the return value --- matcher/src/pattern.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/matcher/src/pattern.rs b/matcher/src/pattern.rs index 4801017..11ec2de 100644 --- a/matcher/src/pattern.rs +++ b/matcher/src/pattern.rs @@ -395,6 +395,28 @@ impl Atom { items.sort_by_key(|(_, score)| Reverse(*score)); items } + + /// Same as `match_list` expect that it returns the index in addition to the match tuple + pub fn match_list_with_index>( + &self, + items: impl IntoIterator, + matcher: &mut Matcher, + ) -> Vec<(T, u16, usize)> { + if self.needle.is_empty() { + return items.into_iter().map(|item| (item, 0, 0)).collect(); + } + let mut buf = Vec::new(); + let mut items: Vec<_> = items + .into_iter() + .enumerate() + .filter_map(|(index, item)| { + self.score(Utf32Str::new(item.as_ref(), &mut buf), matcher) + .map(|score| (item, score, index)) + }) + .collect(); + items.sort_by_key(|(_, score, _)| Reverse(*score)); + items + } } fn pattern_atoms(pattern: &str) -> impl Iterator + '_ { From 93d182afe627fba7af5196eaceac19580c7cc1af Mon Sep 17 00:00:00 2001 From: Chris West Date: Sat, 18 May 2024 00:26:39 -0600 Subject: [PATCH 2/3] added match_list_with_index to other location to maintain parity between Atom and Pattern --- matcher/src/pattern.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/matcher/src/pattern.rs b/matcher/src/pattern.rs index 11ec2de..16b6ad4 100644 --- a/matcher/src/pattern.rs +++ b/matcher/src/pattern.rs @@ -499,6 +499,28 @@ impl Pattern { items.sort_by_key(|(_, score)| Reverse(*score)); items } + /// + /// Same as `match_list` expect that it returns the index in addition to the match tuple + pub fn match_list_with_index>( + &self, + items: impl IntoIterator, + matcher: &mut Matcher, + ) -> Vec<(T, u32, usize)> { + if self.atoms.is_empty() { + return items.into_iter().map(|item| (item, 0, 0)).collect(); + } + let mut buf = Vec::new(); + let mut items: Vec<_> = items + .into_iter() + .enumerate() + .filter_map(|(index, item)| { + self.score(Utf32Str::new(item.as_ref(), &mut buf), matcher) + .map(|score| (item, score, index)) + }) + .collect(); + items.sort_by_key(|(_, score, _)| Reverse(*score)); + items + } /// Matches this pattern against `haystack` (using the allocation and configuration /// from `matcher`) and calculates a ranking score. See the [`Matcher`]. From 3c03271bfb15cafd1fc9e6bf97019364d0502631 Mon Sep 17 00:00:00 2001 From: Chris West Date: Sat, 18 May 2024 00:38:36 -0600 Subject: [PATCH 3/3] update comments --- matcher/src/pattern.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/matcher/src/pattern.rs b/matcher/src/pattern.rs index 16b6ad4..1f3ffe6 100644 --- a/matcher/src/pattern.rs +++ b/matcher/src/pattern.rs @@ -396,7 +396,8 @@ impl Atom { items } - /// Same as `match_list` expect that it returns the index in addition to the match tuple + /// Same as `match_list` expect that it returns the index appended to the tuple + /// Return tuple is (Match, score, index) pub fn match_list_with_index>( &self, items: impl IntoIterator, @@ -499,8 +500,9 @@ impl Pattern { items.sort_by_key(|(_, score)| Reverse(*score)); items } - /// - /// Same as `match_list` expect that it returns the index in addition to the match tuple + + /// Same as `match_list` expect that it returns the index appended to the tuple + /// Return tuple is (Match, score, index) pub fn match_list_with_index>( &self, items: impl IntoIterator,