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
use crate::world::{Entities, EntityIdSet, World};
use super::{QueryItem, QueryState, ReadOnlyWorldQuery, WorldQuery};
#[allow(dead_code)]
pub struct QueryIter<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery = ()> {
pub(crate) query_state: &'s QueryState<Q, F>,
pub(crate) cursor: QueryIterationCursor<'w, Q, F>,
}
impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIter<'w, 's, Q, F> {
#[inline]
pub unsafe fn new(
query_state: &'s QueryState<Q, F>,
world: &'w World,
last_change_tick: u32,
change_tick: u32,
) -> Self {
Self {
query_state,
cursor: unsafe {
QueryIterationCursor::new(query_state, world, last_change_tick, change_tick)
},
}
}
}
impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Iterator for QueryIter<'w, 's, Q, F> {
type Item = QueryItem<'w, Q>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.cursor.next()
}
}
pub(crate) struct QueryIterationCursor<'w, Q: WorldQuery, F: ReadOnlyWorldQuery = ()> {
pub(crate) entity_ids: EntityIdSet,
pub(crate) current_index: usize,
pub(crate) entities: &'w Entities,
pub(crate) fetch: Q::Fetch<'w>,
pub(crate) filter: F::Fetch<'w>,
}
impl<'w, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIterationCursor<'w, Q, F> {
#[inline]
unsafe fn new(
query_state: &QueryState<Q, F>,
world: &'w World,
last_change_tick: u32,
change_tick: u32,
) -> Self {
query_state.debug_validate_world(world);
let fetch = unsafe {
Q::init_fetch(
world,
&query_state.query_state,
last_change_tick,
change_tick,
)
};
let filter = unsafe {
F::init_fetch(
world,
&query_state.filter_state,
last_change_tick,
change_tick,
)
};
Self {
entity_ids: query_state.get_entities(world),
current_index: 0,
entities: &world.entities,
fetch,
filter,
}
}
#[inline]
fn next(&mut self) -> Option<QueryItem<'w, Q>> {
loop {
if self.current_index >= self.entity_ids.len() {
return None;
}
if self.entity_ids.contains(self.current_index) {
let entity = unsafe { self.entities.get_unchecked(self.current_index) };
if unsafe { F::filter_fetch(&mut self.filter, entity) } {
self.current_index += 1;
return Some(unsafe { Q::fetch(&mut self.fetch, entity) });
}
}
self.current_index += 1;
}
}
}