Skip to content

Refactoring: less moving - more references #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::path::Path;
use std::sync::Arc;

extern crate bmw_routing_engine;

Expand Down Expand Up @@ -32,9 +33,9 @@ fn main() {
let to = read_into_vector(path.join("test/target").to_str().unwrap()).expect("could not read target");
let ground_truth = read_into_vector(path.join("test/travel_time_length").to_str().unwrap()).expect("could not read travel_time_length");

let graph = Graph::new(first_out, head, travel_time);
let graph = Arc::new(Graph::new(first_out, head, travel_time));
let mut simple_server = DijkServer::new(graph.clone());
let mut bi_dir_server = BiDijkServer::<Graph, Graph>::new(graph.clone());
let mut bi_dir_server = BiDijkServer::new(graph.clone());
let async_server = AsyncDijkServer::new(graph.clone());
let mut async_bi_dir_server = AsyncBiDijkServer::new(graph.clone());

Expand All @@ -47,7 +48,7 @@ fn main() {
inverted_order[node as usize] = i as u32;
}
let mut ch_server = CHServer::new((Graph::new(ch_first_out, ch_head, ch_weight).ch_split(&inverted_order), None));
let mut ch_server_with_own_ch = CHServer::new(contraction_hierarchy::contract(graph, ch_order));
let mut ch_server_with_own_ch = CHServer::new(contraction_hierarchy::contract(&graph, ch_order));

for ((&from, &to), &ground_truth) in from.iter().zip(to.iter()).zip(ground_truth.iter()).take(100) {
let ground_truth = match ground_truth {
Expand Down
8 changes: 4 additions & 4 deletions engine/src/shortest_path/contraction_hierarchy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct ContractionGraph {
}

impl ContractionGraph {
fn new(graph: FirstOutGraph, node_order: Vec<NodeId>) -> ContractionGraph {
fn new(graph: &FirstOutGraph, node_order: Vec<NodeId>) -> ContractionGraph {
let n = graph.num_nodes();
let mut node_ranks = vec![0; n];
for (i, &node) in node_order.iter().enumerate() {
Expand Down Expand Up @@ -164,8 +164,8 @@ impl<'a> PartialContractionGraph<'a> {

fn shortcut_required(&self, from: NodeId, to: NodeId, shortcut_weight: Weight) -> bool {
let mut server = ::shortest_path::query::bidirectional_dijkstra::Server {
forward_dijkstra: SteppedDijkstra::new(ForwardWrapper { graph: &self }),
backward_dijkstra: SteppedDijkstra::new(BackwardWrapper { graph: &self }),
forward_dijkstra: SteppedDijkstra::new(Box::new(ForwardWrapper { graph: &self })),
backward_dijkstra: SteppedDijkstra::new(Box::new(BackwardWrapper { graph: &self })),
tentative_distance: INFINITY,
maximum_distance: shortcut_weight,
meeting_node: 0
Expand All @@ -179,7 +179,7 @@ impl<'a> PartialContractionGraph<'a> {
}
}

pub fn contract(graph: FirstOutGraph, node_order: Vec<NodeId>) -> ((FirstOutGraph, FirstOutGraph), Option<(Vec<NodeId>, Vec<NodeId>)>) {
pub fn contract(graph: &FirstOutGraph, node_order: Vec<NodeId>) -> ((FirstOutGraph, FirstOutGraph), Option<(Vec<NodeId>, Vec<NodeId>)>) {
let mut graph = ContractionGraph::new(graph, node_order);
graph.contract();
graph.as_first_out_graphs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ptr;
use std::sync::{Arc, Barrier};
use std::sync::atomic::compiler_fence;
use std::sync::atomic::Ordering::SeqCst;
use std::ops::Deref;

use shortest_path::timestamped_vector::TimestampedVector;

Expand All @@ -28,7 +29,7 @@ pub struct Server {
}

impl Server {
pub fn new(graph: Graph) -> Server {
pub fn new<C: 'static + Send + Deref<Target = Graph>>(graph: C) -> Server {
let (forward_query_sender, forward_query_receiver) = channel();
let (forward_progress_sender, forward_progress_receiver) = channel();
let (backward_query_sender, backward_query_receiver) = channel();
Expand Down Expand Up @@ -79,7 +80,7 @@ impl Server {
});

thread::spawn(move || {
let mut dijkstra = SteppedDijkstra::new(reversed);
let mut dijkstra = SteppedDijkstra::new(Box::new(reversed));

unsafe { *backward_distances_pointer.pointer = dijkstra.distances_pointer() };
compiler_fence(SeqCst);
Expand Down
4 changes: 3 additions & 1 deletion engine/src/shortest_path/query/async/dijkstra.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::*;

use std::ops::Deref;

#[derive(Debug)]
pub struct Server {
query_sender: Sender<ServerControl>,
progress_receiver: Receiver<QueryProgress>
}

impl Server {
pub fn new(graph: Graph) -> Server {
pub fn new<C: 'static + Send + Deref<Target = Graph>>(graph: C) -> Server {
let (query_sender, query_receiver) = channel();
let (progress_sender, progress_receiver) = channel();

Expand Down
16 changes: 10 additions & 6 deletions engine/src/shortest_path/query/bidirectional_dijkstra.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
use std::collections::LinkedList;
use std::ops::Deref;

use super::*;

#[derive(Debug)]
pub struct Server<G: DijkstrableGraph, H: DijkstrableGraph> {
pub forward_dijkstra: SteppedDijkstra<G>,
pub backward_dijkstra: SteppedDijkstra<H>,
pub struct Server<G: DijkstrableGraph, H: DijkstrableGraph, C: Deref<Target = G>> {
pub forward_dijkstra: SteppedDijkstra<G, C>,
pub backward_dijkstra: SteppedDijkstra<H, Box<H>>,
pub tentative_distance: Weight,
pub maximum_distance: Weight,
pub meeting_node: NodeId
}

impl<G: DijkstrableGraph, H: DijkstrableGraph> Server<G, H> {
pub fn new(graph: Graph) -> Server<Graph, Graph> {
impl<C: Deref<Target = Graph>> Server<Graph, Graph, C> {
pub fn new(graph: C) -> Server<Graph, Graph, C> {
let reversed = graph.reverse();

Server {
forward_dijkstra: SteppedDijkstra::new(graph),
backward_dijkstra: SteppedDijkstra::new(reversed),
backward_dijkstra: SteppedDijkstra::new(Box::new(reversed)),
tentative_distance: INFINITY,
maximum_distance: INFINITY,
meeting_node: 0
}
}
}

impl<G: DijkstrableGraph, H: DijkstrableGraph, C: Deref<Target = G>> Server<G, H, C> {
pub fn distance(&mut self, from: NodeId, to: NodeId) -> Option<Weight> {
// initialize
self.tentative_distance = INFINITY;
Expand Down
8 changes: 4 additions & 4 deletions engine/src/shortest_path/query/contraction_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use super::*;

#[derive(Debug)]
pub struct Server {
forward_dijkstra: SteppedDijkstra<Graph>,
backward_dijkstra: SteppedDijkstra<Graph>,
forward_dijkstra: SteppedDijkstra<Graph, Box<Graph>>,
backward_dijkstra: SteppedDijkstra<Graph, Box<Graph>>,
tentative_distance: Weight,
meeting_node: NodeId,
shortcut_middle_nodes: Option<(Vec<NodeId>, Vec<NodeId>)>
Expand All @@ -13,8 +13,8 @@ pub struct Server {
impl Server {
pub fn new(((up, down), shortcut_middle_nodes): ((Graph, Graph), Option<(Vec<NodeId>, Vec<NodeId>)>)) -> Server {
Server {
forward_dijkstra: SteppedDijkstra::new(up),
backward_dijkstra: SteppedDijkstra::new(down),
forward_dijkstra: SteppedDijkstra::new(Box::new(up)),
backward_dijkstra: SteppedDijkstra::new(Box::new(down)),
tentative_distance: INFINITY,
meeting_node: 0,
shortcut_middle_nodes
Expand Down
9 changes: 5 additions & 4 deletions engine/src/shortest_path/query/dijkstra.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::*;

use std::ops::Deref;
use std::collections::LinkedList;

#[derive(Debug)]
pub struct Server {
dijkstra: SteppedDijkstra<Graph>,
pub struct Server<C: Deref<Target = Graph>> {
dijkstra: SteppedDijkstra<Graph, C>,
}

impl Server {
pub fn new(graph: Graph) -> Server {
impl<C: Deref<Target = Graph>> Server<C> {
pub fn new(graph: C) -> Server<C> {
Server {
dijkstra: SteppedDijkstra::new(graph)
}
Expand Down
9 changes: 5 additions & 4 deletions engine/src/shortest_path/stepped_dijkstra.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use std::ops::Deref;
use index_heap::{IndexdMinHeap, Indexing};
use super::timestamped_vector::TimestampedVector;

Expand All @@ -21,8 +22,8 @@ impl Indexing for State {
}

#[derive(Debug)]
pub struct SteppedDijkstra<Graph: DijkstrableGraph> {
graph: Graph,
pub struct SteppedDijkstra<Graph: DijkstrableGraph, C: Deref<Target = Graph>> {
graph: C,
distances: TimestampedVector<Weight>,
predecessors: Vec<NodeId>,
closest_node_priority_queue: IndexdMinHeap<State>,
Expand All @@ -32,8 +33,8 @@ pub struct SteppedDijkstra<Graph: DijkstrableGraph> {
result: Option<Option<Weight>>
}

impl<Graph: DijkstrableGraph> SteppedDijkstra<Graph> {
pub fn new(graph: Graph) -> SteppedDijkstra<Graph> {
impl<Graph: DijkstrableGraph, C: Deref<Target = Graph>> SteppedDijkstra<Graph, C> {
pub fn new(graph: C) -> SteppedDijkstra<Graph, C> {
let n = graph.num_nodes();

SteppedDijkstra {
Expand Down
5 changes: 3 additions & 2 deletions search_space_visualization/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::env;
use std::path::Path;
use std::cmp::max;
use std::cmp::Ordering;
use std::rc::Rc;

use bmw_routing_engine::*;
use graph::first_out_graph::FirstOutGraph as Graph;
Expand Down Expand Up @@ -82,8 +83,8 @@ fn main() {
let head = read_into_vector(path.join("head").to_str().unwrap()).expect("could not read head");
let travel_time = read_into_vector(path.join("travel_time").to_str().unwrap()).expect("could not read travel_time");

let graph = Graph::new(first_out, head, travel_time);
let mut query_server = DijkServer::<Graph, Graph>::new(graph.clone());
let graph = Rc::new(Graph::new(first_out, head, travel_time));
let mut query_server = DijkServer::new(graph.clone());

let find_closest = |(p_lat, p_lon): (f32, f32)| -> NodeId {
lat.iter().zip(lon.iter()).enumerate().min_by_key(|&(_, (lat, lon))| {
Expand Down
4 changes: 2 additions & 2 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Ord for NonNan {

#[derive(Debug)]
struct RoutingWrapper {
server: shortest_path::query::bidirectional_dijkstra::Server<Graph, Graph>,
server: shortest_path::query::bidirectional_dijkstra::Server<Graph, Graph, Box<Graph>>,
lat: Vec<f32>,
lng: Vec<f32>
}
Expand Down Expand Up @@ -118,7 +118,7 @@ fn main() {
let lng = read_into_vector(path.join("longitude").to_str().unwrap()).expect("could not read first_out");

let wrapper = RoutingWrapper {
server: shortest_path::query::bidirectional_dijkstra::Server::<Graph, Graph>::new(graph), lat, lng
server: shortest_path::query::bidirectional_dijkstra::Server::new(Box::new(graph)), lat, lng
};

let config = rocket::config::Config::build(rocket::config::Environment::Staging)
Expand Down