Skip to content

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

Closed

Description

@DutchGhost

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.

Activity

  1. added
    T-libs-api[DEPRECATED; DO NOT USE]
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Dec 11, 2018
  2. scottmcm commented on Dec 15, 2018

    @scottmcm
    Member

    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

  3. changed the title [-]VecDeque rotation[/-] [+]Tracking issue for VecDeque::rotate_{left|right} (feature vecdeque_rotate)[/+] on Dec 20, 2018
  4. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Dec 20, 2018
  5. scottmcm commented on Dec 20, 2018

    @scottmcm
    Member

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

  6. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    and removed
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Dec 20, 2018
  7. added a commit that references this issue on Dec 20, 2018
  8. added a commit that references this issue on Dec 21, 2018
  9. added a commit that references this issue on Dec 22, 2018
  10. added a commit that references this issue on Dec 22, 2018
  11. DutchGhost commented on Apr 17, 2019

    @DutchGhost
    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?

  12. 1 remaining item

  13. rfcbot commented on May 1, 2019

    @rfcbot

    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.

  14. added
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.
    on May 1, 2019
  15. rfcbot commented on May 8, 2019

    @rfcbot

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

  16. added
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    and removed
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    on May 8, 2019
  17. WiSaGaN commented on May 14, 2019

    @WiSaGaN
    Contributor

    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?

  18. DutchGhost commented on May 14, 2019

    @DutchGhost
    Author

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

  19. WiSaGaN commented on May 14, 2019

    @WiSaGaN
    Contributor

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

  20. rfcbot commented on May 18, 2019

    @rfcbot

    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.

  21. removed
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    on May 18, 2019
  22. added a commit that references this issue on May 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions