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
use rayon::prelude::*;
use crate::{
HyperedgeIndex,
HyperedgeTrait,
Hypergraph,
VertexIndex,
VertexTrait,
errors::HypergraphError,
};
impl<V, HE> Hypergraph<V, HE>
where
V: VertexTrait,
HE: HyperedgeTrait,
{
/// Joins two or more hyperedges from the hypergraph into one single entity.
/// All the vertices are moved to the first hyperedge in the provided order.
pub fn join_hyperedges(
&mut self,
hyperedges: &[HyperedgeIndex],
) -> Result<(), HypergraphError<V, HE>> {
// If the provided hyperedges are less than two, skip the operation.
if hyperedges.len() < 2 {
return Err(HypergraphError::HyperedgesInvalidJoin);
}
// Try to collect all the vertices from the provided hyperedges.
match hyperedges
.par_iter()
.map(|hyperedge_index| self.get_hyperedge_vertices(*hyperedge_index))
.collect::<Result<Vec<Vec<VertexIndex>>, HypergraphError<V, HE>>>()
{
Err(err) => Err(err),
Ok(joined_vertices) => {
// The goal is to move all the vertices from the provided
// hyperedges to the first one.
self.update_hyperedge_vertices(
hyperedges[0],
joined_vertices.into_par_iter().flatten().collect(),
)?;
// Get the tail.
let tail = &hyperedges[1..];
// Removes the other hyperedges.
for hyperedge_index in tail {
self.remove_hyperedge(*hyperedge_index)?;
}
Ok(())
}
}
}
}