Skip to content

Instantly share code, notes, and snippets.

@leshow
Created August 3, 2018 19:20
Show Gist options
  • Select an option

  • Save leshow/e1bf8e7d94676961b66e75071b8b9683 to your computer and use it in GitHub Desktop.

Select an option

Save leshow/e1bf8e7d94676961b66e75071b8b9683 to your computer and use it in GitHub Desktop.
extern crate crossbeam_utils;
use std::{
collections::HashSet,
env,
error::Error,
fs,
sync::{Arc, Mutex},
};
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
let n = args.len() - 1;
let maps_list = Arc::new(Mutex::new(Vec::with_capacity(n)));
crossbeam_utils::thread::scope(|scope| {
for i in 1..(n + 1) {
let file = &args[i][..];
println!("listing: {}", file);
let maps_list = maps_list.clone();
scope.spawn(move || match get_set(file) {
Ok(s) => maps_list.lock().unwrap().push(s),
Err(e) => eprintln!("error encountered collecting files: {} \n {}", file, e),
});
}
});
let maps = maps_list.lock().unwrap();
let xor = maps.iter().fold(HashSet::new(), |acc, set| &acc ^ &set);
println!("{:?}", xor);
Ok(())
}
fn get_set<S>(file: S) -> Result<HashSet<String>, Box<dyn Error>>
where
S: AsRef<str>,
{
let mut set = HashSet::new();
let contents = fs::read_dir(file.as_ref())?;
for entry in contents {
let dir = entry?;
if let Some(name) = dir.path().file_name() {
set.insert(name.to_str().unwrap().to_string());
}
}
Ok(set)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment