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
35
36
37
38
39
40
41
42
43
44
45
use crate::bits::BitIter;
use crate::svec::{Store, StoreMut, VectorMask};
use std::mem;

/// Iterate over the non-zero (non-mutable) elements of a vector
pub struct DataIter<'a, S>
where
    S: Store,
{
    pub(crate) iterator: BitIter<&'a VectorMask>,
    pub(crate) store: &'a S,
}

impl<'a, S> Iterator for DataIter<'a, S>
where
    S: 'a + Store,
{
    type Item = &'a S::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.iterator.next().map(|idx| self.store.get(idx))
    }
}

/// Iterate over the non-zero (mutable) elements of a vector
pub struct DataIterMut<'a, S>
where
    S: StoreMut,
{
    pub(crate) iterator: BitIter<&'a VectorMask>,
    pub(crate) store: &'a mut S,
}

impl<'a, S> Iterator for DataIterMut<'a, S>
where
    S: 'a + StoreMut,
{
    type Item = &'a mut S::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.iterator
            .next()
            .map(|idx| unsafe { mem::transmute(self.store.get_mut(idx)) }) // GAT
    }
}