From fbc83db2be186f1472d935a57692dac69286898b Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 14 Jul 2024 15:16:40 -0400 Subject: [PATCH 1/9] Start using rustworkx_modules functions --- src/centrality.rs | 14 ++++++++ src/connectivity/mod.rs | 11 +++++- src/lib.rs | 73 ++++++++++------------------------------ src/shortest_path/mod.rs | 36 +++++++++++++++++++- 4 files changed, 77 insertions(+), 57 deletions(-) diff --git a/src/centrality.rs b/src/centrality.rs index ca055cec37..72ad734822 100644 --- a/src/centrality.rs +++ b/src/centrality.rs @@ -14,6 +14,7 @@ use std::convert::TryFrom; +use crate::declare_rustworkx_module; use crate::digraph; use crate::graph; use crate::iterators::{CentralityMapping, EdgeCentralityMapping}; @@ -29,6 +30,19 @@ use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use rustworkx_core::centrality; +declare_rustworkx_module!( + graph_betweenness_centrality, + digraph_betweenness_centrality, + graph_closeness_centrality, + digraph_closeness_centrality, + graph_edge_betweenness_centrality, + digraph_edge_betweenness_centrality, + graph_eigenvector_centrality, + digraph_eigenvector_centrality, + graph_katz_centrality, + digraph_katz_centrality +); + /// Compute the betweenness centrality of all nodes in a PyGraph. /// /// Betweenness centrality of a node :math:`v` is the sum of the diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index c3023d6d8b..808fa63d0b 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -17,7 +17,7 @@ mod johnson_simple_cycles; mod subgraphs; use super::{ - digraph, get_edge_iter_with_weights, graph, score, weight_callable, InvalidNode, NullGraph, + declare_rustworkx_module, digraph, get_edge_iter_with_weights, graph, score, weight_callable, InvalidNode, NullGraph, }; use hashbrown::{HashMap, HashSet}; @@ -46,6 +46,15 @@ use rustworkx_core::coloring::two_color; use rustworkx_core::connectivity; use rustworkx_core::dag_algo::longest_path; +declare_rustworkx_module!( + cycle_basis, + simple_cycles, + strongly_connected_components, + digraph_find_cycle, + number_connected_components, + connected_components +); + /// Return a list of cycles which form a basis for cycles of a given PyGraph /// /// A basis for cycles of a graph is a minimal collection of diff --git a/src/lib.rs b/src/lib.rs index 79f183462f..7f527330e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,6 @@ mod union; use bisimulation::*; use cartesian_product::*; -use centrality::*; use coloring::*; use connectivity::*; use dag_algo::*; @@ -436,6 +435,11 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { "JSONDeserializationError", py.get_type_bound::(), )?; + + centrality::rustworkx_module(m)?; + connectivity::rustworkx_module(m)?; + shortest_path::rustworkx_module(m)?; + m.add_wrapped(wrap_pyfunction!(bfs_successors))?; m.add_wrapped(wrap_pyfunction!(bfs_predecessors))?; m.add_wrapped(wrap_pyfunction!(graph_bfs_search))?; @@ -447,8 +451,6 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(dag_weighted_longest_path))?; m.add_wrapped(wrap_pyfunction!(dag_weighted_longest_path_length))?; m.add_wrapped(wrap_pyfunction!(transitive_reduction))?; - m.add_wrapped(wrap_pyfunction!(number_connected_components))?; - m.add_wrapped(wrap_pyfunction!(connected_components))?; m.add_wrapped(wrap_pyfunction!(is_connected))?; m.add_wrapped(wrap_pyfunction!(node_connected_component))?; m.add_wrapped(wrap_pyfunction!(number_weakly_connected_components))?; @@ -472,16 +474,6 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(descendants))?; m.add_wrapped(wrap_pyfunction!(ancestors))?; m.add_wrapped(wrap_pyfunction!(lexicographical_topological_sort))?; - m.add_wrapped(wrap_pyfunction!(graph_floyd_warshall))?; - m.add_wrapped(wrap_pyfunction!(digraph_floyd_warshall))?; - m.add_wrapped(wrap_pyfunction!(graph_floyd_warshall_numpy))?; - m.add_wrapped(wrap_pyfunction!(digraph_floyd_warshall_numpy))?; - m.add_wrapped(wrap_pyfunction!( - graph_floyd_warshall_successor_and_distance - ))?; - m.add_wrapped(wrap_pyfunction!( - digraph_floyd_warshall_successor_and_distance - ))?; m.add_wrapped(wrap_pyfunction!(collect_runs))?; m.add_wrapped(wrap_pyfunction!(collect_bicolor_runs))?; m.add_wrapped(wrap_pyfunction!(layers))?; @@ -495,46 +487,8 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(digraph_longest_simple_path))?; m.add_wrapped(wrap_pyfunction!(graph_all_simple_paths))?; m.add_wrapped(wrap_pyfunction!(digraph_all_simple_paths))?; - m.add_wrapped(wrap_pyfunction!(graph_dijkstra_shortest_paths))?; - m.add_wrapped(wrap_pyfunction!(digraph_dijkstra_shortest_paths))?; - m.add_wrapped(wrap_pyfunction!(graph_all_shortest_paths))?; - m.add_wrapped(wrap_pyfunction!(digraph_all_shortest_paths))?; m.add_wrapped(wrap_pyfunction!(graph_has_path))?; m.add_wrapped(wrap_pyfunction!(digraph_has_path))?; - m.add_wrapped(wrap_pyfunction!(graph_dijkstra_shortest_path_lengths))?; - m.add_wrapped(wrap_pyfunction!(digraph_dijkstra_shortest_path_lengths))?; - m.add_wrapped(wrap_pyfunction!(graph_bellman_ford_shortest_paths))?; - m.add_wrapped(wrap_pyfunction!(digraph_bellman_ford_shortest_paths))?; - m.add_wrapped(wrap_pyfunction!(graph_bellman_ford_shortest_path_lengths))?; - m.add_wrapped(wrap_pyfunction!(digraph_bellman_ford_shortest_path_lengths))?; - m.add_wrapped(wrap_pyfunction!(negative_edge_cycle))?; - m.add_wrapped(wrap_pyfunction!(find_negative_cycle))?; - m.add_wrapped(wrap_pyfunction!(digraph_all_pairs_dijkstra_path_lengths))?; - m.add_wrapped(wrap_pyfunction!(digraph_all_pairs_dijkstra_shortest_paths))?; - m.add_wrapped(wrap_pyfunction!(graph_all_pairs_dijkstra_path_lengths))?; - m.add_wrapped(wrap_pyfunction!(graph_all_pairs_dijkstra_shortest_paths))?; - m.add_wrapped(wrap_pyfunction!( - digraph_all_pairs_bellman_ford_path_lengths - ))?; - m.add_wrapped(wrap_pyfunction!( - digraph_all_pairs_bellman_ford_shortest_paths - ))?; - m.add_wrapped(wrap_pyfunction!(graph_all_pairs_bellman_ford_path_lengths))?; - m.add_wrapped(wrap_pyfunction!( - graph_all_pairs_bellman_ford_shortest_paths - ))?; - m.add_wrapped(wrap_pyfunction!(graph_betweenness_centrality))?; - m.add_wrapped(wrap_pyfunction!(digraph_betweenness_centrality))?; - m.add_wrapped(wrap_pyfunction!(graph_closeness_centrality))?; - m.add_wrapped(wrap_pyfunction!(digraph_closeness_centrality))?; - m.add_wrapped(wrap_pyfunction!(graph_edge_betweenness_centrality))?; - m.add_wrapped(wrap_pyfunction!(digraph_edge_betweenness_centrality))?; - m.add_wrapped(wrap_pyfunction!(graph_eigenvector_centrality))?; - m.add_wrapped(wrap_pyfunction!(digraph_eigenvector_centrality))?; - m.add_wrapped(wrap_pyfunction!(graph_katz_centrality))?; - m.add_wrapped(wrap_pyfunction!(digraph_katz_centrality))?; - m.add_wrapped(wrap_pyfunction!(graph_astar_shortest_path))?; - m.add_wrapped(wrap_pyfunction!(digraph_astar_shortest_path))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_color))?; m.add_wrapped(wrap_pyfunction!(graph_misra_gries_edge_color))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_edge_color))?; @@ -558,12 +512,8 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(directed_barabasi_albert_graph))?; m.add_wrapped(wrap_pyfunction!(directed_random_bipartite_graph))?; m.add_wrapped(wrap_pyfunction!(undirected_random_bipartite_graph))?; - m.add_wrapped(wrap_pyfunction!(cycle_basis))?; - m.add_wrapped(wrap_pyfunction!(simple_cycles))?; - m.add_wrapped(wrap_pyfunction!(strongly_connected_components))?; m.add_wrapped(wrap_pyfunction!(digraph_dfs_edges))?; m.add_wrapped(wrap_pyfunction!(graph_dfs_edges))?; - m.add_wrapped(wrap_pyfunction!(digraph_find_cycle))?; m.add_wrapped(wrap_pyfunction!(digraph_k_shortest_path_lengths))?; m.add_wrapped(wrap_pyfunction!(graph_k_shortest_path_lengths))?; m.add_wrapped(wrap_pyfunction!(is_matching))?; @@ -648,3 +598,16 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pymodule!(generators::generators))?; Ok(()) } + +#[macro_export] +macro_rules! declare_rustworkx_module { + ($($v:ident),*) => { + + pub fn rustworkx_module(m: &pyo3::Bound) -> pyo3::prelude::PyResult<()> { + $( + m.add_wrapped(pyo3::wrap_pyfunction!($v))?; + )* + Ok(()) + } + } +} diff --git a/src/shortest_path/mod.rs b/src/shortest_path/mod.rs index fb9aec4e13..e8e24d0f7f 100644 --- a/src/shortest_path/mod.rs +++ b/src/shortest_path/mod.rs @@ -19,7 +19,10 @@ mod num_shortest_path; use std::convert::TryFrom; -use crate::{digraph, edge_weights_from_callable, graph, CostFn, NegativeCycle, NoPathFound}; +use crate::{ + declare_rustworkx_module, digraph, edge_weights_from_callable, graph, CostFn, NegativeCycle, + NoPathFound, +}; use pyo3::prelude::*; use pyo3::Python; @@ -43,6 +46,37 @@ use crate::iterators::{ PathLengthMapping, PathMapping, }; +declare_rustworkx_module!( + graph_dijkstra_shortest_paths, + digraph_dijkstra_shortest_paths, + graph_all_shortest_paths, + digraph_all_shortest_paths, + graph_dijkstra_shortest_path_lengths, + digraph_dijkstra_shortest_path_lengths, + graph_bellman_ford_shortest_paths, + digraph_bellman_ford_shortest_paths, + graph_bellman_ford_shortest_path_lengths, + digraph_bellman_ford_shortest_path_lengths, + graph_floyd_warshall, + digraph_floyd_warshall, + graph_floyd_warshall_numpy, + digraph_floyd_warshall_numpy, + graph_floyd_warshall_successor_and_distance, + digraph_floyd_warshall_successor_and_distance, + digraph_all_pairs_bellman_ford_path_lengths, + digraph_all_pairs_bellman_ford_shortest_paths, + graph_all_pairs_bellman_ford_path_lengths, + graph_all_pairs_bellman_ford_shortest_paths, + digraph_all_pairs_dijkstra_path_lengths, + digraph_all_pairs_dijkstra_shortest_paths, + graph_all_pairs_dijkstra_path_lengths, + graph_all_pairs_dijkstra_shortest_paths, + graph_astar_shortest_path, + digraph_astar_shortest_path, + negative_edge_cycle, + find_negative_cycle +); + /// Find the shortest path from a node /// /// This function will generate the shortest path from a source node using From 7603a258d121b84c990db1aa325eace8a6630873 Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 14 Jul 2024 15:41:52 -0400 Subject: [PATCH 2/9] Add rustworkx_modules for layout and more --- src/connectivity/mod.rs | 30 ++++++++++++++++-- src/dag_algo/mod.rs | 19 +++++++++++- src/layout/mod.rs | 17 ++++++++++- src/lib.rs | 66 ++-------------------------------------- src/shortest_path/mod.rs | 12 +++++++- 5 files changed, 75 insertions(+), 69 deletions(-) diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index 808fa63d0b..9c7649ded8 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -17,7 +17,8 @@ mod johnson_simple_cycles; mod subgraphs; use super::{ - declare_rustworkx_module, digraph, get_edge_iter_with_weights, graph, score, weight_callable, InvalidNode, NullGraph, + declare_rustworkx_module, digraph, get_edge_iter_with_weights, graph, score, weight_callable, + InvalidNode, NullGraph, }; use hashbrown::{HashMap, HashSet}; @@ -52,7 +53,32 @@ declare_rustworkx_module!( strongly_connected_components, digraph_find_cycle, number_connected_components, - connected_components + connected_components, + node_connected_component, + is_connected, + number_weakly_connected_components, + weakly_connected_components, + is_weakly_connected, + is_semi_connected, + digraph_adjacency_matrix, + graph_adjacency_matrix, + graph_complement, + digraph_complement, + digraph_is_bipartite, + graph_is_bipartite, + graph_isolates, + digraph_isolates, + chain_decomposition, + biconnected_components, + bridges, + articulation_points, + stoer_wagner_min_cut, + graph_all_simple_paths, + digraph_all_simple_paths, + graph_all_pairs_all_simple_paths, + digraph_all_pairs_all_simple_paths, + graph_longest_simple_path, + digraph_longest_simple_path ); /// Return a list of cycles which form a basis for cycles of a given PyGraph diff --git a/src/dag_algo/mod.rs b/src/dag_algo/mod.rs index 65df6afcc7..6cdb0cb3ac 100644 --- a/src/dag_algo/mod.rs +++ b/src/dag_algo/mod.rs @@ -17,7 +17,9 @@ use rustworkx_core::dag_algo::layers as core_layers; use rustworkx_core::dictmap::InitWithHasher; use super::iterators::NodeIndices; -use crate::{digraph, DAGHasCycle, InvalidNode, RxPyResult, StablePyGraph}; +use crate::{ + declare_rustworkx_module, digraph, DAGHasCycle, InvalidNode, RxPyResult, StablePyGraph, +}; use rustworkx_core::dag_algo::collect_bicolor_runs as core_collect_bicolor_runs; use rustworkx_core::dag_algo::collect_runs as core_collect_runs; @@ -37,6 +39,21 @@ use petgraph::visit::NodeIndexable; use num_traits::{Num, Zero}; +declare_rustworkx_module!( + dag_longest_path, + dag_longest_path_length, + dag_weighted_longest_path, + dag_weighted_longest_path_length, + is_directed_acyclic_graph, + transitive_reduction, + topological_sort, + topological_generations, + lexicographical_topological_sort, + collect_runs, + collect_bicolor_runs, + layers +); + /// Calculate the longest path in a directed acyclic graph (DAG). /// /// This function interfaces with the Python `PyDiGraph` object to compute the longest path diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 72c4155e8e..40eb63020a 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -17,7 +17,7 @@ mod shell; mod spiral; mod spring; -use crate::{digraph, graph}; +use crate::{declare_rustworkx_module, digraph, graph}; use spring::Point; use hashbrown::{HashMap, HashSet}; @@ -27,6 +27,21 @@ use pyo3::Python; use crate::iterators::Pos2DMapping; +declare_rustworkx_module!( + graph_random_layout, + digraph_random_layout, + graph_bipartite_layout, + digraph_bipartite_layout, + graph_circular_layout, + digraph_circular_layout, + graph_shell_layout, + digraph_shell_layout, + graph_spiral_layout, + digraph_spiral_layout, + graph_spring_layout, + digraph_spring_layout +); + /// Position nodes using Fruchterman-Reingold force-directed algorithm. /// /// The algorithm simulates a force-directed representation of the network diff --git a/src/lib.rs b/src/lib.rs index 7f527330e9..878763740d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,14 +49,12 @@ use dag_algo::*; use graphml::*; use isomorphism::*; use json::*; -use layout::*; use line_graph::*; use link_analysis::*; use matching::*; use planar::*; use random_graph::*; -use shortest_path::*; use steiner_tree::*; use tensor_product::*; use token_swapper::*; @@ -438,7 +436,9 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { centrality::rustworkx_module(m)?; connectivity::rustworkx_module(m)?; + dag_algo::rustworkx_module(m)?; shortest_path::rustworkx_module(m)?; + layout::rustworkx_module(m)?; m.add_wrapped(wrap_pyfunction!(bfs_successors))?; m.add_wrapped(wrap_pyfunction!(bfs_predecessors))?; @@ -446,18 +446,7 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(digraph_bfs_search))?; m.add_wrapped(wrap_pyfunction!(graph_dijkstra_search))?; m.add_wrapped(wrap_pyfunction!(digraph_dijkstra_search))?; - m.add_wrapped(wrap_pyfunction!(dag_longest_path))?; - m.add_wrapped(wrap_pyfunction!(dag_longest_path_length))?; - m.add_wrapped(wrap_pyfunction!(dag_weighted_longest_path))?; - m.add_wrapped(wrap_pyfunction!(dag_weighted_longest_path_length))?; m.add_wrapped(wrap_pyfunction!(transitive_reduction))?; - m.add_wrapped(wrap_pyfunction!(is_connected))?; - m.add_wrapped(wrap_pyfunction!(node_connected_component))?; - m.add_wrapped(wrap_pyfunction!(number_weakly_connected_components))?; - m.add_wrapped(wrap_pyfunction!(weakly_connected_components))?; - m.add_wrapped(wrap_pyfunction!(is_weakly_connected))?; - m.add_wrapped(wrap_pyfunction!(is_semi_connected))?; - m.add_wrapped(wrap_pyfunction!(is_directed_acyclic_graph))?; m.add_wrapped(wrap_pyfunction!(digraph_is_isomorphic))?; m.add_wrapped(wrap_pyfunction!(graph_is_isomorphic))?; m.add_wrapped(wrap_pyfunction!(digraph_is_subgraph_isomorphic))?; @@ -469,34 +458,14 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(digraph_maximum_bisimulation))?; m.add_wrapped(wrap_pyfunction!(digraph_cartesian_product))?; m.add_wrapped(wrap_pyfunction!(graph_cartesian_product))?; - m.add_wrapped(wrap_pyfunction!(topological_sort))?; - m.add_wrapped(wrap_pyfunction!(topological_generations))?; m.add_wrapped(wrap_pyfunction!(descendants))?; m.add_wrapped(wrap_pyfunction!(ancestors))?; - m.add_wrapped(wrap_pyfunction!(lexicographical_topological_sort))?; - m.add_wrapped(wrap_pyfunction!(collect_runs))?; - m.add_wrapped(wrap_pyfunction!(collect_bicolor_runs))?; - m.add_wrapped(wrap_pyfunction!(layers))?; - m.add_wrapped(wrap_pyfunction!(graph_distance_matrix))?; - m.add_wrapped(wrap_pyfunction!(digraph_distance_matrix))?; - m.add_wrapped(wrap_pyfunction!(digraph_adjacency_matrix))?; - m.add_wrapped(wrap_pyfunction!(graph_adjacency_matrix))?; - m.add_wrapped(wrap_pyfunction!(graph_all_pairs_all_simple_paths))?; - m.add_wrapped(wrap_pyfunction!(digraph_all_pairs_all_simple_paths))?; - m.add_wrapped(wrap_pyfunction!(graph_longest_simple_path))?; - m.add_wrapped(wrap_pyfunction!(digraph_longest_simple_path))?; - m.add_wrapped(wrap_pyfunction!(graph_all_simple_paths))?; - m.add_wrapped(wrap_pyfunction!(digraph_all_simple_paths))?; - m.add_wrapped(wrap_pyfunction!(graph_has_path))?; - m.add_wrapped(wrap_pyfunction!(digraph_has_path))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_color))?; m.add_wrapped(wrap_pyfunction!(graph_misra_gries_edge_color))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_edge_color))?; m.add_wrapped(wrap_pyfunction!(graph_bipartite_edge_color))?; m.add_wrapped(wrap_pyfunction!(graph_two_color))?; m.add_wrapped(wrap_pyfunction!(digraph_two_color))?; - m.add_wrapped(wrap_pyfunction!(graph_is_bipartite))?; - m.add_wrapped(wrap_pyfunction!(digraph_is_bipartite))?; m.add_wrapped(wrap_pyfunction!(graph_line_graph))?; m.add_wrapped(wrap_pyfunction!(graph_tensor_product))?; m.add_wrapped(wrap_pyfunction!(digraph_tensor_product))?; @@ -514,8 +483,6 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(undirected_random_bipartite_graph))?; m.add_wrapped(wrap_pyfunction!(digraph_dfs_edges))?; m.add_wrapped(wrap_pyfunction!(graph_dfs_edges))?; - m.add_wrapped(wrap_pyfunction!(digraph_k_shortest_path_lengths))?; - m.add_wrapped(wrap_pyfunction!(graph_k_shortest_path_lengths))?; m.add_wrapped(wrap_pyfunction!(is_matching))?; m.add_wrapped(wrap_pyfunction!(is_maximal_matching))?; m.add_wrapped(wrap_pyfunction!(max_weight_matching))?; @@ -526,39 +493,10 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(graph_token_swapper))?; m.add_wrapped(wrap_pyfunction!(graph_core_number))?; m.add_wrapped(wrap_pyfunction!(digraph_core_number))?; - m.add_wrapped(wrap_pyfunction!(graph_complement))?; - m.add_wrapped(wrap_pyfunction!(digraph_complement))?; - m.add_wrapped(wrap_pyfunction!(graph_random_layout))?; - m.add_wrapped(wrap_pyfunction!(digraph_random_layout))?; - m.add_wrapped(wrap_pyfunction!(graph_bipartite_layout))?; - m.add_wrapped(wrap_pyfunction!(digraph_bipartite_layout))?; - m.add_wrapped(wrap_pyfunction!(graph_circular_layout))?; - m.add_wrapped(wrap_pyfunction!(digraph_circular_layout))?; - m.add_wrapped(wrap_pyfunction!(graph_shell_layout))?; - m.add_wrapped(wrap_pyfunction!(digraph_shell_layout))?; - m.add_wrapped(wrap_pyfunction!(graph_spiral_layout))?; - m.add_wrapped(wrap_pyfunction!(digraph_spiral_layout))?; - m.add_wrapped(wrap_pyfunction!(graph_spring_layout))?; - m.add_wrapped(wrap_pyfunction!(digraph_spring_layout))?; - m.add_wrapped(wrap_pyfunction!(digraph_num_shortest_paths_unweighted))?; - m.add_wrapped(wrap_pyfunction!(graph_num_shortest_paths_unweighted))?; - m.add_wrapped(wrap_pyfunction!( - digraph_unweighted_average_shortest_path_length - ))?; - m.add_wrapped(wrap_pyfunction!( - graph_unweighted_average_shortest_path_length - ))?; m.add_wrapped(wrap_pyfunction!(metric_closure))?; - m.add_wrapped(wrap_pyfunction!(stoer_wagner_min_cut))?; m.add_wrapped(wrap_pyfunction!(steiner_tree::steiner_tree))?; m.add_wrapped(wrap_pyfunction!(digraph_dfs_search))?; m.add_wrapped(wrap_pyfunction!(graph_dfs_search))?; - m.add_wrapped(wrap_pyfunction!(articulation_points))?; - m.add_wrapped(wrap_pyfunction!(bridges))?; - m.add_wrapped(wrap_pyfunction!(biconnected_components))?; - m.add_wrapped(wrap_pyfunction!(chain_decomposition))?; - m.add_wrapped(wrap_pyfunction!(graph_isolates))?; - m.add_wrapped(wrap_pyfunction!(digraph_isolates))?; m.add_wrapped(wrap_pyfunction!(connected_subgraphs))?; m.add_wrapped(wrap_pyfunction!(is_planar))?; m.add_wrapped(wrap_pyfunction!(read_graphml))?; diff --git a/src/shortest_path/mod.rs b/src/shortest_path/mod.rs index e8e24d0f7f..c570723d68 100644 --- a/src/shortest_path/mod.rs +++ b/src/shortest_path/mod.rs @@ -74,7 +74,17 @@ declare_rustworkx_module!( graph_astar_shortest_path, digraph_astar_shortest_path, negative_edge_cycle, - find_negative_cycle + find_negative_cycle, + digraph_k_shortest_path_lengths, + graph_k_shortest_path_lengths, + digraph_num_shortest_paths_unweighted, + graph_num_shortest_paths_unweighted, + digraph_unweighted_average_shortest_path_length, + graph_unweighted_average_shortest_path_length, + digraph_has_path, + graph_has_path, + graph_distance_matrix, + digraph_distance_matrix ); /// Find the shortest path from a node From c7ff3f00b3c05ffd01df79408943735a6af3025b Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 14 Jul 2024 16:00:23 -0400 Subject: [PATCH 3/9] Even more rustworkx_modules --- src/connectivity/mod.rs | 5 ++++- src/lib.rs | 44 +++++------------------------------------ src/random_graph.rs | 17 +++++++++++++++- src/steiner_tree.rs | 4 +++- src/traversal/mod.rs | 17 +++++++++++++++- src/tree.rs | 4 +++- 6 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index 9c7649ded8..7956b8af87 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -78,7 +78,10 @@ declare_rustworkx_module!( graph_all_pairs_all_simple_paths, digraph_all_pairs_all_simple_paths, graph_longest_simple_path, - digraph_longest_simple_path + digraph_longest_simple_path, + graph_core_number, + digraph_core_number, + connected_subgraphs ); /// Return a list of cycles which form a basis for cycles of a given PyGraph diff --git a/src/lib.rs b/src/lib.rs index 878763740d..950454bed2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,8 +44,6 @@ mod union; use bisimulation::*; use cartesian_product::*; use coloring::*; -use connectivity::*; -use dag_algo::*; use graphml::*; use isomorphism::*; use json::*; @@ -54,13 +52,9 @@ use link_analysis::*; use matching::*; use planar::*; -use random_graph::*; -use steiner_tree::*; use tensor_product::*; use token_swapper::*; use transitivity::*; -use traversal::*; -use tree::*; use union::*; use hashbrown::HashMap; @@ -437,16 +431,13 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { centrality::rustworkx_module(m)?; connectivity::rustworkx_module(m)?; dag_algo::rustworkx_module(m)?; - shortest_path::rustworkx_module(m)?; layout::rustworkx_module(m)?; + random_graph::rustworkx_module(m)?; + shortest_path::rustworkx_module(m)?; + steiner_tree::rustworkx_module(m)?; + traversal::rustworkx_module(m)?; + tree::rustworkx_module(m)?; - m.add_wrapped(wrap_pyfunction!(bfs_successors))?; - m.add_wrapped(wrap_pyfunction!(bfs_predecessors))?; - m.add_wrapped(wrap_pyfunction!(graph_bfs_search))?; - m.add_wrapped(wrap_pyfunction!(digraph_bfs_search))?; - m.add_wrapped(wrap_pyfunction!(graph_dijkstra_search))?; - m.add_wrapped(wrap_pyfunction!(digraph_dijkstra_search))?; - m.add_wrapped(wrap_pyfunction!(transitive_reduction))?; m.add_wrapped(wrap_pyfunction!(digraph_is_isomorphic))?; m.add_wrapped(wrap_pyfunction!(graph_is_isomorphic))?; m.add_wrapped(wrap_pyfunction!(digraph_is_subgraph_isomorphic))?; @@ -458,8 +449,6 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(digraph_maximum_bisimulation))?; m.add_wrapped(wrap_pyfunction!(digraph_cartesian_product))?; m.add_wrapped(wrap_pyfunction!(graph_cartesian_product))?; - m.add_wrapped(wrap_pyfunction!(descendants))?; - m.add_wrapped(wrap_pyfunction!(ancestors))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_color))?; m.add_wrapped(wrap_pyfunction!(graph_misra_gries_edge_color))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_edge_color))?; @@ -469,35 +458,12 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(graph_line_graph))?; m.add_wrapped(wrap_pyfunction!(graph_tensor_product))?; m.add_wrapped(wrap_pyfunction!(digraph_tensor_product))?; - m.add_wrapped(wrap_pyfunction!(directed_gnp_random_graph))?; - m.add_wrapped(wrap_pyfunction!(undirected_gnp_random_graph))?; - m.add_wrapped(wrap_pyfunction!(directed_gnm_random_graph))?; - m.add_wrapped(wrap_pyfunction!(undirected_gnm_random_graph))?; - m.add_wrapped(wrap_pyfunction!(undirected_sbm_random_graph))?; - m.add_wrapped(wrap_pyfunction!(directed_sbm_random_graph))?; - m.add_wrapped(wrap_pyfunction!(random_geometric_graph))?; - m.add_wrapped(wrap_pyfunction!(hyperbolic_random_graph))?; - m.add_wrapped(wrap_pyfunction!(barabasi_albert_graph))?; - m.add_wrapped(wrap_pyfunction!(directed_barabasi_albert_graph))?; - m.add_wrapped(wrap_pyfunction!(directed_random_bipartite_graph))?; - m.add_wrapped(wrap_pyfunction!(undirected_random_bipartite_graph))?; - m.add_wrapped(wrap_pyfunction!(digraph_dfs_edges))?; - m.add_wrapped(wrap_pyfunction!(graph_dfs_edges))?; m.add_wrapped(wrap_pyfunction!(is_matching))?; m.add_wrapped(wrap_pyfunction!(is_maximal_matching))?; m.add_wrapped(wrap_pyfunction!(max_weight_matching))?; - m.add_wrapped(wrap_pyfunction!(minimum_spanning_edges))?; - m.add_wrapped(wrap_pyfunction!(minimum_spanning_tree))?; m.add_wrapped(wrap_pyfunction!(graph_transitivity))?; m.add_wrapped(wrap_pyfunction!(digraph_transitivity))?; m.add_wrapped(wrap_pyfunction!(graph_token_swapper))?; - m.add_wrapped(wrap_pyfunction!(graph_core_number))?; - m.add_wrapped(wrap_pyfunction!(digraph_core_number))?; - m.add_wrapped(wrap_pyfunction!(metric_closure))?; - m.add_wrapped(wrap_pyfunction!(steiner_tree::steiner_tree))?; - m.add_wrapped(wrap_pyfunction!(digraph_dfs_search))?; - m.add_wrapped(wrap_pyfunction!(graph_dfs_search))?; - m.add_wrapped(wrap_pyfunction!(connected_subgraphs))?; m.add_wrapped(wrap_pyfunction!(is_planar))?; m.add_wrapped(wrap_pyfunction!(read_graphml))?; m.add_wrapped(wrap_pyfunction!(digraph_node_link_json))?; diff --git a/src/random_graph.rs b/src/random_graph.rs index 7c981c5ee8..9aadb5bdc4 100644 --- a/src/random_graph.rs +++ b/src/random_graph.rs @@ -12,7 +12,7 @@ #![allow(clippy::float_cmp)] -use crate::{digraph, graph, StablePyGraph}; +use crate::{declare_rustworkx_module, digraph, graph, StablePyGraph}; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; @@ -31,6 +31,21 @@ use rand_pcg::Pcg64; use rustworkx_core::generators as core_generators; +declare_rustworkx_module!( + directed_gnp_random_graph, + undirected_gnp_random_graph, + directed_gnm_random_graph, + undirected_gnm_random_graph, + undirected_sbm_random_graph, + directed_sbm_random_graph, + random_geometric_graph, + hyperbolic_random_graph, + barabasi_albert_graph, + directed_barabasi_albert_graph, + directed_random_bipartite_graph, + undirected_random_bipartite_graph +); + /// Return a :math:`G_{np}` directed random graph, also known as an /// Erdős-Rényi graph or a binomial graph. /// diff --git a/src/steiner_tree.rs b/src/steiner_tree.rs index 57819b9922..b1fdcb4c06 100644 --- a/src/steiner_tree.rs +++ b/src/steiner_tree.rs @@ -22,11 +22,13 @@ use pyo3::Python; use petgraph::stable_graph::{EdgeIndex, EdgeReference, NodeIndex}; use petgraph::visit::{EdgeRef, IntoEdgeReferences}; -use crate::{graph, is_valid_weight}; +use crate::{declare_rustworkx_module, graph, is_valid_weight}; use rustworkx_core::steiner_tree::metric_closure as core_metric_closure; use rustworkx_core::steiner_tree::steiner_tree as core_steiner_tree; +declare_rustworkx_module!(metric_closure, steiner_tree); + /// Return the metric closure of a graph /// /// The metric closure of a graph is the complete graph in which each edge is diff --git a/src/traversal/mod.rs b/src/traversal/mod.rs index f6ce66a767..ffd37c0c71 100644 --- a/src/traversal/mod.rs +++ b/src/traversal/mod.rs @@ -24,7 +24,7 @@ use rustworkx_core::traversal::{ descendants as core_descendants, dfs_edges, dijkstra_search, }; -use super::{digraph, graph, iterators, CostFn}; +use super::{declare_rustworkx_module, digraph, graph, iterators, CostFn}; use std::convert::TryFrom; @@ -38,6 +38,21 @@ use petgraph::graph::NodeIndex; use crate::iterators::EdgeList; +declare_rustworkx_module!( + digraph_dfs_edges, + graph_dfs_edges, + digraph_bfs_search, + graph_bfs_search, + digraph_dfs_search, + graph_dfs_search, + digraph_dijkstra_search, + graph_dijkstra_search, + bfs_successors, + bfs_predecessors, + descendants, + ancestors +); + /// Get an edge list of the tree edges from a depth-first traversal /// /// The pseudo-code for the DFS algorithm is listed below. The output diff --git a/src/tree.rs b/src/tree.rs index b9426346c2..6f41e1ab05 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -12,7 +12,7 @@ use std::cmp::Ordering; -use super::{graph, weight_callable}; +use super::{declare_rustworkx_module, graph, weight_callable}; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; @@ -27,6 +27,8 @@ use rayon::prelude::*; use crate::iterators::WeightedEdgeList; +declare_rustworkx_module!(minimum_spanning_edges, minimum_spanning_tree); + /// Find the edges in the minimum spanning tree or forest of a graph /// using Kruskal's algorithm. /// From 2f499c047ecea28ea6b90fd4fb161d6db1b9ab5b Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 14 Jul 2024 16:13:03 -0400 Subject: [PATCH 4/9] Even more rustworkx_modules (again!) --- src/bisimulation.rs | 4 +++- src/cartesian_product.rs | 4 +++- src/isomorphism/mod.rs | 11 ++++++++++- src/json/mod.rs | 9 ++++++++- src/lib.rs | 29 ++++++----------------------- src/link_analysis.rs | 4 +++- src/union.rs | 4 +++- 7 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/bisimulation.rs b/src/bisimulation.rs index 4a0525f38c..33e9d57324 100644 --- a/src/bisimulation.rs +++ b/src/bisimulation.rs @@ -8,10 +8,12 @@ use hashbrown::hash_map::Entry; use hashbrown::{HashMap, HashSet}; use crate::iterators::{IndexPartitionBlock, RelationalCoarsestPartition}; -use crate::{digraph, Directed, StablePyGraph}; +use crate::{declare_rustworkx_module, digraph, Directed, StablePyGraph}; use petgraph::graph; use petgraph::Direction::{Incoming, Outgoing}; +declare_rustworkx_module!(digraph_maximum_bisimulation); + type Block = Vec; type Counterimage = HashMap>; type NodeToBlockVec = Vec>>; diff --git a/src/cartesian_product.rs b/src/cartesian_product.rs index d938859f5c..cc3e924c08 100644 --- a/src/cartesian_product.rs +++ b/src/cartesian_product.rs @@ -11,7 +11,7 @@ // under the License. use crate::iterators::ProductNodeMap; -use crate::{digraph, graph, StablePyGraph}; +use crate::{declare_rustworkx_module, digraph, graph, StablePyGraph}; use hashbrown::HashMap; @@ -21,6 +21,8 @@ use petgraph::{algo, EdgeType}; use pyo3::prelude::*; use pyo3::Python; +declare_rustworkx_module!(graph_cartesian_product, digraph_cartesian_product); + fn cartesian_product( py: Python, first: &StablePyGraph, diff --git a/src/isomorphism/mod.rs b/src/isomorphism/mod.rs index 6e48f1f108..c0b1289281 100644 --- a/src/isomorphism/mod.rs +++ b/src/isomorphism/mod.rs @@ -14,13 +14,22 @@ mod vf2; -use crate::{digraph, graph}; +use crate::{declare_rustworkx_module, digraph, graph}; use std::cmp::Ordering; use pyo3::prelude::*; use pyo3::Python; +declare_rustworkx_module!( + graph_is_isomorphic, + digraph_is_isomorphic, + graph_is_subgraph_isomorphic, + digraph_is_subgraph_isomorphic, + digraph_vf2_mapping, + graph_vf2_mapping +); + /// Determine if 2 directed graphs are isomorphic /// /// This checks if 2 graphs are isomorphic both structurally and also diff --git a/src/json/mod.rs b/src/json/mod.rs index 4f6ee50e28..d6e39a14c0 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -15,12 +15,19 @@ mod node_link_data; use std::fs::File; use std::io::BufReader; -use crate::{digraph, graph, JSONDeserializationError, StablePyGraph}; +use crate::{declare_rustworkx_module, digraph, graph, JSONDeserializationError, StablePyGraph}; use petgraph::{algo, Directed, Undirected}; use pyo3::prelude::*; use pyo3::Python; +declare_rustworkx_module!( + digraph_node_link_json, + graph_node_link_json, + from_node_link_json_file, + parse_node_link_json +); + /// Parse a node-link format JSON file to generate a graph /// /// :param path str: The path to the JSON file to load diff --git a/src/lib.rs b/src/lib.rs index 950454bed2..e9f98959e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,21 +41,15 @@ mod traversal; mod tree; mod union; -use bisimulation::*; -use cartesian_product::*; use coloring::*; use graphml::*; -use isomorphism::*; -use json::*; use line_graph::*; -use link_analysis::*; use matching::*; use planar::*; use tensor_product::*; use token_swapper::*; use transitivity::*; -use union::*; use hashbrown::HashMap; use numpy::Complex64; @@ -428,27 +422,22 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { py.get_type_bound::(), )?; + bisimulation::rustworkx_module(m)?; + cartesian_product::rustworkx_module(m)?; centrality::rustworkx_module(m)?; connectivity::rustworkx_module(m)?; dag_algo::rustworkx_module(m)?; + isomorphism::rustworkx_module(m)?; + json::rustworkx_module(m)?; layout::rustworkx_module(m)?; + link_analysis::rustworkx_module(m)?; random_graph::rustworkx_module(m)?; shortest_path::rustworkx_module(m)?; steiner_tree::rustworkx_module(m)?; traversal::rustworkx_module(m)?; tree::rustworkx_module(m)?; + union::rustworkx_module(m)?; - m.add_wrapped(wrap_pyfunction!(digraph_is_isomorphic))?; - m.add_wrapped(wrap_pyfunction!(graph_is_isomorphic))?; - m.add_wrapped(wrap_pyfunction!(digraph_is_subgraph_isomorphic))?; - m.add_wrapped(wrap_pyfunction!(graph_is_subgraph_isomorphic))?; - m.add_wrapped(wrap_pyfunction!(digraph_vf2_mapping))?; - m.add_wrapped(wrap_pyfunction!(graph_vf2_mapping))?; - m.add_wrapped(wrap_pyfunction!(digraph_union))?; - m.add_wrapped(wrap_pyfunction!(graph_union))?; - m.add_wrapped(wrap_pyfunction!(digraph_maximum_bisimulation))?; - m.add_wrapped(wrap_pyfunction!(digraph_cartesian_product))?; - m.add_wrapped(wrap_pyfunction!(graph_cartesian_product))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_color))?; m.add_wrapped(wrap_pyfunction!(graph_misra_gries_edge_color))?; m.add_wrapped(wrap_pyfunction!(graph_greedy_edge_color))?; @@ -466,12 +455,6 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(graph_token_swapper))?; m.add_wrapped(wrap_pyfunction!(is_planar))?; m.add_wrapped(wrap_pyfunction!(read_graphml))?; - m.add_wrapped(wrap_pyfunction!(digraph_node_link_json))?; - m.add_wrapped(wrap_pyfunction!(graph_node_link_json))?; - m.add_wrapped(wrap_pyfunction!(from_node_link_json_file))?; - m.add_wrapped(wrap_pyfunction!(parse_node_link_json))?; - m.add_wrapped(wrap_pyfunction!(pagerank))?; - m.add_wrapped(wrap_pyfunction!(hits))?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/src/link_analysis.rs b/src/link_analysis.rs index c43e3db8e3..e8d8a0d52d 100644 --- a/src/link_analysis.rs +++ b/src/link_analysis.rs @@ -18,7 +18,7 @@ use pyo3::Python; use crate::digraph::PyDiGraph; use crate::iterators::CentralityMapping; -use crate::{weight_callable, FailedToConverge}; +use crate::{declare_rustworkx_module, weight_callable, FailedToConverge}; use hashbrown::HashMap; use ndarray::prelude::*; @@ -29,6 +29,8 @@ use petgraph::visit::NodeIndexable; use rustworkx_core::dictmap::*; use sprs::{CsMat, TriMat}; +declare_rustworkx_module!(pagerank, hits); + /// Computes the PageRank of the nodes in a :class:`~PyDiGraph`. /// /// For details on the PageRank, refer to: diff --git a/src/union.rs b/src/union.rs index f095cd0172..d59298e191 100644 --- a/src/union.rs +++ b/src/union.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use crate::{digraph, find_node_by_weight, graph, StablePyGraph}; +use crate::{declare_rustworkx_module, digraph, find_node_by_weight, graph, StablePyGraph}; use petgraph::stable_graph::NodeIndex; use petgraph::visit::{EdgeRef, IntoEdgeReferences, NodeIndexable}; @@ -19,6 +19,8 @@ use petgraph::{algo, EdgeType}; use pyo3::prelude::*; use pyo3::Python; +declare_rustworkx_module!(graph_union, digraph_union); + #[derive(Copy, Clone)] enum Entry { Merged(T), From 99bc71fce34463200ee7b5fada649483b56c2698 Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 14 Jul 2024 16:35:45 -0400 Subject: [PATCH 5/9] Conclude rustworkx_modules refactor --- CONTRIBUTING.md | 13 ++++++++----- src/coloring.rs | 11 ++++++++++- src/graphml.rs | 4 +++- src/lib.rs | 35 +++++++++-------------------------- src/line_graph.rs | 4 +++- src/matching/mod.rs | 3 +++ src/planar/mod.rs | 3 +++ src/tensor_product.rs | 4 +++- src/token_swapper.rs | 3 +++ src/transitivity.rs | 4 +++- 10 files changed, 48 insertions(+), 36 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5ae80b1d9e..3fba0f413c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,11 +31,12 @@ you for an overview of our simplified source tree: │ │ └── more_pure_rust_code.rs ``` -#### Module exports in `lib.rs` +#### Exporting new functions -To add new functions, you will need to export them in `lib.rs`. `lib.rs` will -import functions defined in Rust modules (see the next section), and export -them to Python using `m.add_wrapped(wrap_pyfunction!(your_new_function))?;` +To add new functions, you will need to export them in the +`declare_rustworkx_module!` statement in the Rust file you are editing. +If the function is not added to that statement, the Rust compiler +will complain about dead-code and Python will not find the function. #### Adding and changing functions in modules @@ -66,7 +67,9 @@ pub fn your_new_function( > __NOTE:__ If you create a new `your_module.rs`, remember to declare and import it in `lib.rs`: > ```rust > mod your_module; -> use your_module::*; +> // more code follows +> // inside the function named rustworkx +> `your_module::rustworkx_module(m)?;` > ``` #### Module directories: when a single file is not enough diff --git a/src/coloring.rs b/src/coloring.rs index cf844b1a22..1d3c0d898e 100644 --- a/src/coloring.rs +++ b/src/coloring.rs @@ -11,7 +11,7 @@ // under the License. use crate::GraphNotBipartite; -use crate::{digraph, graph, EdgeIndex, NodeIndex}; +use crate::{declare_rustworkx_module, digraph, graph, EdgeIndex, NodeIndex}; use pyo3::prelude::*; use pyo3::types::PyDict; @@ -27,6 +27,15 @@ use rustworkx_core::coloring::{ pub use rustworkx_core::coloring::ColoringStrategy as ColoringStrategyCore; +declare_rustworkx_module!( + graph_greedy_color, + graph_misra_gries_edge_color, + graph_greedy_edge_color, + graph_bipartite_edge_color, + graph_two_color, + digraph_two_color +); + /// Greedy coloring strategies available for `graph_greedy_color` /// /// .. list-table:: Strategy description diff --git a/src/graphml.rs b/src/graphml.rs index 6211b25ce2..47b9c58a4a 100644 --- a/src/graphml.rs +++ b/src/graphml.rs @@ -33,7 +33,9 @@ use pyo3::exceptions::PyException; use pyo3::prelude::*; use pyo3::PyErr; -use crate::{digraph::PyDiGraph, graph::PyGraph, StablePyGraph}; +use crate::{declare_rustworkx_module, digraph::PyDiGraph, graph::PyGraph, StablePyGraph}; + +declare_rustworkx_module!(read_graphml); pub enum Error { Xml(String), diff --git a/src/lib.rs b/src/lib.rs index e9f98959e8..1f260f7005 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,15 +41,7 @@ mod traversal; mod tree; mod union; -use coloring::*; -use graphml::*; -use line_graph::*; - use matching::*; -use planar::*; -use tensor_product::*; -use token_swapper::*; -use transitivity::*; use hashbrown::HashMap; use numpy::Complex64; @@ -425,36 +417,27 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { bisimulation::rustworkx_module(m)?; cartesian_product::rustworkx_module(m)?; centrality::rustworkx_module(m)?; + coloring::rustworkx_module(m)?; connectivity::rustworkx_module(m)?; dag_algo::rustworkx_module(m)?; + graphml::rustworkx_module(m)?; isomorphism::rustworkx_module(m)?; json::rustworkx_module(m)?; layout::rustworkx_module(m)?; + line_graph::rustworkx_module(m)?; link_analysis::rustworkx_module(m)?; + matching::rustworkx_module(m)?; + planar::rustworkx_module(m)?; random_graph::rustworkx_module(m)?; shortest_path::rustworkx_module(m)?; steiner_tree::rustworkx_module(m)?; + tensor_product::rustworkx_module(m)?; + token_swapper::rustworkx_module(m)?; + transitivity::rustworkx_module(m)?; traversal::rustworkx_module(m)?; tree::rustworkx_module(m)?; union::rustworkx_module(m)?; - m.add_wrapped(wrap_pyfunction!(graph_greedy_color))?; - m.add_wrapped(wrap_pyfunction!(graph_misra_gries_edge_color))?; - m.add_wrapped(wrap_pyfunction!(graph_greedy_edge_color))?; - m.add_wrapped(wrap_pyfunction!(graph_bipartite_edge_color))?; - m.add_wrapped(wrap_pyfunction!(graph_two_color))?; - m.add_wrapped(wrap_pyfunction!(digraph_two_color))?; - m.add_wrapped(wrap_pyfunction!(graph_line_graph))?; - m.add_wrapped(wrap_pyfunction!(graph_tensor_product))?; - m.add_wrapped(wrap_pyfunction!(digraph_tensor_product))?; - m.add_wrapped(wrap_pyfunction!(is_matching))?; - m.add_wrapped(wrap_pyfunction!(is_maximal_matching))?; - m.add_wrapped(wrap_pyfunction!(max_weight_matching))?; - m.add_wrapped(wrap_pyfunction!(graph_transitivity))?; - m.add_wrapped(wrap_pyfunction!(digraph_transitivity))?; - m.add_wrapped(wrap_pyfunction!(graph_token_swapper))?; - m.add_wrapped(wrap_pyfunction!(is_planar))?; - m.add_wrapped(wrap_pyfunction!(read_graphml))?; m.add_class::()?; m.add_class::()?; m.add_class::()?; @@ -481,7 +464,7 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; - m.add_class::()?; + m.add_class::()?; m.add_wrapped(wrap_pymodule!(generators::generators))?; Ok(()) } diff --git a/src/line_graph.rs b/src/line_graph.rs index 38f3ee4741..08932b66bd 100644 --- a/src/line_graph.rs +++ b/src/line_graph.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use crate::{graph, StablePyGraph}; +use crate::{declare_rustworkx_module, graph, StablePyGraph}; use hashbrown::HashMap; @@ -23,6 +23,8 @@ use rustworkx_core::line_graph::line_graph; use pyo3::prelude::*; use pyo3::Python; +declare_rustworkx_module!(graph_line_graph); + /// Constructs the line graph of a :class:`~.PyGraph` object. /// /// The line graph `L(G)` of a graph `G` represents the adjacencies between edges of G. diff --git a/src/matching/mod.rs b/src/matching/mod.rs index af9740ad94..adbf4f8842 100644 --- a/src/matching/mod.rs +++ b/src/matching/mod.rs @@ -22,8 +22,11 @@ use petgraph::graph::NodeIndex; use petgraph::prelude::*; use petgraph::visit::IntoEdgeReferences; +use crate::declare_rustworkx_module; use crate::weight_callable; +declare_rustworkx_module!(is_matching, is_maximal_matching, max_weight_matching); + /// Compute a maximum-weighted matching for a :class:`~rustworkx.PyGraph` /// /// A matching is a subset of edges in which no node occurs more than once. diff --git a/src/planar/mod.rs b/src/planar/mod.rs index 2683c871a4..0715b61bfa 100644 --- a/src/planar/mod.rs +++ b/src/planar/mod.rs @@ -10,11 +10,14 @@ // License for the specific language governing permissions and limitations // under the License. +use crate::declare_rustworkx_module; use crate::graph::PyGraph; use rustworkx_core::planar; use pyo3::prelude::*; +declare_rustworkx_module!(is_planar); + /// Check if an undirected graph is planar. /// /// A graph is planar iff it can be drawn in a plane without any edge diff --git a/src/tensor_product.rs b/src/tensor_product.rs index 134cb9b8ba..d7f586bb16 100644 --- a/src/tensor_product.rs +++ b/src/tensor_product.rs @@ -11,7 +11,7 @@ // under the License. use crate::iterators::ProductNodeMap; -use crate::{digraph, graph, StablePyGraph}; +use crate::{declare_rustworkx_module, digraph, graph, StablePyGraph}; use hashbrown::HashMap; @@ -21,6 +21,8 @@ use petgraph::{algo, EdgeType}; use pyo3::prelude::*; use pyo3::Python; +declare_rustworkx_module!(graph_tensor_product, digraph_tensor_product); + fn tensor_product( py: Python, first: &StablePyGraph, diff --git a/src/token_swapper.rs b/src/token_swapper.rs index a4b460a868..9298574ba1 100644 --- a/src/token_swapper.rs +++ b/src/token_swapper.rs @@ -10,6 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. +use crate::declare_rustworkx_module; use crate::graph; use crate::iterators::EdgeList; use crate::InvalidMapping; @@ -19,6 +20,8 @@ use petgraph::graph::NodeIndex; use pyo3::prelude::*; use rustworkx_core::token_swapper; +declare_rustworkx_module!(graph_token_swapper); + /// This module performs an approximately optimal Token Swapping algorithm /// Supports partial mappings (i.e. not-permutations) for graphs with missing tokens. /// diff --git a/src/transitivity.rs b/src/transitivity.rs index 6bcb25f94f..a602b64ae1 100644 --- a/src/transitivity.rs +++ b/src/transitivity.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use super::{digraph, graph}; +use super::{declare_rustworkx_module, digraph, graph}; use hashbrown::HashSet; use pyo3::prelude::*; @@ -18,6 +18,8 @@ use pyo3::prelude::*; use petgraph::graph::NodeIndex; use rayon::prelude::*; +declare_rustworkx_module!(digraph_transitivity, graph_transitivity); + fn _graph_triangles(graph: &graph::PyGraph, node: usize) -> (usize, usize) { let mut triangles: usize = 0; From b7ea061e2828da5df980409457d5421d301ef94d Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 14 Jul 2024 16:38:25 -0400 Subject: [PATCH 6/9] Address two warnings --- src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1f260f7005..5646f0badb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,8 +41,6 @@ mod traversal; mod tree; mod union; -use matching::*; - use hashbrown::HashMap; use numpy::Complex64; @@ -51,7 +49,6 @@ use pyo3::exceptions::PyException; use pyo3::exceptions::PyValueError; use pyo3::import_exception; use pyo3::prelude::*; -use pyo3::wrap_pyfunction; use pyo3::wrap_pymodule; use pyo3::Python; From ab14db6f482deed5d6d1d37144694faac115f240 Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Mon, 15 Jul 2024 08:29:38 -0400 Subject: [PATCH 7/9] Better naming --- CONTRIBUTING.md | 4 ++-- src/bisimulation.rs | 4 ++-- src/cartesian_product.rs | 4 ++-- src/centrality.rs | 4 ++-- src/coloring.rs | 4 ++-- src/connectivity/mod.rs | 4 ++-- src/dag_algo/mod.rs | 4 ++-- src/graphml.rs | 4 ++-- src/isomorphism/mod.rs | 4 ++-- src/json/mod.rs | 4 ++-- src/layout/mod.rs | 4 ++-- src/lib.rs | 50 ++++++++++++++++++++-------------------- src/line_graph.rs | 4 ++-- src/link_analysis.rs | 4 ++-- src/matching/mod.rs | 4 ++-- src/planar/mod.rs | 4 ++-- src/random_graph.rs | 4 ++-- src/shortest_path/mod.rs | 4 ++-- src/steiner_tree.rs | 4 ++-- src/tensor_product.rs | 4 ++-- src/token_swapper.rs | 4 ++-- src/transitivity.rs | 4 ++-- src/traversal/mod.rs | 4 ++-- src/tree.rs | 4 ++-- src/union.rs | 4 ++-- 25 files changed, 73 insertions(+), 73 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3fba0f413c..3fc383feb4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,7 +34,7 @@ you for an overview of our simplified source tree: #### Exporting new functions To add new functions, you will need to export them in the -`declare_rustworkx_module!` statement in the Rust file you are editing. +`export_rustworkx_functions!` statement in the Rust file you are editing. If the function is not added to that statement, the Rust compiler will complain about dead-code and Python will not find the function. @@ -69,7 +69,7 @@ pub fn your_new_function( > mod your_module; > // more code follows > // inside the function named rustworkx -> `your_module::rustworkx_module(m)?;` +> `your_module::register_rustworkx_functions(m)?;` > ``` #### Module directories: when a single file is not enough diff --git a/src/bisimulation.rs b/src/bisimulation.rs index 33e9d57324..181977aa58 100644 --- a/src/bisimulation.rs +++ b/src/bisimulation.rs @@ -8,11 +8,11 @@ use hashbrown::hash_map::Entry; use hashbrown::{HashMap, HashSet}; use crate::iterators::{IndexPartitionBlock, RelationalCoarsestPartition}; -use crate::{declare_rustworkx_module, digraph, Directed, StablePyGraph}; +use crate::{export_rustworkx_functions, digraph, Directed, StablePyGraph}; use petgraph::graph; use petgraph::Direction::{Incoming, Outgoing}; -declare_rustworkx_module!(digraph_maximum_bisimulation); +export_rustworkx_functions!(digraph_maximum_bisimulation); type Block = Vec; type Counterimage = HashMap>; diff --git a/src/cartesian_product.rs b/src/cartesian_product.rs index cc3e924c08..26d749f4f3 100644 --- a/src/cartesian_product.rs +++ b/src/cartesian_product.rs @@ -11,7 +11,7 @@ // under the License. use crate::iterators::ProductNodeMap; -use crate::{declare_rustworkx_module, digraph, graph, StablePyGraph}; +use crate::{export_rustworkx_functions, digraph, graph, StablePyGraph}; use hashbrown::HashMap; @@ -21,7 +21,7 @@ use petgraph::{algo, EdgeType}; use pyo3::prelude::*; use pyo3::Python; -declare_rustworkx_module!(graph_cartesian_product, digraph_cartesian_product); +export_rustworkx_functions!(graph_cartesian_product, digraph_cartesian_product); fn cartesian_product( py: Python, diff --git a/src/centrality.rs b/src/centrality.rs index 72ad734822..48b96a54d2 100644 --- a/src/centrality.rs +++ b/src/centrality.rs @@ -14,7 +14,7 @@ use std::convert::TryFrom; -use crate::declare_rustworkx_module; +use crate::export_rustworkx_functions; use crate::digraph; use crate::graph; use crate::iterators::{CentralityMapping, EdgeCentralityMapping}; @@ -30,7 +30,7 @@ use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use rustworkx_core::centrality; -declare_rustworkx_module!( +export_rustworkx_functions!( graph_betweenness_centrality, digraph_betweenness_centrality, graph_closeness_centrality, diff --git a/src/coloring.rs b/src/coloring.rs index 1d3c0d898e..f13d2080db 100644 --- a/src/coloring.rs +++ b/src/coloring.rs @@ -11,7 +11,7 @@ // under the License. use crate::GraphNotBipartite; -use crate::{declare_rustworkx_module, digraph, graph, EdgeIndex, NodeIndex}; +use crate::{export_rustworkx_functions, digraph, graph, EdgeIndex, NodeIndex}; use pyo3::prelude::*; use pyo3::types::PyDict; @@ -27,7 +27,7 @@ use rustworkx_core::coloring::{ pub use rustworkx_core::coloring::ColoringStrategy as ColoringStrategyCore; -declare_rustworkx_module!( +export_rustworkx_functions!( graph_greedy_color, graph_misra_gries_edge_color, graph_greedy_edge_color, diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index 7956b8af87..4a3235c982 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -17,7 +17,7 @@ mod johnson_simple_cycles; mod subgraphs; use super::{ - declare_rustworkx_module, digraph, get_edge_iter_with_weights, graph, score, weight_callable, + export_rustworkx_functions, digraph, get_edge_iter_with_weights, graph, score, weight_callable, InvalidNode, NullGraph, }; @@ -47,7 +47,7 @@ use rustworkx_core::coloring::two_color; use rustworkx_core::connectivity; use rustworkx_core::dag_algo::longest_path; -declare_rustworkx_module!( +export_rustworkx_functions!( cycle_basis, simple_cycles, strongly_connected_components, diff --git a/src/dag_algo/mod.rs b/src/dag_algo/mod.rs index 6cdb0cb3ac..ad4a6ecf6c 100644 --- a/src/dag_algo/mod.rs +++ b/src/dag_algo/mod.rs @@ -18,7 +18,7 @@ use rustworkx_core::dictmap::InitWithHasher; use super::iterators::NodeIndices; use crate::{ - declare_rustworkx_module, digraph, DAGHasCycle, InvalidNode, RxPyResult, StablePyGraph, + export_rustworkx_functions, digraph, DAGHasCycle, InvalidNode, RxPyResult, StablePyGraph, }; use rustworkx_core::dag_algo::collect_bicolor_runs as core_collect_bicolor_runs; @@ -39,7 +39,7 @@ use petgraph::visit::NodeIndexable; use num_traits::{Num, Zero}; -declare_rustworkx_module!( +export_rustworkx_functions!( dag_longest_path, dag_longest_path_length, dag_weighted_longest_path, diff --git a/src/graphml.rs b/src/graphml.rs index 47b9c58a4a..f90f56bb95 100644 --- a/src/graphml.rs +++ b/src/graphml.rs @@ -33,9 +33,9 @@ use pyo3::exceptions::PyException; use pyo3::prelude::*; use pyo3::PyErr; -use crate::{declare_rustworkx_module, digraph::PyDiGraph, graph::PyGraph, StablePyGraph}; +use crate::{export_rustworkx_functions, digraph::PyDiGraph, graph::PyGraph, StablePyGraph}; -declare_rustworkx_module!(read_graphml); +export_rustworkx_functions!(read_graphml); pub enum Error { Xml(String), diff --git a/src/isomorphism/mod.rs b/src/isomorphism/mod.rs index c0b1289281..d10b73bb9b 100644 --- a/src/isomorphism/mod.rs +++ b/src/isomorphism/mod.rs @@ -14,14 +14,14 @@ mod vf2; -use crate::{declare_rustworkx_module, digraph, graph}; +use crate::{export_rustworkx_functions, digraph, graph}; use std::cmp::Ordering; use pyo3::prelude::*; use pyo3::Python; -declare_rustworkx_module!( +export_rustworkx_functions!( graph_is_isomorphic, digraph_is_isomorphic, graph_is_subgraph_isomorphic, diff --git a/src/json/mod.rs b/src/json/mod.rs index d6e39a14c0..370c252b1c 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -15,13 +15,13 @@ mod node_link_data; use std::fs::File; use std::io::BufReader; -use crate::{declare_rustworkx_module, digraph, graph, JSONDeserializationError, StablePyGraph}; +use crate::{export_rustworkx_functions, digraph, graph, JSONDeserializationError, StablePyGraph}; use petgraph::{algo, Directed, Undirected}; use pyo3::prelude::*; use pyo3::Python; -declare_rustworkx_module!( +export_rustworkx_functions!( digraph_node_link_json, graph_node_link_json, from_node_link_json_file, diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 40eb63020a..3c672602eb 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -17,7 +17,7 @@ mod shell; mod spiral; mod spring; -use crate::{declare_rustworkx_module, digraph, graph}; +use crate::{export_rustworkx_functions, digraph, graph}; use spring::Point; use hashbrown::{HashMap, HashSet}; @@ -27,7 +27,7 @@ use pyo3::Python; use crate::iterators::Pos2DMapping; -declare_rustworkx_module!( +export_rustworkx_functions!( graph_random_layout, digraph_random_layout, graph_bipartite_layout, diff --git a/src/lib.rs b/src/lib.rs index 5646f0badb..fe840053e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -411,29 +411,29 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { py.get_type_bound::(), )?; - bisimulation::rustworkx_module(m)?; - cartesian_product::rustworkx_module(m)?; - centrality::rustworkx_module(m)?; - coloring::rustworkx_module(m)?; - connectivity::rustworkx_module(m)?; - dag_algo::rustworkx_module(m)?; - graphml::rustworkx_module(m)?; - isomorphism::rustworkx_module(m)?; - json::rustworkx_module(m)?; - layout::rustworkx_module(m)?; - line_graph::rustworkx_module(m)?; - link_analysis::rustworkx_module(m)?; - matching::rustworkx_module(m)?; - planar::rustworkx_module(m)?; - random_graph::rustworkx_module(m)?; - shortest_path::rustworkx_module(m)?; - steiner_tree::rustworkx_module(m)?; - tensor_product::rustworkx_module(m)?; - token_swapper::rustworkx_module(m)?; - transitivity::rustworkx_module(m)?; - traversal::rustworkx_module(m)?; - tree::rustworkx_module(m)?; - union::rustworkx_module(m)?; + bisimulation::register_rustworkx_functions(m)?; + cartesian_product::register_rustworkx_functions(m)?; + centrality::register_rustworkx_functions(m)?; + coloring::register_rustworkx_functions(m)?; + connectivity::register_rustworkx_functions(m)?; + dag_algo::register_rustworkx_functions(m)?; + graphml::register_rustworkx_functions(m)?; + isomorphism::register_rustworkx_functions(m)?; + json::register_rustworkx_functions(m)?; + layout::register_rustworkx_functions(m)?; + line_graph::register_rustworkx_functions(m)?; + link_analysis::register_rustworkx_functions(m)?; + matching::register_rustworkx_functions(m)?; + planar::register_rustworkx_functions(m)?; + random_graph::register_rustworkx_functions(m)?; + shortest_path::register_rustworkx_functions(m)?; + steiner_tree::register_rustworkx_functions(m)?; + tensor_product::register_rustworkx_functions(m)?; + token_swapper::register_rustworkx_functions(m)?; + transitivity::register_rustworkx_functions(m)?; + traversal::register_rustworkx_functions(m)?; + tree::register_rustworkx_functions(m)?; + union::register_rustworkx_functions(m)?; m.add_class::()?; m.add_class::()?; @@ -467,10 +467,10 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { } #[macro_export] -macro_rules! declare_rustworkx_module { +macro_rules! export_rustworkx_functions { ($($v:ident),*) => { - pub fn rustworkx_module(m: &pyo3::Bound) -> pyo3::prelude::PyResult<()> { + pub fn register_rustworkx_functions(m: &pyo3::Bound) -> pyo3::prelude::PyResult<()> { $( m.add_wrapped(pyo3::wrap_pyfunction!($v))?; )* diff --git a/src/line_graph.rs b/src/line_graph.rs index 08932b66bd..18ad7be3ec 100644 --- a/src/line_graph.rs +++ b/src/line_graph.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use crate::{declare_rustworkx_module, graph, StablePyGraph}; +use crate::{export_rustworkx_functions, graph, StablePyGraph}; use hashbrown::HashMap; @@ -23,7 +23,7 @@ use rustworkx_core::line_graph::line_graph; use pyo3::prelude::*; use pyo3::Python; -declare_rustworkx_module!(graph_line_graph); +export_rustworkx_functions!(graph_line_graph); /// Constructs the line graph of a :class:`~.PyGraph` object. /// diff --git a/src/link_analysis.rs b/src/link_analysis.rs index e8d8a0d52d..44632f0063 100644 --- a/src/link_analysis.rs +++ b/src/link_analysis.rs @@ -18,7 +18,7 @@ use pyo3::Python; use crate::digraph::PyDiGraph; use crate::iterators::CentralityMapping; -use crate::{declare_rustworkx_module, weight_callable, FailedToConverge}; +use crate::{export_rustworkx_functions, weight_callable, FailedToConverge}; use hashbrown::HashMap; use ndarray::prelude::*; @@ -29,7 +29,7 @@ use petgraph::visit::NodeIndexable; use rustworkx_core::dictmap::*; use sprs::{CsMat, TriMat}; -declare_rustworkx_module!(pagerank, hits); +export_rustworkx_functions!(pagerank, hits); /// Computes the PageRank of the nodes in a :class:`~PyDiGraph`. /// diff --git a/src/matching/mod.rs b/src/matching/mod.rs index adbf4f8842..01ab90b4bb 100644 --- a/src/matching/mod.rs +++ b/src/matching/mod.rs @@ -22,10 +22,10 @@ use petgraph::graph::NodeIndex; use petgraph::prelude::*; use petgraph::visit::IntoEdgeReferences; -use crate::declare_rustworkx_module; +use crate::export_rustworkx_functions; use crate::weight_callable; -declare_rustworkx_module!(is_matching, is_maximal_matching, max_weight_matching); +export_rustworkx_functions!(is_matching, is_maximal_matching, max_weight_matching); /// Compute a maximum-weighted matching for a :class:`~rustworkx.PyGraph` /// diff --git a/src/planar/mod.rs b/src/planar/mod.rs index 0715b61bfa..685d5afe8f 100644 --- a/src/planar/mod.rs +++ b/src/planar/mod.rs @@ -10,13 +10,13 @@ // License for the specific language governing permissions and limitations // under the License. -use crate::declare_rustworkx_module; +use crate::export_rustworkx_functions; use crate::graph::PyGraph; use rustworkx_core::planar; use pyo3::prelude::*; -declare_rustworkx_module!(is_planar); +export_rustworkx_functions!(is_planar); /// Check if an undirected graph is planar. /// diff --git a/src/random_graph.rs b/src/random_graph.rs index 9aadb5bdc4..439e3157a6 100644 --- a/src/random_graph.rs +++ b/src/random_graph.rs @@ -12,7 +12,7 @@ #![allow(clippy::float_cmp)] -use crate::{declare_rustworkx_module, digraph, graph, StablePyGraph}; +use crate::{export_rustworkx_functions, digraph, graph, StablePyGraph}; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; @@ -31,7 +31,7 @@ use rand_pcg::Pcg64; use rustworkx_core::generators as core_generators; -declare_rustworkx_module!( +export_rustworkx_functions!( directed_gnp_random_graph, undirected_gnp_random_graph, directed_gnm_random_graph, diff --git a/src/shortest_path/mod.rs b/src/shortest_path/mod.rs index c570723d68..9a841c2cab 100644 --- a/src/shortest_path/mod.rs +++ b/src/shortest_path/mod.rs @@ -20,7 +20,7 @@ mod num_shortest_path; use std::convert::TryFrom; use crate::{ - declare_rustworkx_module, digraph, edge_weights_from_callable, graph, CostFn, NegativeCycle, + export_rustworkx_functions, digraph, edge_weights_from_callable, graph, CostFn, NegativeCycle, NoPathFound, }; @@ -46,7 +46,7 @@ use crate::iterators::{ PathLengthMapping, PathMapping, }; -declare_rustworkx_module!( +export_rustworkx_functions!( graph_dijkstra_shortest_paths, digraph_dijkstra_shortest_paths, graph_all_shortest_paths, diff --git a/src/steiner_tree.rs b/src/steiner_tree.rs index b1fdcb4c06..abffdcc34c 100644 --- a/src/steiner_tree.rs +++ b/src/steiner_tree.rs @@ -22,12 +22,12 @@ use pyo3::Python; use petgraph::stable_graph::{EdgeIndex, EdgeReference, NodeIndex}; use petgraph::visit::{EdgeRef, IntoEdgeReferences}; -use crate::{declare_rustworkx_module, graph, is_valid_weight}; +use crate::{export_rustworkx_functions, graph, is_valid_weight}; use rustworkx_core::steiner_tree::metric_closure as core_metric_closure; use rustworkx_core::steiner_tree::steiner_tree as core_steiner_tree; -declare_rustworkx_module!(metric_closure, steiner_tree); +export_rustworkx_functions!(metric_closure, steiner_tree); /// Return the metric closure of a graph /// diff --git a/src/tensor_product.rs b/src/tensor_product.rs index d7f586bb16..5452978b19 100644 --- a/src/tensor_product.rs +++ b/src/tensor_product.rs @@ -11,7 +11,7 @@ // under the License. use crate::iterators::ProductNodeMap; -use crate::{declare_rustworkx_module, digraph, graph, StablePyGraph}; +use crate::{export_rustworkx_functions, digraph, graph, StablePyGraph}; use hashbrown::HashMap; @@ -21,7 +21,7 @@ use petgraph::{algo, EdgeType}; use pyo3::prelude::*; use pyo3::Python; -declare_rustworkx_module!(graph_tensor_product, digraph_tensor_product); +export_rustworkx_functions!(graph_tensor_product, digraph_tensor_product); fn tensor_product( py: Python, diff --git a/src/token_swapper.rs b/src/token_swapper.rs index 9298574ba1..720438b31d 100644 --- a/src/token_swapper.rs +++ b/src/token_swapper.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use crate::declare_rustworkx_module; +use crate::export_rustworkx_functions; use crate::graph; use crate::iterators::EdgeList; use crate::InvalidMapping; @@ -20,7 +20,7 @@ use petgraph::graph::NodeIndex; use pyo3::prelude::*; use rustworkx_core::token_swapper; -declare_rustworkx_module!(graph_token_swapper); +export_rustworkx_functions!(graph_token_swapper); /// This module performs an approximately optimal Token Swapping algorithm /// Supports partial mappings (i.e. not-permutations) for graphs with missing tokens. diff --git a/src/transitivity.rs b/src/transitivity.rs index a602b64ae1..5bc4345a4d 100644 --- a/src/transitivity.rs +++ b/src/transitivity.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use super::{declare_rustworkx_module, digraph, graph}; +use super::{export_rustworkx_functions, digraph, graph}; use hashbrown::HashSet; use pyo3::prelude::*; @@ -18,7 +18,7 @@ use pyo3::prelude::*; use petgraph::graph::NodeIndex; use rayon::prelude::*; -declare_rustworkx_module!(digraph_transitivity, graph_transitivity); +export_rustworkx_functions!(digraph_transitivity, graph_transitivity); fn _graph_triangles(graph: &graph::PyGraph, node: usize) -> (usize, usize) { let mut triangles: usize = 0; diff --git a/src/traversal/mod.rs b/src/traversal/mod.rs index ffd37c0c71..145c1dc105 100644 --- a/src/traversal/mod.rs +++ b/src/traversal/mod.rs @@ -24,7 +24,7 @@ use rustworkx_core::traversal::{ descendants as core_descendants, dfs_edges, dijkstra_search, }; -use super::{declare_rustworkx_module, digraph, graph, iterators, CostFn}; +use super::{export_rustworkx_functions, digraph, graph, iterators, CostFn}; use std::convert::TryFrom; @@ -38,7 +38,7 @@ use petgraph::graph::NodeIndex; use crate::iterators::EdgeList; -declare_rustworkx_module!( +export_rustworkx_functions!( digraph_dfs_edges, graph_dfs_edges, digraph_bfs_search, diff --git a/src/tree.rs b/src/tree.rs index 6f41e1ab05..2f69a917c5 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -12,7 +12,7 @@ use std::cmp::Ordering; -use super::{declare_rustworkx_module, graph, weight_callable}; +use super::{export_rustworkx_functions, graph, weight_callable}; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; @@ -27,7 +27,7 @@ use rayon::prelude::*; use crate::iterators::WeightedEdgeList; -declare_rustworkx_module!(minimum_spanning_edges, minimum_spanning_tree); +export_rustworkx_functions!(minimum_spanning_edges, minimum_spanning_tree); /// Find the edges in the minimum spanning tree or forest of a graph /// using Kruskal's algorithm. diff --git a/src/union.rs b/src/union.rs index d59298e191..2094457e84 100644 --- a/src/union.rs +++ b/src/union.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use crate::{declare_rustworkx_module, digraph, find_node_by_weight, graph, StablePyGraph}; +use crate::{export_rustworkx_functions, digraph, find_node_by_weight, graph, StablePyGraph}; use petgraph::stable_graph::NodeIndex; use petgraph::visit::{EdgeRef, IntoEdgeReferences, NodeIndexable}; @@ -19,7 +19,7 @@ use petgraph::{algo, EdgeType}; use pyo3::prelude::*; use pyo3::Python; -declare_rustworkx_module!(graph_union, digraph_union); +export_rustworkx_functions!(graph_union, digraph_union); #[derive(Copy, Clone)] enum Entry { From 86018b477a6e23db388622c8d75ae779ef2c9033 Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Mon, 15 Jul 2024 08:30:21 -0400 Subject: [PATCH 8/9] Fix CONTRIBUTING.md --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3fc383feb4..a580efed86 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -69,7 +69,7 @@ pub fn your_new_function( > mod your_module; > // more code follows > // inside the function named rustworkx -> `your_module::register_rustworkx_functions(m)?;` +> your_module::register_rustworkx_functions(m)?; > ``` #### Module directories: when a single file is not enough From 68dd946433773c1cd0fcda5e0c19f666d7dd2930 Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Mon, 15 Jul 2024 08:35:18 -0400 Subject: [PATCH 9/9] Cargo fmt --- src/bisimulation.rs | 2 +- src/cartesian_product.rs | 2 +- src/centrality.rs | 2 +- src/coloring.rs | 2 +- src/connectivity/mod.rs | 2 +- src/dag_algo/mod.rs | 2 +- src/graphml.rs | 2 +- src/isomorphism/mod.rs | 2 +- src/json/mod.rs | 2 +- src/layout/mod.rs | 2 +- src/random_graph.rs | 2 +- src/shortest_path/mod.rs | 2 +- src/tensor_product.rs | 2 +- src/transitivity.rs | 2 +- src/traversal/mod.rs | 2 +- src/union.rs | 2 +- 16 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/bisimulation.rs b/src/bisimulation.rs index 181977aa58..1a10ede907 100644 --- a/src/bisimulation.rs +++ b/src/bisimulation.rs @@ -8,7 +8,7 @@ use hashbrown::hash_map::Entry; use hashbrown::{HashMap, HashSet}; use crate::iterators::{IndexPartitionBlock, RelationalCoarsestPartition}; -use crate::{export_rustworkx_functions, digraph, Directed, StablePyGraph}; +use crate::{digraph, export_rustworkx_functions, Directed, StablePyGraph}; use petgraph::graph; use petgraph::Direction::{Incoming, Outgoing}; diff --git a/src/cartesian_product.rs b/src/cartesian_product.rs index 26d749f4f3..37525e8cc5 100644 --- a/src/cartesian_product.rs +++ b/src/cartesian_product.rs @@ -11,7 +11,7 @@ // under the License. use crate::iterators::ProductNodeMap; -use crate::{export_rustworkx_functions, digraph, graph, StablePyGraph}; +use crate::{digraph, export_rustworkx_functions, graph, StablePyGraph}; use hashbrown::HashMap; diff --git a/src/centrality.rs b/src/centrality.rs index 48b96a54d2..b5ab15edb6 100644 --- a/src/centrality.rs +++ b/src/centrality.rs @@ -14,8 +14,8 @@ use std::convert::TryFrom; -use crate::export_rustworkx_functions; use crate::digraph; +use crate::export_rustworkx_functions; use crate::graph; use crate::iterators::{CentralityMapping, EdgeCentralityMapping}; use crate::CostFn; diff --git a/src/coloring.rs b/src/coloring.rs index f13d2080db..fe063f9133 100644 --- a/src/coloring.rs +++ b/src/coloring.rs @@ -11,7 +11,7 @@ // under the License. use crate::GraphNotBipartite; -use crate::{export_rustworkx_functions, digraph, graph, EdgeIndex, NodeIndex}; +use crate::{digraph, export_rustworkx_functions, graph, EdgeIndex, NodeIndex}; use pyo3::prelude::*; use pyo3::types::PyDict; diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index 4a3235c982..87cbffd02e 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -17,7 +17,7 @@ mod johnson_simple_cycles; mod subgraphs; use super::{ - export_rustworkx_functions, digraph, get_edge_iter_with_weights, graph, score, weight_callable, + digraph, export_rustworkx_functions, get_edge_iter_with_weights, graph, score, weight_callable, InvalidNode, NullGraph, }; diff --git a/src/dag_algo/mod.rs b/src/dag_algo/mod.rs index ad4a6ecf6c..d1669715c8 100644 --- a/src/dag_algo/mod.rs +++ b/src/dag_algo/mod.rs @@ -18,7 +18,7 @@ use rustworkx_core::dictmap::InitWithHasher; use super::iterators::NodeIndices; use crate::{ - export_rustworkx_functions, digraph, DAGHasCycle, InvalidNode, RxPyResult, StablePyGraph, + digraph, export_rustworkx_functions, DAGHasCycle, InvalidNode, RxPyResult, StablePyGraph, }; use rustworkx_core::dag_algo::collect_bicolor_runs as core_collect_bicolor_runs; diff --git a/src/graphml.rs b/src/graphml.rs index f90f56bb95..c9b210d079 100644 --- a/src/graphml.rs +++ b/src/graphml.rs @@ -33,7 +33,7 @@ use pyo3::exceptions::PyException; use pyo3::prelude::*; use pyo3::PyErr; -use crate::{export_rustworkx_functions, digraph::PyDiGraph, graph::PyGraph, StablePyGraph}; +use crate::{digraph::PyDiGraph, export_rustworkx_functions, graph::PyGraph, StablePyGraph}; export_rustworkx_functions!(read_graphml); diff --git a/src/isomorphism/mod.rs b/src/isomorphism/mod.rs index d10b73bb9b..9f0f860b88 100644 --- a/src/isomorphism/mod.rs +++ b/src/isomorphism/mod.rs @@ -14,7 +14,7 @@ mod vf2; -use crate::{export_rustworkx_functions, digraph, graph}; +use crate::{digraph, export_rustworkx_functions, graph}; use std::cmp::Ordering; diff --git a/src/json/mod.rs b/src/json/mod.rs index 370c252b1c..822544fa32 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -15,7 +15,7 @@ mod node_link_data; use std::fs::File; use std::io::BufReader; -use crate::{export_rustworkx_functions, digraph, graph, JSONDeserializationError, StablePyGraph}; +use crate::{digraph, export_rustworkx_functions, graph, JSONDeserializationError, StablePyGraph}; use petgraph::{algo, Directed, Undirected}; use pyo3::prelude::*; diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 3c672602eb..50e8520257 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -17,7 +17,7 @@ mod shell; mod spiral; mod spring; -use crate::{export_rustworkx_functions, digraph, graph}; +use crate::{digraph, export_rustworkx_functions, graph}; use spring::Point; use hashbrown::{HashMap, HashSet}; diff --git a/src/random_graph.rs b/src/random_graph.rs index 439e3157a6..12d45763e4 100644 --- a/src/random_graph.rs +++ b/src/random_graph.rs @@ -12,7 +12,7 @@ #![allow(clippy::float_cmp)] -use crate::{export_rustworkx_functions, digraph, graph, StablePyGraph}; +use crate::{digraph, export_rustworkx_functions, graph, StablePyGraph}; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; diff --git a/src/shortest_path/mod.rs b/src/shortest_path/mod.rs index 9a841c2cab..2c833a4cfb 100644 --- a/src/shortest_path/mod.rs +++ b/src/shortest_path/mod.rs @@ -20,7 +20,7 @@ mod num_shortest_path; use std::convert::TryFrom; use crate::{ - export_rustworkx_functions, digraph, edge_weights_from_callable, graph, CostFn, NegativeCycle, + digraph, edge_weights_from_callable, export_rustworkx_functions, graph, CostFn, NegativeCycle, NoPathFound, }; diff --git a/src/tensor_product.rs b/src/tensor_product.rs index 5452978b19..1b2c68f367 100644 --- a/src/tensor_product.rs +++ b/src/tensor_product.rs @@ -11,7 +11,7 @@ // under the License. use crate::iterators::ProductNodeMap; -use crate::{export_rustworkx_functions, digraph, graph, StablePyGraph}; +use crate::{digraph, export_rustworkx_functions, graph, StablePyGraph}; use hashbrown::HashMap; diff --git a/src/transitivity.rs b/src/transitivity.rs index 5bc4345a4d..576e126dbe 100644 --- a/src/transitivity.rs +++ b/src/transitivity.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use super::{export_rustworkx_functions, digraph, graph}; +use super::{digraph, export_rustworkx_functions, graph}; use hashbrown::HashSet; use pyo3::prelude::*; diff --git a/src/traversal/mod.rs b/src/traversal/mod.rs index 145c1dc105..61c9c2d3a8 100644 --- a/src/traversal/mod.rs +++ b/src/traversal/mod.rs @@ -24,7 +24,7 @@ use rustworkx_core::traversal::{ descendants as core_descendants, dfs_edges, dijkstra_search, }; -use super::{export_rustworkx_functions, digraph, graph, iterators, CostFn}; +use super::{digraph, export_rustworkx_functions, graph, iterators, CostFn}; use std::convert::TryFrom; diff --git a/src/union.rs b/src/union.rs index 2094457e84..3b67a13712 100644 --- a/src/union.rs +++ b/src/union.rs @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations // under the License. -use crate::{export_rustworkx_functions, digraph, find_node_by_weight, graph, StablePyGraph}; +use crate::{digraph, export_rustworkx_functions, find_node_by_weight, graph, StablePyGraph}; use petgraph::stable_graph::NodeIndex; use petgraph::visit::{EdgeRef, IntoEdgeReferences, NodeIndexable};