From a5cc68b6b390dfea3ed7bf0765ae8202124822a5 Mon Sep 17 00:00:00 2001 From: Avery Winters Date: Wed, 1 Nov 2023 15:22:28 -0500 Subject: [PATCH] Rework sublist to use windows --- rust/sublist/src/lib.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/rust/sublist/src/lib.rs b/rust/sublist/src/lib.rs index ce403af..977d16e 100644 --- a/rust/sublist/src/lib.rs +++ b/rust/sublist/src/lib.rs @@ -8,13 +8,7 @@ pub enum Comparison { pub fn sublist(first: &[T], second: &[T]) -> Comparison { let is_sublist = |first: &[_], second: &[_]| { - second - .iter() - .enumerate() - .map(|(i, _)| &second[i..]) - .take_while(|suffix| first.len() <= suffix.len()) - .map(|suffix| &suffix[..first.len()]) - .any(|sublist| first == sublist) + first.is_empty() || second.windows(first.len()).any(|window| first == window) }; if first == second { Comparison::Equal