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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use super::abstract_mut::AbstractMut;
use super::with_id::LastId;
use crate::entity_id::EntityId;
#[cfg(feature = "parallel")]
use rayon::iter::plumbing::UnindexedProducer;

#[allow(missing_docs)]
pub struct Mixed<Storage> {
    pub(super) storage: Storage,
    pub(super) indices: *const EntityId,
    pub(super) current: usize,
    pub(super) end: usize,
    pub(super) mask: u16,
    pub(super) last_id: EntityId,
}

unsafe impl<Storage: Send> Send for Mixed<Storage> {}

impl<Storage: AbstractMut> Iterator for Mixed<Storage> {
    type Item = <Storage as AbstractMut>::Out;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        while self.current < self.end {
            self.current += 1;

            let id = unsafe { *self.indices.add(self.current - 1) };

            if let Some(data_indices) = self.storage.indices_of(id, self.current - 1, self.mask) {
                self.last_id = id;
                return Some(unsafe { self.storage.get_datas(data_indices) });
            }
        }

        None
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(self.end - self.current))
    }
    #[inline]
    fn fold<B, F>(mut self, mut init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        while self.current < self.end {
            self.current += 1;

            let id = unsafe { *self.indices.add(self.current - 1) };

            if let Some(data_indices) = self.storage.indices_of(id, self.current - 1, self.mask) {
                self.last_id = id;
                init = f(init, unsafe { self.storage.get_datas(data_indices) });
            }
        }

        init
    }
}

impl<Storage: AbstractMut> DoubleEndedIterator for Mixed<Storage> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        while self.current < self.end {
            self.current += 1;

            let id = unsafe { *self.indices.add(self.end - self.current - 1) };

            if let Some(data_indices) =
                self.storage
                    .indices_of(id, self.end - self.current - 1, self.mask)
            {
                self.last_id = id;
                return Some(unsafe { self.storage.get_datas(data_indices) });
            }
        }

        None
    }

    #[inline]
    fn rfold<B, F>(mut self, mut init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        while self.current < self.end {
            self.current += 1;

            let id = unsafe { *self.indices.add(self.end - self.current - 1) };

            if let Some(data_indices) =
                self.storage
                    .indices_of(id, self.end - self.current - 1, self.mask)
            {
                self.last_id = id;
                init = f(init, unsafe { self.storage.get_datas(data_indices) });
            }
        }

        init
    }
}

impl<Storage: AbstractMut> LastId for Mixed<Storage> {
    #[inline]
    unsafe fn last_id(&self) -> EntityId {
        self.last_id
    }
    #[inline]
    unsafe fn last_id_back(&self) -> EntityId {
        self.last_id
    }
}

#[cfg(feature = "parallel")]
impl<Storage: AbstractMut + Clone + Send> UnindexedProducer for Mixed<Storage> {
    type Item = <Storage as AbstractMut>::Out;

    #[inline]
    fn split(mut self) -> (Self, Option<Self>) {
        let len = self.end - self.current;

        if len >= 2 {
            let clone = Mixed {
                storage: self.storage.clone(),
                indices: self.indices,
                current: self.current + (len / 2),
                end: self.end,
                mask: self.mask,
                last_id: self.last_id,
            };

            self.end = clone.current;

            (self, Some(clone))
        } else {
            (self, None)
        }
    }

    #[inline]
    fn fold_with<F>(self, folder: F) -> F
    where
        F: rayon::iter::plumbing::Folder<Self::Item>,
    {
        folder.consume_iter(self)
    }
}