Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/petgraph/src/graph_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use core::{
};

use fixedbitset::FixedBitSet;
#[cfg(feature = "rayon")]
use rayon::prelude::*;

use crate::{
Directed, Direction, EdgeType, Incoming, IntoWeightedEdge, Outgoing, Undirected,
Expand Down Expand Up @@ -1183,6 +1185,26 @@ where
}
}

/// Return a parallel iterator yielding mutable access to all node weights.
#[cfg(feature = "rayon")]
pub fn par_node_weights_mut(&mut self) -> impl ParallelIterator<Item = &mut N>
where
N: Send + Sync,
Ix: Send + Sync,
{
self.nodes.par_iter_mut().map(|x| &mut x.weight)
}

/// Return a parallel iterator yielding immutable access to all node weights.
#[cfg(feature = "rayon")]
pub fn par_node_weights(&self) -> impl ParallelIterator<Item = &N>
where
N: Send + Sync,
Ix: Send + Sync,
{
self.nodes.par_iter().map(|x| &x.weight)
}

/// Return an iterator over the edge indices of the graph
pub fn edge_indices(&self) -> EdgeIndices<Ix> {
EdgeIndices {
Expand Down Expand Up @@ -1220,6 +1242,26 @@ where
}
}

/// Return a parallel iterator yielding mutable access to all edge weights.
#[cfg(feature = "rayon")]
pub fn par_edge_weights_mut(&mut self) -> impl ParallelIterator<Item = &mut E>
where
E: Send + Sync,
Ix: Send + Sync,
{
self.edges.par_iter_mut().map(|x| &mut x.weight)
}

/// Return an iterator yielding immutable access to all node weights.
#[cfg(feature = "rayon")]
pub fn par_edge_weights(&self) -> impl ParallelIterator<Item = &E>
where
E: Send + Sync,
Ix: Send + Sync,
{
self.edges.par_iter().map(|x| &x.weight)
}

// Remaining methods are of the more internal flavour, read-only access to
// the data structure's internals.

Expand Down
50 changes: 50 additions & 0 deletions crates/petgraph/src/graph_impl/stable_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use core::{
};

use fixedbitset::FixedBitSet;
#[cfg(feature = "rayon")]
use rayon::prelude::*;

use super::{DIRECTIONS, Edge, Frozen, GraphError, Node, Pair, index_twice};
// reexport those things that are shared with Graph
Expand Down Expand Up @@ -681,6 +683,30 @@ where
.filter_map(|maybe_node| maybe_node.as_mut())
}

/// Return a parallel iterator yielding immutable access to all node weights.
#[cfg(feature = "rayon")]
pub fn par_node_weights(&self) -> impl ParallelIterator<Item = &N>
where
N: Send + Sync,
Ix: Send + Sync,
{
self.g
.par_node_weights()
.filter_map(|maybe_node| maybe_node.as_ref())
}

/// Return a parallel iterator yielding mutable access to all node weights.
#[cfg(feature = "rayon")]
pub fn par_node_weights_mut(&mut self) -> impl ParallelIterator<Item = &mut N>
where
N: Send + Sync,
Ix: Send + Sync,
{
self.g
.par_node_weights_mut()
.filter_map(|maybe_node| maybe_node.as_mut())
}

/// Return an iterator over the node indices of the graph
pub fn node_indices(&self) -> NodeIndices<'_, N, Ix> {
NodeIndices {
Expand Down Expand Up @@ -728,6 +754,30 @@ where
.filter_map(|maybe_edge| maybe_edge.as_mut())
}

/// Return a parallel iterator yielding immutable access to all edge weights.
#[cfg(feature = "rayon")]
pub fn par_edge_weights(&self) -> impl ParallelIterator<Item = &E>
where
E: Send + Sync,
Ix: Send + Sync,
{
self.g
.par_edge_weights()
.filter_map(|maybe_edge| maybe_edge.as_ref())
}

