-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify the api layer, type annotations, the binder and the rust core
- Loading branch information
1 parent
f2238fd
commit 8001f6e
Showing
8 changed files
with
183 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use rustworkx_core::connectivity::minimal_cycle_basis; | ||
|
||
use pyo3::exceptions::PyIndexError; | ||
use pyo3::prelude::*; | ||
use pyo3::Python; | ||
|
||
use petgraph::graph::NodeIndex; | ||
use petgraph::prelude::*; | ||
use petgraph::visit::EdgeIndexable; | ||
use petgraph::EdgeType; | ||
|
||
use crate::{CostFn, StablePyGraph}; | ||
|
||
pub fn minimum_cycle_basis_map<Ty: EdgeType + Sync>( | ||
py: Python, | ||
graph: &StablePyGraph<Ty>, | ||
edge_cost_fn: PyObject, | ||
) -> PyResult<Vec<Vec<NodeIndex>>> { | ||
if graph.node_count() == 0 { | ||
return Ok(vec![]); | ||
} else if graph.edge_count() == 0 { | ||
return Ok(vec![]); | ||
} | ||
let edge_cost_callable = CostFn::from(edge_cost_fn); | ||
let mut edge_weights: Vec<Option<f64>> = Vec::with_capacity(graph.edge_bound()); | ||
for index in 0..=graph.edge_bound() { | ||
let raw_weight = graph.edge_weight(EdgeIndex::new(index)); | ||
match raw_weight { | ||
Some(weight) => edge_weights.push(Some(edge_cost_callable.call(py, weight)?)), | ||
None => edge_weights.push(None), | ||
}; | ||
} | ||
let edge_cost = |e: EdgeIndex| -> PyResult<f64> { | ||
match edge_weights[e.index()] { | ||
Some(weight) => Ok(weight), | ||
None => Err(PyIndexError::new_err("No edge found for index")), | ||
} | ||
}; | ||
let cycle_basis = minimal_cycle_basis(graph, |e| edge_cost(e.id())).unwrap(); | ||
Ok(cycle_basis) | ||
} |
Oops, something went wrong.