Add second queue to make paralellization easier

This commit is contained in:
Daniel S. 2019-06-06 18:22:45 +02:00
parent 6365c58bd4
commit f00237cb65

View file

@ -5,6 +5,8 @@ extern crate serde_derive;
extern crate fnv;
extern crate humantime;
// extern crate rayon;
// use rayon::prelude::*;
use fnv::{FnvHashMap, FnvHashSet};
use humantime::format_duration;
use rstar::{PointDistance, RTree, RTreeObject, AABB};
@ -181,7 +183,7 @@ impl Router {
return self.scoopable.contains(&sys.id);
}
pub fn route(&mut self, src: &[f32; 3], dst: &[f32; 3]) -> Vec<(&System, &Point)> {
pub fn route(&mut self, src: &[f32; 3], dst: &[f32; 3]) -> Option<Vec<(&System, &Point)>> {
let start_sys = self.closest(src);
let goal_sys = self.closest(dst);
let total = self.tree.size() as f32;
@ -189,11 +191,12 @@ impl Router {
let mut seen = FnvHashSet::default();
let t_start = Instant::now();
let mut depth = 0;
let mut found = false;
let mut queue: VecDeque<(usize, &Point)> = VecDeque::new();
let mut r_queue: VecDeque<(usize, &Point)> = VecDeque::new();
let mut queue_next: VecDeque<(usize, &Point)> = VecDeque::new();
queue.push_front((0, &start_sys));
r_queue.push_front((0, &goal_sys));
seen.insert(start_sys.id);
while !queue.is_empty() {
while let Some((d, sys)) = queue.pop_front() {
if d != depth {
depth = d;
@ -208,10 +211,28 @@ impl Router {
std::io::stdout().flush().unwrap();
}
if sys.id == goal_sys.id {
found = true;
break;
}
let nbs = self
.neighbours(&sys)
.filter(|&nb| (self.valid(nb) || (nb.id == goal_sys.id)));
for nb in nbs {
if seen.insert(nb.id) {
prev.insert(nb.id, sys.id);
queue_next.push_back((d + 1, nb));
}
}
}
std::mem::swap(&mut queue, &mut queue_next);
}
println!();
if !found {
return None;
}
let points = self.preload_points();
let mut v: Vec<(&System, &Point)> = Vec::new();
let mut prev_sys_id = sys.id;
let mut prev_sys_id = goal_sys.id;
loop {
if let Some(sys) = self.systems.get(&prev_sys_id) {
v.push((sys, points[&sys.id]));
@ -226,20 +247,7 @@ impl Router {
}
}
v.reverse();
return v;
}
let nbs = self
.neighbours(&sys)
.filter(|&nb| (self.valid(nb) || (nb.id == goal_sys.id)));
for nb in nbs {
if seen.insert(nb.id) {
prev.insert(nb.id, sys.id);
queue.push_back((d + 1, nb));
}
}
}
println!("No route found!");
return Vec::new();
return Some(v);
}
}
@ -254,6 +262,10 @@ fn main() {
&[-1111.5625, -134.21875, 65269.75], // Beagle Point
// &[-7095.375, 401.25, 2396.8125], // V1357 Cygni,
); // Ix -> BP 537
let route = match route {
Some(r) => r,
None => Vec::new(),
};
println!(
"Done in {} ({} Jumps)!\n",
format_duration(t_route.elapsed()),