Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Stunkymonkey committed Jul 9, 2023
1 parent ba18c2a commit 4d41e04
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 75 deletions.
35 changes: 16 additions & 19 deletions pre/src/contraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn calc_shortcuts(
let pair = (source_node, target_node);
let lower_edges = (*source_edge, *target_edge);
if let Some(old_minima) = minimum_neighbor_distances.get_mut(&pair) {
if (*old_minima).0 > weight {
if (old_minima).0 > weight {
*old_minima = (weight, lower_edges);
}
} else {
Expand Down Expand Up @@ -75,9 +75,9 @@ pub fn calc_shortcuts(
}

fn remove_redundant_edges(
mut edges: &mut Vec<Way>,
mut up_offset: &mut Vec<EdgeId>,
mut down_offset: &mut Vec<EdgeId>,
edges: &mut Vec<Way>,
up_offset: &mut Vec<EdgeId>,
down_offset: &mut Vec<EdgeId>,
down_index: &mut Vec<EdgeId>,
amount_nodes: usize,
) {
Expand Down Expand Up @@ -119,8 +119,7 @@ fn remove_redundant_edges(
}

// update graph
*down_index =
offset::generate_offsets(&mut edges, &mut up_offset, &mut down_offset, amount_nodes);
*down_index = offset::generate_offsets(edges, up_offset, down_offset, amount_nodes);
}

fn sort_edges_ranked(
Expand Down Expand Up @@ -180,10 +179,10 @@ fn revert_indices(edges: &mut Vec<Way>) {
/// run full contraction
pub fn run_contraction(
nodes: &mut Vec<Node>,
mut edges: &mut Vec<Way>,
mut up_offset: &mut Vec<EdgeId>,
mut down_offset: &mut Vec<EdgeId>,
mut down_index: &mut Vec<EdgeId>,
edges: &mut Vec<Way>,
up_offset: &mut Vec<EdgeId>,
down_offset: &mut Vec<EdgeId>,
down_index: &mut Vec<EdgeId>,
) {
let amount_nodes: usize = nodes.len();
// for keeping track of new created edge_ids
Expand Down Expand Up @@ -318,8 +317,7 @@ pub fn run_contraction(
edges.par_extend(&shortcuts);

// recalc edge-indices
*down_index =
offset::generate_offsets(&mut edges, &mut up_offset, &mut down_offset, amount_nodes);
*down_index = offset::generate_offsets(edges, up_offset, down_offset, amount_nodes);

// move I to their Level
for node in &minimas {
Expand All @@ -346,9 +344,9 @@ pub fn run_contraction(
// remove never used edges
remove_redundant_edges(
&mut resulting_edges,
&mut up_offset,
&mut down_offset,
&mut down_index,
up_offset,
down_offset,
down_index,
amount_nodes,
);

Expand All @@ -358,14 +356,13 @@ pub fn run_contraction(

*edges = resulting_edges;
// and calculate the offsets
*down_index =
offset::generate_offsets(&mut edges, &mut up_offset, &mut down_offset, amount_nodes);
*down_index = offset::generate_offsets(edges, up_offset, down_offset, amount_nodes);

// sort edges from top to down ranks for bidijkstra
sort_edges_ranked(&mut edges, down_offset, &mut down_index, nodes);
sort_edges_ranked(edges, down_offset, down_index, nodes);

// revert the ids back to usual ids
revert_indices(&mut edges);
revert_indices(edges);
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions pre/src/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ fn fill_offset(edges: Vec<NodeId>, offset: &mut Vec<usize>) {
/// make sure edges are already sorted!!
pub fn generate_offsets_unstable(
edges: &mut Vec<Way>,
mut up_offset: &mut Vec<EdgeId>,
mut down_offset: &mut Vec<EdgeId>,
up_offset: &mut Vec<EdgeId>,
down_offset: &mut Vec<EdgeId>,
amount_nodes: usize,
) -> Vec<EdgeId> {
up_offset.clear();
Expand All @@ -24,12 +24,12 @@ pub fn generate_offsets_unstable(

// generate up edges
let sources: Vec<EdgeId> = edges.iter().map(|x| x.source).rev().collect();
fill_offset(sources, &mut up_offset);
fill_offset(sources, up_offset);

// generate down edges, but without sorting edges
// first collect offsets
let targets: Vec<EdgeId> = edges.iter().map(|x| x.target).rev().collect();
fill_offset(targets, &mut down_offset);
fill_offset(targets, down_offset);
let mut down_index = vec![INVALID_EDGE; edges.len()];
// fill offsets, where not already filled
for (i, edge) in edges.iter().enumerate() {
Expand Down
25 changes: 12 additions & 13 deletions pre/src/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn node_degree(node: NodeId, up_offset: &[EdgeId], down_offset: &[EdgeId]) -
#[allow(clippy::too_many_arguments)]
fn edge_difference(
node: NodeId,
mut dijkstra: &mut dijkstra::Dijkstra,
dijkstra: &mut dijkstra::Dijkstra,
shortcut_id: &AtomicUsize,
edges: &[Way],
up_offset: &[EdgeId],
Expand All @@ -18,7 +18,7 @@ fn edge_difference(
) -> isize {
let shortcuts = contraction::calc_shortcuts(
node,
&mut dijkstra,
dijkstra,
edges,
up_offset,
down_offset,
Expand All @@ -33,7 +33,7 @@ fn edge_difference(
#[allow(clippy::too_many_arguments)]
pub fn calculate_single_heuristic(
node: NodeId,
mut dijkstra: &mut dijkstra::Dijkstra,
dijkstra: &mut dijkstra::Dijkstra,
deleted_neighbors: &[Weight],
shortcut_id: &AtomicUsize,
edges: &[Way],
Expand All @@ -45,7 +45,7 @@ pub fn calculate_single_heuristic(
deleted_neighbors[node] as isize
+ edge_difference(
node,
&mut dijkstra,
dijkstra,
shortcut_id,
edges,
up_offset,
Expand All @@ -72,7 +72,7 @@ pub fn calculate_heuristics(
heuristics.push(AtomicIsize::new(0));
}

let mut nodes: Vec<NodeId> = (0..amount_nodes).into_iter().collect();
let mut nodes: Vec<NodeId> = (0..amount_nodes).collect();

let thread_count = num_cpus::get();
let chunk_size = (amount_nodes + thread_count - 1) / thread_count;
Expand All @@ -94,7 +94,7 @@ pub fn calculate_heuristics(
down_index,
rank,
);
heuristics[*node as usize].store(new_value, Ordering::Relaxed);
heuristics[*node].store(new_value, Ordering::Relaxed);
}
});
}
Expand All @@ -107,7 +107,7 @@ pub fn calculate_heuristics(
#[allow(clippy::too_many_arguments)]
pub fn update_neighbor_heuristics(
mut neighbors: Vec<NodeId>,
heuristics: &mut Vec<AtomicIsize>,
heuristics: &mut [AtomicIsize],
deleted_neighbors: &[Weight],
shortcut_id: &AtomicUsize,
rank: usize,
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn update_neighbor_heuristics(
down_index,
rank,
);
heuristics[*neighbor as usize].store(new_value, Ordering::Relaxed);
heuristics[*neighbor].store(new_value, Ordering::Relaxed);
}
});
}
Expand All @@ -155,16 +155,15 @@ pub fn get_independent_set(
down_offset: &[EdgeId],
down_index: &[NodeId],
) -> Vec<NodeId> {
let subset: Vec<NodeId>;
let mut remaining_nodes_vector: Vec<NodeId> = remaining_nodes.iter().copied().collect();
if remaining_nodes.len() > 10_000 {
let subset: Vec<NodeId> = if remaining_nodes.len() > 10_000 {
// sort remaining_nodes via heuristic
remaining_nodes_vector.par_sort_by_key(|&node| heuristics[node].load(Ordering::Relaxed));
// take lower 1/4
subset = (&remaining_nodes_vector[0..remaining_nodes_vector.len() / 4]).to_vec();
(remaining_nodes_vector[0..remaining_nodes_vector.len() / 4]).to_vec()
} else {
subset = remaining_nodes_vector;
}
remaining_nodes_vector
};

minimas_bool.unvisit_all();
// mark all neighbors with greater equal value as invalid
Expand Down
2 changes: 1 addition & 1 deletion pre/src/osm_pbf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn get_pbf(filename: &str) -> osmpbfreader::OsmPbfReader<std::fs::File> {
println!("{} not found", filename);
std::process::exit(1);
}
let r = File::open(&path).unwrap();
let r = File::open(path).unwrap();
OsmPbfReader::new(r)
}

Expand Down
71 changes: 35 additions & 36 deletions web/src/bidijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,47 +178,18 @@ impl Dijkstra {
}

// walk shortcuts from meeting point to end
fn walk_down(&self, edge: EdgeId, is_upwards: bool, mut path: &mut Vec<NodeId>, edges: &[Way]) {
self.resolve_edge(edge, &mut path, is_upwards, edges);
fn walk_down(&self, edge: EdgeId, is_upwards: bool, path: &mut Vec<NodeId>, edges: &[Way]) {
resolve_edge(edge, path, is_upwards, edges);

let current_edge = edges[edge];
let prev;

if is_upwards {
prev = self.dist_up[current_edge.source];
let prev = if is_upwards {
self.dist_up[current_edge.source]
} else {
prev = self.dist_down[current_edge.target];
}
self.dist_down[current_edge.target]
};
if let Some(child) = prev.1 {
self.walk_down(child, is_upwards, &mut path, edges);
}
}

/// resolve shortcuts to original edges
fn resolve_edge(
&self,
edge: EdgeId,
mut path: &mut Vec<NodeId>,
is_upwards: bool,
edges: &[Way],
) {
match (&edges[edge].contrated_previous, &edges[edge].contrated_next) {
(Some(previous), Some(next)) => {
if is_upwards {
self.resolve_edge(*next, &mut path, is_upwards, edges);
self.resolve_edge(*previous, &mut path, is_upwards, edges);
} else {
self.resolve_edge(*previous, &mut path, is_upwards, edges);
self.resolve_edge(*next, &mut path, is_upwards, edges);
}
}
_ => {
if is_upwards {
path.push(edges[edge].source)
} else {
path.push(edges[edge].target)
}
}
self.walk_down(child, is_upwards, path, edges);
}
}

Expand Down Expand Up @@ -267,3 +238,31 @@ impl Dijkstra {
false
}
}

/// resolve shortcuts to original edges
fn resolve_edge(
// &self,
edge: EdgeId,
path: &mut Vec<NodeId>,
is_upwards: bool,
edges: &[Way],
) {
match (&edges[edge].contrated_previous, &edges[edge].contrated_next) {
(Some(previous), Some(next)) => {
if is_upwards {
resolve_edge(*next, path, is_upwards, edges);
resolve_edge(*previous, path, is_upwards, edges);
} else {
resolve_edge(*previous, path, is_upwards, edges);
resolve_edge(*next, path, is_upwards, edges);
}
}
_ => {
if is_upwards {
path.push(edges[edge].source)
} else {
path.push(edges[edge].target)
}
}
}
}
6 changes: 4 additions & 2 deletions web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ async fn shortest_path(

#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "info");
//std::env::set_var("RUST_LOG", "debug");
std::env::set_var("RUST_LOG", "actix_web=trace");
std::env::set_var("RUST_BACKTRACE", "1");
env_logger::init();

// read file
Expand All @@ -154,7 +156,7 @@ async fn main() -> std::io::Result<()> {
println!("Starting server at: http://localhost:8080");
HttpServer::new(move || {
// initialize thread-local dijkstra
let dijkstra = RefCell::new(Dijkstra::new(amount_nodes));
let dijkstra = web::Data::new(RefCell::new(Dijkstra::new(amount_nodes)));
App::new()
.wrap(middleware::Logger::default())
.app_data(web::JsonConfig::default().limit(1024))
Expand Down

0 comments on commit 4d41e04

Please sign in to comment.