Guest User

Untitled

a guest
Jun 24th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #[derive(PartialEq, Debug)]
  2. pub enum Comparison {
  3. Equal,
  4. Unequal,
  5. Sublist,
  6. Superlist,
  7. }
  8.  
  9. fn sublists<'a, T>(l: &'a [T]) -> impl 'a + Iterator {
  10. let empty: &[T] = &[]; // I can't put this directly in once as it gets seen as &[_; 0]
  11. std::iter::once(empty).chain((0..l.len()).flat_map(move |i| l.windows(i)))
  12. }
  13.  
  14. pub fn sublist<T>(l1: &[T], l2: &[T]) -> Comparison
  15. where
  16. T: PartialEq,
  17. {
  18. if l1 == l2 {
  19. Comparison::Equal
  20. } else if sublists(l2).find(l1).is_some() {
  21. Comparison::Sublist
  22. } else if sublists(l1).find(l2).is_some() {
  23. Comparison::Superlist
  24. } else {
  25. Comparison::Unequal
  26. }
  27. }
  28.  
  29. fn main() {
  30. assert_eq!(Comparison::Sublist, sublist(&[1, 2, 3], &[1, 2, 3, 4, 5]));
  31. }
Add Comment
Please, Sign In to add comment