Created
September 12, 2018 14:14
-
-
Save maretekent/98f5e6a5ee1204d09a0e50cf32e19a01 to your computer and use it in GitHub Desktop.
Spin vector
This file contains 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::Shr; | |
#[derive(PartialEq, Debug)] | |
struct SpinVector<T: Clone> { | |
vec: Vec<T>, | |
} | |
impl<T: Clone> Shr<usize> for SpinVector<T> { | |
type Output = Self; | |
fn shr(self, rhs: usize) -> SpinVector<T> { | |
// rotate the vector by `rhs` places | |
let (a, b) = self.vec.split_at(self.vec.len() - rhs); | |
let mut spun_vector: Vec<T> = vec![]; | |
spun_vector.extend_from_slice(b); | |
spun_vector.extend_from_slice(a); | |
SpinVector { vec: spun_vector } | |
} | |
} | |
fn main() { | |
println!("{:?}", SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use std::ops::Shl;
#[derive(PartialEq, Debug)]
struct SpinVector<T: Clone> {
vec: Vec,
}
impl<T: Clone> Shl for SpinVector {
type Output = Self;
}
fn main() {
println!("{:?}", SpinVector { vec: vec![0, 1, 2, 3, 4] } << 1 );
}