-
Notifications
You must be signed in to change notification settings - Fork 29
Add internal_queue #13
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
Merged
matsu7874
merged 9 commits into
rust-lang-ja:master
from
TonalidadeHidrica:feature/internal_queue
Sep 11, 2020
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fdac8e3
Add internal_queue
TonalidadeHidrica e8fb094
Modified according to the pull request comments
TonalidadeHidrica 83e4ec0
Change visibility
TonalidadeHidrica ae02989
Derive Default for SimpleQueue
TonalidadeHidrica f23859d
Return &T in pop method in SimpleQueue
TonalidadeHidrica d020fcb
Make `pop` function safer
TonalidadeHidrica 036948a
Allow deadcode
TonalidadeHidrica ec7afe0
Function `front` now returns Option
TonalidadeHidrica 0859129
Add a test
TonalidadeHidrica 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
struct SimpleQueue<T> { | ||
payload: Vec<T>, | ||
pos: usize, | ||
} | ||
|
||
impl<T> SimpleQueue<T> | ||
where | ||
T: Copy, | ||
{ | ||
fn reserve(&mut self, n: i32) { | ||
let n = n as usize; | ||
if n > self.payload.len() { | ||
self.payload.reserve(n - self.payload.len()); | ||
} | ||
} | ||
|
||
fn size(&self) -> i32 { | ||
(self.payload.len() - self.pos) as i32 | ||
} | ||
|
||
fn empty(&self) -> bool { | ||
self.pos == self.payload.len() | ||
} | ||
|
||
fn push(&mut self, t: &T) { | ||
self.payload.push(*t); | ||
} | ||
|
||
// Do we need mutable version? | ||
fn front(&self) -> &T { | ||
&self.payload[self.pos] | ||
} | ||
|
||
fn clear(&mut self) { | ||
self.payload.clear(); | ||
self.pos = 0; | ||
} | ||
|
||
fn pop(&mut self) { | ||
self.pos += 1; | ||
} | ||
} |
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.
Why we want
T
to beCopy
? It seems to me that passingT
itself instead of reference&T
inpush
is more natural.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.
Because it was what the original code did. But yes, it's so unnatural and complicated to write in Rust, so I'll fix it.
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.
I think removing
Copy
is better in the normal Rust libraries, but considering this is a internal code and is only used in the implementation of the maxflow algorithm (only for integer and pair of integers), I also think we can restrict the level of abstraction. Original C++ implementation are not using move semantics, either. For more general purposes we should useVecDeque
in my opinion. At least it doesn't have enough interface to use with non-Copy
type, e.g. no way to get the ownership of the queued value back.As an alternative, we can use
T
directly instead of using references&T
ifT: Copy
to make it more natural.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.
Sorry for late response....
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.
I see. At least I changed
&T
toT
inpush
method. Also it seemed that removingT: Copy
does not do any harm, so I simply removed it for now.