@@ -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