fix: adding a self-loop should not add two edges (#183)#187
Open
dlyongemallo wants to merge 2 commits intozxcalc:masterfrom
Open
fix: adding a self-loop should not add two edges (#183)#187dlyongemallo wants to merge 2 commits intozxcalc:masterfrom
dlyongemallo wants to merge 2 commits intozxcalc:masterfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where self-loops (edges from a vertex to itself) were being added twice to the graph's internal data structure. The fix adds a check to prevent the duplicate addition of the reverse half-edge when source and target vertices are the same.
- Added a check
if s != tto prevent adding the reverse half-edge for self-loops inadd_edge_with_type - Added a test case
self_loop_not_duplicatedto verify the fix
Comments suppressed due to low confidence (2)
quizx/src/vec_graph.rs:250
- The
remove_edgemethod will incorrectly remove self-loops twice. Whens == t, bothremove_half_edge(s, t)andremove_half_edge(t, s)will attempt to remove the same edge from the adjacency list, but the second call will fail to find it (since it was already removed). While this may not cause a crash, it's inconsistent with theadd_edge_with_typefix. Consider adding a similarif s != tcheck here to only callremove_half_edge(t, s)when the vertices differ.
fn remove_edge(&mut self, s: V, t: V) {
self.nume -= 1;
self.remove_half_edge(s, t);
self.remove_half_edge(t, s);
}
quizx/src/vec_graph.rs:279
- The
set_edge_typemethod has the same issue with self-loops as the original bug inadd_edge_with_type. Whens == t, the method will update the same edge twice (lines 268 and 275), which is unnecessary and could cause issues if the implementation changes. Consider adding anif s != tcheck before the second update block (lines 273-278) to match the fix inadd_edge_with_type.
fn set_edge_type(&mut self, s: V, t: V, ety: EType) {
if let Some(Some(nhd)) = self.edata.get_mut(s) {
let i = Graph::index(nhd, t).expect("Edge not found");
nhd[i] = (t, ety);
} else {
panic!("Source vertex not found");
}
if let Some(Some(nhd)) = self.edata.get_mut(t) {
let i = Graph::index(nhd, s).expect("Edge not found");
nhd[i] = (s, ety);
} else {
panic!("Target vertex not found");
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
f22db29 to
bee0bee
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.