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

Function argsort_permutation_by

rust/stringzilla.rs:2213–2228  ·  view source on GitHub ↗

Sorts a sequence of items by comparing their corresponding byte-slice representations. The size of the permutation is inferred from the length of the `order` slice. # Example ```rust use stringzilla::stringzilla as sz; #[derive(Debug)] struct Person { name: &'static str, age: u32 } let people = [ Person { name: "Charlie", age: 20 }, Person { name: "Alice", age: 25 }, Person { name: "Bob", age:

(mapper: F, order: &mut [SortedIdx])

Source from the content-addressed store, hash-verified

2211/// assert_eq!(&order, &[1, 2, 0]); // "Alice", "Bob", "Charlie"
2212/// ```
2213pub fn argsort_permutation_by<F, A>(mapper: F, order: &mut [SortedIdx]) -> Result<(), Status>
2214where
2215 F: Fn(usize) -> A,
2216 A: AsRef<[u8]>,
2217{
2218 // Adapter closure: given an index, call the provided mapper and then transmute the
2219 // resulting slice to have a `'static` lifetime. This transmute is safe as long as
2220 // the FFI call is synchronous and the returned slices are only used during the call.
2221 let adapter = move |i: usize| -> &'static [u8] {
2222 let binding = mapper(i);
2223 let slice = binding.as_ref();
2224 unsafe { core::mem::transmute(slice) }
2225 };
2226
2227 _argsort_permutation_impl(adapter, order)
2228}
2229
2230/// Helper that takes an adapter (with a concrete type) and performs the FFI call.
2231fn _argsort_permutation_impl<FAdapter>(adapter: FAdapter, order: &mut [SortedIdx]) -> Result<(), Status>

Callers 2

argsort_permutationFunction · 0.85

Calls 1

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…