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
use std::marker::PhantomData;
use std::ops::Add;

use num_traits::Zero;

use h3ron::collections::H3Treemap;
use h3ron::{H3Cell, H3Edge, HasH3Resolution};

use crate::graph::node::NodeType;
use crate::graph::{EdgeValue, GetEdge, GetNodeType};

/// wrapper to exclude cells from traversal during routing
pub struct ExcludeCells<'a, G, W> {
    cells_to_exclude: &'a H3Treemap<H3Cell>,
    inner_graph: &'a G,
    phantom_weight: PhantomData<W>,
}

impl<'a, G, W> ExcludeCells<'a, G, W>
where
    G: GetNodeType + GetEdge<WeightType = W> + HasH3Resolution,
    W: PartialOrd + PartialEq + Add + Copy + Send + Ord + Zero + Sync,
{
    pub fn new(inner_graph: &'a G, cells_to_exclude: &'a H3Treemap<H3Cell>) -> Self {
        Self {
            cells_to_exclude,
            inner_graph,
            phantom_weight: Default::default(),
        }
    }
}

impl<'a, G, W> GetNodeType for ExcludeCells<'a, G, W>
where
    G: GetNodeType + GetEdge<WeightType = W> + HasH3Resolution,
    W: PartialOrd + PartialEq + Add + Copy + Send + Ord + Zero + Sync,
{
    fn get_node_type(&self, cell: &H3Cell) -> Option<&NodeType> {
        if self.cells_to_exclude.contains(cell) {
            None
        } else {
            self.inner_graph.get_node_type(cell)
        }
    }
}

impl<'a, G, W> GetEdge for ExcludeCells<'a, G, W>
where
    G: GetNodeType + GetEdge<WeightType = W> + HasH3Resolution,
    W: PartialOrd + PartialEq + Add + Copy + Send + Ord + Zero + Sync,
{
    type WeightType = G::WeightType;

    fn get_edge(&self, edge: &H3Edge) -> Option<EdgeValue<Self::WeightType>> {
        if self
            .cells_to_exclude
            .contains(&edge.destination_index_unchecked())
        {
            None
        } else if let Some(edge_value) = self.inner_graph.get_edge(edge) {
            // remove the longedge when it contains any excluded cell
            let filtered_longedge_opt =
                if let Some((longedge, longedge_weight)) = edge_value.longedge {
                    if longedge.is_disjoint(self.cells_to_exclude) {
                        Some((longedge, longedge_weight))
                    } else {
                        None
                    }
                } else {
                    None
                };

            Some(EdgeValue {
                weight: edge_value.weight,
                longedge: filtered_longedge_opt,
            })
        } else {
            None
        }
    }
}

impl<'a, G, W> HasH3Resolution for ExcludeCells<'a, G, W>
where
    G: GetNodeType + GetEdge<WeightType = W> + HasH3Resolution,
    W: PartialOrd + PartialEq + Add + Copy + Send + Ord + Zero + Sync,
{
    fn h3_resolution(&self) -> u8 {
        self.inner_graph.h3_resolution()
    }
}