Loading rust/src/common.rs +2 −4 Original line number Diff line number Diff line Loading @@ -28,7 +28,6 @@ impl SystemSerde { } } #[derive(Debug, Clone, Deserialize, Serialize)] pub struct System { pub id: u32, Loading @@ -42,13 +41,12 @@ pub struct System { impl Ord for System { fn cmp(&self, other: &Self) -> Ordering { return self.id.cmp(&other.id); self.id.cmp(&other.id) } } impl PartialOrd for System { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { return Some(self.cmp(other)); Some(self.cmp(other)) } } rust/src/lib.rs +42 −40 Original line number Diff line number Diff line Loading @@ -2,26 +2,28 @@ extern crate strsim; mod common; mod preprocess; mod route; use std::collections::HashMap; use common::{System, SystemSerde}; use pyo3::exceptions::*; use pyo3::prelude::*; use pyo3::types::{PyDict, PyList}; use std::collections::HashMap; use std::path::PathBuf; use common::{System,SystemSerde}; fn find_matches(path:&PathBuf,names:Vec<String>) -> Result<HashMap<String,(f64,Option<System>)>,String> { fn find_matches( path: &PathBuf, names: Vec<String>, ) -> Result<HashMap<String, (f64, Option<System>)>, String> { let mut best: HashMap<String, (f64, Option<System>)> = HashMap::new(); for name in &names { best.insert(name.to_string(), (0.0, None)); }; } let mut reader = match csv::ReaderBuilder::new().from_path(path) { Ok(rdr) => rdr, Err(e) => { return Err(format!("Error opening {}: {}", path.to_str().unwrap(), e).to_string()); } }; let systems=reader .deserialize::<SystemSerde>(); let systems = reader.deserialize::<SystemSerde>(); for sys in systems { let sys = sys.unwrap(); for name in &names { Loading @@ -33,7 +35,7 @@ fn find_matches(path:&PathBuf,names:Vec<String>) -> Result<HashMap<String,(f64,O }); } } return Ok(best); Ok(best) } #[pymodule] Loading Loading @@ -67,7 +69,7 @@ pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> { &PathBuf::from(infile_bodies), &PathBuf::from(infile_systems), &PathBuf::from(outfile), Box::new(callback_wrapped), &callback_wrapped, ) .unwrap(); Ok(state.to_object(py)) Loading Loading @@ -96,10 +98,10 @@ pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> { ret.set_item(key, (diff, ret_dict).to_object(py))?; } } return Ok(ret.to_object(py)); }, Ok(ret.to_object(py)) } Err(e) => { return Err(PyErr::new::<ValueError, _>(e)); Err(PyErr::new::<ValueError, _>(e)) } } } Loading Loading @@ -165,7 +167,7 @@ pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> { keep_first, keep_last, primary, radius_mult radius_mult, }; let none = ().to_object(py); match route(opts) { Loading rust/src/preprocess.rs +73 −54 Original line number Diff line number Diff line use crate::common::SystemSerde; use fnv::FnvHashMap; use pyo3::prelude::*; use serde::Deserialize; use serde_json::Result; use std::fs::File; use std::io::Seek; use std::io::{BufRead, BufReader, BufWriter, SeekFrom}; use std::path::PathBuf; use std::time::Instant; use std::str; use pyo3::prelude::*; use std::time::Instant; #[derive(Debug, Deserialize)] #[allow(non_snake_case)] Loading Loading @@ -48,7 +48,6 @@ pub struct PreprocessState { pub count: usize, } fn get_mult(star_type: &str) -> f32 { if star_type.contains("White Dwarf") { return 1.5; Loading @@ -59,7 +58,11 @@ fn get_mult(star_type: &str) -> f32 { 1.0 } fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> { fn process( path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (), callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> std::io::Result<()> { let mut buffer = String::new(); let fh = File::open(path)?; let total_size = fh.metadata()?.len(); Loading @@ -70,7 +73,7 @@ fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: total: total_size, done: 0, count: 0, message: format!("Processing {} ...", path.to_str().unwrap()) message: format!("Processing {} ...", path.to_str().unwrap()), }; println!("Loading {} ...", path.to_str().unwrap()); while let Ok(n) = reader.read_line(&mut buffer) { Loading @@ -93,16 +96,23 @@ fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: Ok(()) } fn process_systems(path: &PathBuf,callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>> ) -> FnvHashMap<i32, System> { fn process_systems( path: &PathBuf, callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> FnvHashMap<i32, System> { let mut ret = FnvHashMap::default(); process(path, &mut |line| { process( path, &mut |line| { let sys_res: Result<System> = serde_json::from_str(&line); if let Ok(sys) = sys_res { ret.insert(sys.id, sys); } else { eprintln!("\nError parsing: {}\n\t{:?}\n", line, sys_res.unwrap_err()); } },callback) }, callback, ) .unwrap(); ret } Loading @@ -125,7 +135,7 @@ fn process_bodies( path: &PathBuf, out_path: &PathBuf, systems: &mut FnvHashMap<i32, System>, callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>, callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> std::io::Result<()> { println!( "Processing {} into {} ...", Loading @@ -134,7 +144,9 @@ fn process_bodies( ); let mut n: u32 = 0; let mut wtr = csv::Writer::from_path(out_path)?; process(path, &mut |line| { process( path, &mut |line| { if !line.contains("Star") { return; } Loading Loading @@ -164,14 +176,21 @@ fn process_bodies( } else { eprintln!("\nError parsing: {}\n\t{:?}\n", line, body_res.unwrap_err()); } },callback) }, callback, ) .unwrap(); println!("Total Systems: {}", n); systems.clear(); Ok(()) } pub fn preprocess_files(bodies: &PathBuf,systems:&PathBuf,out_path:&PathBuf,callback: Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> { pub fn preprocess_files( bodies: &PathBuf, systems: &PathBuf, out_path: &PathBuf, callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> std::io::Result<()> { if !out_path.exists() { let mut systems = process_systems(systems, &callback); process_bodies(bodies, out_path, &mut systems, &callback)?; Loading rust/src/route.rs +14 −15 Original line number Diff line number Diff line Loading @@ -104,6 +104,7 @@ impl System { pub fn dist2(&self, p: &[f32; 3]) -> f32 { dist2(&self.pos, p) } pub fn distp(&self, p: &System) -> f32 { dist(&self.pos, &p.pos) } Loading Loading @@ -320,12 +321,12 @@ impl Router { return scoopable; } let df = src.distp(dst); return (sys.distp(src) + sys.distp(dst)) < (df * (1.0 + self.radius_mult)); (sys.distp(src) + sys.distp(dst)) < (df * (1.0 + self.radius_mult)) } pub fn best_multiroute( &self, waypoints: &Vec<System>, waypoints: &[System], range: f32, keep: (bool, bool), mode: Mode, Loading Loading @@ -367,14 +368,14 @@ impl Router { pub fn multiroute( &self, waypoints: &Vec<System>, waypoints: &[System], range: f32, mode: Mode, factor: f32, ) -> Result<Vec<System>, String> { let mut route: Vec<System> = Vec::new(); for pair in waypoints.windows(2) { match pair.clone() { match pair { [src, dst] => { let block = match mode { Mode::BFS => self.route_bfs(&src, &dst, range)?, Loading Loading @@ -423,13 +424,13 @@ impl Router { for ent in systems { match ent { SysEntry::ID(i) => match sys_by_id.get(i) { Some(sys) => ret.push(sys.clone().clone()), Some(sys) => ret.push((*sys).clone()), None => { return Err(format!("System: {:?} not found", ent)); } }, SysEntry::Name(n) => match sys_by_name.get(n) { Some(sys) => ret.push(sys.clone().clone()), Some(sys) => ret.push((*sys).clone()), None => { return Err(format!("System: {:?} not found", ent)); } Loading Loading @@ -912,9 +913,7 @@ pub fn route(opts: RouteOpts) -> Result<Option<Vec<System>>, String> { }; let systems: Vec<System> = router .resolve_systems(&opts.systems)? .iter() .map(|sys| sys.clone()) .collect(); .to_vec(); if opts.precompute { for sys in systems { router.route_tree = None; Loading Loading
rust/src/common.rs +2 −4 Original line number Diff line number Diff line Loading @@ -28,7 +28,6 @@ impl SystemSerde { } } #[derive(Debug, Clone, Deserialize, Serialize)] pub struct System { pub id: u32, Loading @@ -42,13 +41,12 @@ pub struct System { impl Ord for System { fn cmp(&self, other: &Self) -> Ordering { return self.id.cmp(&other.id); self.id.cmp(&other.id) } } impl PartialOrd for System { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { return Some(self.cmp(other)); Some(self.cmp(other)) } }
rust/src/lib.rs +42 −40 Original line number Diff line number Diff line Loading @@ -2,26 +2,28 @@ extern crate strsim; mod common; mod preprocess; mod route; use std::collections::HashMap; use common::{System, SystemSerde}; use pyo3::exceptions::*; use pyo3::prelude::*; use pyo3::types::{PyDict, PyList}; use std::collections::HashMap; use std::path::PathBuf; use common::{System,SystemSerde}; fn find_matches(path:&PathBuf,names:Vec<String>) -> Result<HashMap<String,(f64,Option<System>)>,String> { fn find_matches( path: &PathBuf, names: Vec<String>, ) -> Result<HashMap<String, (f64, Option<System>)>, String> { let mut best: HashMap<String, (f64, Option<System>)> = HashMap::new(); for name in &names { best.insert(name.to_string(), (0.0, None)); }; } let mut reader = match csv::ReaderBuilder::new().from_path(path) { Ok(rdr) => rdr, Err(e) => { return Err(format!("Error opening {}: {}", path.to_str().unwrap(), e).to_string()); } }; let systems=reader .deserialize::<SystemSerde>(); let systems = reader.deserialize::<SystemSerde>(); for sys in systems { let sys = sys.unwrap(); for name in &names { Loading @@ -33,7 +35,7 @@ fn find_matches(path:&PathBuf,names:Vec<String>) -> Result<HashMap<String,(f64,O }); } } return Ok(best); Ok(best) } #[pymodule] Loading Loading @@ -67,7 +69,7 @@ pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> { &PathBuf::from(infile_bodies), &PathBuf::from(infile_systems), &PathBuf::from(outfile), Box::new(callback_wrapped), &callback_wrapped, ) .unwrap(); Ok(state.to_object(py)) Loading Loading @@ -96,10 +98,10 @@ pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> { ret.set_item(key, (diff, ret_dict).to_object(py))?; } } return Ok(ret.to_object(py)); }, Ok(ret.to_object(py)) } Err(e) => { return Err(PyErr::new::<ValueError, _>(e)); Err(PyErr::new::<ValueError, _>(e)) } } } Loading Loading @@ -165,7 +167,7 @@ pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> { keep_first, keep_last, primary, radius_mult radius_mult, }; let none = ().to_object(py); match route(opts) { Loading
rust/src/preprocess.rs +73 −54 Original line number Diff line number Diff line use crate::common::SystemSerde; use fnv::FnvHashMap; use pyo3::prelude::*; use serde::Deserialize; use serde_json::Result; use std::fs::File; use std::io::Seek; use std::io::{BufRead, BufReader, BufWriter, SeekFrom}; use std::path::PathBuf; use std::time::Instant; use std::str; use pyo3::prelude::*; use std::time::Instant; #[derive(Debug, Deserialize)] #[allow(non_snake_case)] Loading Loading @@ -48,7 +48,6 @@ pub struct PreprocessState { pub count: usize, } fn get_mult(star_type: &str) -> f32 { if star_type.contains("White Dwarf") { return 1.5; Loading @@ -59,7 +58,11 @@ fn get_mult(star_type: &str) -> f32 { 1.0 } fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> { fn process( path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (), callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> std::io::Result<()> { let mut buffer = String::new(); let fh = File::open(path)?; let total_size = fh.metadata()?.len(); Loading @@ -70,7 +73,7 @@ fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: total: total_size, done: 0, count: 0, message: format!("Processing {} ...", path.to_str().unwrap()) message: format!("Processing {} ...", path.to_str().unwrap()), }; println!("Loading {} ...", path.to_str().unwrap()); while let Ok(n) = reader.read_line(&mut buffer) { Loading @@ -93,16 +96,23 @@ fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: Ok(()) } fn process_systems(path: &PathBuf,callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>> ) -> FnvHashMap<i32, System> { fn process_systems( path: &PathBuf, callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> FnvHashMap<i32, System> { let mut ret = FnvHashMap::default(); process(path, &mut |line| { process( path, &mut |line| { let sys_res: Result<System> = serde_json::from_str(&line); if let Ok(sys) = sys_res { ret.insert(sys.id, sys); } else { eprintln!("\nError parsing: {}\n\t{:?}\n", line, sys_res.unwrap_err()); } },callback) }, callback, ) .unwrap(); ret } Loading @@ -125,7 +135,7 @@ fn process_bodies( path: &PathBuf, out_path: &PathBuf, systems: &mut FnvHashMap<i32, System>, callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>, callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> std::io::Result<()> { println!( "Processing {} into {} ...", Loading @@ -134,7 +144,9 @@ fn process_bodies( ); let mut n: u32 = 0; let mut wtr = csv::Writer::from_path(out_path)?; process(path, &mut |line| { process( path, &mut |line| { if !line.contains("Star") { return; } Loading Loading @@ -164,14 +176,21 @@ fn process_bodies( } else { eprintln!("\nError parsing: {}\n\t{:?}\n", line, body_res.unwrap_err()); } },callback) }, callback, ) .unwrap(); println!("Total Systems: {}", n); systems.clear(); Ok(()) } pub fn preprocess_files(bodies: &PathBuf,systems:&PathBuf,out_path:&PathBuf,callback: Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> { pub fn preprocess_files( bodies: &PathBuf, systems: &PathBuf, out_path: &PathBuf, callback: &dyn Fn(&PreprocessState) -> PyResult<PyObject>, ) -> std::io::Result<()> { if !out_path.exists() { let mut systems = process_systems(systems, &callback); process_bodies(bodies, out_path, &mut systems, &callback)?; Loading
rust/src/route.rs +14 −15 Original line number Diff line number Diff line Loading @@ -104,6 +104,7 @@ impl System { pub fn dist2(&self, p: &[f32; 3]) -> f32 { dist2(&self.pos, p) } pub fn distp(&self, p: &System) -> f32 { dist(&self.pos, &p.pos) } Loading Loading @@ -320,12 +321,12 @@ impl Router { return scoopable; } let df = src.distp(dst); return (sys.distp(src) + sys.distp(dst)) < (df * (1.0 + self.radius_mult)); (sys.distp(src) + sys.distp(dst)) < (df * (1.0 + self.radius_mult)) } pub fn best_multiroute( &self, waypoints: &Vec<System>, waypoints: &[System], range: f32, keep: (bool, bool), mode: Mode, Loading Loading @@ -367,14 +368,14 @@ impl Router { pub fn multiroute( &self, waypoints: &Vec<System>, waypoints: &[System], range: f32, mode: Mode, factor: f32, ) -> Result<Vec<System>, String> { let mut route: Vec<System> = Vec::new(); for pair in waypoints.windows(2) { match pair.clone() { match pair { [src, dst] => { let block = match mode { Mode::BFS => self.route_bfs(&src, &dst, range)?, Loading Loading @@ -423,13 +424,13 @@ impl Router { for ent in systems { match ent { SysEntry::ID(i) => match sys_by_id.get(i) { Some(sys) => ret.push(sys.clone().clone()), Some(sys) => ret.push((*sys).clone()), None => { return Err(format!("System: {:?} not found", ent)); } }, SysEntry::Name(n) => match sys_by_name.get(n) { Some(sys) => ret.push(sys.clone().clone()), Some(sys) => ret.push((*sys).clone()), None => { return Err(format!("System: {:?} not found", ent)); } Loading Loading @@ -912,9 +913,7 @@ pub fn route(opts: RouteOpts) -> Result<Option<Vec<System>>, String> { }; let systems: Vec<System> = router .resolve_systems(&opts.systems)? .iter() .map(|sys| sys.clone()) .collect(); .to_vec(); if opts.precompute { for sys in systems { router.route_tree = None; Loading