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

use crate::error::Error;
use h3ron::collections::H3Treemap;
use h3ron::{H3Cell, H3DirectedEdge, HasH3Resolution};

use crate::graph::node::NodeType;
use crate::graph::{EdgeWeight, GetCellEdges, GetCellNode};

/// 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: GetCellNode + GetCellEdges<EdgeWeightType = W> + HasH3Resolution,
{
    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> GetCellNode for ExcludeCells<'a, G, W>
where
    G: GetCellNode,
{
    fn get_cell_node(&self, cell: &H3Cell) -> Option<NodeType> {
        if self.cells_to_exclude.contains(cell) {
            None
        } else {
            self.inner_graph.get_cell_node(cell)
        }
    }
}

impl<'a, G, W> GetCellEdges for ExcludeCells<'a, G, W>
where
    G: GetCellEdges<EdgeWeightType = W>,
{
    type EdgeWeightType = G::EdgeWeightType;

    fn get_edges_originating_from(
        &self,
        cell: &H3Cell,
    ) -> Result<Vec<(H3DirectedEdge, EdgeWeight<Self::EdgeWeightType>)>, Error> {
        if self.cells_to_exclude.contains(cell) {
            Ok(vec![])
        } else {
            let found = self.inner_graph.get_edges_originating_from(cell)?;
            let mut not_excluded = Vec::with_capacity(found.len());
            for (edge, edge_value) in found {
                if self.cells_to_exclude.contains(&edge.destination_cell()?) {
                    continue;
                }

                // 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
                    };

                not_excluded.push((
                    edge,
                    EdgeWeight {
                        weight: edge_value.weight,
                        longedge: filtered_longedge_opt,
                    },
                ));
            }
            Ok(not_excluded)
        }
    }
}

impl<'a, G, W> HasH3Resolution for ExcludeCells<'a, G, W>
where
    G: HasH3Resolution,
{
    fn h3_resolution(&self) -> u8 {
        self.inner_graph.h3_resolution()
    }
}