New methods for adding child edges to graph.

This commit is contained in:
Stu Black 2016-04-17 14:12:37 -04:00
parent f57667f410
commit 6ce69e5d12
2 changed files with 43 additions and 2 deletions

View File

@ -182,6 +182,42 @@ impl<'a, T, S, A> MutChildList<'a, T, S, A> where T: Hash + Eq + Clone + 'a, S:
pub fn iter<'s>(&'s self) -> ChildListIter<'s, T, S, A> {
self.get_source_node().get_child_list().iter()
}
/// Adds a child edge to the vertex labeled by `child_label`. If no such
/// vertex exists, it is created and associated with the data returned by
/// `f`. Returns a mutable edge handle for the new edge, with a lifetime
/// limited to a borrow of `self`.
pub fn add_child<'s, F>(&'s mut self, child_label: T, f: F, edge_data: A)
-> MutEdge<'s, T, S, A>
where F: FnOnce() -> S {
let target_id = match self.graph.state_ids.get_or_insert(child_label) {
NamespaceInsertion::Present(id) => id,
NamespaceInsertion::New(id) => {
self.graph.add_raw_vertex(f());
id
},
};
let edge_id =
EdgeId(self.graph.add_edge_from_raw(self.id, target_id, edge_data).get_id());
MutEdge { graph: self.graph, id: edge_id, }
}
/// Adds a child edge to the vertex labeled by `child_label`. If no such
/// vertex exists, it is created and associated with the data returned by
/// `f`. Returns a mutable edge handle for the new edge.
pub fn to_add_child<F>(self, child_label: T, f: F, edge_data: A) -> MutEdge<'a, T, S, A>
where F: FnOnce() -> S {
let target_id = match self.graph.state_ids.get_or_insert(child_label) {
NamespaceInsertion::Present(id) => id,
NamespaceInsertion::New(id) => {
self.graph.add_raw_vertex(f());
id
},
};
let edge_id =
EdgeId(self.graph.add_edge_from_raw(self.id, target_id, edge_data).get_id());
MutEdge { graph: self.graph, id: edge_id, }
}
}
/// A traversible list of a vertex's incoming edges.

View File

@ -124,6 +124,12 @@ impl<T, S, A> Graph<T, S, A> where T: Hash + Eq + Clone {
make_mut_node(self, node_id)
}
fn add_edge_from_raw<'s>(&'s mut self, source: VertexId, target: VertexId, data: A)
-> MutEdge<'s, T, S, A> {
let arc_id = self.add_raw_edge(data, source, target);
make_mut_edge(self, arc_id)
}
/// Adds an edge from the vertex with state data `source` to the vertex with
/// state data `dest`. If vertices are not found for `source` or `dest`,
/// they are added, with the data provided by `source_data` and `dest_data`
@ -150,8 +156,7 @@ impl<T, S, A> Graph<T, S, A> where T: Hash + Eq + Clone {
id
},
};
let arc_id = self.add_raw_edge(edge_data, source_id, dest_id);
make_mut_edge(self, arc_id)
self.add_edge_from_raw(source_id, dest_id, edge_data)
}
/// Returns the number of vertices in the graph.