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
use crate::biedgedgraph::BiedgedGraph;
use petgraph::unionfind::UnionFind;
use fxhash::FxHashMap;
#[derive(Clone)]
pub struct Projection {
pub size: usize,
union_find: UnionFind<usize>,
inverse: Option<FxHashMap<u64, Vec<u64>>>,
}
pub type InverseProjection = FxHashMap<u64, Vec<u64>>;
impl Projection {
#[inline]
pub fn copy_without_inverse(&self) -> Self {
Projection {
size: self.size,
union_find: self.union_find.clone(),
inverse: None,
}
}
#[inline]
pub fn new_for_biedged_graph(graph: &BiedgedGraph) -> Self {
let size = (graph.max_net_vertex + 1) as usize;
let union_find = UnionFind::new(size);
let inverse = None;
Self {
size,
union_find,
inverse,
}
}
#[inline]
pub fn find(&self, x: u64) -> u64 {
let x = x as usize;
self.union_find.find(x) as u64
}
#[inline]
pub fn find_mut(&mut self, x: u64) -> u64 {
let x = x as usize;
self.union_find.find_mut(x) as u64
}
#[inline]
pub fn find_edge(&self, x: u64, y: u64) -> (u64, u64) {
let x = self.union_find.find(x as usize);
let y = self.union_find.find(y as usize);
(x as u64, y as u64)
}
#[inline]
pub fn find_edge_mut(&mut self, x: u64, y: u64) -> (u64, u64) {
let x = self.union_find.find_mut(x as usize);
let y = self.union_find.find_mut(y as usize);
(x as u64, y as u64)
}
#[inline]
pub fn union(&mut self, x: u64, y: u64) -> bool {
self.union_find.union(x as usize, y as usize)
}
#[inline]
pub fn equiv(&self, x: u64, y: u64) -> bool {
self.union_find.equiv(x as usize, y as usize)
}
#[inline]
pub fn kept_pair(&mut self, x: u64, y: u64) -> (u64, u64) {
let union = self.union_find.find_mut(x as usize) as u64;
if union == x {
(union, y)
} else {
(union, x)
}
}
fn build_inverse_replace(&mut self) {
let mut inverse: InverseProjection = FxHashMap::default();
let reps = self.union_find.clone().into_labeling();
for (i, k) in reps.iter().enumerate() {
let i = i as u64;
let k = *k as u64;
inverse.entry(k).or_default().push(i);
}
self.inverse = Some(inverse);
}
pub fn build_inverse(&mut self) -> bool {
if self.inverse.is_none() {
self.build_inverse_replace();
true
} else {
false
}
}
#[inline]
pub fn mut_get_inverse(&mut self) -> &InverseProjection {
if let Some(ref inv) = self.inverse {
inv
} else {
self.build_inverse_replace();
self.inverse.as_ref().unwrap()
}
}
#[inline]
pub fn get_inverse(&self) -> Option<&InverseProjection> {
self.inverse.as_ref()
}
#[inline]
pub fn projected_from(&self, x: u64) -> Option<&[u64]> {
let inverse = self.inverse.as_ref()?;
let projected = inverse.get(&x)?;
Some(projected.as_slice())
}
}
#[inline]
pub fn id_to_black_edge(n: u64) -> (u64, u64) {
let left = n * 2;
let right = left + 1;
(left, right)
}
#[inline]
pub fn end_to_black_edge(n: u64) -> (u64, u64) {
if n % 2 == 0 {
(n, n + 1)
} else {
(n - 1, n)
}
}
#[inline]
pub fn opposite_vertex(n: u64) -> u64 {
if n % 2 == 0 {
n + 1
} else {
n - 1
}
}
#[inline]
pub fn id_from_black_edge(n: u64) -> u64 {
n / 2
}