SHARE
TWEET

Untitled

a guest Sep 17th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use rayon::prelude::*; // 1.1.0
  2.  
  3. fn main() {
  4.     let mut vecs = vec![
  5.         vec![1, 2, 3],
  6.         vec![1, 2, 3],
  7.         vec![1, 2, 3],
  8.         vec![1, 2, 3],
  9.         vec![1, 2, 3],
  10.         vec![1, 2, 3],
  11.     ];
  12.  
  13.     let mut indices = vec![4, 3, 1];
  14.  
  15.     // Ensure all indices are unique:
  16.     indices.sort_unstable();
  17.     let len = indices.len();
  18.     indices.dedup();
  19.     assert!(indices.len() == len);
  20.  
  21.     let mut_vecs = get_multi_mut(&mut vecs, &indices);
  22.     // This causes an underflow on debug and a different error on release
  23.     //let mut_vecs = get_multi_mut(&mut vecs, &[0,0,0]);
  24.  
  25.     mut_vecs.into_par_iter().for_each(|vec| {
  26.         vec.push(4);
  27.     });
  28.  
  29.     println!("{:?}", vecs);
  30. }
  31.  
  32. fn get_multi_mut<'a, T>(mut slice: &'a mut [T], indices: &[usize]) -> Vec<&'a mut T> {
  33.     let mut offset = 0;
  34.     let mut result = Vec::with_capacity(indices.len());
  35.  
  36.     for &i in indices {
  37.         let (vec, rest) = slice.split_at_mut(i - offset + 1);
  38.         slice = rest;
  39.         offset = i + 1;
  40.         result.push(vec.last_mut().unwrap());
  41.     }
  42.  
  43.     result
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top