struct CircularIterator<'a, D: 'a>
{
length: usize,
lastIndex: usize,
nextIndex: usize,
data: &'a mut [&'a mut D],
}
impl<'a, D> CircularIterator<'a, D>
{
#[inline(always)]
pub fn new(data: &'a mut [&'a mut D]) -> Self
{
let length = data.len();
CircularIterator
{
length: length,
lastIndex: if length == 0
{
0
}
else
{
length - 1
},
nextIndex: 0,
data: data,
}
}
#[inline(always)]
pub fn iter_mut<F>(&mut self, mut callback: F)
where F: FnMut(&mut D) -> bool
{
let length = self.length;
let lastIndex = self.lastIndex;
let mut nextIndex;
let mut count = 0;
let ref mut data = self.data;
while count < length
{
nextIndex = self.nextIndex;
if nextIndex == lastIndex
{
nextIndex = 0;
}
else
{
nextIndex += 1;
}
let datum = unsafe { data.get_unchecked_mut(nextIndex) };
if callback(datum)
{
self.nextIndex = nextIndex;
break;
}
count += 1;
}
}
}