-
Notifications
You must be signed in to change notification settings - Fork 208
Add EdgeList and WeightedEdgeList return types #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d27d8b8
fe00f51
d40a14f
7483ee4
ca8db71
44742c5
a8d71c9
407bd59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,3 +109,5 @@ Return Iterator Types | |
|
|
||
| retworkx.BFSSuccessors | ||
| retworkx.NodeIndices | ||
| retworkx.EdgeList | ||
| retworkx.WeightedEdgeList | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ use petgraph::visit::{ | |
| }; | ||
|
|
||
| use super::dot_utils::build_dot; | ||
| use super::iterators::NodeIndices; | ||
| use super::iterators::{EdgeList, NodeIndices, WeightedEdgeList}; | ||
| use super::{ | ||
| is_directed_acyclic_graph, DAGHasCycle, DAGWouldCycle, NoEdgeBetweenNodes, | ||
| NoSuitableNeighbors, NodesRemoved, | ||
|
|
@@ -661,11 +661,14 @@ impl PyDiGraph { | |
| /// ``source`` and ``target`` are the node indices. | ||
| /// | ||
| /// :returns: An edge list with weights | ||
| /// :rtype: list | ||
| pub fn edge_list(&self) -> Vec<(usize, usize)> { | ||
| self.edge_references() | ||
| .map(|edge| (edge.source().index(), edge.target().index())) | ||
| .collect() | ||
| /// :rtype: EdgeList | ||
| pub fn edge_list(&self) -> EdgeList { | ||
| EdgeList { | ||
| edges: self | ||
| .edge_references() | ||
| .map(|edge| (edge.source().index(), edge.target().index())) | ||
| .collect(), | ||
| } | ||
| } | ||
|
|
||
| /// Get edge list with weights | ||
|
|
@@ -675,20 +678,20 @@ impl PyDiGraph { | |
| /// payload of the edge. | ||
| /// | ||
| /// :returns: An edge list with weights | ||
| /// :rtype: list | ||
| pub fn weighted_edge_list( | ||
| &self, | ||
| py: Python, | ||
| ) -> Vec<(usize, usize, PyObject)> { | ||
| self.edge_references() | ||
| .map(|edge| { | ||
| ( | ||
| edge.source().index(), | ||
| edge.target().index(), | ||
| edge.weight().clone_ref(py), | ||
| ) | ||
| }) | ||
| .collect() | ||
| /// :rtype: WeightedEdgeList | ||
| pub fn weighted_edge_list(&self, py: Python) -> WeightedEdgeList { | ||
| WeightedEdgeList { | ||
| edges: self | ||
| .edge_references() | ||
| .map(|edge| { | ||
| ( | ||
| edge.source().index(), | ||
| edge.target().index(), | ||
| edge.weight().clone_ref(py), | ||
| ) | ||
| }) | ||
| .collect(), | ||
| } | ||
| } | ||
|
|
||
| /// Remove a node from the graph. | ||
|
|
@@ -1160,21 +1163,22 @@ impl PyDiGraph { | |
|
|
||
| let mut edges_to_add: Vec<(usize, usize, PyObject)> = Vec::new(); | ||
| for dir in &DIRECTIONS { | ||
| let edges; | ||
| if dir == &petgraph::Direction::Outgoing { | ||
| edges = self.out_edges(u); | ||
| } else { | ||
| edges = self.in_edges(u); | ||
| } | ||
|
|
||
| for edge in edges { | ||
| for edge in self.graph.edges_directed(NodeIndex::new(u), *dir) { | ||
| let s = edge.source(); | ||
| let d = edge.target(); | ||
|
|
||
| if s == u { | ||
| edges_to_add.push((v, d, edge.weight().clone())); | ||
| if s.index() == u { | ||
| edges_to_add.push(( | ||
| v, | ||
| d.index(), | ||
| edge.weight().clone_ref(py), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, is it just because in the previous commits this hadn't been added yet, and so the type was just changed? I'd have thought some tests would fail in that case, though.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was working before because it was using The change I made here (besides switching to petgraph method
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I missed the second half of the question. The
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it!:) |
||
| )); | ||
| } else { | ||
| edges_to_add.push((s, v, edge.weight().clone())); | ||
| edges_to_add.push(( | ||
| s.index(), | ||
| v, | ||
| edge.weight().clone_ref(py), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1397,16 +1401,16 @@ impl PyDiGraph { | |
| /// | ||
| /// :returns: A list of tuples of the form: | ||
| /// ``(parent_index, node_index, edge_data)``` | ||
| /// :rtype: list | ||
| /// :rtype: WeightedEdgeList | ||
| #[text_signature = "(self, node, /)"] | ||
| pub fn in_edges(&self, node: usize) -> Vec<(usize, usize, &PyObject)> { | ||
| pub fn in_edges(&self, py: Python, node: usize) -> WeightedEdgeList { | ||
| let index = NodeIndex::new(node); | ||
| let dir = petgraph::Direction::Incoming; | ||
| let raw_edges = self.graph.edges_directed(index, dir); | ||
| let out_list: Vec<(usize, usize, &PyObject)> = raw_edges | ||
| .map(|x| (x.source().index(), node, x.weight())) | ||
| let out_list: Vec<(usize, usize, PyObject)> = raw_edges | ||
| .map(|x| (x.source().index(), node, x.weight().clone_ref(py))) | ||
| .collect(); | ||
| out_list | ||
| WeightedEdgeList { edges: out_list } | ||
| } | ||
|
|
||
| /// Get the index and edge data for all children of a node. | ||
|
|
@@ -1418,16 +1422,16 @@ impl PyDiGraph { | |
| /// | ||
| /// :returns out_edges: A list of tuples of the form: | ||
| /// ```(node_index, child_index, edge_data)``` | ||
| /// :rtype: list | ||
| /// :rtype: WeightedEdgeList | ||
| #[text_signature = "(self, node, /)"] | ||
| pub fn out_edges(&self, node: usize) -> Vec<(usize, usize, &PyObject)> { | ||
| pub fn out_edges(&self, py: Python, node: usize) -> WeightedEdgeList { | ||
| let index = NodeIndex::new(node); | ||
| let dir = petgraph::Direction::Outgoing; | ||
| let raw_edges = self.graph.edges_directed(index, dir); | ||
| let out_list: Vec<(usize, usize, &PyObject)> = raw_edges | ||
| .map(|x| (node, x.target().index(), x.weight())) | ||
| let out_list: Vec<(usize, usize, PyObject)> = raw_edges | ||
| .map(|x| (node, x.target().index(), x.weight().clone_ref(py))) | ||
| .collect(); | ||
| out_list | ||
| WeightedEdgeList { edges: out_list } | ||
| } | ||
|
|
||
| /// Add new nodes to the graph. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