-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove panic! instances #69
Draft
urschrei
wants to merge
2
commits into
master
Choose a base branch
from
remove_panic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
use std::error; | ||
|
||
use crate::envelope::Envelope; | ||
use crate::node::{envelope_for_children, ParentNode, RTreeNode}; | ||
use crate::object::RTreeObject; | ||
|
@@ -26,7 +28,7 @@ where | |
} | ||
|
||
impl InsertionStrategy for RStarInsertionStrategy { | ||
fn insert<T, Params>(tree: &mut RTree<T, Params>, t: T) | ||
fn insert<T, Params>(tree: &mut RTree<T, Params>, t: T) -> Result<(), Box<dyn error::Error>> | ||
where | ||
Params: RTreeParams, | ||
T: RTreeObject, | ||
|
@@ -42,15 +44,16 @@ impl InsertionStrategy for RStarInsertionStrategy { | |
let mut target_height = 0; | ||
let mut insertion_stack = Vec::new(); | ||
match first { | ||
InsertionResult::Split(node) => insertion_stack.push(PerformSplit(node)), | ||
InsertionResult::Reinsert(nodes_to_reinsert, real_target_height) => { | ||
Ok(InsertionResult::Split(node)) => insertion_stack.push(PerformSplit(node)), | ||
Ok(InsertionResult::Reinsert(nodes_to_reinsert, real_target_height)) => { | ||
insertion_stack.extend(nodes_to_reinsert.into_iter().map(PerformReinsert)); | ||
target_height = real_target_height; | ||
} | ||
InsertionResult::Complete => {} | ||
Ok(InsertionResult::Complete) => {} | ||
_ => (), | ||
}; | ||
|
||
while let Some(next) = insertion_stack.pop() { | ||
Ok(while let Some(next) = insertion_stack.pop() { | ||
match next { | ||
PerformSplit(node) => { | ||
// The root node was split, create a new root and increase height | ||
|
@@ -68,13 +71,13 @@ impl InsertionStrategy for RStarInsertionStrategy { | |
match forced_insertion::<T, Params>(root, node_to_reinsert, target_height) { | ||
InsertionResult::Split(node) => insertion_stack.push(PerformSplit(node)), | ||
InsertionResult::Reinsert(_, _) => { | ||
panic!("Unexpected reinsert. This is a bug in rstar.") | ||
Err("Unexpected reinsert. This is a bug in rstar.")? | ||
} | ||
InsertionResult::Complete => {} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -114,7 +117,7 @@ fn recursive_insert<T, Params>( | |
node: &mut ParentNode<T>, | ||
t: RTreeNode<T>, | ||
current_height: usize, | ||
) -> InsertionResult<T> | ||
) -> Result<InsertionResult<T>, Box<dyn error::Error>> | ||
where | ||
T: RTreeObject, | ||
Params: RTreeParams, | ||
|
@@ -125,24 +128,24 @@ where | |
if node.children.len() < expand_index { | ||
// Force insertion into this node | ||
node.children.push(t); | ||
return resolve_overflow::<_, Params>(node, current_height); | ||
return Ok(resolve_overflow::<_, Params>(node, current_height)); | ||
} | ||
|
||
let expand = if let RTreeNode::Parent(ref mut follow) = node.children[expand_index] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line also good to compare to the panic invocation below. This indexing into |
||
recursive_insert::<_, Params>(follow, t, current_height + 1) | ||
} else { | ||
panic!("This is a bug in rstar.") | ||
return Err("Something has gone badly wrong while attempting to insert a value".into()); | ||
}; | ||
|
||
match expand { | ||
InsertionResult::Split(child) => { | ||
Ok(InsertionResult::Split(child)) => { | ||
node.envelope.merge(&child.envelope()); | ||
node.children.push(child); | ||
resolve_overflow::<_, Params>(node, current_height) | ||
Ok(resolve_overflow::<_, Params>(node, current_height)) | ||
} | ||
InsertionResult::Reinsert(a, b) => { | ||
Ok(InsertionResult::Reinsert(a, b)) => { | ||
node.envelope = envelope_for_children(&node.children); | ||
InsertionResult::Reinsert(a, b) | ||
Ok(InsertionResult::Reinsert(a, b)) | ||
} | ||
other => other, | ||
} | ||
|
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://rust-lang.github.io/rust-clippy/master/index.html#try_err
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Actually, I think running
cargo fmt --all -- --check
andcargo clippy --all -- --deny warnings
as part of the CI would be nice.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To give a bit more background about why this panic should never be hit: The algorithm will from time to time (when a node overflows) attempt to reinsert (remove and insert again) a point to improve the tree's internal structure. This is what
InsertionResult::Reinsert(_,_)
means - it tells the top level function (the beginning of the recursion) that some point should be reinserted.Since inserting that point can lead to another reinsertion (and possibly even an endless loop), reinsertion is only attempted once. That's where
forced_insertion
differs - it will never allow re-inserting an element.I think the best way to get rid of the panic would be to modify the return type of
forced_insert
to a newForcedInsertionResult
enum which doesn't contain a reinsert entry. Then this branch would not appear in the first place.