-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle.rs
36 lines (33 loc) · 934 Bytes
/
cycle.rs
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
use std::ops::Index;
pub struct Cycle {
max: isize,
slice: Vec<Option<usize>>
}
impl Index<isize> for Cycle {
type Output=Option<usize>;
fn index(&self, index: isize) -> &Option<usize> {
println!("Index {}",index);
if self.max == 0 { //empty interval, return &None
return &self.slice[0];
}
if index == 0 {
return &self.slice[0];
}
let b_ind = self.max + ((index.abs()%self.max) * index.abs()/index);
let b_ind = b_ind%self.max;
return &self.slice[b_ind as usize];
}
}
impl Cycle {
pub fn new(max: usize) -> Cycle {
let mut v = Vec::new();
for i in 0..max {
v.push(Some(i));
}
v.push(None);
Cycle {
max: max as isize,
slice: v
}
}
}