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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::rc::Rc;

use crate::prelude::*;

/// Read-only iterator over components matching a given bitset
pub type ComponentBitsetIterator<'a, T> =
    std::iter::Map<UntypedComponentBitsetIterator<'a>, for<'b> fn(SchemaRef<'b>) -> &'b T>;

/// Read-only iterator over components matching a given bitset.
/// Returns None for entities matching bitset but not in this ComponentStore.
pub type ComponentBitsetOptionalIterator<'a, T> = std::iter::Map<
    UntypedComponentOptionalBitsetIterator<'a>,
    for<'b> fn(Option<SchemaRef<'b>>) -> Option<&'b T>,
>;

/// Mutable iterator over components matching a given bitset
pub type ComponentBitsetIteratorMut<'a, T> = std::iter::Map<
    UntypedComponentBitsetIteratorMut<'a>,
    for<'b> fn(SchemaRefMut<'b>) -> &'b mut T,
>;

/// Mutable iterator over components matching a given bitset.
/// Returns None for entities matching bitset but not in this ComponentStore.
pub type ComponentBitsetOptionalIteratorMut<'a, T> = std::iter::Map<
    UntypedComponentOptionalBitsetIteratorMut<'a>,
    for<'b> fn(Option<SchemaRefMut<'b>>) -> Option<&'b mut T>,
>;

/// 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.
pub struct UntypedComponentBitsetIterator<'a> {
    pub(crate) current_id: usize,
    pub(crate) components: &'a UntypedComponentStore,
    pub(crate) bitset: Rc<BitSetVec>,
}

impl<'a> Iterator for UntypedComponentBitsetIterator<'a> {
    type Item = SchemaRef<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let max_id = self.components.max_id;
        while self.current_id < max_id
            && !(self.bitset.bit_test(self.current_id)
                && self.components.bitset.bit_test(self.current_id))
        {
            self.current_id += 1;
        }
        let ret = if self.current_id < max_id {
            // SAFE: Here we are just getting a pointer, not doing anything unsafe with it.
            Some(unsafe {
                SchemaRef::from_ptr_schema(
                    self.components.storage.unchecked_idx(self.current_id),
                    self.components.schema,
                )
            })
        } else {
            None
        };
        self.current_id += 1;
        ret
    }
}

/// Iterate over component store returning `Option<SchemaRef<'a>>`,
/// filtered by bitset of iterator, but not bitset of own ComponentStore. Returns None on
/// bitset entries that do not have this Component.
pub struct UntypedComponentOptionalBitsetIterator<'a> {
    /// The component bitset iterator.
    pub inner: UntypedComponentBitsetIterator<'a>,
    /// The number of components (enabled bits in the `inner` iterator).
    pub components_count: usize,
    /// The number of enabled bits in the input bitset.
    pub query_count: usize,
    /// The number of enabled bits discovered in the query bitset. Iteration is complete when this
    /// reaches `query_count`.
    pub found: usize,
}

impl<'a> Iterator for UntypedComponentOptionalBitsetIterator<'a> {
    type Item = Option<SchemaRef<'a>>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.found >= self.query_count {
            // All enabled bits from the query bitset have been discovered. The rest is just zeros,
            // we are done iterating.
            return None;
        }

        if self.components_count == 0 {
            // The component store is empty, no need to test all of the bits.
            self.found += 1;
            return Some(None);
        }

        // We stop iterating at bitset length, not component store length, as we want to iterate over
        // whole bitset and return None for entities that don't have this optional component.
        let max_id = self.inner.bitset.bit_len();
        while self.inner.current_id < max_id && !self.inner.bitset.bit_test(self.inner.current_id) {
            self.inner.current_id += 1;
        }

        let ret = if self.inner.components.bitset.bit_test(self.inner.current_id) {
            self.found += 1;
            // SAFE: Here we are just getting a pointer, not doing anything unsafe with it.
            Some(Some(unsafe {
                SchemaRef::from_ptr_schema(
                    self.inner
                        .components
                        .storage
                        .unchecked_idx(self.inner.current_id),
                    self.inner.components.schema,
                )
            }))
        } else {
            // Component at current_id is not in store, however we are still iterating,
            // later ids in self.bitset may have components in store.
            self.found += 1;
            Some(None)
        };

        self.inner.current_id += 1;

        ret
    }
}

