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
use crate::{
HyperedgeTrait,
Hypergraph,
VertexIndex,
VertexTrait,
errors::HypergraphError,
};
impl<V, HE> Hypergraph<V, HE>
where
V: VertexTrait,
HE: HyperedgeTrait,
{
/// Updates the weight of a vertex by index.
pub fn update_vertex_weight(
&mut self,
vertex_index: VertexIndex,
weight: V,
) -> Result<(), HypergraphError<V, HE>> {
let internal_index = self.get_internal_vertex(vertex_index)?;
let (previous_weight, index_set) = self
.vertices
.get_index(internal_index)
.map(|(previous_weight, index_set)| (previous_weight.to_owned(), index_set.clone()))
.ok_or(HypergraphError::InternalVertexIndexNotFound(internal_index))?;
// Return an error if the new weight is the same as the previous one.
if weight == previous_weight {
return Err(HypergraphError::VertexWeightUnchanged {
index: vertex_index,
weight,
});
}
// Return an error if the new weight is already assigned to another
// vertex.
if self.vertices.contains_key(&weight) {
return Err(HypergraphError::VertexWeightAlreadyAssigned(weight));
}
// We can't directly replace the value in the map.
// First, we need to insert the new weight, it will end up
// being at the last position.
// Since we have already checked that the new weight is not in the
// map, we can safely perform the operation without checking its output.
self.vertices.insert(weight, index_set);
// Then we use swap and remove. This will remove the previous weight
// and insert the new one at the index position of the former.
// This doesn't alter the indexing.
// See the update_hyperedge_weight method for more detailed explanation.
// Since we know that the internal index is correct, we can safely
// perform the operation without checking its output.
self.vertices.swap_remove_index(internal_index);
// Return a unit.
Ok(())
}
}