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

Function intersection_by

rust/stringzilla.rs:2339–2379  ·  view source on GitHub ↗

Intersects two sequences (inner join) using their elements corresponding byte-slice views. The caller must provide a closure that maps an index to the byte slice representation of the corresponding element in the first and second sequences. # Example ```rust use stringzilla::stringzilla as sz; #[derive(Debug)] struct Person { name: &'static str, age: u32 } let people1 = [ Person { name: "Charl

(
    mapper1: F,
    mapper2: G,
    seed: u64,
    positions1: &mut [SortedIdx],
    positions2: &mut [SortedIdx],
)

Source from the content-addressed store, hash-verified

2337/// assert!(n == 3); // "Alice", "Bob", and "Charlie" are common.
2338/// ```
2339pub fn intersection_by<F, G, A, B>(
2340 mapper1: F,
2341 mapper2: G,
2342 seed: u64,
2343 positions1: &mut [SortedIdx],
2344 positions2: &mut [SortedIdx],
2345) -> Result<usize, Status>
2346where
2347 F: Fn(usize) -> A,
2348 A: AsRef<[u8]>,
2349 G: Fn(usize) -> B,
2350 B: AsRef<[u8]>,
2351{
2352 if positions1.len() != positions2.len() {
2353 return Err(Status::BadAlloc);
2354 }
2355
2356 // Adapter closure: given an index, call the provided mapper and then transmute the
2357 // resulting slice to have a `'static` lifetime. This transmute is safe as long as
2358 // the FFI call is synchronous and the returned slices are only used during the call.
2359 let adapter1 = move |i: usize| -> &'static [u8] {
2360 let binding = mapper1(i);
2361 let slice = binding.as_ref();
2362 unsafe { core::mem::transmute(slice) }
2363 };
2364 let adapter2 = move |i: usize| -> &'static [u8] {
2365 let binding = mapper2(i);
2366 let slice = binding.as_ref();
2367 unsafe { core::mem::transmute(slice) }
2368 };
2369
2370 _intersection_by_impl(
2371 adapter1,
2372 adapter2,
2373 seed,
2374 positions1,
2375 positions2,
2376 positions1.len(),
2377 positions2.len(),
2378 )
2379}
2380
2381fn _intersection_by_impl<FAdapter, GAdapter>(
2382 adapter1: FAdapter,

Callers 2

intersection_by_customFunction · 0.85
intersection_size_checksFunction · 0.85

Calls 2

_intersection_by_implFunction · 0.85
lenMethod · 0.80

Tested by 1

intersection_by_customFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…