/// Iterate mutably over component store returning `Option<SchemaRef<'a>>`,
/// filtered by bitset of iterator, but not bitset of own ComponentStore. Returns None on
/// bitset entries that do not have this Component.
pub struct UntypedComponentOptionalBitsetIteratorMut<'a> {
    /// The component bitset iterator.
    pub inner: UntypedComponentBitsetIteratorMut<'a>,
    /// The number of components (enabled bits in the `inner` iterator).
    pub components_count: usize,
    /// The number of enabled bits in the input bitset.
    pub query_count: usize,
    /// The number of enabled bits discovered in the query bitset. Iteration is complete when this
    /// reaches `query_count`.
    pub found: usize,
}

impl<'a> Iterator for UntypedComponentOptionalBitsetIteratorMut<'a> {
    type Item = Option<SchemaRefMut<'a>>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.found >= self.query_count {
            // All enabled bits from the query bitset have been discovered. The rest is just zeros,
            // we are done iterating.
            return None;
        }

        if self.components_count == 0 {
            // The component store is empty, no need to test all of the bits.
            self.found += 1;
            return Some(None);
        }

        // We do not stop iterating at component store length, as we want to iterate over
        // whole bitset and return None for entities that don't have this optional component.
        let max_id = self.inner.bitset.bit_len();
        while self.inner.current_id < max_id && !self.inner.bitset.bit_test(self.inner.current_id) {
            self.inner.current_id += 1;
        }

        let ret = if self.inner.components.bitset.bit_test(self.inner.current_id) {
            self.found += 1;
            // SAFE: Here we are just getting a pointer, not doing anything unsafe with it.
            Some(Some(unsafe {
                SchemaRefMut::from_ptr_schema(
                    self.inner
                        .components
                        .storage
                        .unchecked_idx(self.inner.current_id),
                    self.inner.components.schema,
                )
            }))
        } else {
            // Component at current_id is not in store, however we are still iterating,
            // later ids in self.bitset may have components in store.
            self.found += 1;
            Some(None)
        };

        self.inner.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.
pub struct UntypedComponentBitsetIteratorMut<'a> {
    pub(crate) current_id: usize,
    pub(crate) components: &'a mut UntypedComponentStore,
    pub(crate) bitset: Rc<BitSetVec>,
}

impl<'a> Iterator for UntypedComponentBitsetIteratorMut<'a> {
    type Item = SchemaRefMut<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let max_id = self.components.max_id;
        while self.current_id < max_id
            && !(self.bitset.bit_test(self.current_id)
                && self.components.bitset.bit_test(self.current_id))
        {
            self.current_id += 1;
        }
        let ret = if self.current_id < max_id {
            // SAFE: We know that the index is within bounds, and we know that the pointer will be
            // valid for the new lifetime.
            Some(unsafe {
                SchemaRefMut::from_ptr_schema(
                    self.components.storage.unchecked_idx(self.current_id),
                    self.components.schema,
                )
            })
        } else {
            None
        };
        self.current_id += 1;
        ret
    }
}

#[cfg(test)]
mod tests {
    #![allow(non_snake_case)]

    use super::*;

    #[derive(Clone, HasSchema, Default)]
    struct A;

    #[derive(Clone, HasSchema, Default)]
    struct B;

    #[test]
    fn iter_with_empty_bitset() {
        let mut entities = Entities::default();
        let e = entities.create();
        let mut components = ComponentStore::<A>::default();

        components.insert(e, A);

        let bitset = Rc::new(BitSetVec::default());
        assert_eq!(components.iter_with_bitset(bitset.clone()).count(), 0);
        assert_eq!(components.iter_mut_with_bitset(bitset).count(), 0);
    }

