summaryrefslogtreecommitdiff
path: root/graph
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-01-22 11:49:47 +0800
committerJSDurand <mmemmew@gmail.com>2023-01-22 11:49:47 +0800
commite8ea01319b3a9032a3f4f69f65e9ca96562b87b9 (patch)
tree674e7337dce0b9433b9ddfe745b0cf82f528d3ec /graph
parent973c789dae479dd8383b0f7f9cfa5f167fdf3d38 (diff)
forest: clone correctly
Now the forest can detect if a node is packed or cloned, and correctly clones a node in those circumstances. But it still needs to be tested.
Diffstat (limited to 'graph')
-rw-r--r--graph/src/builder.rs3
-rw-r--r--graph/src/labelled/binary.rs16
2 files changed, 19 insertions, 0 deletions
diff --git a/graph/src/builder.rs b/graph/src/builder.rs
index b04e7f6..c5f9252 100644
--- a/graph/src/builder.rs
+++ b/graph/src/builder.rs
@@ -82,6 +82,9 @@ pub trait BuilderMut {
/// Add an edge from the source to the target.
fn add_edge(&mut self, source: usize, target: usize, label: Self::Label) -> Result<(), Error>;
+ /// Set the label of an existing node to a new label.
+ fn set_label(&mut self, node_id: usize, label: Self::Label) -> Result<(), Error>;
+
/// Remove an edge from the source to the target.
///
/// Since some graphs are labelled, the users are allowed to pass
diff --git a/graph/src/labelled/binary.rs b/graph/src/labelled/binary.rs
index e3e1943..201dda2 100644
--- a/graph/src/labelled/binary.rs
+++ b/graph/src/labelled/binary.rs
@@ -551,6 +551,22 @@ impl<'a, T: GraphLabel> BuilderMut for PLGBuilderMut<'a, T> {
Ok(())
}
+
+ #[inline]
+ fn set_label(&mut self, node_id: usize, label: Self::Label) -> Result<(), Error> {
+ if !self.graph.has_node(node_id) {
+ return Err(Error::IndexOutOfBounds(node_id, self.nodes_len()));
+ }
+
+ // node_id is now guaranteed to be valid.
+
+ self.graph.nodes.get_mut(node_id).unwrap().label = label;
+
+ self.graph.label_index_map.remove(&label);
+ self.graph.label_index_map.insert(label, node_id);
+
+ Ok(())
+ }
}
#[cfg(test)]