/// Return an iterator yielding mutable access to all edge weights.
#[cfg(feature = "rayon")]
pub fn par_edge_weights_mut(&mut self) -> impl ParallelIterator<Item = &mut E>
where
E: Send + Sync,
Ix: Send + Sync,
{
self.g
.par_edge_weights_mut()
.filter_map(|maybe_edge| maybe_edge.as_mut())
}

/// Access the source and target nodes for `e`.
pub fn edge_endpoints(&self, e: EdgeIndex<Ix>) -> Option<(NodeIndex<Ix>, NodeIndex<Ix>)> {
match self.g.edges.get(e.index()) {
Expand Down
54 changes: 54 additions & 0 deletions crates/petgraph/tests/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use petgraph::{
Topo, VisitMap, Walker,
},
};
#[cfg(feature = "rayon")]
use rayon::iter::ParallelIterator;

fn set<I>(iter: I) -> HashSet<I::Item>
where
Expand Down Expand Up @@ -3243,3 +3245,55 @@ fn test_edges_connecting_iteration_order() {
]
);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_node_weights() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
}
let result = graph.par_node_weights().copied().collect::<Vec<_>>();
let expected = (0..100).collect::<Vec<_>>();
assert_eq!(result, expected);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_node_weights_mut() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
}
graph.par_node_weights_mut().for_each(|x| *x *= 2);
let result = graph.par_node_weights().copied().collect::<Vec<_>>();
let expected = (0..100).map(|x| x * 2).collect::<Vec<_>>();
assert_eq!(result, expected);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_edge_weights() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
graph.add_edge(NodeIndex::new(i as usize), NodeIndex::new(i as usize), i);
}
let result = graph.par_edge_weights().copied().collect::<Vec<_>>();
let expected = (0..100).collect::<Vec<_>>();
assert_eq!(result, expected);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_edge_weights_mut() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
graph.add_edge(NodeIndex::new(i as usize), NodeIndex::new(i as usize), i);
}
graph.par_edge_weights_mut().for_each(|x| *x *= 2);
let result = graph.par_edge_weights().copied().collect::<Vec<_>>();
let expected = (0..100).map(|x| x * 2).collect::<Vec<_>>();
assert_eq!(result, expected);
}
54 changes: 54 additions & 0 deletions crates/petgraph/tests/stable_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use petgraph::{
stable_graph::{edge_index as e, node_index as n},
visit::{EdgeIndexable, IntoEdgeReferences, IntoNodeReferences, NodeIndexable},
};
#[cfg(feature = "rayon")]
use rayon::iter::ParallelIterator;

fn assert_graph_consistent<N, E, Ty, Ix>(g: &StableGraph<N, E, Ty, Ix>)
where
Expand Down Expand Up @@ -1308,3 +1310,55 @@ fn test_edges_connecting_iteration_order() {
]
);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_node_weights() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
}
let result = graph.par_node_weights().copied().collect::<Vec<_>>();
let expected = (0..100).collect::<Vec<_>>();
assert_eq!(result, expected);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_node_weights_mut() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
}
graph.par_node_weights_mut().for_each(|x| *x *= 2);
let result = graph.par_node_weights().copied().collect::<Vec<_>>();
let expected = (0..100).map(|x| x * 2).collect::<Vec<_>>();
assert_eq!(result, expected);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_edge_weights() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
graph.add_edge(NodeIndex::new(i as usize), NodeIndex::new(i as usize), i);
}
let result = graph.par_edge_weights().copied().collect::<Vec<_>>();
let expected = (0..100).collect::<Vec<_>>();
assert_eq!(result, expected);
}

#[cfg(feature = "rayon")]
#[test]
fn test_parallel_edge_weights_mut() {
let mut graph: StableDiGraph<i32, i32> = StableGraph::new();
for i in 0..100 {
graph.add_node(i);
graph.add_edge(NodeIndex::new(i as usize), NodeIndex::new(i as usize), i);
}
graph.par_edge_weights_mut().for_each(|x| *x *= 2);
let result = graph.par_edge_weights().copied().collect::<Vec<_>>();
let expected = (0..100).map(|x| x * 2).collect::<Vec<_>>();
assert_eq!(result, expected);
}
Loading