    #[test]
    /// Test that iterating with optional components does not filter entities.
    fn iter_with_optional() {
        // Initialize two total entities, both with B, one with A.
        let mut entities = Entities::default();
        let e1 = entities.create();
        let e2 = entities.create();
        let mut components_a = ComponentStore::<A>::default();
        components_a.insert(e1, A);

        let mut components_b = ComponentStore::<B>::default();
        components_b.insert(e1, B);
        components_b.insert(e2, B);

        // Iterate over all entities, optionally retrieve A
        {
            let comp_a = Ref::new(&components_a);
            let mut count_a = 0;
            let mut count = 0;
            for (_, a) in entities.iter_with(&Optional(&comp_a)) {
                count += 1;
                if a.is_some() {
                    count_a += 1;
                }
            }
            assert_eq!(count_a, 1);
            assert_eq!(count, 2);
        }
        // Mutably Iterate over all entities, optionally retrieve A
        {
            let mut comp_a_mut = RefMut::new(&mut components_a);
            let mut count_a = 0;
            let mut count = 0;
            for (_, a) in entities.iter_with(&mut OptionalMut(&mut comp_a_mut)) {
                count += 1;
                if a.is_some() {
                    count_a += 1;
                }
            }
            assert_eq!(count_a, 1);
            assert_eq!(count, 2);
        }

        // Iterate over entities with B and optionaly retrieve A
        {
            let comp_a = Ref::new(&components_a);
            let comp_b = Ref::new(&components_b);
            let mut count_a = 0;
            let mut count = 0;
            for (_, (a, _b)) in entities.iter_with((&Optional(&comp_a), &comp_b)) {
                count += 1;
                if a.is_some() {
                    count_a += 1;
                }
            }
            assert_eq!(count_a, 1);
            assert_eq!(count, 2);
        }

        // Iterate over entities with A, and optionally retrieve B
        {
            let comp_a = Ref::new(&components_a);
            let comp_b = Ref::new(&components_b);
            let mut count = 0;
            for (_, (_a, b)) in entities.iter_with((&comp_a, &Optional(&comp_b))) {
                count += 1;
                assert!(b.is_some());
            }
            assert_eq!(count, 1);
        }

        // Make sure that entities with only optional components are still filtered by others,
        // and not included in query.
        //
        // Case: 4 entities, we query over A and Optionally C, where entities have comps: 0:[AB],1:[B],2:[C],3:[A]
        // Filtered by A, should iterate over entities 0 and 3. Verify that entitiy 2 with C is not included.
        {
            let e3 = entities.create();
            let e4 = entities.create();
            let mut components_c = ComponentStore::<A>::default();
            components_c.insert(e3, A);
            components_a.insert(e4, A);
            let comp_a = Ref::new(&components_a);
            let comp_c = Ref::new(&components_c);

            let mut count = 0;
            for (_, (_, c)) in entities.iter_with((&comp_a, &Optional(&comp_c))) {
                count += 1;
                // Should not iterate over entity with C, as it does not have A.
                assert!(c.is_none());
            }
            // Expected two entities with A
            assert_eq!(count, 2);
        }
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq, HasSchema, Default)]
    struct X(u32);

    #[derive(Debug, Clone, Copy, PartialEq, Eq, HasSchema, Default)]
    struct Y(u32);

    fn entity(index: u32) -> Entity {
        Entity::new(index, 0)
    }

    fn store<C: HasSchema>(entities: &[u32], ctor: fn(u32) -> C) -> ComponentStore<C> {
        let mut store = ComponentStore::default();
        for &i in entities {
            store.insert(entity(i), ctor(i));
        }
        store
    }

    fn bitset(enabled: &[usize]) -> BitSetVec {
        let mut bitset = BitSetVec::default();
        for &i in enabled {
            bitset.bit_set(i);
        }
        bitset
    }

    #[test]
    fn get_single_with_bitset__multiple_required() {
        {
            let bitset = bitset(&[]);
            let (store_x, store_y) = (store(&[], X), store(&[], Y));
            let query = (&Ref::new(&store_x), &Ref::new(&store_y));

            let result = query.get_single_with_bitset(Rc::new(bitset));

            assert_eq!(result, Err(QuerySingleError::NoEntities));
        }

        {
            let bitset = bitset(&[1]);
            let store_x = store(&[1], X);
            let store_y = store(&[1], Y);
            let query = (&Ref::new(&store_x), &Ref::new(&store_y));

            let result = query.get_single_with_bitset(Rc::new(bitset));

            assert_eq!(result, Ok((&X(1), &Y(1))));
        }

        {
            let bitset = bitset(&[1, 2]);
            let store_x = store(&[1, 2], X);
            let store_y = store(&[1, 2], Y);
            let query = (&Ref::new(&store_x), &Ref::new(&store_y));

            let result = query.get_single_with_bitset(Rc::new(bitset));

            assert_eq!(result, Err(QuerySingleError::MultipleEntities));
        }
    }
}