1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::ops::{Add, AddAssign};
use serde::{Deserialize, Serialize};
use h3ron::H3Cell;
use crate::graph::GetNodeType;
#[derive(PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum NodeType {
Origin,
Destination,
OriginAndDestination,
}
impl NodeType {
pub fn is_origin(&self) -> bool {
match self {
NodeType::Origin => true,
NodeType::Destination => false,
NodeType::OriginAndDestination => true,
}
}
pub fn is_destination(&self) -> bool {
match self {
NodeType::Origin => false,
NodeType::Destination => true,
NodeType::OriginAndDestination => true,
}
}
}
impl Add<NodeType> for NodeType {
type Output = NodeType;
fn add(self, rhs: NodeType) -> Self::Output {
if rhs == self {
self
} else {
Self::OriginAndDestination
}
}
}
impl AddAssign<NodeType> for NodeType {
fn add_assign(&mut self, rhs: NodeType) {
if self != &rhs {
*self = Self::OriginAndDestination
}
}
}
pub enum GapBridgedCellNode {
DirectConnection(H3Cell),
WithGap(H3Cell, H3Cell),
NoConnection(H3Cell),
}
impl GapBridgedCellNode {
pub fn cell(&self) -> H3Cell {
match self {
Self::DirectConnection(cell) => *cell,
Self::WithGap(cell, _) => *cell,
Self::NoConnection(cell) => *cell,
}
}
pub fn corresponding_cell_in_graph(&self) -> Option<H3Cell> {
match self {
Self::DirectConnection(cell) => Some(*cell),
Self::WithGap(_, cell) => Some(*cell),
_ => None,
}
}
}
pub trait GetGapBridgedCellNodes {
fn gap_bridged_corresponding_node(
&self,
cell: &H3Cell,
num_gap_cells_to_graph: u32,
) -> GapBridgedCellNode {
self.gap_bridged_corresponding_node_filtered(cell, num_gap_cells_to_graph, |_, _| true)
}
fn gap_bridged_corresponding_node_filtered<F>(
&self,
cell: &H3Cell,
num_gap_cells_to_graph: u32,
nodetype_filter_fn: F,
) -> GapBridgedCellNode
where
F: Fn(&H3Cell, &NodeType) -> bool + Send + Sync + Copy;
}
impl<G> GetGapBridgedCellNodes for G
where
G: GetNodeType + Sync,
{
fn gap_bridged_corresponding_node_filtered<F>(
&self,
cell: &H3Cell,
num_gap_cells_to_graph: u32,
nodetype_filter_fn: F,
) -> GapBridgedCellNode
where
F: Fn(&H3Cell, &NodeType) -> bool + Send + Sync + Copy,
{
if self
.get_node_type(cell)
.map(|node_type| nodetype_filter_fn(cell, node_type))
.unwrap_or(false)
{
GapBridgedCellNode::DirectConnection(*cell)
} else if num_gap_cells_to_graph > 0 {
let mut neighbors = cell.k_ring_distances(1, num_gap_cells_to_graph.max(1));
neighbors.sort_unstable_by_key(|neighbor| neighbor.0);
let mut selected_neighbor: Option<H3Cell> = None;
for neighbor in neighbors {
if self
.get_node_type(&neighbor.1)
.map(|node_type| nodetype_filter_fn(&neighbor.1, node_type))
.unwrap_or(false)
{
selected_neighbor = Some(neighbor.1);
break;
}
}
selected_neighbor
.map(|neighbor| GapBridgedCellNode::WithGap(*cell, neighbor))
.unwrap_or_else(|| GapBridgedCellNode::NoConnection(*cell))
} else {
GapBridgedCellNode::NoConnection(*cell)
}
}
}
#[cfg(test)]
mod tests {
use crate::graph::node::NodeType;
#[test]
fn test_nodetype_add() {
assert_eq!(NodeType::Origin, NodeType::Origin + NodeType::Origin);
assert_eq!(
NodeType::Destination,
NodeType::Destination + NodeType::Destination
);
assert_eq!(
NodeType::OriginAndDestination,
NodeType::Origin + NodeType::Destination
);
assert_eq!(
NodeType::OriginAndDestination,
NodeType::OriginAndDestination + NodeType::Destination
);
assert_eq!(
NodeType::OriginAndDestination,
NodeType::Destination + NodeType::Origin
);
}
#[test]
fn test_nodetype_addassign() {
let mut n1 = NodeType::Origin;
n1 += NodeType::Origin;
assert_eq!(n1, NodeType::Origin);
let mut n2 = NodeType::Origin;
n2 += NodeType::OriginAndDestination;
assert_eq!(n2, NodeType::OriginAndDestination);
let mut n3 = NodeType::Destination;
n3 += NodeType::OriginAndDestination;
assert_eq!(n3, NodeType::OriginAndDestination);
let mut n4 = NodeType::Destination;
n4 += NodeType::Origin;
assert_eq!(n4, NodeType::OriginAndDestination);
}
}