简体   繁体   English

交换切片的元素(就地)

[英]Swapping Elements of a Slice (in-place)

I have posted my solution in the answers below.我已经在下面的答案中发布了我的解决方案。

The question will not be updated with even more code to not further increase clutter.这个问题不会用更多的代码更新,以免进一步增加混乱。


I'm trying to rotate all elements in a Vec<Vec<T>> clockwise.我正在尝试顺时针旋转Vec<Vec<T>>中的所有元素。 The vector is guaranteed to be square, as in v.len() == v[0].len() .向量保证是方形的,如v.len() == v[0].len()

The idea is to这个想法是

  • find all elements that are equivalent under rotational symmetry to v 's center找到在旋转对称下与v中心等效的所有元素
  • swap these elements in place, using std::mem::swap使用 std::mem::swap 交换这些元素

My current code does not change the state of the vec.我当前的代码不会改变 vec 的状态。 How do I fix this?我该如何解决?

fn rotate<T>(v: &mut Vec<Vec<T>>) {

    // swap elements equivalent to position i on each ring r 
    // limit l = side length of current ring
    //
    // + 0 - - - - +   r = 0 -> l = 6
    // | + 1 - - + |   r = 1 -> l = 4
    // | | + 2 + | |   r = 2 -> l = 2
    // | | |   | | |
    // | | + - + | |   swap:
    // | + - - - + |     a b c d
    // + - - - - - +   > b a c d
    //                 > c a b d
    //                 > d a b c

    for r in 0..((v.len() + 1) / 2 {
        let l = v.len() - 1 - r;
        for i in r..l {
            let mut a = & pieces[  r  ][ r+i ];
            let mut b = & pieces[ r+i ][ l-r ];
            let mut c = & pieces[ l-r ][l-r-i];
            let mut d = & pieces[l-r-i][  r  ];

            _rot_cw(&mut a, &mut b, &mut c, &mut d)},
        }
    }

    fn _rot_cw<T>(a: &mut T, b: &mut T, c: &mut T, d: &mut T) {
        //rotates a->b, b->c, c->d, d->a
        std::mem::swap(a, b);
        std::mem::swap(a, c);
        std::mem::swap(a, d);
    }
}

Edit:编辑:
Fixed minor issues in the original code above, thanks to @Jmb.感谢@Jmb,修复了上面原始代码中的小问题。 Here's my current code, again running into borrowing issues:这是我当前的代码,再次遇到借用问题:

fn rotate_square_slice<T>(slice: &mut Vec<T>, rows: usize) {
    for r in 0..(slice.len()+1)/2 {
        let l = slice.len() -1 - r;
        for i in r..l {
            let a = &mut slice.get_mut(rows *    r    +  r+i ).unwrap();
            let b = &mut slice.get_mut(rows *  (r+i)  +  l-r ).unwrap();
            let c = &mut slice.get_mut(rows *  (l-r)  + l-r-i).unwrap();
            let d = &mut slice.get_mut(rows * (l-r-i) +   r  ).unwrap();

            std::mem::swap(a, b);
            std::mem::swap(a, c);
            std::mem::swap(a, d);
        }
    }
}

Swapping elements in a slice can be done by using the slice's swap() method .可以使用 切片的swap()方法来swap()切片中的元素。

Solving that problem, the code now looks like this:解决这个问题,代码现在是这样的:

fn rotate_square_slice<T>(slice: &mut [T], size: usize) {
    for r in 0..(size + 1) / 2 {
        let l = size - 1 - r;
        for i in r..l {    
            // b, c & d are the indices with rotational symmetry to a,
            // shifted by 90°, 180° & 270° respectively
            
            let a = size *    r    +  r+i ;
            let b = size *  (r+i)  +  l-r ;
            let c = size *  (l-r)  + l-r-i;
            let d = size * (l-r-i) +   r  ;

            slice.swap(a, b);
            slice.swap(a, c);
            slice.swap(a, d);
        }
    }
}

I have, however, run into an issue with correctly indexing the slice.但是,我遇到了正确索引切片的问题。 The question can be found here:问题可以在这里找到:

Rotational Symmetry Indexing in a 1D "Square" Array 一维“方形”阵列中的旋转对称索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM