64 lines
2.3 KiB
Rust
64 lines
2.3 KiB
Rust
// cargo-deps: crossbeam-channel="0.5.1"
|
|
extern crate crossbeam_channel;
|
|
use std::io::{BufReader,BufRead, BufWriter,Write};
|
|
use std::fs::File;
|
|
use std::collections::HashMap;
|
|
use std::collections::HashSet;
|
|
use crossbeam_channel::unbounded;
|
|
use crossbeam_channel::Receiver;
|
|
use std::thread;
|
|
fn process(rx: Receiver<String>) -> (HashMap<usize,usize>,HashMap<usize,usize>) {
|
|
let mut hm_min: HashMap<usize,usize> = HashMap::new();
|
|
let mut hm_max: HashMap<usize,usize> = HashMap::new();
|
|
while let Ok(line) = rx.recv() {
|
|
let line: Vec<usize> = line.split(",").map(|s| s.parse::<usize>().unwrap()).collect();
|
|
let id=line[0];
|
|
let mut depth=line[1];
|
|
hm_min.entry(id).and_modify(|e| {
|
|
*e=*e.min(&mut depth);
|
|
}).or_insert(depth);
|
|
hm_max.entry(id).and_modify(|e| {
|
|
*e=*e.max(&mut depth);
|
|
}).or_insert(depth);
|
|
}
|
|
(hm_min,hm_max)
|
|
}
|
|
fn main() {
|
|
let (tx,rx) = unbounded();
|
|
let mut threads: Vec<_> = (0..8).map(|_| {
|
|
let rx=rx.clone();
|
|
thread::spawn(|| {
|
|
process(rx)
|
|
})
|
|
}).collect();
|
|
let fh = BufReader::new(File::open(std::env::args().nth(1).unwrap()).unwrap());
|
|
fh.lines().flatten().for_each(|line| {
|
|
tx.send(line).unwrap();
|
|
});
|
|
drop(tx);
|
|
let mut hm_min: HashMap<usize,usize> = HashMap::new();
|
|
let mut hm_max: HashMap<usize,usize> = HashMap::new();
|
|
for thread in threads.drain(..) {
|
|
let (min,max)=thread.join().unwrap();
|
|
println!("Thread: {:?}",(min.len(),max.len()));
|
|
for (id,depth) in min {
|
|
hm_min.entry(id).and_modify(|e| {
|
|
*e=(*e).min(depth);
|
|
}).or_insert(depth);
|
|
}
|
|
for (id,depth) in max {
|
|
hm_max.entry(id).and_modify(|e| {
|
|
*e=(*e).max(depth);
|
|
}).or_insert(depth);
|
|
}
|
|
}
|
|
println!("Final: {:?}",(hm_min.len(),hm_max.len()));
|
|
let mut fh_max = BufWriter::new(File::create("route_log_max.txt").unwrap());
|
|
for (id,depth) in hm_max {
|
|
write!(fh_max,"{},{}\n",id,depth).unwrap();
|
|
}
|
|
let mut fh_min = BufWriter::new(File::create("route_log_min.txt").unwrap());
|
|
for (id,depth) in hm_min {
|
|
write!(fh_min,"{},{}\n",id,depth).unwrap();
|
|
}
|
|
} |