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
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.
///
/// The new weight need not be unique — multiple vertices may carry the same
/// weight value. Returns [`HypergraphError::VertexWeightUnchanged`] if
/// `weight` equals the current weight (no-op guard).
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(internal_index)
.map(|(w, s)| (*w, s.clone()))
.ok_or(HypergraphError::InternalVertexIndexNotFound(internal_index))?;
if weight == previous_weight {
return Err(HypergraphError::VertexWeightUnchanged {
index: vertex_index,
weight,
});
}
// Append the new entry at the end, then swap-remove the old position so
// that the new entry lands at `internal_index`. The BiHashMap mapping is
// unchanged — `internal_index` still maps to `vertex_index`.
self.vertices.push((weight, index_set));
self.vertices.swap_remove(internal_index);
Ok(())
}
}