Rework sublist to use windows

This commit is contained in:
Avery Winters 2023-11-01 15:22:28 -05:00
parent 2e255e2c04
commit a5cc68b6b3
Signed by: avery
SSH key fingerprint: SHA256:eesvLB5MMqHLZrAMFt6kEhqJWnASMLcET6Sgmw0FqZI

View file

@ -8,13 +8,7 @@ pub enum Comparison {
pub fn sublist<T: PartialEq>(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