Skip to content

Commit ee012d1

Browse files
committed
[web] refactor
1 parent 3a3b545 commit ee012d1

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

web/src/graph.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct Graph {
1616
nodes: Vec<Node>,
1717
ways: Vec<Way>,
1818
offset: Vec<usize>,
19-
grid: HashMap<(usize, usize), Vec<usize>>
19+
grid: HashMap<(usize, usize), Vec<usize>>,
2020
}
2121

2222
#[derive(Copy, Clone, Eq, PartialEq)]
@@ -37,12 +37,17 @@ impl PartialOrd for State {
3737
}
3838

3939
impl Graph {
40-
pub fn new(nodes: Vec<Node>, ways: Vec<Way>, offset: Vec<usize>, grid: HashMap<(usize, usize), Vec<usize>>) -> Self {
40+
pub fn new(
41+
nodes: Vec<Node>,
42+
ways: Vec<Way>,
43+
offset: Vec<usize>,
44+
grid: HashMap<(usize, usize), Vec<usize>>,
45+
) -> Self {
4146
Graph {
4247
nodes,
4348
ways,
4449
offset,
45-
grid
50+
grid,
4651
}
4752
}
4853

@@ -54,14 +59,13 @@ impl Graph {
5459
for node_id in adjacent_nodes {
5560
match self.nodes.get(node_id) {
5661
Some(node) => {
57-
let distance =
58-
calc_distance(lat, long, node.latitude, node.longitude);
62+
let distance = calc_distance(lat, long, node.latitude, node.longitude);
5963
if distance < min_distance {
6064
min_distance = distance;
6165
min_distance_id = node_id;
6266
}
63-
},
64-
None => continue
67+
}
68+
None => continue,
6569
}
6670
}
6771
return min_distance_id;
@@ -92,24 +96,26 @@ impl Graph {
9296
let mut node_ids = Vec::<usize>::new();
9397
match self.grid.get(&(lat_grid, lng_grid)) {
9498
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
95-
None => ()
99+
None => (),
96100
}
97101
let mut in_dist: usize = 1;
98102
loop {
99-
for x in lat_grid-in_dist..lat_grid+in_dist+1 {
100-
for y in lng_grid-in_dist..lng_grid+in_dist+1 {
101-
if (x < lat_grid+in_dist || x > lat_grid-in_dist) && (y < lng_grid-in_dist || y > lng_grid+in_dist) {
103+
for x in lat_grid - in_dist..lat_grid + in_dist + 1 {
104+
for y in lng_grid - in_dist..lng_grid + in_dist + 1 {
105+
if (x < lat_grid + in_dist || x > lat_grid - in_dist)
106+
&& (y < lng_grid - in_dist || y > lng_grid + in_dist)
107+
{
102108
// both coordinates are bigger or smaller than the outer bounds => in inner square => already investigated
103109
continue;
104110
}
105-
match self.grid.get(&(x,y)) {
111+
match self.grid.get(&(x, y)) {
106112
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
107-
None => continue
113+
None => continue,
108114
}
109115
}
110116
}
111117
if node_ids.len() > 0 {
112-
return node_ids
118+
return node_ids;
113119
} else {
114120
// search in next level
115121
in_dist += 1;

0 commit comments

Comments
 (0)