Skip to content
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

Tracking issue for VecDeque::rotate_{left|right} (feature vecdeque_rotate) #56686

Closed
DutchGhost opened this issue Dec 10, 2018 · 10 comments · Fixed by #60678
Closed

Tracking issue for VecDeque::rotate_{left|right} (feature vecdeque_rotate) #56686

DutchGhost opened this issue Dec 10, 2018 · 10 comments · Fixed by #60678
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@DutchGhost
Copy link
Contributor

Currently if you want to rotate a VecDeque n elements to the left or right, you'd have to write something like this:

use std::collections::VecDeque;

fn main() {
    let mut v = (0..10).collect::<VecDeque<_>>();
    
    println!("{:?}", v); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    // rotate left by 2,
    for _ in 0..2 {
        if let Some(popped_front) = v.pop_front() {
            v.push_back(popped_front);
        }
    }
    
    println!("{:?}", v); // [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
    
    // rotate right by 4
    for _ in 0..4 {
        if let Some(popped_back) = v.pop_back() {
            v.push_front(popped_back);
        }
    }
    
    println!("{:?}", v); //[8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
    
}

I wonder if it would be possible to add 2 functions:

  • rotate_left(mid: usize)
  • rotate_right(mid: usize)

You then only have to write:

use std::collections::VecDeque;

fn main() {
    let mut v = (0..10).collect::<VecDeque<_>>();
    
    println!("{:?}", v); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    // rotate left by 2,
    v.rotate_left(2)
    println!("{:?}", v); // [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
    
    // rotate right by 4
    v.rotate_right(4);
    
    println!("{:?}", v); //[8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
    
}

This would be equal to slice's rotate_left and rotate_right.
It could probably be implemented on a lower level than with pop's and pushes for a little more efficiency.

@frewsxcv frewsxcv added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Dec 11, 2018
@scottmcm
Copy link
Member

scottmcm commented Dec 15, 2018

Ooh, I like this -- VecDeque is a circular buffer internally, so it should be doable with just a single at most two ptr::copy. EDIT: PR #56842

@scottmcm scottmcm changed the title VecDeque rotation Tracking issue for VecDeque::rotate_{left|right} (feature vecdeque_rotate) Dec 20, 2018
@scottmcm scottmcm added the C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. label Dec 20, 2018
@scottmcm
Copy link
Member

PR is r+'d, so I turned this into a tracking issue.

@scottmcm scottmcm added B-unstable Blocker: Implemented in the nightly compiler and unstable. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Dec 20, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this issue Dec 20, 2018
…chton

Add unstable VecDeque::rotate_{left|right}

Like the ones on slices, but more efficient because vecdeque is a circular buffer.

Issue that proposed this: rust-lang#56686

~~:bomb: Please someone look very carefully at the `unsafe` in this!  The `wrap_copy` seems to be exactly what this method needs, and the `len` passed to it is never more than half the length of the deque, but I haven't managed to prove to myself that it's correct :bomb:~~ I think I proved that this code meets the requirement of the unsafe code it's calling; please double-check, of course.
pietroalbini added a commit to pietroalbini/rust that referenced this issue Dec 21, 2018
…chton

Add unstable VecDeque::rotate_{left|right}

Like the ones on slices, but more efficient because vecdeque is a circular buffer.

Issue that proposed this: rust-lang#56686

~~:bomb: Please someone look very carefully at the `unsafe` in this!  The `wrap_copy` seems to be exactly what this method needs, and the `len` passed to it is never more than half the length of the deque, but I haven't managed to prove to myself that it's correct :bomb:~~ I think I proved that this code meets the requirement of the unsafe code it's calling; please double-check, of course.
Centril added a commit to Centril/rust that referenced this issue Dec 22, 2018
…chton

Add unstable VecDeque::rotate_{left|right}

Like the ones on slices, but more efficient because vecdeque is a circular buffer.

Issue that proposed this: rust-lang#56686

~~:bomb: Please someone look very carefully at the `unsafe` in this!  The `wrap_copy` seems to be exactly what this method needs, and the `len` passed to it is never more than half the length of the deque, but I haven't managed to prove to myself that it's correct :bomb:~~ I think I proved that this code meets the requirement of the unsafe code it's calling; please double-check, of course.
bors added a commit that referenced this issue Dec 22, 2018
Add unstable VecDeque::rotate_{left|right}

Like the ones on slices, but more efficient because vecdeque is a circular buffer.

Issue that proposed this: #56686

~~:bomb: Please someone look very carefully at the `unsafe` in this!  The `wrap_copy` seems to be exactly what this method needs, and the `len` passed to it is never more than half the length of the deque, but I haven't managed to prove to myself that it's correct :bomb:~~ I think I proved that this code meets the requirement of the unsafe code it's calling; please double-check, of course.
@DutchGhost
Copy link
Contributor Author

I've since the feature replaced any code that used the pop - push technique with the rotate methods. This was fairly easy to do.

It's been a few months now, are there any issue's preventing this from landing stable?

@sfackler
Copy link
Member

sfackler commented May 1, 2019

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented May 1, 2019

Team member @sfackler has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 1, 2019
@rfcbot
Copy link

rfcbot commented May 8, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels May 8, 2019
@WiSaGaN
Copy link
Contributor

WiSaGaN commented May 14, 2019

Is "left" or "right" the word we want to use here? Previously we have deprecated trim_left (trim_right) of String in favor of trim_start and trim_end so that we do not assume the string is laid out from left to right. Here we seem to assume left is to front while right is to back? Are we able to use some words that express the front and back without assuming a left-to-right layout?

@DutchGhost
Copy link
Contributor Author

DutchGhost commented May 14, 2019

As discussed here, we have to use those names to stay consistent with the slice rotate_{left |right} methods #56842 (comment)

@WiSaGaN
Copy link
Contributor

WiSaGaN commented May 14, 2019

@DutchGhost , sorry I missed that. That makes sense.

@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label May 18, 2019
@rfcbot
Copy link

rfcbot commented May 18, 2019

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label May 18, 2019
Centril added a commit to Centril/rust that referenced this issue May 19, 2019
Stabilize vecdeque_rotate

This PR stabilizes the vecdeque_rotate feature.
r? @scottmcm

Closes rust-lang#56686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants