-
Notifications
You must be signed in to change notification settings - Fork 13.3k
VecDeque missing operations for "front" handling. #92547
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
Comments
Maybe I don't understand you right, but As an alternative you could also use |
No it doesn't. If I have
What happens is that self.queue has it's right hand side (the back) removed similar to truncate. The only way to emulate this with "split off" to remove from the front (left) is:
|
fn something(&mut self) {
self.queue = self.queue.split_off(4);
} |
Given rust's pride in docs and accessibility, I think that methods for front operations should exist, so that others don't have to play a puzzle-solving game to achieve this behaviour. |
Improving the documentation might be sufficient. |
Actually, it's not @the8472. If you look at the source to vecdeque you will see that truncate operates differently to split_off, meaning that it's not sufficient to suggest it as a replacement to truncate. For split_off, retaining the right side, then sure, a documentation improvement would be sufficient. I still think truncate_front is needed. |
Yup, I agree with @Firstyear , achieving truncate at front of a deque using split_off or drop/drain workarounds is not ideal. A method such as truncate_front is very similar to the existing implementation of truncate, by altering the front and rear pointers, which would be the most optimized. |
|
I also think that truncate_front is needed. |
I want to add that I would also use such functions, The workaround in this comment is not ideal as the capacity of |
Would love to see this added as well! |
(sorry for piggybacking off this post) I can add it if it would be acceptable. |
Just adding my voice here in support for corresponding "front" operations. |
Please add it. I really need truncate_front |
|
From the docs of VecDeque:
"""
A double-ended queue implemented with a growable ring buffer.
The “default” usage of this type as a queue is to use push_back to add to the queue, and pop_front to remove from the queue. extend and append push onto the back in this manner, and iterating over VecDeque goes front to back.
"""
From this, we can assume the default way to use VecDeque when visualised is:
VecDeque has a number of operations to reduce the size of the content in the VecDeque such as truncate or split_off, but these assume you want to truncate or split from the back.
However, what is missing is equivalents for operating on the front of the VecDeque
This created confusion for me wanting to use VecDeque as an ordered ringbuffer since VecDeque will iterate front to back, and recommends you push_back, but trying to maintain a fixed capacity requires you to either manually pop_front to len, OR you need to actually store your VecDeque in reverse, and rely on iter().rev() allowing you to use truncate.
Since VecDeque intends to be "double ended" it would be useful and convenient to have a number of the methods given front variants. My non-exhaustive list is:
The text was updated successfully, but these errors were encountered: