Created
June 22, 2017 15:31
-
-
Save leshow/075e81aa2c588bc0cfb7eecb64707a5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::ops::Shl; | |
| #[derive(Debug)] | |
| struct V<T>(Vec<T>); | |
| impl<T> Shl<usize> for V<T> { | |
| type Output = Self; | |
| fn shl(mut self, rhs: usize) -> V<T> { | |
| let l = self.0.len(); | |
| rotate_by(&mut self.0, 0, rhs-1); | |
| rotate_by(&mut self.0, rhs, l-1); | |
| rotate_by(&mut self.0, 0, l-1); | |
| fn rotate_by<T>(arr: &mut [T], mut left: usize, mut right: usize) { | |
| while left < right { | |
| arr.swap(left, right); | |
| left += 1; | |
| right -= 1; | |
| } | |
| } | |
| self | |
| } | |
| } | |
| fn main() { | |
| let v_ = vec![1,2,3,4,5]; | |
| let v = V(v_); | |
| println!("{:?}", (v << 2)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment