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
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{BitSetVec, BitSet};
// TODO try to reuse code between the two iterators

/// Iterates over components using a provided bitset.
/// Each time the bitset has a 1 in index i, the iterator will fetch data
/// from the storage at index i and return it as an `Option`.
pub struct ComponentIterator<'a, T> {
    pub(crate) current_id: usize,
    pub(crate) max_id: usize,
    pub(crate) storage: &'a Vec<Option<T>>,
    pub(crate) bitset: std::rc::Rc<BitSetVec>,
}

impl<'a, T> Iterator for ComponentIterator<'a, T> {
    type Item = Option<&'a T>;
    fn next(&mut self) -> Option<Self::Item> {
        while !self.bitset.bit_test(self.current_id) && self.current_id <= self.max_id {
            self.current_id += 1;
        }
        let ret = if self.current_id < self.max_id {
            Some(self.storage[self.current_id].as_ref())
        } else {
            None
        };
        self.current_id += 1;
        ret
    }
}

/// Iterates over components using a provided bitset.
/// Each time the bitset has a 1 in index i, the iterator will fetch data
/// from the storage at index i and return it as an `Option`.
pub struct ComponentIteratorMut<'a, T> {
    pub(crate) current_id: usize,
    pub(crate) max_id: usize,
    pub(crate) storage: &'a mut Vec<Option<T>>,
    pub(crate) bitset: std::rc::Rc<BitSetVec>,
}

impl<'a, T> Iterator for ComponentIteratorMut<'a, T> {
    type Item = Option<&'a mut T>;
    fn next(&mut self) -> Option<Self::Item> {
        while !self.bitset.bit_test(self.current_id) && self.current_id <= self.max_id {
            self.current_id += 1;
        }
        let ret = if self.current_id < self.max_id {
            let r = self.storage[self.current_id].as_mut().map(|e| unsafe {
                let ptr: *mut T = e;
                &mut *ptr
            });
            Some(r)
        } else {
            None
        };
        self.current_id += 1;
        ret
    }
}