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
use crate::hydrostructure::incremental_hydrostructure::IncrementalSafetyTracker;
use traitgraph::implementation::subgraphs::incremental_subgraph::IncrementalSubgraph;
use traitgraph::interface::subgraph::SubgraphBase;
use traitgraph::interface::ImmutableGraphContainer;

/// A type that keeps counts of the nodes in the different hydrostructure components to dynamically determine if they contain nodes.
/// It reports if a bridge-like walk is safe in the 1-circular node-centric model.
pub struct NodeCentricComponentTracker {
    sea_node_count: usize,
    vapor_node_count: usize,
    cloud_node_count: usize,
    river_node_count: usize,
}

impl<'a, Graph: ImmutableGraphContainer + SubgraphBase> IncrementalSafetyTracker<'a, Graph>
    for NodeCentricComponentTracker
where
    <Graph as SubgraphBase>::RootGraph: ImmutableGraphContainer,
{
    fn new_with_empty_subgraph(_graph: &'a Graph) -> Self {
        Self {
            sea_node_count: 0,
            vapor_node_count: 0,
            cloud_node_count: 0,
            river_node_count: 0,
        }
    }

    fn clear(&mut self) {
        self.sea_node_count = 0;
        self.vapor_node_count = 0;
        self.cloud_node_count = 0;
        self.river_node_count = 0;
    }

    fn reset(&mut self, r_plus: &IncrementalSubgraph<Graph>, r_minus: &IncrementalSubgraph<Graph>) {
        IncrementalSafetyTracker::<'a, Graph>::clear(self);
        for node in r_plus.root().node_indices() {
            match (
                r_plus.contains_node_index(node),
                r_minus.contains_node_index(node),
            ) {
                (true, true) => {
                    self.vapor_node_count = self
                        .vapor_node_count
                        .checked_add(1)
                        .expect("Overflow in node count")
                }
                (true, false) => {
                    self.sea_node_count = self
                        .sea_node_count
                        .checked_add(1)
                        .expect("Overflow in node count")
                }
                (false, true) => {
                    self.cloud_node_count = self
                        .cloud_node_count
                        .checked_add(1)
                        .expect("Overflow in node count")
                }
                (false, false) => {
                    self.river_node_count = self
                        .river_node_count
                        .checked_add(1)
                        .expect("Overflow in node count")
                }
            }
        }
    }

    fn add_incremental_subgraph_step(
        &mut self,
        r_plus: &IncrementalSubgraph<Graph>,
        r_minus: &IncrementalSubgraph<Graph>,
    ) {
        for node in r_plus.new_nodes() {
            if r_minus.contains_node_index(*node) {
                self.cloud_node_count = self
                    .cloud_node_count
                    .checked_sub(1)
                    .expect("Overflow in node count");
                self.vapor_node_count = self
                    .vapor_node_count
                    .checked_add(1)
                    .expect("Overflow in node count");
            } else {
                self.river_node_count = self
                    .river_node_count
                    .checked_sub(1)
                    .expect("Overflow in node count");
                self.sea_node_count = self
                    .sea_node_count
                    .checked_add(1)
                    .expect("Overflow in node count");
            }
        }
    }

    fn remove_incremental_subgraph_step(
        &mut self,
        r_plus: &IncrementalSubgraph<Graph>,
        r_minus: &IncrementalSubgraph<Graph>,
    ) {
        for node in r_minus.new_nodes() {
            if r_plus.contains_node_index(*node) {
                self.vapor_node_count = self
                    .vapor_node_count
                    .checked_sub(1)
                    .expect("Overflow in node count");
                self.sea_node_count = self
                    .sea_node_count
                    .checked_add(1)
                    .expect("Overflow in node count");
            } else {
                self.cloud_node_count = self
                    .cloud_node_count
                    .checked_sub(1)
                    .expect("Overflow in node count");
                self.river_node_count = self
                    .river_node_count
                    .checked_add(1)
                    .expect("Overflow in node count");
            }
        }
    }

    fn is_safe(&self, is_forward_univocal: bool, is_backward_univocal: bool) -> bool {
        // We assume that the walk is bridge-like.
        if is_forward_univocal && is_backward_univocal {
            // If the walk is biunivocal then the head of its end is a node in the river.
            true
        } else if is_forward_univocal {
            // If the walk is forward-univocal, then all nodes in the sea fall into the river temporarily.
            // Also, the sea is then empty, so the sea-cloud condition does not hold.
            self.river_node_count + self.sea_node_count > 0
        } else if is_backward_univocal {
            // If the walk is backward-univocal, then all nodes in the cloud fall into the river temporarily.
            // Also, the cloud is then empty, so the sea-cloud condition does not hold.
            self.river_node_count + self.cloud_node_count > 0
        } else {
            self.river_node_count > 0 || (self.cloud_node_count > 0 && self.sea_node_count > 0)
        }
    }

    fn does_safety_equal_bridge_like() -> bool {
        false
    }
}