cleaned up the code

This commit is contained in:
Johannes Erwerle 2022-09-16 17:14:28 +02:00
parent ea0e22506d
commit 275c2d933e
3 changed files with 57 additions and 35 deletions

View file

@ -1,18 +1,23 @@
use crate::alt::LandmarkSet;
use crate::gridgraph::GridGraph;
use crate::gridgraph::{EdgeCost, GridGraph};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fs::File;
use std::io::BufReader;
use std::process::exit;
/// an approximation of the earths radius.
pub const EARTH_RADIUS: f64 = 6_371_000.0; // meters
/// serialization format for routing queries.
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub struct RoutingQuery {
pub source: usize,
pub destination: usize,
}
/// loads the graph from the given path.
/// exits if an error occurs during loading.
pub fn load_graph(path: &str) -> Box<GridGraph> {
println!("Loading file from {}", path);
let file = match File::open(path) {
@ -36,6 +41,8 @@ pub fn load_graph(path: &str) -> Box<GridGraph> {
graph
}
/// loads a set of landmarks from the given path.
/// exits if an error occurs during loading.
pub fn load_landmarks(path: &str) -> LandmarkSet {
let landmarks = match File::open(path) {
Ok(f) => f,
@ -47,3 +54,25 @@ pub fn load_landmarks(path: &str) -> LandmarkSet {
bincode::deserialize_from(BufReader::new(landmarks)).unwrap()
}
/// A heap element for Dijkstra's algorithm.
///
/// The comparison functions are inverted, so that Rusts MaxHeap works as a
/// MinHeap.
#[derive(Eq, PartialEq)]
pub struct DijkstraElement {
pub index: u32,
pub cost: EdgeCost,
}
impl Ord for DijkstraElement {
fn cmp(&self, other: &Self) -> Ordering {
other.cost.cmp(&self.cost)
}
}
impl PartialOrd for DijkstraElement {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}