#[cfg(feature = "parallel")]
use rayon::prelude::*;
pub(crate) fn map<T, F>(n: usize, f: F) -> Vec<T>
where
T: Send,
F: Fn(usize) -> T + Sync + Send,
{
#[cfg(feature = "parallel")]
{
(0..n).into_par_iter().map(f).collect()
}
#[cfg(not(feature = "parallel"))]
{
(0..n).map(f).collect()
}
}
pub(crate) fn map_init<S, T, I, F>(n: usize, init: I, f: F) -> Vec<T>
where
T: Send,
I: Fn() -> S + Sync + Send,
F: Fn(&mut S, usize) -> T + Sync + Send,
{
#[cfg(feature = "parallel")]
{
(0..n).into_par_iter().map_init(init, f).collect()
}
#[cfg(not(feature = "parallel"))]
{
let mut scratch = init();
(0..n).map(|i| f(&mut scratch, i)).collect()
}
}
pub(crate) fn chunk_len(n: usize) -> usize {
(n / 256).clamp(1, 4096)
}
pub(crate) fn chunk_range(n: usize, chunk: usize, c: usize) -> std::ops::Range<usize> {
c * chunk..n.min((c + 1) * chunk)
}
pub(crate) fn sort_unstable<T: Ord + Send>(v: &mut [T]) {
#[cfg(feature = "parallel")]
v.par_sort_unstable();
#[cfg(not(feature = "parallel"))]
v.sort_unstable();
}
#[cfg(test)]
mod tests {
#[test]
fn order_is_preserved() {
let v = super::map(10_000, |i| i * 2);
assert!(v.iter().enumerate().all(|(i, &x)| x == 2 * i));
let w = super::map_init(1000, Vec::<usize>::new, |s, i| {
s.push(i);
i
});
assert_eq!(w, (0..1000).collect::<Vec<_>>());
let mut u = vec![5, 3, 9, 1];
super::sort_unstable(&mut u);
assert_eq!(u, [1, 3, 5, 9]);
}
}