1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
use std::collections::HashSet;
use std::hash::Hash;
/// Returns the elements of `a` that also appear in `b`, in `a`'s order.
///
/// Duplicates in `a` are kept: only membership in `b` decides whether an element stays.
///
/// # Arguments
///
/// - `a` - The first slice.
/// - `b` - The second slice.
///
/// # Examples
///
/// ```
/// use helpers4::array::intersection;
///
/// assert_eq!(intersection(&[1, 2, 3, 2], &[2, 3, 4]), vec![2, 3, 2]);
/// assert_eq!(intersection(&[1], &[2]), Vec::<i32>::new());
/// ```
pub fn intersection<T: Clone + Eq + Hash>(a: &[T], b: &[T]) -> Vec<T> {
let wanted: HashSet<&T> = b.iter().collect();
a.iter()
.filter(|item| wanted.contains(item))
.cloned()
.collect()
}
#[cfg(test)]
#[path = "intersection.test.rs"]
mod tests;
#[cfg(test)]
#[path = "intersection.spec.rs"]
mod spec;