MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / intersection_by_custom

Function intersection_by_custom

rust/stringzilla.rs:3771–3822  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3769
3770 #[test]
3771 fn intersection_by_custom() {
3772 // Define a custom type.
3773 #[derive(Debug)]
3774 #[allow(dead_code)]
3775 struct Person {
3776 name: &'static str,
3777 age: u32, //? We won't use this field for intersection
3778 }
3779
3780 let group1 = [
3781 Person { name: "Alice", age: 25 },
3782 Person { name: "Bob", age: 30 },
3783 Person {
3784 name: "Charlie",
3785 age: 35,
3786 },
3787 ];
3788 let group2 = [
3789 Person { name: "David", age: 40 },
3790 Person {
3791 name: "Charlie",
3792 age: 50,
3793 },
3794 Person { name: "Alice", age: 60 },
3795 ];
3796 let mut out1 = [0; 3];
3797 let mut out2 = [0; 3];
3798
3799 let n = sz::intersection_by(
3800 |i: sz::SortedIdx| group1[i].name.as_bytes(),
3801 |j: sz::SortedIdx| group2[j].name.as_bytes(),
3802 0,
3803 &mut out1,
3804 &mut out2,
3805 )
3806 .expect("intersection_by failed");
3807 assert!(n <= group1.len().min(group2.len()));
3808
3809 // Use the indices for `group1` to get common names.
3810 let common_from_api: HashSet<_> = out1[..n].iter().map(|&i| group1[i].name).collect();
3811
3812 // Compute expected common names using a `HashSet`.
3813 let expected: HashSet<_> = group1
3814 .iter()
3815 .map(|p| p.name)
3816 .collect::<HashSet<_>>()
3817 .intersection(&group2.iter().map(|p| p.name).collect())
3818 .cloned()
3819 .collect();
3820
3821 assert_eq!(common_from_api, expected);
3822 }
3823
3824 #[test]
3825 #[should_panic]

Callers

nothing calls this directly

Calls 3

intersection_byFunction · 0.85
as_bytesMethod · 0.80
iterMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…