summaryrefslogtreecommitdiff
path: root/src/util.rs
blob: 217f0b49334d18e556d89e3780f66e9145b95def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/// Threading utilities
pub mod thread {
    /// Like `thread::spawn`, but with a `name` argument
    pub fn spawn_named<F, T, S>(name: S, f: F) -> ::std::thread::JoinHandle<T>
        where F: FnOnce() -> T,
              F: Send + 'static,
              T: Send + 'static,
              S: Into<String>
    {
        ::std::thread::Builder::new().name(name.into()).spawn(f).expect("thread spawn works")
    }
}

/// Types that can have their elements rotated
pub trait Rotate {
    fn rotate(&mut self, positions: isize);
}

impl<T> Rotate for [T] {
    fn rotate(&mut self, positions: isize) {
        // length is needed over and over
        let len = self.len();

        // Enforce positions in [0, len) and treat negative rotations as a
        // posititive rotation of len - positions.
        let positions = if positions > 0 {
            positions as usize % len
        } else {
            len - (-positions as usize) % len
        };

        // If positions is 0 or the entire slice, it's a noop.
        if positions == 0 || positions == len {
            return;
        }

        self[..positions].reverse();
        self[positions..].reverse();
        self.reverse();
    }
}


#[cfg(test)]
mod tests {
    use super::Rotate;

    #[test]
    fn rotate_forwards_works() {
        let s = &mut [1, 2, 3, 4, 5];
        s.rotate(1);
        assert_eq!(&[2, 3, 4, 5, 1], s);
    }

    #[test]
    fn rotate_backwards_works() {
        let s = &mut [1, 2, 3, 4, 5];
        s.rotate(-1);
        assert_eq!(&[5, 1, 2, 3, 4], s);
    }

    #[test]
    fn rotate_multiple_forwards() {
        let s = &mut [1, 2, 3, 4, 5, 6, 7];
        s.rotate(2);
        assert_eq!(&[3, 4, 5, 6, 7, 1, 2], s);
    }

    #[test]
    fn rotate_multiple_backwards() {
        let s = &mut [1, 2, 3, 4, 5];
        s.rotate(-3);
        assert_eq!(&[3, 4, 5, 1, 2], s);
    }

    #[test]
    fn rotate_forwards_overflow() {
        let s = &mut [1, 2, 3, 4, 5];
        s.rotate(6);
        assert_eq!(&[2, 3, 4, 5, 1], s);
    }

    #[test]
    fn rotate_backwards_overflow() {
        let s = &mut [1, 2, 3, 4, 5];
        s.rotate(-6);
        assert_eq!(&[5, 1, 2, 3, 4], s);
    }
}