-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
add faster safe rust variant that does not drop needlessly #52
Merged
+126
−1
Merged
Changes from 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use std::mem; | ||
|
||
use rand; | ||
|
||
type NodeCell = Option<Box<Node>>; | ||
|
||
struct Node { | ||
x: i32, | ||
y: i32, | ||
left: NodeCell, | ||
right: NodeCell, | ||
} | ||
|
||
impl Node { | ||
fn new(x: i32) -> Self { | ||
Self { | ||
x, | ||
y: rand::random::<i32>(), | ||
left: None, | ||
right: None, | ||
} | ||
} | ||
} | ||
|
||
fn merge(lower: NodeCell, greater: NodeCell) -> NodeCell { | ||
match (lower, greater) { | ||
(None, greater) => greater, | ||
|
||
(lower, None) => lower, | ||
|
||
(Some(mut lower_node), Some(mut greater_node)) => { | ||
if lower_node.y < greater_node.y { | ||
let mut merged = merge(lower_node.right.take(), Some(greater_node)); | ||
mem::swap(&mut lower_node.right, &mut merged); | ||
mem::forget(merged); | ||
Some(lower_node) | ||
} else { | ||
let mut merged = merge(Some(lower_node), greater_node.left.take()); | ||
mem::swap(&mut greater_node.left, &mut merged); | ||
mem::forget(merged); | ||
Some(greater_node) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn split_binary(orig: NodeCell, value: i32) -> (NodeCell, NodeCell) { | ||
if let Some(mut orig_node) = orig { | ||
if orig_node.x < value { | ||
let mut split_pair = split_binary(orig_node.right.take(), value); | ||
::std::mem::swap(&mut orig_node.right, &mut split_pair.0); | ||
::std::mem::forget(split_pair.0); | ||
(Some(orig_node), split_pair.1) | ||
} else { | ||
let mut split_pair = split_binary(orig_node.left.take(), value); | ||
::std::mem::swap(&mut orig_node.left, &mut split_pair.1); | ||
::std::mem::forget(split_pair.1); | ||
(split_pair.0, Some(orig_node)) | ||
} | ||
} else { | ||
(None, None) | ||
} | ||
} | ||
|
||
fn merge3(lower: NodeCell, equal: NodeCell, greater: NodeCell) -> NodeCell { | ||
merge(merge(lower, equal), greater) | ||
} | ||
|
||
struct SplitResult { | ||
lower: NodeCell, | ||
equal: NodeCell, | ||
greater: NodeCell, | ||
} | ||
|
||
fn split(orig: NodeCell, value: i32) -> SplitResult { | ||
let (lower, equal_greater) = split_binary(orig, value); | ||
let (equal, greater) = split_binary(equal_greater, value + 1); | ||
SplitResult { | ||
lower, | ||
equal, | ||
greater, | ||
} | ||
} | ||
|
||
pub struct Tree { | ||
root: NodeCell, | ||
} | ||
|
||
impl Tree { | ||
pub fn new() -> Self { | ||
Self { root: None } | ||
} | ||
|
||
pub fn has_value(&mut self, x: i32) -> bool { | ||
let splited = split(self.root.take(), x); | ||
let res = splited.equal.is_some(); | ||
self.root = merge3(splited.lower, splited.equal, splited.greater); | ||
res | ||
} | ||
|
||
pub fn insert(&mut self, x: i32) { | ||
let mut splited = split(self.root.take(), x); | ||
if splited.equal.is_none() { | ||
splited.equal = Some(Box::new(Node::new(x))); | ||
} | ||
self.root = merge3(splited.lower, splited.equal, splited.greater); | ||
} | ||
|
||
pub fn erase(&mut self, x: i32) { | ||
let splited = split(self.root.take(), x); | ||
self.root = merge(splited.lower, splited.greater); | ||
} | ||
} |
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.
::std::
can be dropped here, right?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.
Right, I've updated to fix this.