SHARE
TWEET

Untitled

a guest Aug 25th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use rayon::prelude::*;
  2. use std::collections::HashSet;
  3.  
  4. fn par_swap_remove<T>(v: &mut Vec<T>, indices: &Vec<usize>)
  5. where
  6.     T: Send + Sync + std::fmt::Debug + Clone,
  7. {
  8.     struct SafePtr<T: ?Sized>(*mut T);
  9.     unsafe impl<T: ?Sized> Send for SafePtr<T> {}
  10.     unsafe impl<T: ?Sized> Sync for SafePtr<T> {}
  11.  
  12.     if indices.len() == 0 {
  13.         return;
  14.     }
  15.  
  16.     // Exercise for the reader to validate the incomming parallel iterator
  17.  
  18.     let ptr = SafePtr(v.as_mut_ptr());
  19.  
  20.     println!("{:?} : Input", v);
  21.     let mut i = 0;
  22.     for i2 in (0..indices.len()).rev() {
  23.         println!("Removing {}", indices[i2]);
  24.         v.swap_remove(indices[i2]);
  25.         println!("{:?} : After Remove", v);
  26.         i = i2;
  27.         if indices[i2] < v.len() - indices.len() {
  28.             break;
  29.         }
  30.     }
  31.     println!("==> Done with seq.");
  32.  
  33.     {
  34.         let v2 = &v;
  35.         indices[..i]
  36.             .iter()
  37.             .enumerate()
  38.             .for_each(move |(i2, idx)| unsafe {
  39.                 let SafePtr(ptr) = ptr;
  40.  
  41.                 let hole: *mut _ = ptr.add(*idx);
  42.                 println!("i  = {}", i);
  43.                 println!("i2 = {}", i2);
  44.                 println!("Swap {} <-> {}", idx, v2.len() - i2 - 1);
  45.                 let last = ptr.add(v2.len() - i2 - 1);
  46.                 std::ptr::swap(hole, last);
  47.                 println!("{:?} : After Remove", v2);
  48.             });
  49.     }
  50.     println!(
  51.         "len - indices.len() = {} - {} = {}",
  52.         v.len(),
  53.         indices.len(),
  54.         v.len() - indices.len()
  55.     );
  56.     v.truncate(v.len() - indices.len() + indices.len() - i);
  57. }
  58.  
  59. fn assert_same_set(a: &Vec<usize>, b: &Vec<usize>) {
  60.     assert_eq!(
  61.         a.iter().collect::<HashSet<_>>(),
  62.         b.iter().collect::<HashSet<_>>(),
  63.         "Should have same elements"
  64.     );
  65. }
  66.  
  67. fn main() {
  68.     let mut v = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
  69.     let mut v2 = v.clone();
  70.     let mut indices: Vec<usize> = vec![0, 1, 2, 4, 10, 9];
  71.     indices.sort_by(|a, b| a.cmp(b));
  72.     par_swap_remove(&mut v, &indices);
  73.  
  74.     for i in indices.iter().rev() {
  75.         v2.swap_remove(*i);
  76.     }
  77.  
  78.     assert_same_set(&v, &v2);
  79.     println!("{:?} : Final", v);
  80.     println!("{:?} : Final v2", v2);
  81. }
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