Skip to content

Commit c683aed

Browse files
authored
Consolidate the hashset/match syntax (#1406)
1 parent d7b3f4f commit c683aed

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

rustworkx-core/src/dag_algo.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::error::Error;
1616
use std::fmt::{Display, Formatter};
1717
use std::hash::Hash;
1818

19+
use hashbrown::hash_set::Entry;
1920
use hashbrown::{HashMap, HashSet};
2021
use std::fmt::Debug;
2122
use std::mem::swap;
@@ -442,11 +443,15 @@ where
442443
.neighbors_directed(*node, petgraph::Direction::Outgoing);
443444
let mut used_indices: HashSet<G::NodeId> = HashSet::new();
444445
for succ in children {
445-
// Skip duplicate successors
446-
if used_indices.contains(&succ) {
447-
continue;
446+
match used_indices.entry(succ) {
447+
Entry::Occupied(_) => {
448+
// Skip duplicate successors
449+
continue;
450+
}
451+
Entry::Vacant(used_indices_entry) => {
452+
used_indices_entry.insert();
453+
}
448454
}
449-
used_indices.insert(succ);
450455
let mut multiplicity: usize = 0;
451456
let raw_edges: G::EdgesDirected = self
452457
.graph

rustworkx-core/src/graph_ext/contraction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use crate::dictmap::{DictMap, InitWithHasher};
1616
use crate::err::{ContractError, ContractSimpleError};
1717
use crate::graph_ext::NodeRemovable;
18-
use indexmap::map::Entry::{Occupied, Vacant};
18+
use indexmap::map::Entry;
1919
use indexmap::IndexSet;
2020
use petgraph::data::Build;
2121
use petgraph::graphmap;
@@ -458,10 +458,10 @@ where
458458
let mut kvs = DictMap::with_capacity(xs.len());
459459
for (k, v) in xs {
460460
match kvs.entry(k) {
461-
Occupied(entry) => {
461+
Entry::Occupied(entry) => {
462462
*entry.into_mut() = merge_fn(&v, entry.get())?;
463463
}
464-
Vacant(entry) => {
464+
Entry::Vacant(entry) => {
465465
entry.insert(v);
466466
}
467467
}

rustworkx-core/src/shortest_path/astar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use std::collections::BinaryHeap;
2222
use std::hash::Hash;
2323

24-
use hashbrown::hash_map::Entry::{Occupied, Vacant};
24+
use hashbrown::hash_map::Entry;
2525
use hashbrown::HashMap;
2626

2727
use petgraph::algo::Measure;
@@ -144,7 +144,7 @@ where
144144
let mut next_score = node_score + cost;
145145

146146
match scores.entry(next) {
147-
Occupied(ent) => {
147+
Entry::Occupied(ent) => {
148148
let old_score = *ent.get();
149149
if next_score < old_score {
150150
*ent.into_mut() = next_score;
@@ -153,7 +153,7 @@ where
153153
next_score = old_score;
154154
}
155155
}
156-
Vacant(ent) => {
156+
Entry::Vacant(ent) => {
157157
ent.insert(next_score);
158158
path_tracker.set_predecessor(next, node);
159159
}

rustworkx-core/src/traversal/dijkstra_visit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use std::collections::BinaryHeap;
2121
use std::hash::Hash;
2222

23-
use hashbrown::hash_map::Entry::{Occupied, Vacant};
23+
use hashbrown::hash_map::Entry;
2424
use hashbrown::HashMap;
2525

2626
use petgraph::algo::Measure;
@@ -252,7 +252,7 @@ where
252252
let cost = edge_cost(edge)?;
253253
let next_score = node_score + cost;
254254
match scores.entry(next) {
255-
Occupied(ent) => {
255+
Entry::Occupied(ent) => {
256256
if next_score < *ent.get() {
257257
try_control_with_result!(
258258
visitor(DijkstraEvent::EdgeRelaxed(node, next, edge.weight())),
@@ -267,7 +267,7 @@ where
267267
);
268268
}
269269
}
270-
Vacant(ent) => {
270+
Entry::Vacant(ent) => {
271271
try_control_with_result!(
272272
visitor(DijkstraEvent::EdgeRelaxed(node, next, edge.weight())),
273273
continue

src/digraph.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -782,12 +782,15 @@ impl PyDiGraph {
782782

783783
for edge in raw_edges {
784784
let succ = edge.target();
785-
if !used_indices.contains(&succ) {
786-
let edge_weight = edge.weight();
787-
if filter_edge(edge_weight)? {
788-
used_indices.insert(succ);
789-
successors.push(self.graph.node_weight(succ).unwrap());
785+
match used_indices.entry(succ) {
786+
Entry::Vacant(used_indices_entry) => {
787+
let edge_weight = edge.weight();
788+
if filter_edge(edge_weight)? {
789+
used_indices_entry.insert();
790+
successors.push(self.graph.node_weight(succ).unwrap());
791+
}
790792
}
793+
Entry::Occupied(_) => {}
791794
}
792795
}
793796
Ok(successors)
@@ -841,12 +844,15 @@ impl PyDiGraph {
841844

842845
for edge in raw_edges {
843846
let pred = edge.source();
844-
if !used_indices.contains(&pred) {
845-
let edge_weight = edge.weight();
846-
if filter_edge(edge_weight)? {
847-
used_indices.insert(pred);
848-
predec.push(self.graph.node_weight(pred).unwrap());
847+
match used_indices.entry(pred) {
848+
Entry::Vacant(used_indices_entry) => {
849+
let edge_weight = edge.weight();
850+
if filter_edge(edge_weight)? {
851+
used_indices_entry.insert();
852+
predec.push(self.graph.node_weight(pred).unwrap());
853+
}
849854
}
855+
Entry::Occupied(_) => {}
850856
}
851857
}
852858
Ok(predec)

0 commit comments

Comments
 (0)