Skip to content

Instantly share code, notes, and snippets.

@maretekent
Created September 12, 2018 14:14
Show Gist options
  • Save maretekent/98f5e6a5ee1204d09a0e50cf32e19a01 to your computer and use it in GitHub Desktop.
Save maretekent/98f5e6a5ee1204d09a0e50cf32e19a01 to your computer and use it in GitHub Desktop.
Spin vector
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 );
}
@maretekent
Copy link
Author

use std::ops::Shl;

#[derive(PartialEq, Debug)]
struct SpinVector<T: Clone> {
vec: Vec,
}

impl<T: Clone> Shl for SpinVector {
type Output = Self;

fn shl(self, lhs: usize) -> SpinVector<T> {
    // rotate the vector by `lhs` places
    let (a, b) = self.vec.split_at(self.vec.len() - lhs);
    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] } << 1 );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment