Allow setting value of a node without touching its connections.

This commit is contained in:
James Pace 2026-06-20 20:12:09 -04:00
parent 9873b6bd52
commit b078e13cb6
2 changed files with 14 additions and 0 deletions

View File

@ -51,6 +51,16 @@ impl<NodeValueT: NodeValue> Graph<NodeValueT> {
Ok(new_node_key)
}
/// Replace the value of `key` with value `value`.
/// `key` must already exist in the graph, and no connections will be modified.
pub fn replace_value_of(&mut self, key: &Key, value: NodeValueT) -> Result<(), GraphError> {
if let Some(node) = self.nodes.get_mut(*key) {
node.set_value(value);
return Ok(());
}
return Err(GraphError::from_msg("Can't set value of invalid key."));
}
/// Get the value of key `key` if the key is valid.
pub fn value_of(&self, key: &Key) -> Result<NodeValueT, GraphError> {
if key >= &self.nodes.len() {

View File

@ -46,4 +46,8 @@ impl<NodeValueT: NodeValue> Node<NodeValueT> {
pub fn value(&self) -> NodeValueT {
self.value.clone()
}
pub fn set_value(&mut self, value: NodeValueT) {
self.value = value;
}
}