Skip to content

Commit ef5d17d

Browse files
RaoulLuquehustrust
andauthored
docs: Collection of Doc fixes (#856)
This PR implements two quick Doc fixes as well as different smaller fixes: - The link in the `lib.rs` docs was to `functions` in the `algo` module, instead of the top level `index.html` - The top-level `index.html` in the `algo` module seemed very bare-bone, therefore I added a little bit of text instead of none - Adds examples for different functions like `map` and `filter_map` are added. --------- Signed-off-by: hustrust <hustrust@outlook.com> Co-authored-by: hustrust <hustrust@outlook.com>
1 parent b682695 commit ef5d17d

File tree

8 files changed

+282
-17
lines changed

8 files changed

+282
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated by Cargo
22
/target/
33
Cargo.lock
4-
4+
rustc-ice-*
55

66
.DS_Store
77
.vscode/

CONTRIBUTING.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ contributing. The structure of this guide is as follows:
2828
- [Creating pull requests](#creating-pull-requests)
2929
- [Reviewing pull requests](#reviewing-pull-requests)
3030

31-
- [Setup](#-setup)
32-
- [Building](#-building)
31+
- [Setup](#%EF%B8%8F-setup)
32+
- [Building](#%EF%B8%8F-building)
3333
- [Testing](#-testing)
34-
- [Benchmarks](#-benchmarks)
34+
- [Lints](#-lints)
35+
- [Benchmarks](#%EF%B8%8F-benchmarks)
3536

3637
- [Contributors](#-contributors)
3738

src/algo/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
//! Graph algorithms.
2-
//!
3-
//! It is a goal to gradually migrate the algorithms to be based on graph traits
4-
//! so that they are generally applicable. For now, some of these still require
5-
//! the `Graph` type.
1+
/*!
2+
This module contains most of `petgraph`'s algorithms to operate on graphs. Some, very simple search
3+
algorithms like depth-first search or breadth-first search are implemented in the
4+
[`visit`](crate::visit) module.
5+
6+
The `algo` module contains multiple submodules, each implementing a specific algorithm or set of
7+
algorithms. Some functions, like [`connected_components`], are implemented directly in this module.
8+
9+
It is a goal to gradually migrate the algorithms to be based on graph traits
10+
so that they are generally applicable. For now, some of these still require
11+
the `Graph` type.
12+
*/
613

714
pub mod articulation_points;
815
pub mod astar;

src/algo/scc/tarjan_scc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<N> TarjanScc<N> {
4444
/// [1]: https://homepages.ecs.vuw.ac.nz/~djp/files/P05.pdf
4545
/// [2]: https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
4646
///
47-
/// Calls `f` for each strongly strongly connected component (scc).
47+
/// Calls `f` for each strongly connected component (scc).
4848
/// The order of node ids within each scc is arbitrary, but the order of
4949
/// the sccs is their postorder (reverse topological sort).
5050
///

src/dot/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ use crate::visit::{
3838
/// // 1 [label="\"B\""]
3939
/// // 2 [label="\"C\""]
4040
/// // 3 [label="\"D\""]
41-
/// // 0 -> 1
42-
/// // 0 -> 2
43-
/// // 0 -> 3
44-
/// // 1 -> 2
45-
/// // 1 -> 3
46-
/// // 2 -> 3
41+
/// // 0 -> 1 [ ]
42+
/// // 0 -> 2 [ ]
43+
/// // 0 -> 3 [ ]
44+
/// // 1 -> 2 [ ]
45+
/// // 1 -> 3 [ ]
46+
/// // 2 -> 3 [ ]
4747
/// // }
4848
///
4949
/// // If you need multiple config options, just list them all in the slice.

src/graph_impl/mod.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,23 @@ where
14901490
/// or they are filled with default values.
14911491
///
14921492
/// Nodes are inserted automatically to match the edges.
1493+
///
1494+
/// ```
1495+
/// use petgraph::Graph;
1496+
///
1497+
/// let mut g = Graph::<(), i32>::new();
1498+
/// let a = g.add_node(());
1499+
/// let b = g.add_node(());
1500+
/// let c = g.add_node(());
1501+
/// let d = g.add_node(());
1502+
///
1503+
/// g.extend_with_edges(&[
1504+
/// (a, b, 7),
1505+
/// (a, c, 8),
1506+
/// (a, d, 9),
1507+
/// (b, c, 10),
1508+
/// ]);
1509+
/// ```
14931510
pub fn extend_with_edges<I>(&mut self, iterable: I)
14941511
where
14951512
I: IntoIterator,
@@ -1519,6 +1536,40 @@ where
15191536
/// graph indices as `self`.
15201537
///
15211538
/// If you want a consuming version of this function, see [`map_owned`](struct.Graph.html#method.map_owned).
1539+
///
1540+
/// ```
1541+
/// use petgraph::graph::UnGraph;
1542+
///
1543+
/// // Create an undirected graph with city names as node data and their distances as edge data.
1544+
/// let mut g = UnGraph::<String, u32>::new_undirected();
1545+
///
1546+
/// let bie = g.add_node("Bielefeld".to_owned());
1547+
/// let del = g.add_node("New Delhi".to_owned());
1548+
/// let mex = g.add_node("Mexico City".to_owned());
1549+
/// let syd = g.add_node("Sydney".to_owned());
1550+
///
1551+
/// // Add distances in kilometers as edge data.
1552+
/// g.extend_with_edges(&[
1553+
/// (bie, del, 6_000),
1554+
/// (bie, mex, 10_000),
1555+
/// (bie, syd, 16_000),
1556+
/// (del, mex, 14_000),
1557+
/// (del, syd, 12_000),
1558+
/// (mex, syd, 15_000),
1559+
/// ]);
1560+
///
1561+
/// // We might now want to change up the distances to be in miles instead and to be strings.
1562+
/// // We can do this using the `map` method, which takes two closures for the node and edge data,
1563+
/// // respectively, and returns a new graph with the transformed data.
1564+
/// let g_miles: UnGraph<String, i32> = g.map(
1565+
/// |_, city| city.to_owned(),
1566+
/// |_, &distance| (distance as f64 * 0.621371).round() as i32,
1567+
/// );
1568+
///
1569+
/// for &edge_weight in g_miles.edge_weights() {
1570+
/// assert!(edge_weight < 10_000);
1571+
/// }
1572+
/// ```
15221573
pub fn map<'a, F, G, N2, E2>(
15231574
&'a self,
15241575
mut node_map: F,
@@ -1550,6 +1601,39 @@ where
15501601
/// as `self`.
15511602
///
15521603
/// If you want a non-consuming version of this function, see [`map`](struct.Graph.html#method.map).
1604+
/// ```
1605+
/// use petgraph::graph::UnGraph;
1606+
///
1607+
/// // Create an undirected graph with city names as node data and their distances as edge data.
1608+
/// let mut g = UnGraph::<String, u32>::new_undirected();
1609+
///
1610+
/// let bie = g.add_node("Bielefeld".to_owned());
1611+
/// let del = g.add_node("New Delhi".to_owned());
1612+
/// let mex = g.add_node("Mexico City".to_owned());
1613+
/// let syd = g.add_node("Sydney".to_owned());
1614+
///
1615+
/// // Add distances in kilometers as edge data.
1616+
/// g.extend_with_edges(&[
1617+
/// (bie, del, 6_000),
1618+
/// (bie, mex, 10_000),
1619+
/// (bie, syd, 16_000),
1620+
/// (del, mex, 14_000),
1621+
/// (del, syd, 12_000),
1622+
/// (mex, syd, 15_000),
1623+
/// ]);
1624+
///
1625+
/// // We might now want to change up the distances to be in miles instead and to be strings.
1626+
/// // We can do this using the `map` method, which takes two closures for the node and edge data,
1627+
/// // respectively, and returns a new graph with the transformed data.
1628+
/// let g_miles: UnGraph<String, i32> = g.map_owned(
1629+
/// |_, city| city,
1630+
/// |_, distance| (distance as f64 * 0.621371).round() as i32,
1631+
/// );
1632+
///
1633+
/// for &edge_weight in g_miles.edge_weights() {
1634+
/// assert!(edge_weight < 10_000);
1635+
/// }
1636+
/// ```
15531637
pub fn map_owned<F, G, N2, E2>(self, mut node_map: F, mut edge_map: G) -> Graph<N2, E2, Ty, Ix>
15541638
where
15551639
F: FnMut(NodeIndex<Ix>, N) -> N2,
@@ -1584,6 +1668,27 @@ where
15841668
/// the same graph indices as `self`.
15851669
///
15861670
/// If you want a consuming version of this function, see [`filter_map_owned`](struct.Graph.html#method.filter_map_owned).
1671+
///
1672+
/// ```
1673+
/// use petgraph::Graph;
1674+
///
1675+
/// // Create a graph with integer node weights
1676+
/// let mut g = Graph::<u32, ()>::new();
1677+
/// let a = g.add_node(0);
1678+
/// let b = g.add_node(2);
1679+
/// let c = g.add_node(5);
1680+
/// let d = g.add_node(7);
1681+
/// let e = g.add_node(4);
1682+
/// g.extend_with_edges(&[(a, b, ()), (a, c, ()), (b, d, ()), (c, d, ()), (d, e, ())]);
1683+
///
1684+
/// // Filter the graph such that only nodes with weight greater than 2 are kept.
1685+
/// let g_filtered = g.filter_map(
1686+
/// |_, &node_weight| if node_weight > 2 { Some(node_weight) } else { None },
1687+
/// |_, &edge_weight| Some(edge_weight),
1688+
/// );
1689+
///
1690+
/// assert_eq!(g_filtered.node_count(), 3);
1691+
/// ```
15871692
pub fn filter_map<'a, F, G, N2, E2>(
15881693
&'a self,
15891694
mut node_map: F,
@@ -1629,6 +1734,27 @@ where
16291734
/// the same graph indices as `self`.
16301735
///
16311736
/// If you want a non-consuming version of this function, see [`filter_map`](struct.Graph.html#method.filter_map).
1737+
///
1738+
/// ```
1739+
/// use petgraph::Graph;
1740+
///
1741+
/// // Create a graph with integer node weights
1742+
/// let mut g = Graph::<u32, ()>::new();
1743+
/// let a = g.add_node(0);
1744+
/// let b = g.add_node(2);
1745+
/// let c = g.add_node(5);
1746+
/// let d = g.add_node(7);
1747+
/// let e = g.add_node(4);
1748+
/// g.extend_with_edges(&[(a, b, ()), (a, c, ()), (b, d, ()), (c, d, ()), (d, e, ())]);
1749+
///
1750+
/// // Filter the graph such that only nodes with weight greater than 2 are kept.
1751+
/// let g_filtered = g.filter_map_owned(
1752+
/// |_, node_weight| if node_weight > 2 { Some(node_weight) } else { None },
1753+
/// |_, edge_weight| Some(edge_weight),
1754+
/// );
1755+
///
1756+
/// assert_eq!(g_filtered.node_count(), 3);
1757+
/// ```
16321758
pub fn filter_map_owned<F, G, N2, E2>(
16331759
self,
16341760
mut node_map: F,

src/graph_impl/stable_graph/mod.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,40 @@ where
11061106
/// graph indices as `self`.
11071107
///
11081108
/// If you want a consuming version of this function, see [`map_owned`](struct.StableGraph.html#method.map_owned).
1109+
///
1110+
/// ```
1111+
/// use petgraph::stable_graph::StableGraph;
1112+
///
1113+
/// // Create an undirected graph with city names as node data and their distances as edge data.
1114+
/// let mut g = StableGraph::<String, u32>::new();
1115+
///
1116+
/// let bie = g.add_node("Bielefeld".to_owned());
1117+
/// let del = g.add_node("New Delhi".to_owned());
1118+
/// let mex = g.add_node("Mexico City".to_owned());
1119+
/// let syd = g.add_node("Sydney".to_owned());
1120+
///
1121+
/// // Add distances in kilometers as edge data.
1122+
/// g.extend_with_edges(&[
1123+
/// (bie, del, 6_000),
1124+
/// (bie, mex, 10_000),
1125+
/// (bie, syd, 16_000),
1126+
/// (del, mex, 14_000),
1127+
/// (del, syd, 12_000),
1128+
/// (mex, syd, 15_000),
1129+
/// ]);
1130+
///
1131+
/// // We might now want to change up the distances to be in miles instead and to be strings.
1132+
/// // We can do this using the `map` method, which takes two closures for the node and edge data,
1133+
/// // respectively, and returns a new graph with the transformed data.
1134+
/// let g_miles: StableGraph<String, i32> = g.map(
1135+
/// |_, city| city.to_owned(),
1136+
/// |_, &distance| (distance as f64 * 0.621371).round() as i32,
1137+
/// );
1138+
///
1139+
/// for &edge_weight in g_miles.edge_weights() {
1140+
/// assert!(edge_weight < 10_000);
1141+
/// }
1142+
/// ```
11091143
pub fn map<'a, F, G, N2, E2>(
11101144
&'a self,
11111145
mut node_map: F,
@@ -1135,6 +1169,40 @@ where
11351169
/// graph indices as `self`.
11361170
///
11371171
/// If you want a non-consuming version of this function, see [`map`](struct.StableGraph.html#method.map).
1172+
///
1173+
/// ```
1174+
/// use petgraph::stable_graph::StableGraph;
1175+
///
1176+
/// // Create an undirected graph with city names as node data and their distances as edge data.
1177+
/// let mut g = StableGraph::<String, u32>::new();
1178+
///
1179+
/// let bie = g.add_node("Bielefeld".to_owned());
1180+
/// let del = g.add_node("New Delhi".to_owned());
1181+
/// let mex = g.add_node("Mexico City".to_owned());
1182+
/// let syd = g.add_node("Sydney".to_owned());
1183+
///
1184+
/// // Add distances in kilometers as edge data.
1185+
/// g.extend_with_edges(&[
1186+
/// (bie, del, 6_000),
1187+
/// (bie, mex, 10_000),
1188+
/// (bie, syd, 16_000),
1189+
/// (del, mex, 14_000),
1190+
/// (del, syd, 12_000),
1191+
/// (mex, syd, 15_000),
1192+
/// ]);
1193+
///
1194+
/// // We might now want to change up the distances to be in miles instead and to be strings.
1195+
/// // We can do this using the `map` method, which takes two closures for the node and edge data,
1196+
/// // respectively, and returns a new graph with the transformed data.
1197+
/// let g_miles: StableGraph<String, i32> = g.map_owned(
1198+
/// |_, city| city,
1199+
/// |_, distance| (distance as f64 * 0.621371).round() as i32,
1200+
/// );
1201+
///
1202+
/// for &edge_weight in g_miles.edge_weights() {
1203+
/// assert!(edge_weight < 10_000);
1204+
/// }
1205+
/// ```
11381206
pub fn map_owned<F, G, N2, E2>(
11391207
self,
11401208
mut node_map: F,
@@ -1170,6 +1238,29 @@ where
11701238
/// indices.
11711239
///
11721240
/// If you want a consuming version of this function, see [`filter_map_owned`](struct.StableGraph.html#method.filter_map_owned).
1241+
///
1242+
/// ```
1243+
/// use petgraph::stable_graph::StableGraph;
1244+
///
1245+
/// // Create a graph with integer node weights
1246+
/// let mut g = StableGraph::<u32, ()>::new();
1247+
/// let a = g.add_node(0);
1248+
/// let b = g.add_node(2);
1249+
/// let c = g.add_node(5);
1250+
/// let d = g.add_node(7);
1251+
/// let e = g.add_node(4);
1252+
/// g.extend_with_edges(&[(a, b, ()), (a, c, ()), (b, d, ()), (c, d, ()), (d, e, ())]);
1253+
///
1254+
/// // Filter the graph such that only nodes with weight greater than 2 are kept.
1255+
/// let g_filtered = g.filter_map(
1256+
/// |_, &node_weight| if node_weight > 2 { Some(node_weight) } else { None },
1257+
/// |_, &edge_weight| Some(edge_weight),
1258+
/// );
1259+
///
1260+
/// assert_eq!(g_filtered.node_count(), 3);
1261+
/// // Node and edge indices are preserved.
1262+
/// assert_eq!(g_filtered.node_weight(c), Some(&5));
1263+
/// ```
11731264
pub fn filter_map<'a, F, G, N2, E2>(
11741265
&'a self,
11751266
mut node_map: F,
@@ -1236,6 +1327,29 @@ where
12361327
/// indices.
12371328
///
12381329
/// If you want a non-consuming version of this function, see [`filter_map`](struct.StableGraph.html#method.filter_map).
1330+
///
1331+
/// ```
1332+
/// use petgraph::stable_graph::StableGraph;
1333+
///
1334+
/// // Create a graph with integer node weights
1335+
/// let mut g = StableGraph::<u32, ()>::new();
1336+
/// let a = g.add_node(0);
1337+
/// let b = g.add_node(2);
1338+
/// let c = g.add_node(5);
1339+
/// let d = g.add_node(7);
1340+
/// let e = g.add_node(4);
1341+
/// g.extend_with_edges(&[(a, b, ()), (a, c, ()), (b, d, ()), (c, d, ()), (d, e, ())]);
1342+
///
1343+
/// // Filter the graph such that only nodes with weight greater than 2 are kept.
1344+
/// let g_filtered = g.filter_map_owned(
1345+
/// |_, node_weight| if node_weight > 2 { Some(node_weight) } else { None },
1346+
/// |_, edge_weight| Some(edge_weight),
1347+
/// );
1348+
///
1349+
/// assert_eq!(g_filtered.node_count(), 3);
1350+
/// // Node and edge indices are preserved.
1351+
/// assert_eq!(g_filtered.node_weight(c), Some(&5));
1352+
/// ```
12391353
pub fn filter_map_owned<F, G, N2, E2>(
12401354
self,
12411355
mut node_map: F,
@@ -1298,6 +1412,23 @@ where
12981412
/// or they are filled with default values.
12991413
///
13001414
/// Nodes are inserted automatically to match the edges.
1415+
///
1416+
/// ```
1417+
/// use petgraph::stable_graph::StableGraph;
1418+
///
1419+
/// let mut g = StableGraph::<(), i32>::new();
1420+
/// let a = g.add_node(());
1421+
/// let b = g.add_node(());
1422+
/// let c = g.add_node(());
1423+
/// let d = g.add_node(());
1424+
///
1425+
/// g.extend_with_edges(&[
1426+
/// (a, b, 7),
1427+
/// (a, c, 8),
1428+
/// (a, d, 9),
1429+
/// (b, c, 10),
1430+
/// ]);
1431+
/// ```
13011432
pub fn extend_with_edges<I>(&mut self, iterable: I)
13021433
where
13031434
I: IntoIterator,

0 commit comments

Comments
 (0)