bevy_ecs/query/
iter.rs

1use super::{QueryData, QueryFilter, ReadOnlyQueryData};
2use crate::{
3    archetype::{Archetype, ArchetypeEntity, Archetypes},
4    bundle::Bundle,
5    component::Tick,
6    entity::{ContainsEntity, Entities, Entity, EntityEquivalent, EntitySet, EntitySetIterator},
7    query::{ArchetypeFilter, DebugCheckedUnwrap, QueryState, StorageId},
8    storage::{Table, TableRow, Tables},
9    world::{
10        unsafe_world_cell::UnsafeWorldCell, EntityMut, EntityMutExcept, EntityRef, EntityRefExcept,
11        FilteredEntityMut, FilteredEntityRef,
12    },
13};
14use alloc::vec::Vec;
15use core::{
16    cmp::Ordering,
17    fmt::{self, Debug, Formatter},
18    iter::FusedIterator,
19    mem::MaybeUninit,
20    ops::Range,
21};
22
23/// An [`Iterator`] over query results of a [`Query`](crate::system::Query).
24///
25/// This struct is created by the [`Query::iter`](crate::system::Query::iter) and
26/// [`Query::iter_mut`](crate::system::Query::iter_mut) methods.
27pub struct QueryIter<'w, 's, D: QueryData, F: QueryFilter> {
28    world: UnsafeWorldCell<'w>,
29    tables: &'w Tables,
30    archetypes: &'w Archetypes,
31    query_state: &'s QueryState<D, F>,
32    cursor: QueryIterationCursor<'w, 's, D, F>,
33}
34
35impl<'w, 's, D: QueryData, F: QueryFilter> QueryIter<'w, 's, D, F> {
36    /// # Safety
37    /// - `world` must have permission to access any of the components registered in `query_state`.
38    /// - `world` must be the same one used to initialize `query_state`.
39    pub(crate) unsafe fn new(
40        world: UnsafeWorldCell<'w>,
41        query_state: &'s QueryState<D, F>,
42        last_run: Tick,
43        this_run: Tick,
44    ) -> Self {
45        QueryIter {
46            world,
47            query_state,
48            // SAFETY: We only access table data that has been registered in `query_state`.
49            tables: unsafe { &world.storages().tables },
50            archetypes: world.archetypes(),
51            // SAFETY: The invariants are upheld by the caller.
52            cursor: unsafe { QueryIterationCursor::init(world, query_state, last_run, this_run) },
53        }
54    }
55
56    /// Creates a new separate iterator yielding the same remaining items of the current one.
57    /// Advancing the new iterator will not advance the original one, which will resume at the
58    /// point it was left at.
59    ///
60    /// Differently from [`remaining_mut`](QueryIter::remaining_mut) the new iterator does not
61    /// borrow from the original one. However it can only be called from an iterator over read only
62    /// items.
63    ///
64    /// # Example
65    ///
66    /// ```
67    /// # use bevy_ecs::prelude::*;
68    /// #
69    /// # #[derive(Component)]
70    /// # struct ComponentA;
71    ///
72    /// fn combinations(query: Query<&ComponentA>) {
73    ///     let mut iter = query.iter();
74    ///     while let Some(a) = iter.next() {
75    ///         for b in iter.remaining() {
76    ///             // Check every combination (a, b)
77    ///         }
78    ///     }
79    /// }
80    /// ```
81    pub fn remaining(&self) -> QueryIter<'w, 's, D, F>
82    where
83        D: ReadOnlyQueryData,
84    {
85        QueryIter {
86            world: self.world,
87            tables: self.tables,
88            archetypes: self.archetypes,
89            query_state: self.query_state,
90            cursor: self.cursor.clone(),
91        }
92    }
93
94    /// Creates a new separate iterator yielding the same remaining items of the current one.
95    /// Advancing the new iterator will not advance the original one, which will resume at the
96    /// point it was left at.
97    ///
98    /// This method can be called on iterators over mutable items. However the original iterator
99    /// will be borrowed while the new iterator exists and will thus not be usable in that timespan.
100    ///
101    /// # Example
102    ///
103    /// ```
104    /// # use bevy_ecs::prelude::*;
105    /// #
106    /// # #[derive(Component)]
107    /// # struct ComponentA;
108    ///
109    /// fn combinations(mut query: Query<&mut ComponentA>) {
110    ///     let mut iter = query.iter_mut();
111    ///     while let Some(a) = iter.next() {
112    ///         for b in iter.remaining_mut() {
113    ///             // Check every combination (a, b)
114    ///         }
115    ///     }
116    /// }
117    /// ```
118    pub fn remaining_mut(&mut self) -> QueryIter<'_, 's, D, F> {
119        QueryIter {
120            world: self.world,
121            tables: self.tables,
122            archetypes: self.archetypes,
123            query_state: self.query_state,
124            cursor: self.cursor.reborrow(),
125        }
126    }
127
128    /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
129    /// from a storage.
130    ///
131    ///  # Safety
132    ///  - `range` must be in `[0, storage::entity_count)` or None.
133    #[inline]
134    pub(super) unsafe fn fold_over_storage_range<B, Func>(
135        &mut self,
136        mut accum: B,
137        func: &mut Func,
138        storage: StorageId,
139        range: Option<Range<usize>>,
140    ) -> B
141    where
142        Func: FnMut(B, D::Item<'w>) -> B,
143    {
144        if self.cursor.is_dense {
145            // SAFETY: `self.cursor.is_dense` is true, so storage ids are guaranteed to be table ids.
146            let table_id = unsafe { storage.table_id };
147            // SAFETY: Matched table IDs are guaranteed to still exist.
148            let table = unsafe { self.tables.get(table_id).debug_checked_unwrap() };
149
150            let range = range.unwrap_or(0..table.entity_count());
151            accum =
152                // SAFETY:
153                // - The fetched table matches both D and F
154                // - caller ensures `range` is within `[0, table.entity_count)`
155                // - The if block ensures that the query iteration is dense
156                unsafe { self.fold_over_table_range(accum, func, table, range) };
157        } else {
158            // SAFETY: `self.cursor.is_dense` is false, so storage ids are guaranteed to be archetype ids.
159            let archetype_id = unsafe { storage.archetype_id };
160            // SAFETY: Matched archetype IDs are guaranteed to still exist.
161            let archetype = unsafe { self.archetypes.get(archetype_id).debug_checked_unwrap() };
162            // SAFETY: Matched table IDs are guaranteed to still exist.
163            let table = unsafe { self.tables.get(archetype.table_id()).debug_checked_unwrap() };
164
165            let range = range.unwrap_or(0..archetype.len());
166
167            // When an archetype and its table have equal entity counts, dense iteration can be safely used.
168            // this leverages cache locality to optimize performance.
169            if table.entity_count() == archetype.len() {
170                accum =
171                // SAFETY:
172                // - The fetched archetype matches both D and F
173                // - The provided archetype and its' table have the same length.
174                // - caller ensures `range` is within `[0, archetype.len)`
175                // - The if block ensures that the query iteration is not dense.
176                unsafe { self.fold_over_dense_archetype_range(accum, func, archetype,range) };
177            } else {
178                accum =
179                // SAFETY:
180                // - The fetched archetype matches both D and F
181                // - caller ensures `range` is within `[0, archetype.len)`
182                // - The if block ensures that the query iteration is not dense.
183                unsafe { self.fold_over_archetype_range(accum, func, archetype,range) };
184            }
185        }
186        accum
187    }
188
189    /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
190    /// from a table.
191    ///
192    /// # Safety
193    ///  - all `rows` must be in `[0, table.entity_count)`.
194    ///  - `table` must match D and F
195    ///  - The query iteration must be dense (i.e. `self.query_state.is_dense` must be true).
196    #[inline]
197    pub(super) unsafe fn fold_over_table_range<B, Func>(
198        &mut self,
199        mut accum: B,
200        func: &mut Func,
201        table: &'w Table,
202        rows: Range<usize>,
203    ) -> B
204    where
205        Func: FnMut(B, D::Item<'w>) -> B,
206    {
207        if table.is_empty() {
208            return accum;
209        }
210        debug_assert!(
211            rows.end <= u32::MAX as usize,
212            "TableRow is only valid up to u32::MAX"
213        );
214
215        D::set_table(&mut self.cursor.fetch, &self.query_state.fetch_state, table);
216        F::set_table(
217            &mut self.cursor.filter,
218            &self.query_state.filter_state,
219            table,
220        );
221
222        let entities = table.entities();
223        for row in rows {
224            // SAFETY: Caller assures `row` in range of the current archetype.
225            let entity = unsafe { entities.get_unchecked(row) };
226            let row = TableRow::from_usize(row);
227
228            // SAFETY: set_table was called prior.
229            // Caller assures `row` in range of the current archetype.
230            let fetched = unsafe { !F::filter_fetch(&mut self.cursor.filter, *entity, row) };
231            if fetched {
232                continue;
233            }
234
235            // SAFETY: set_table was called prior.
236            // Caller assures `row` in range of the current archetype.
237            let item = D::fetch(&mut self.cursor.fetch, *entity, row);
238
239            accum = func(accum, item);
240        }
241        accum
242    }
243
244    /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
245    /// from an archetype.
246    ///
247    /// # Safety
248    ///  - all `indices` must be in `[0, archetype.len())`.
249    ///  - `archetype` must match D and F
250    ///  - The query iteration must not be dense (i.e. `self.query_state.is_dense` must be false).
251    #[inline]
252    pub(super) unsafe fn fold_over_archetype_range<B, Func>(
253        &mut self,
254        mut accum: B,
255        func: &mut Func,
256        archetype: &'w Archetype,
257        indices: Range<usize>,
258    ) -> B
259    where
260        Func: FnMut(B, D::Item<'w>) -> B,
261    {
262        if archetype.is_empty() {
263            return accum;
264        }
265        let table = self.tables.get(archetype.table_id()).debug_checked_unwrap();
266        D::set_archetype(
267            &mut self.cursor.fetch,
268            &self.query_state.fetch_state,
269            archetype,
270            table,
271        );
272        F::set_archetype(
273            &mut self.cursor.filter,
274            &self.query_state.filter_state,
275            archetype,
276            table,
277        );
278
279        let entities = archetype.entities();
280        for index in indices {
281            // SAFETY: Caller assures `index` in range of the current archetype.
282            let archetype_entity = unsafe { entities.get_unchecked(index) };
283
284            // SAFETY: set_archetype was called prior.
285            // Caller assures `index` in range of the current archetype.
286            let fetched = unsafe {
287                !F::filter_fetch(
288                    &mut self.cursor.filter,
289                    archetype_entity.id(),
290                    archetype_entity.table_row(),
291                )
292            };
293            if fetched {
294                continue;
295            }
296
297            // SAFETY: set_archetype was called prior, `index` is an archetype index in range of the current archetype
298            // Caller assures `index` in range of the current archetype.
299            let item = unsafe {
300                D::fetch(
301                    &mut self.cursor.fetch,
302                    archetype_entity.id(),
303                    archetype_entity.table_row(),
304                )
305            };
306
307            accum = func(accum, item);
308        }
309        accum
310    }
311
312    /// Executes the equivalent of [`Iterator::fold`] over a contiguous segment
313    /// from an archetype which has the same entity count as its table.
314    ///
315    /// # Safety
316    ///  - all `indices` must be in `[0, archetype.len())`.
317    ///  - `archetype` must match D and F
318    ///  - `archetype` must have the same length with it's table.
319    ///  - The query iteration must not be dense (i.e. `self.query_state.is_dense` must be false).
320    #[inline]
321    pub(super) unsafe fn fold_over_dense_archetype_range<B, Func>(
322        &mut self,
323        mut accum: B,
324        func: &mut Func,
325        archetype: &'w Archetype,
326        rows: Range<usize>,
327    ) -> B
328    where
329        Func: FnMut(B, D::Item<'w>) -> B,
330    {
331        if archetype.is_empty() {
332            return accum;
333        }
334        debug_assert!(
335            rows.end <= u32::MAX as usize,
336            "TableRow is only valid up to u32::MAX"
337        );
338        let table = self.tables.get(archetype.table_id()).debug_checked_unwrap();
339        debug_assert!(
340            archetype.len() == table.entity_count(),
341            "archetype and it's table must have the same length. "
342        );
343
344        D::set_archetype(
345            &mut self.cursor.fetch,
346            &self.query_state.fetch_state,
347            archetype,
348            table,
349        );
350        F::set_archetype(
351            &mut self.cursor.filter,
352            &self.query_state.filter_state,
353            archetype,
354            table,
355        );
356        let entities = table.entities();
357        for row in rows {
358            // SAFETY: Caller assures `row` in range of the current archetype.
359            let entity = unsafe { *entities.get_unchecked(row) };
360            let row = TableRow::from_usize(row);
361
362            // SAFETY: set_table was called prior.
363            // Caller assures `row` in range of the current archetype.
364            let filter_matched = unsafe { F::filter_fetch(&mut self.cursor.filter, entity, row) };
365            if !filter_matched {
366                continue;
367            }
368
369            // SAFETY: set_table was called prior.
370            // Caller assures `row` in range of the current archetype.
371            let item = D::fetch(&mut self.cursor.fetch, entity, row);
372
373            accum = func(accum, item);
374        }
375        accum
376    }
377
378    /// Sorts all query items into a new iterator, using the query lens as a key.
379    ///
380    /// This sort is stable (i.e., does not reorder equal elements).
381    ///
382    /// This uses [`slice::sort`] internally.
383    ///
384    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
385    /// This includes the allowed parameter type changes listed under [allowed transmutes].
386    /// However, the lens uses the filter of the original query when present.
387    ///
388    /// The sort is not cached across system runs.
389    ///
390    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
391    ///
392    /// # Panics
393    ///
394    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
395    ///
396    /// # Examples
397    /// ```rust
398    /// # use bevy_ecs::prelude::*;
399    /// # use std::{ops::{Deref, DerefMut}, iter::Sum};
400    /// #
401    /// # #[derive(Component)]
402    /// # struct PartMarker;
403    /// #
404    /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
405    /// # struct PartIndex(usize);
406    /// #
407    /// # #[derive(Component, Clone, Copy)]
408    /// # struct PartValue(f32);
409    /// #
410    /// # impl Deref for PartValue {
411    /// #     type Target = f32;
412    /// #
413    /// #     fn deref(&self) -> &Self::Target {
414    /// #         &self.0
415    /// #     }
416    /// # }
417    /// #
418    /// # #[derive(Component)]
419    /// # struct ParentValue(f32);
420    /// #
421    /// # impl Deref for ParentValue {
422    /// #     type Target = f32;
423    /// #
424    /// #     fn deref(&self) -> &Self::Target {
425    /// #         &self.0
426    /// #     }
427    /// # }
428    /// #
429    /// # impl DerefMut for ParentValue {
430    /// #     fn deref_mut(&mut self) -> &mut Self::Target {
431    /// #         &mut self.0
432    /// #     }
433    /// # }
434    /// #
435    /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
436    /// # struct Length(usize);
437    /// #
438    /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
439    /// # struct Width(usize);
440    /// #
441    /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
442    /// # struct Height(usize);
443    /// #
444    /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
445    /// # struct ParentEntity(Entity);
446    /// #
447    /// # #[derive(Component, Clone, Copy)]
448    /// # struct ChildPartCount(usize);
449    /// #
450    /// # impl Deref for ChildPartCount {
451    /// #     type Target = usize;
452    /// #
453    /// #     fn deref(&self) -> &Self::Target {
454    /// #         &self.0
455    /// #     }
456    /// # }
457    /// # let mut world = World::new();
458    /// // We can ensure that a query always returns in the same order.
459    /// fn system_1(query: Query<(Entity, &PartIndex)>) {
460    ///     let parts: Vec<(Entity, &PartIndex)> = query.iter().sort::<&PartIndex>().collect();
461    /// }
462    ///
463    /// // We can freely rearrange query components in the key.
464    /// fn system_2(query: Query<(&Length, &Width, &Height), With<PartMarker>>) {
465    ///     for (length, width, height) in query.iter().sort::<(&Height, &Length, &Width)>() {
466    ///         println!("height: {height:?}, width: {width:?}, length: {length:?}")
467    ///     }
468    /// }
469    ///
470    /// // We can sort by Entity without including it in the original Query.
471    /// // Here, we match iteration orders between query iterators.
472    /// fn system_3(
473    ///     part_query: Query<(&PartValue, &ParentEntity)>,
474    ///     mut parent_query: Query<(&ChildPartCount, &mut ParentValue)>,
475    /// ) {
476    ///     let part_values = &mut part_query
477    ///         .into_iter()
478    ///         .sort::<&ParentEntity>()
479    ///         .map(|(&value, parent_entity)| *value);
480    ///
481    ///     for (&child_count, mut parent_value) in parent_query.iter_mut().sort::<Entity>() {
482    ///         **parent_value = part_values.take(*child_count).sum();
483    ///     }
484    /// }
485    /// #
486    /// # let mut schedule = Schedule::default();
487    /// # schedule.add_systems((system_1, system_2, system_3));
488    /// # schedule.run(&mut world);
489    /// ```
490    pub fn sort<L: ReadOnlyQueryData + 'w>(
491        self,
492    ) -> QuerySortedIter<
493        'w,
494        's,
495        D,
496        F,
497        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
498    >
499    where
500        for<'lw> L::Item<'lw>: Ord,
501    {
502        self.sort_impl::<L>(|keyed_query| keyed_query.sort())
503    }
504
505    /// Sorts all query items into a new iterator, using the query lens as a key.
506    ///
507    /// This sort is unstable (i.e., may reorder equal elements).
508    ///
509    /// This uses [`slice::sort_unstable`] internally.
510    ///
511    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
512    /// This includes the allowed parameter type changes listed under [allowed transmutes]..
513    /// However, the lens uses the filter of the original query when present.
514    ///
515    /// The sort is not cached across system runs.
516    ///
517    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
518    ///
519    /// # Panics
520    ///
521    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
522    ///
523    /// # Example
524    /// ```
525    /// # use bevy_ecs::prelude::*;
526    /// #
527    /// # let mut world = World::new();
528    /// #
529    /// # #[derive(Component)]
530    /// # struct PartMarker;
531    /// #
532    /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
533    /// enum Flying {
534    ///     Enabled,
535    ///     Disabled
536    /// };
537    ///
538    /// // We perform an unstable sort by a Component with few values.
539    /// fn system_1(query: Query<&Flying, With<PartMarker>>) {
540    ///     let part_values: Vec<&Flying> = query.iter().sort_unstable::<&Flying>().collect();
541    /// }
542    /// #
543    /// # let mut schedule = Schedule::default();
544    /// # schedule.add_systems((system_1));
545    /// # schedule.run(&mut world);
546    /// ```
547    pub fn sort_unstable<L: ReadOnlyQueryData + 'w>(
548        self,
549    ) -> QuerySortedIter<
550        'w,
551        's,
552        D,
553        F,
554        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
555    >
556    where
557        for<'lw> L::Item<'lw>: Ord,
558    {
559        self.sort_impl::<L>(|keyed_query| keyed_query.sort_unstable())
560    }
561
562    /// Sorts all query items into a new iterator with a comparator function over the query lens.
563    ///
564    /// This sort is stable (i.e., does not reorder equal elements).
565    ///
566    /// This uses [`slice::sort_by`] internally.
567    ///
568    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
569    /// This includes the allowed parameter type changes listed under [allowed transmutes].
570    /// However, the lens uses the filter of the original query when present.
571    ///
572    /// The sort is not cached across system runs.
573    ///
574    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
575    ///
576    /// # Panics
577    ///
578    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
579    ///
580    /// # Example
581    /// ```
582    /// # use bevy_ecs::prelude::*;
583    /// # use std::ops::Deref;
584    /// #
585    /// # impl Deref for PartValue {
586    /// #     type Target = f32;
587    /// #
588    /// #     fn deref(&self) -> &Self::Target {
589    /// #         &self.0
590    /// #     }
591    /// # }
592    /// #
593    /// # let mut world = World::new();
594    /// #
595    /// #[derive(Component)]
596    /// struct PartValue(f32);
597    ///
598    /// // We can use a cmp function on components do not implement Ord.
599    /// fn system_1(query: Query<&PartValue>) {
600    ///     // Sort part values according to `f32::total_comp`.
601    ///     let part_values: Vec<&PartValue> = query
602    ///         .iter()
603    ///         .sort_by::<&PartValue>(|value_1, value_2| value_1.total_cmp(*value_2))
604    ///         .collect();
605    /// }
606    /// #
607    /// # let mut schedule = Schedule::default();
608    /// # schedule.add_systems((system_1));
609    /// # schedule.run(&mut world);
610    /// ```
611    pub fn sort_by<L: ReadOnlyQueryData + 'w>(
612        self,
613        mut compare: impl FnMut(&L::Item<'_>, &L::Item<'_>) -> Ordering,
614    ) -> QuerySortedIter<
615        'w,
616        's,
617        D,
618        F,
619        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
620    > {
621        self.sort_impl::<L>(move |keyed_query| {
622            keyed_query.sort_by(|(key_1, _), (key_2, _)| compare(key_1, key_2));
623        })
624    }
625
626    /// Sorts all query items into a new iterator with a comparator function over the query lens.
627    ///
628    /// This sort is unstable (i.e., may reorder equal elements).
629    ///
630    /// This uses [`slice::sort_unstable_by`] internally.
631    ///
632    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
633    /// This includes the allowed parameter type changes listed under [allowed transmutes].
634    /// However, the lens uses the filter of the original query when present.
635    ///
636    /// The sort is not cached across system runs.
637    ///
638    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
639    ///
640    /// # Panics
641    ///
642    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
643    pub fn sort_unstable_by<L: ReadOnlyQueryData + 'w>(
644        self,
645        mut compare: impl FnMut(&L::Item<'_>, &L::Item<'_>) -> Ordering,
646    ) -> QuerySortedIter<
647        'w,
648        's,
649        D,
650        F,
651        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
652    > {
653        self.sort_impl::<L>(move |keyed_query| {
654            keyed_query.sort_unstable_by(|(key_1, _), (key_2, _)| compare(key_1, key_2));
655        })
656    }
657
658    /// Sorts all query items into a new iterator with a key extraction function over the query lens.
659    ///
660    /// This sort is stable (i.e., does not reorder equal elements).
661    ///
662    /// This uses [`slice::sort_by_key`] internally.
663    ///
664    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
665    /// This includes the allowed parameter type changes listed under [allowed transmutes].
666    /// However, the lens uses the filter of the original query when present.
667    ///
668    /// The sort is not cached across system runs.
669    ///
670    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
671    ///
672    /// # Panics
673    ///
674    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
675    ///
676    /// # Example
677    /// ```
678    /// # use bevy_ecs::prelude::*;
679    /// # use std::ops::Deref;
680    /// #
681    /// # #[derive(Component)]
682    /// # struct PartMarker;
683    /// #
684    /// # impl Deref for PartValue {
685    /// #     type Target = f32;
686    /// #
687    /// #     fn deref(&self) -> &Self::Target {
688    /// #         &self.0
689    /// #     }
690    /// # }
691    /// #
692    /// # let mut world = World::new();
693    /// #
694    /// #[derive(Component)]
695    /// struct AvailableMarker;
696    ///
697    /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
698    /// enum Rarity {
699    ///   Common,
700    ///   Rare,
701    ///   Epic,
702    ///   Legendary
703    /// };
704    ///
705    /// #[derive(Component)]
706    /// struct PartValue(f32);
707    ///
708    /// // We can sort with the internals of components that do not implement Ord.
709    /// fn system_1(query: Query<(Entity, &PartValue)>) {
710    ///     // Sort by the sines of the part values.
711    ///     let parts: Vec<(Entity, &PartValue)> = query
712    ///         .iter()
713    ///         .sort_by_key::<&PartValue, _>(|value| value.sin() as usize)
714    ///         .collect();
715    /// }
716    ///
717    /// // We can define our own custom comparison functions over an EntityRef.
718    /// fn system_2(query: Query<EntityRef, With<PartMarker>>) {
719    ///     // Sort by whether parts are available and their rarity.
720    ///     // We want the available legendaries to come first, so we reverse the iterator.
721    ///     let parts: Vec<EntityRef> = query.iter()
722    ///         .sort_by_key::<EntityRef, _>(|entity_ref| {
723    ///             (
724    ///                 entity_ref.contains::<AvailableMarker>(),
725    ///                 entity_ref.get::<Rarity>().copied()
726    ///             )
727    ///         })
728    ///         .rev()
729    ///         .collect();
730    /// }
731    /// # let mut schedule = Schedule::default();
732    /// # schedule.add_systems((system_1, system_2));
733    /// # schedule.run(&mut world);
734    /// ```
735    pub fn sort_by_key<L: ReadOnlyQueryData + 'w, K>(
736        self,
737        mut f: impl FnMut(&L::Item<'_>) -> K,
738    ) -> QuerySortedIter<
739        'w,
740        's,
741        D,
742        F,
743        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
744    >
745    where
746        K: Ord,
747    {
748        self.sort_impl::<L>(move |keyed_query| keyed_query.sort_by_key(|(lens, _)| f(lens)))
749    }
750
751    /// Sorts all query items into a new iterator with a key extraction function over the query lens.
752    ///
753    /// This sort is unstable (i.e., may reorder equal elements).
754    ///
755    /// This uses [`slice::sort_unstable_by_key`] internally.
756    ///
757    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
758    /// This includes the allowed parameter type changes listed under [allowed transmutes].
759    /// However, the lens uses the filter of the original query when present.
760    ///
761    /// The sort is not cached across system runs.
762    ///
763    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
764    ///
765    /// # Panics
766    ///
767    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
768    pub fn sort_unstable_by_key<L: ReadOnlyQueryData + 'w, K>(
769        self,
770        mut f: impl FnMut(&L::Item<'_>) -> K,
771    ) -> QuerySortedIter<
772        'w,
773        's,
774        D,
775        F,
776        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
777    >
778    where
779        K: Ord,
780    {
781        self.sort_impl::<L>(move |keyed_query| {
782            keyed_query.sort_unstable_by_key(|(lens, _)| f(lens));
783        })
784    }
785
786    /// Sort all query items into a new iterator with a key extraction function over the query lens.
787    ///
788    /// This sort is stable (i.e., does not reorder equal elements).
789    ///
790    /// This uses [`slice::sort_by_cached_key`] internally.
791    ///
792    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
793    /// This includes the allowed parameter type changes listed under [allowed transmutes].
794    /// However, the lens uses the filter of the original query when present.
795    ///
796    /// The sort is not cached across system runs.
797    ///
798    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
799    ///
800    /// # Panics
801    ///
802    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
803    pub fn sort_by_cached_key<L: ReadOnlyQueryData + 'w, K>(
804        self,
805        mut f: impl FnMut(&L::Item<'_>) -> K,
806    ) -> QuerySortedIter<
807        'w,
808        's,
809        D,
810        F,
811        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
812    >
813    where
814        K: Ord,
815    {
816        self.sort_impl::<L>(move |keyed_query| keyed_query.sort_by_cached_key(|(lens, _)| f(lens)))
817    }
818
819    /// Shared implementation for the various `sort` methods.
820    /// This uses the lens to collect the items for sorting, but delegates the actual sorting to the provided closure.
821    ///
822    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
823    /// This includes the allowed parameter type changes listed under [allowed transmutes].
824    /// However, the lens uses the filter of the original query when present.
825    ///
826    /// The sort is not cached across system runs.
827    ///
828    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
829    ///
830    /// # Panics
831    ///
832    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.
833    fn sort_impl<L: ReadOnlyQueryData + 'w>(
834        self,
835        f: impl FnOnce(&mut Vec<(L::Item<'_>, NeutralOrd<Entity>)>),
836    ) -> QuerySortedIter<
837        'w,
838        's,
839        D,
840        F,
841        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
842    > {
843        // On the first successful iteration of `QueryIterationCursor`, `archetype_entities` or `table_entities`
844        // will be set to a non-zero value. The correctness of this method relies on this.
845        // I.e. this sort method will execute if and only if `next` on `QueryIterationCursor` of a
846        // non-empty `QueryIter` has not yet been called. When empty, this sort method will not panic.
847        if !self.cursor.archetype_entities.is_empty() || !self.cursor.table_entities.is_empty() {
848            panic!("it is not valid to call sort() after next()")
849        }
850
851        let world = self.world;
852
853        let query_lens_state = self.query_state.transmute_filtered::<(L, Entity), F>(world);
854
855        // SAFETY:
856        // `self.world` has permission to access the required components.
857        // The original query iter has not been iterated on, so no items are aliased from it.
858        // `QueryIter::new` ensures `world` is the same one used to initialize `query_state`.
859        let query_lens = unsafe { query_lens_state.query_unchecked_manual(world) }.into_iter();
860        let mut keyed_query: Vec<_> = query_lens
861            .map(|(key, entity)| (key, NeutralOrd(entity)))
862            .collect();
863        f(&mut keyed_query);
864        let entity_iter = keyed_query.into_iter().map(|(.., entity)| entity.0);
865        // SAFETY:
866        // `self.world` has permission to access the required components.
867        // Each lens query item is dropped before the respective actual query item is accessed.
868        unsafe {
869            QuerySortedIter::new(
870                world,
871                self.query_state,
872                entity_iter,
873                world.last_change_tick(),
874                world.change_tick(),
875            )
876        }
877    }
878}
879
880impl<'w, 's, D: QueryData, F: QueryFilter> Iterator for QueryIter<'w, 's, D, F> {
881    type Item = D::Item<'w>;
882
883    #[inline(always)]
884    fn next(&mut self) -> Option<Self::Item> {
885        // SAFETY:
886        // `tables` and `archetypes` belong to the same world that the cursor was initialized for.
887        // `query_state` is the state that was passed to `QueryIterationCursor::init`.
888        unsafe {
889            self.cursor
890                .next(self.tables, self.archetypes, self.query_state)
891        }
892    }
893
894    fn size_hint(&self) -> (usize, Option<usize>) {
895        let max_size = self.cursor.max_remaining(self.tables, self.archetypes);
896        let archetype_query = F::IS_ARCHETYPAL;
897        let min_size = if archetype_query { max_size } else { 0 };
898        (min_size, Some(max_size))
899    }
900
901    #[inline]
902    fn fold<B, Func>(mut self, init: B, mut func: Func) -> B
903    where
904        Func: FnMut(B, Self::Item) -> B,
905    {
906        let mut accum = init;
907        // Empty any remaining uniterated values from the current table/archetype
908        while self.cursor.current_row != self.cursor.current_len {
909            let Some(item) = self.next() else { break };
910            accum = func(accum, item);
911        }
912
913        for id in self.cursor.storage_id_iter.clone().copied() {
914            // SAFETY:
915            // - The range(None) is equivalent to [0, storage.entity_count)
916            accum = unsafe { self.fold_over_storage_range(accum, &mut func, id, None) };
917        }
918        accum
919    }
920}
921
922// This is correct as [`QueryIter`] always returns `None` once exhausted.
923impl<'w, 's, D: QueryData, F: QueryFilter> FusedIterator for QueryIter<'w, 's, D, F> {}
924
925// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
926unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator for QueryIter<'w, 's, Entity, F> {}
927
928// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
929unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator for QueryIter<'w, 's, EntityRef<'_>, F> {}
930
931// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
932unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator for QueryIter<'w, 's, EntityMut<'_>, F> {}
933
934// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
935unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator
936    for QueryIter<'w, 's, FilteredEntityRef<'_>, F>
937{
938}
939
940// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
941unsafe impl<'w, 's, F: QueryFilter> EntitySetIterator
942    for QueryIter<'w, 's, FilteredEntityMut<'_>, F>
943{
944}
945
946// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
947unsafe impl<'w, 's, F: QueryFilter, B: Bundle> EntitySetIterator
948    for QueryIter<'w, 's, EntityRefExcept<'_, B>, F>
949{
950}
951
952// SAFETY: [`QueryIter`] is guaranteed to return every matching entity once and only once.
953unsafe impl<'w, 's, F: QueryFilter, B: Bundle> EntitySetIterator
954    for QueryIter<'w, 's, EntityMutExcept<'_, B>, F>
955{
956}
957
958impl<'w, 's, D: QueryData, F: QueryFilter> Debug for QueryIter<'w, 's, D, F> {
959    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
960        f.debug_struct("QueryIter").finish()
961    }
962}
963
964impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter> Clone for QueryIter<'w, 's, D, F> {
965    fn clone(&self) -> Self {
966        self.remaining()
967    }
968}
969
970/// An [`Iterator`] over sorted query results of a [`Query`](crate::system::Query).
971///
972/// This struct is created by the [`QueryIter::sort`], [`QueryIter::sort_unstable`],
973/// [`QueryIter::sort_by`], [`QueryIter::sort_unstable_by`], [`QueryIter::sort_by_key`],
974/// [`QueryIter::sort_unstable_by_key`], and [`QueryIter::sort_by_cached_key`] methods.
975pub struct QuerySortedIter<'w, 's, D: QueryData, F: QueryFilter, I>
976where
977    I: Iterator<Item = Entity>,
978{
979    entity_iter: I,
980    entities: &'w Entities,
981    tables: &'w Tables,
982    archetypes: &'w Archetypes,
983    fetch: D::Fetch<'w>,
984    query_state: &'s QueryState<D, F>,
985}
986
987impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator> QuerySortedIter<'w, 's, D, F, I>
988where
989    I: Iterator<Item = Entity>,
990{
991    /// # Safety
992    /// - `world` must have permission to access any of the components registered in `query_state`.
993    /// - `world` must be the same one used to initialize `query_state`.
994    /// - `entity_list` must only contain unique entities or be empty.
995    pub(crate) unsafe fn new<EntityList: IntoIterator<IntoIter = I>>(
996        world: UnsafeWorldCell<'w>,
997        query_state: &'s QueryState<D, F>,
998        entity_list: EntityList,
999        last_run: Tick,
1000        this_run: Tick,
1001    ) -> QuerySortedIter<'w, 's, D, F, I> {
1002        let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
1003        QuerySortedIter {
1004            query_state,
1005            entities: world.entities(),
1006            archetypes: world.archetypes(),
1007            // SAFETY: We only access table data that has been registered in `query_state`.
1008            // This means `world` has permission to access the data we use.
1009            tables: &world.storages().tables,
1010            fetch,
1011            entity_iter: entity_list.into_iter(),
1012        }
1013    }
1014
1015    /// # Safety
1016    /// `entity` must stem from `self.entity_iter`, and not have been passed before.
1017    #[inline(always)]
1018    unsafe fn fetch_next(&mut self, entity: Entity) -> D::Item<'w> {
1019        let (location, archetype, table);
1020        // SAFETY:
1021        // `tables` and `archetypes` belong to the same world that the [`QueryIter`]
1022        // was initialized for.
1023        unsafe {
1024            location = self.entities.get(entity).debug_checked_unwrap();
1025            archetype = self
1026                .archetypes
1027                .get(location.archetype_id)
1028                .debug_checked_unwrap();
1029            table = self.tables.get(location.table_id).debug_checked_unwrap();
1030        }
1031
1032        // SAFETY: `archetype` is from the world that `fetch` was created for,
1033        // `fetch_state` is the state that `fetch` was initialized with
1034        unsafe {
1035            D::set_archetype(
1036                &mut self.fetch,
1037                &self.query_state.fetch_state,
1038                archetype,
1039                table,
1040            );
1041        }
1042
1043        // The entity list has already been filtered by the query lens, so we forego filtering here.
1044        // SAFETY:
1045        // - set_archetype was called prior, `location.archetype_row` is an archetype index in range of the current archetype
1046        // - fetch is only called once for each entity.
1047        unsafe { D::fetch(&mut self.fetch, entity, location.table_row) }
1048    }
1049}
1050
1051impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator> Iterator
1052    for QuerySortedIter<'w, 's, D, F, I>
1053where
1054    I: Iterator<Item = Entity>,
1055{
1056    type Item = D::Item<'w>;
1057
1058    #[inline(always)]
1059    fn next(&mut self) -> Option<Self::Item> {
1060        let entity = self.entity_iter.next()?;
1061        // SAFETY: `entity` is passed from `entity_iter` the first time.
1062        unsafe { self.fetch_next(entity).into() }
1063    }
1064
1065    fn size_hint(&self) -> (usize, Option<usize>) {
1066        self.entity_iter.size_hint()
1067    }
1068}
1069
1070impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator> DoubleEndedIterator
1071    for QuerySortedIter<'w, 's, D, F, I>
1072where
1073    I: DoubleEndedIterator<Item = Entity>,
1074{
1075    #[inline(always)]
1076    fn next_back(&mut self) -> Option<Self::Item> {
1077        let entity = self.entity_iter.next_back()?;
1078        // SAFETY: `entity` is passed from `entity_iter` the first time.
1079        unsafe { self.fetch_next(entity).into() }
1080    }
1081}
1082
1083impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator> ExactSizeIterator
1084    for QuerySortedIter<'w, 's, D, F, I>
1085where
1086    I: ExactSizeIterator<Item = Entity>,
1087{
1088}
1089
1090// This is correct as [`QuerySortedIter`] returns `None` once exhausted if `entity_iter` does.
1091impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator> FusedIterator
1092    for QuerySortedIter<'w, 's, D, F, I>
1093where
1094    I: FusedIterator<Item = Entity>,
1095{
1096}
1097
1098// SAFETY:
1099// `I` stems from a collected and sorted `EntitySetIterator` ([`QueryIter`]).
1100// Fetching unique entities maintains uniqueness.
1101unsafe impl<'w, 's, F: QueryFilter, I: Iterator<Item = Entity>> EntitySetIterator
1102    for QuerySortedIter<'w, 's, Entity, F, I>
1103{
1104}
1105
1106impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item = Entity>> Debug
1107    for QuerySortedIter<'w, 's, D, F, I>
1108{
1109    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1110        f.debug_struct("QuerySortedIter").finish()
1111    }
1112}
1113
1114/// An [`Iterator`] over the query items generated from an iterator of [`Entity`]s.
1115///
1116/// Items are returned in the order of the provided iterator.
1117/// Entities that don't match the query are skipped.
1118///
1119/// This struct is created by the [`Query::iter_many`](crate::system::Query::iter_many) and [`Query::iter_many_mut`](crate::system::Query::iter_many_mut) methods.
1120pub struct QueryManyIter<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
1121{
1122    world: UnsafeWorldCell<'w>,
1123    entity_iter: I,
1124    entities: &'w Entities,
1125    tables: &'w Tables,
1126    archetypes: &'w Archetypes,
1127    fetch: D::Fetch<'w>,
1128    filter: F::Fetch<'w>,
1129    query_state: &'s QueryState<D, F>,
1130}
1131
1132impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
1133    QueryManyIter<'w, 's, D, F, I>
1134{
1135    /// # Safety
1136    /// - `world` must have permission to access any of the components registered in `query_state`.
1137    /// - `world` must be the same one used to initialize `query_state`.
1138    pub(crate) unsafe fn new<EntityList: IntoIterator<IntoIter = I>>(
1139        world: UnsafeWorldCell<'w>,
1140        query_state: &'s QueryState<D, F>,
1141        entity_list: EntityList,
1142        last_run: Tick,
1143        this_run: Tick,
1144    ) -> QueryManyIter<'w, 's, D, F, I> {
1145        let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
1146        let filter = F::init_fetch(world, &query_state.filter_state, last_run, this_run);
1147        QueryManyIter {
1148            world,
1149            query_state,
1150            entities: world.entities(),
1151            archetypes: world.archetypes(),
1152            // SAFETY: We only access table data that has been registered in `query_state`.
1153            // This means `world` has permission to access the data we use.
1154            tables: &world.storages().tables,
1155            fetch,
1156            filter,
1157            entity_iter: entity_list.into_iter(),
1158        }
1159    }
1160
1161    /// # Safety
1162    /// All arguments must stem from the same valid `QueryManyIter`.
1163    ///
1164    /// The lifetime here is not restrictive enough for Fetch with &mut access,
1165    /// as calling `fetch_next_aliased_unchecked` multiple times can produce multiple
1166    /// references to the same component, leading to unique reference aliasing.
1167    ///
1168    /// It is always safe for shared access.
1169    #[inline(always)]
1170    unsafe fn fetch_next_aliased_unchecked(
1171        entity_iter: impl Iterator<Item: EntityEquivalent>,
1172        entities: &'w Entities,
1173        tables: &'w Tables,
1174        archetypes: &'w Archetypes,
1175        fetch: &mut D::Fetch<'w>,
1176        filter: &mut F::Fetch<'w>,
1177        query_state: &'s QueryState<D, F>,
1178    ) -> Option<D::Item<'w>> {
1179        for entity_borrow in entity_iter {
1180            let entity = entity_borrow.entity();
1181            let Some(location) = entities.get(entity) else {
1182                continue;
1183            };
1184
1185            if !query_state
1186                .matched_archetypes
1187                .contains(location.archetype_id.index())
1188            {
1189                continue;
1190            }
1191
1192            let archetype = archetypes.get(location.archetype_id).debug_checked_unwrap();
1193            let table = tables.get(location.table_id).debug_checked_unwrap();
1194
1195            // SAFETY: `archetype` is from the world that `fetch/filter` were created for,
1196            // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
1197            unsafe {
1198                D::set_archetype(fetch, &query_state.fetch_state, archetype, table);
1199            }
1200            // SAFETY: `table` is from the world that `fetch/filter` were created for,
1201            // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
1202            unsafe {
1203                F::set_archetype(filter, &query_state.filter_state, archetype, table);
1204            }
1205
1206            // SAFETY: set_archetype was called prior.
1207            // `location.archetype_row` is an archetype index row in range of the current archetype, because if it was not, the match above would have `continue`d
1208            if unsafe { F::filter_fetch(filter, entity, location.table_row) } {
1209                // SAFETY:
1210                // - set_archetype was called prior, `location.archetype_row` is an archetype index in range of the current archetype
1211                // - fetch is only called once for each entity.
1212                return Some(unsafe { D::fetch(fetch, entity, location.table_row) });
1213            }
1214        }
1215        None
1216    }
1217
1218    /// Get next result from the query
1219    #[inline(always)]
1220    pub fn fetch_next(&mut self) -> Option<D::Item<'_>> {
1221        // SAFETY:
1222        // All arguments stem from self.
1223        // We are limiting the returned reference to self,
1224        // making sure this method cannot be called multiple times without getting rid
1225        // of any previously returned unique references first, thus preventing aliasing.
1226        unsafe {
1227            Self::fetch_next_aliased_unchecked(
1228                &mut self.entity_iter,
1229                self.entities,
1230                self.tables,
1231                self.archetypes,
1232                &mut self.fetch,
1233                &mut self.filter,
1234                self.query_state,
1235            )
1236            .map(D::shrink)
1237        }
1238    }
1239
1240    /// Sorts all query items into a new iterator, using the query lens as a key.
1241    ///
1242    /// This sort is stable (i.e., does not reorder equal elements).
1243    ///
1244    /// This uses [`slice::sort`] internally.
1245    ///
1246    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1247    /// This includes the allowed parameter type changes listed under [allowed transmutes].
1248    /// However, the lens uses the filter of the original query when present.
1249    ///
1250    /// The sort is not cached across system runs.
1251    ///
1252    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1253    ///
1254    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1255    /// called on [`QueryManyIter`] before.
1256    ///
1257    /// # Examples
1258    /// ```rust
1259    /// # use bevy_ecs::prelude::*;
1260    /// # use std::{ops::{Deref, DerefMut}, iter::Sum};
1261    /// #
1262    /// # #[derive(Component)]
1263    /// # struct PartMarker;
1264    /// #
1265    /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
1266    /// # struct PartIndex(usize);
1267    /// #
1268    /// # #[derive(Component, Clone, Copy)]
1269    /// # struct PartValue(usize);
1270    /// #
1271    /// # impl Deref for PartValue {
1272    /// #     type Target = usize;
1273    /// #
1274    /// #     fn deref(&self) -> &Self::Target {
1275    /// #         &self.0
1276    /// #     }
1277    /// # }
1278    /// #
1279    /// # impl DerefMut for PartValue {
1280    /// #     fn deref_mut(&mut self) -> &mut Self::Target {
1281    /// #         &mut self.0
1282    /// #     }
1283    /// # }
1284    /// #
1285    /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
1286    /// # struct Length(usize);
1287    /// #
1288    /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
1289    /// # struct Width(usize);
1290    /// #
1291    /// # #[derive(Component, Debug, PartialEq, Eq, PartialOrd, Ord)]
1292    /// # struct Height(usize);
1293    /// #
1294    /// # #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
1295    /// # struct ParentEntity(Entity);
1296    /// #
1297    /// # let mut world = World::new();
1298    /// // We can ensure that a query always returns in the same order.
1299    /// fn system_1(query: Query<(Entity, &PartIndex)>) {
1300    /// #   let entity_list: Vec<Entity> = Vec::new();
1301    ///     let parts: Vec<(Entity, &PartIndex)> = query.iter_many(entity_list).sort::<&PartIndex>().collect();
1302    /// }
1303    ///
1304    /// // We can freely rearrange query components in the key.
1305    /// fn system_2(query: Query<(&Length, &Width, &Height), With<PartMarker>>) {
1306    /// #   let entity_list: Vec<Entity> = Vec::new();
1307    ///     for (length, width, height) in query.iter_many(entity_list).sort::<(&Height, &Length, &Width)>() {
1308    ///         println!("height: {height:?}, width: {width:?}, length: {length:?}")
1309    ///     }
1310    /// }
1311    ///
1312    /// // You can use `fetch_next_back` to obtain mutable references in reverse order.
1313    /// fn system_3(
1314    ///     mut query: Query<&mut PartValue>,
1315    /// ) {
1316    /// #   let entity_list: Vec<Entity> = Vec::new();
1317    ///     // We need to collect the internal iterator before iterating mutably
1318    ///     let mut parent_query_iter = query.iter_many_mut(entity_list)
1319    ///         .sort::<Entity>();
1320    ///
1321    ///     let mut scratch_value = 0;
1322    ///     while let Some(mut part_value) = parent_query_iter.fetch_next_back()
1323    ///     {
1324    ///         // some order-dependent operation, here bitwise XOR
1325    ///         **part_value ^= scratch_value;
1326    ///         scratch_value = **part_value;
1327    ///     }
1328    /// }
1329    /// #
1330    /// # let mut schedule = Schedule::default();
1331    /// # schedule.add_systems((system_1, system_2, system_3));
1332    /// # schedule.run(&mut world);
1333    /// ```
1334    pub fn sort<L: ReadOnlyQueryData + 'w>(
1335        self,
1336    ) -> QuerySortedManyIter<
1337        'w,
1338        's,
1339        D,
1340        F,
1341        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1342    >
1343    where
1344        for<'lw> L::Item<'lw>: Ord,
1345    {
1346        self.sort_impl::<L>(|keyed_query| keyed_query.sort())
1347    }
1348
1349    /// Sorts all query items into a new iterator, using the query lens as a key.
1350    ///
1351    /// This sort is unstable (i.e., may reorder equal elements).
1352    ///
1353    /// This uses [`slice::sort_unstable`] internally.
1354    ///
1355    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1356    /// This includes the allowed parameter type changes listed under [allowed transmutes]..
1357    /// However, the lens uses the filter of the original query when present.
1358    ///
1359    /// The sort is not cached across system runs.
1360    ///
1361    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1362    ///
1363    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1364    /// called on [`QueryManyIter`] before.
1365    ///
1366    /// # Example
1367    /// ```
1368    /// # use bevy_ecs::prelude::*;
1369    /// #
1370    /// # let mut world = World::new();
1371    /// #
1372    /// # #[derive(Component)]
1373    /// # struct PartMarker;
1374    /// #
1375    /// # let entity_list: Vec<Entity> = Vec::new();
1376    /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord)]
1377    /// enum Flying {
1378    ///     Enabled,
1379    ///     Disabled
1380    /// };
1381    ///
1382    /// // We perform an unstable sort by a Component with few values.
1383    /// fn system_1(query: Query<&Flying, With<PartMarker>>) {
1384    /// #   let entity_list: Vec<Entity> = Vec::new();
1385    ///     let part_values: Vec<&Flying> = query.iter_many(entity_list).sort_unstable::<&Flying>().collect();
1386    /// }
1387    /// #
1388    /// # let mut schedule = Schedule::default();
1389    /// # schedule.add_systems((system_1));
1390    /// # schedule.run(&mut world);
1391    /// ```
1392    pub fn sort_unstable<L: ReadOnlyQueryData + 'w>(
1393        self,
1394    ) -> QuerySortedManyIter<
1395        'w,
1396        's,
1397        D,
1398        F,
1399        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1400    >
1401    where
1402        for<'lw> L::Item<'lw>: Ord,
1403    {
1404        self.sort_impl::<L>(|keyed_query| keyed_query.sort_unstable())
1405    }
1406
1407    /// Sorts all query items into a new iterator with a comparator function over the query lens.
1408    ///
1409    /// This sort is stable (i.e., does not reorder equal elements).
1410    ///
1411    /// This uses [`slice::sort_by`] internally.
1412    ///
1413    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1414    /// This includes the allowed parameter type changes listed under [allowed transmutes].
1415    /// However, the lens uses the filter of the original query when present.
1416    ///
1417    /// The sort is not cached across system runs.
1418    ///
1419    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1420    ///
1421    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1422    /// called on [`QueryManyIter`] before.
1423    ///
1424    /// # Example
1425    /// ```
1426    /// # use bevy_ecs::prelude::*;
1427    /// # use std::ops::Deref;
1428    /// #
1429    /// # impl Deref for PartValue {
1430    /// #     type Target = f32;
1431    /// #
1432    /// #     fn deref(&self) -> &Self::Target {
1433    /// #         &self.0
1434    /// #     }
1435    /// # }
1436    /// #
1437    /// # let mut world = World::new();
1438    /// # let entity_list: Vec<Entity> = Vec::new();
1439    /// #
1440    /// #[derive(Component)]
1441    /// struct PartValue(f32);
1442    ///
1443    /// // We can use a cmp function on components do not implement Ord.
1444    /// fn system_1(query: Query<&PartValue>) {
1445    /// #   let entity_list: Vec<Entity> = Vec::new();
1446    ///     // Sort part values according to `f32::total_comp`.
1447    ///     let part_values: Vec<&PartValue> = query
1448    ///         .iter_many(entity_list)
1449    ///         .sort_by::<&PartValue>(|value_1, value_2| value_1.total_cmp(*value_2))
1450    ///         .collect();
1451    /// }
1452    /// #
1453    /// # let mut schedule = Schedule::default();
1454    /// # schedule.add_systems((system_1));
1455    /// # schedule.run(&mut world);
1456    /// ```
1457    pub fn sort_by<L: ReadOnlyQueryData + 'w>(
1458        self,
1459        mut compare: impl FnMut(&L::Item<'_>, &L::Item<'_>) -> Ordering,
1460    ) -> QuerySortedManyIter<
1461        'w,
1462        's,
1463        D,
1464        F,
1465        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1466    > {
1467        self.sort_impl::<L>(move |keyed_query| {
1468            keyed_query.sort_by(|(key_1, _), (key_2, _)| compare(key_1, key_2));
1469        })
1470    }
1471
1472    /// Sorts all query items into a new iterator with a comparator function over the query lens.
1473    ///
1474    /// This sort is unstable (i.e., may reorder equal elements).
1475    ///
1476    /// This uses [`slice::sort_unstable_by`] internally.
1477    ///
1478    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1479    /// This includes the allowed parameter type changes listed under [allowed transmutes].
1480    /// However, the lens uses the filter of the original query when present.
1481    ///
1482    /// The sort is not cached across system runs.
1483    ///
1484    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1485    ///
1486    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1487    /// called on [`QueryManyIter`] before.
1488    pub fn sort_unstable_by<L: ReadOnlyQueryData + 'w>(
1489        self,
1490        mut compare: impl FnMut(&L::Item<'_>, &L::Item<'_>) -> Ordering,
1491    ) -> QuerySortedManyIter<
1492        'w,
1493        's,
1494        D,
1495        F,
1496        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1497    > {
1498        self.sort_impl::<L>(move |keyed_query| {
1499            keyed_query.sort_unstable_by(|(key_1, _), (key_2, _)| compare(key_1, key_2));
1500        })
1501    }
1502
1503    /// Sorts all query items into a new iterator with a key extraction function over the query lens.
1504    ///
1505    /// This sort is stable (i.e., does not reorder equal elements).
1506    ///
1507    /// This uses [`slice::sort_by_key`] internally.
1508    ///
1509    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1510    /// This includes the allowed parameter type changes listed under [allowed transmutes].
1511    /// However, the lens uses the filter of the original query when present.
1512    ///
1513    /// The sort is not cached across system runs.
1514    ///
1515    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1516    ///
1517    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1518    /// called on [`QueryManyIter`] before.
1519    ///
1520    /// # Example
1521    /// ```
1522    /// # use bevy_ecs::prelude::*;
1523    /// # use std::ops::Deref;
1524    /// #
1525    /// # #[derive(Component)]
1526    /// # struct PartMarker;
1527    /// #
1528    /// # impl Deref for PartValue {
1529    /// #     type Target = f32;
1530    /// #
1531    /// #     fn deref(&self) -> &Self::Target {
1532    /// #         &self.0
1533    /// #     }
1534    /// # }
1535    /// #
1536    /// # let mut world = World::new();
1537    /// # let entity_list: Vec<Entity> = Vec::new();
1538    /// #
1539    /// #[derive(Component)]
1540    /// struct AvailableMarker;
1541    ///
1542    /// #[derive(Component, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
1543    /// enum Rarity {
1544    ///   Common,
1545    ///   Rare,
1546    ///   Epic,
1547    ///   Legendary
1548    /// };
1549    ///
1550    /// #[derive(Component)]
1551    /// struct PartValue(f32);
1552    ///
1553    /// // We can sort with the internals of components that do not implement Ord.
1554    /// fn system_1(query: Query<(Entity, &PartValue)>) {
1555    /// #   let entity_list: Vec<Entity> = Vec::new();
1556    ///     // Sort by the sines of the part values.
1557    ///     let parts: Vec<(Entity, &PartValue)> = query
1558    ///         .iter_many(entity_list)
1559    ///         .sort_by_key::<&PartValue, _>(|value| value.sin() as usize)
1560    ///         .collect();
1561    /// }
1562    ///
1563    /// // We can define our own custom comparison functions over an EntityRef.
1564    /// fn system_2(query: Query<EntityRef, With<PartMarker>>) {
1565    /// #   let entity_list: Vec<Entity> = Vec::new();
1566    ///     // Sort by whether parts are available and their rarity.
1567    ///     // We want the available legendaries to come first, so we reverse the iterator.
1568    ///     let parts: Vec<EntityRef> = query.iter_many(entity_list)
1569    ///         .sort_by_key::<EntityRef, _>(|entity_ref| {
1570    ///             (
1571    ///                 entity_ref.contains::<AvailableMarker>(),
1572    //                  entity_ref.get::<Rarity>().copied()
1573    ///             )
1574    ///         })
1575    ///         .rev()
1576    ///         .collect();
1577    /// }
1578    /// # let mut schedule = Schedule::default();
1579    /// # schedule.add_systems((system_1, system_2));
1580    /// # schedule.run(&mut world);
1581    /// ```
1582    pub fn sort_by_key<L: ReadOnlyQueryData + 'w, K>(
1583        self,
1584        mut f: impl FnMut(&L::Item<'_>) -> K,
1585    ) -> QuerySortedManyIter<
1586        'w,
1587        's,
1588        D,
1589        F,
1590        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1591    >
1592    where
1593        K: Ord,
1594    {
1595        self.sort_impl::<L>(move |keyed_query| keyed_query.sort_by_key(|(lens, _)| f(lens)))
1596    }
1597
1598    /// Sorts all query items into a new iterator with a key extraction function over the query lens.
1599    ///
1600    /// This sort is unstable (i.e., may reorder equal elements).
1601    ///
1602    /// This uses [`slice::sort_unstable_by_key`] internally.
1603    ///
1604    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1605    /// This includes the allowed parameter type changes listed under [allowed transmutes].
1606    /// However, the lens uses the filter of the original query when present.
1607    ///
1608    /// The sort is not cached across system runs.
1609    ///
1610    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1611    ///
1612    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1613    /// called on [`QueryManyIter`] before.
1614    pub fn sort_unstable_by_key<L: ReadOnlyQueryData + 'w, K>(
1615        self,
1616        mut f: impl FnMut(&L::Item<'_>) -> K,
1617    ) -> QuerySortedManyIter<
1618        'w,
1619        's,
1620        D,
1621        F,
1622        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1623    >
1624    where
1625        K: Ord,
1626    {
1627        self.sort_impl::<L>(move |keyed_query| {
1628            keyed_query.sort_unstable_by_key(|(lens, _)| f(lens));
1629        })
1630    }
1631
1632    /// Sort all query items into a new iterator with a key extraction function over the query lens.
1633    ///
1634    /// This sort is stable (i.e., does not reorder equal elements).
1635    ///
1636    /// This uses [`slice::sort_by_cached_key`] internally.
1637    ///
1638    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1639    /// This includes the allowed parameter type changes listed under [allowed transmutes].
1640    /// However, the lens uses the filter of the original query when present.
1641    ///
1642    /// The sort is not cached across system runs.
1643    ///
1644    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1645    ///
1646    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1647    /// called on [`QueryManyIter`] before.
1648    pub fn sort_by_cached_key<L: ReadOnlyQueryData + 'w, K>(
1649        self,
1650        mut f: impl FnMut(&L::Item<'_>) -> K,
1651    ) -> QuerySortedManyIter<
1652        'w,
1653        's,
1654        D,
1655        F,
1656        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1657    >
1658    where
1659        K: Ord,
1660    {
1661        self.sort_impl::<L>(move |keyed_query| keyed_query.sort_by_cached_key(|(lens, _)| f(lens)))
1662    }
1663
1664    /// Shared implementation for the various `sort` methods.
1665    /// This uses the lens to collect the items for sorting, but delegates the actual sorting to the provided closure.
1666    ///
1667    /// Defining the lens works like [`transmute_lens`](crate::system::Query::transmute_lens).
1668    /// This includes the allowed parameter type changes listed under [allowed transmutes].
1669    /// However, the lens uses the filter of the original query when present.
1670    ///
1671    /// The sort is not cached across system runs.
1672    ///
1673    /// [allowed transmutes]: crate::system::Query#allowed-transmutes
1674    ///
1675    /// Unlike the sort methods on [`QueryIter`], this does NOT panic if `next`/`fetch_next` has been
1676    /// called on [`QueryManyIter`] before.
1677    fn sort_impl<L: ReadOnlyQueryData + 'w>(
1678        self,
1679        f: impl FnOnce(&mut Vec<(L::Item<'_>, NeutralOrd<Entity>)>),
1680    ) -> QuerySortedManyIter<
1681        'w,
1682        's,
1683        D,
1684        F,
1685        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,
1686    > {
1687        let world = self.world;
1688
1689        let query_lens_state = self.query_state.transmute_filtered::<(L, Entity), F>(world);
1690
1691        // SAFETY:
1692        // `self.world` has permission to access the required components.
1693        // The original query iter has not been iterated on, so no items are aliased from it.
1694        // `QueryIter::new` ensures `world` is the same one used to initialize `query_state`.
1695        let query_lens = unsafe { query_lens_state.query_unchecked_manual(world) }
1696            .iter_many_inner(self.entity_iter);
1697        let mut keyed_query: Vec<_> = query_lens
1698            .map(|(key, entity)| (key, NeutralOrd(entity)))
1699            .collect();
1700        f(&mut keyed_query);
1701        // Re-collect into a `Vec` to eagerly drop the lens items.
1702        // They must be dropped before `fetch_next` is called since they may alias
1703        // with other data items if there are duplicate entities in `entity_iter`.
1704        let entity_iter = keyed_query
1705            .into_iter()
1706            .map(|(.., entity)| entity.0)
1707            .collect::<Vec<_>>()
1708            .into_iter();
1709        // SAFETY:
1710        // `self.world` has permission to access the required components.
1711        // Each lens query item is dropped before the respective actual query item is accessed.
1712        unsafe {
1713            QuerySortedManyIter::new(
1714                world,
1715                self.query_state,
1716                entity_iter,
1717                world.last_change_tick(),
1718                world.change_tick(),
1719            )
1720        }
1721    }
1722}
1723
1724impl<'w, 's, D: QueryData, F: QueryFilter, I: DoubleEndedIterator<Item: EntityEquivalent>>
1725    QueryManyIter<'w, 's, D, F, I>
1726{
1727    /// Get next result from the back of the query
1728    #[inline(always)]
1729    pub fn fetch_next_back(&mut self) -> Option<D::Item<'_>> {
1730        // SAFETY:
1731        // All arguments stem from self.
1732        // We are limiting the returned reference to self,
1733        // making sure this method cannot be called multiple times without getting rid
1734        // of any previously returned unique references first, thus preventing aliasing.
1735        unsafe {
1736            Self::fetch_next_aliased_unchecked(
1737                self.entity_iter.by_ref().rev(),
1738                self.entities,
1739                self.tables,
1740                self.archetypes,
1741                &mut self.fetch,
1742                &mut self.filter,
1743                self.query_state,
1744            )
1745            .map(D::shrink)
1746        }
1747    }
1748}
1749
1750impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>> Iterator
1751    for QueryManyIter<'w, 's, D, F, I>
1752{
1753    type Item = D::Item<'w>;
1754
1755    #[inline(always)]
1756    fn next(&mut self) -> Option<Self::Item> {
1757        // SAFETY:
1758        // All arguments stem from self.
1759        // It is safe to alias for ReadOnlyWorldQuery.
1760        unsafe {
1761            Self::fetch_next_aliased_unchecked(
1762                &mut self.entity_iter,
1763                self.entities,
1764                self.tables,
1765                self.archetypes,
1766                &mut self.fetch,
1767                &mut self.filter,
1768                self.query_state,
1769            )
1770        }
1771    }
1772
1773    fn size_hint(&self) -> (usize, Option<usize>) {
1774        let (_, max_size) = self.entity_iter.size_hint();
1775        (0, max_size)
1776    }
1777}
1778
1779impl<
1780        'w,
1781        's,
1782        D: ReadOnlyQueryData,
1783        F: QueryFilter,
1784        I: DoubleEndedIterator<Item: EntityEquivalent>,
1785    > DoubleEndedIterator for QueryManyIter<'w, 's, D, F, I>
1786{
1787    #[inline(always)]
1788    fn next_back(&mut self) -> Option<Self::Item> {
1789        // SAFETY:
1790        // All arguments stem from self.
1791        // It is safe to alias for ReadOnlyWorldQuery.
1792        unsafe {
1793            Self::fetch_next_aliased_unchecked(
1794                self.entity_iter.by_ref().rev(),
1795                self.entities,
1796                self.tables,
1797                self.archetypes,
1798                &mut self.fetch,
1799                &mut self.filter,
1800                self.query_state,
1801            )
1802        }
1803    }
1804}
1805
1806// This is correct as [`QueryManyIter`] always returns `None` once exhausted.
1807impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>>
1808    FusedIterator for QueryManyIter<'w, 's, D, F, I>
1809{
1810}
1811
1812// SAFETY: Fetching unique entities maintains uniqueness.
1813unsafe impl<'w, 's, F: QueryFilter, I: EntitySetIterator> EntitySetIterator
1814    for QueryManyIter<'w, 's, Entity, F, I>
1815{
1816}
1817
1818impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item: EntityEquivalent>> Debug
1819    for QueryManyIter<'w, 's, D, F, I>
1820{
1821    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1822        f.debug_struct("QueryManyIter").finish()
1823    }
1824}
1825
1826/// An [`Iterator`] over the query items generated from an iterator of unique [`Entity`]s.
1827///
1828/// Items are returned in the order of the provided iterator.
1829/// Entities that don't match the query are skipped.
1830///
1831/// In contrast with [`QueryManyIter`], this allows for mutable iteration without a [`fetch_next`] method.
1832///
1833/// This struct is created by the [`iter_many_unique`] and [`iter_many_unique_mut`] methods on [`Query`].
1834///
1835/// [`fetch_next`]: QueryManyIter::fetch_next
1836/// [`iter_many_unique`]: crate::system::Query::iter_many
1837/// [`iter_many_unique_mut`]: crate::system::Query::iter_many_mut
1838/// [`Query`]: crate::system::Query
1839pub struct QueryManyUniqueIter<'w, 's, D: QueryData, F: QueryFilter, I: EntitySetIterator>(
1840    QueryManyIter<'w, 's, D, F, I>,
1841);
1842
1843impl<'w, 's, D: QueryData, F: QueryFilter, I: EntitySetIterator>
1844    QueryManyUniqueIter<'w, 's, D, F, I>
1845{
1846    /// # Safety
1847    /// - `world` must have permission to access any of the components registered in `query_state`.
1848    /// - `world` must be the same one used to initialize `query_state`.
1849    pub(crate) unsafe fn new<EntityList: EntitySet<IntoIter = I>>(
1850        world: UnsafeWorldCell<'w>,
1851        query_state: &'s QueryState<D, F>,
1852        entity_list: EntityList,
1853        last_run: Tick,
1854        this_run: Tick,
1855    ) -> QueryManyUniqueIter<'w, 's, D, F, I> {
1856        QueryManyUniqueIter(QueryManyIter::new(
1857            world,
1858            query_state,
1859            entity_list,
1860            last_run,
1861            this_run,
1862        ))
1863    }
1864}
1865
1866impl<'w, 's, D: QueryData, F: QueryFilter, I: EntitySetIterator> Iterator
1867    for QueryManyUniqueIter<'w, 's, D, F, I>
1868{
1869    type Item = D::Item<'w>;
1870
1871    #[inline(always)]
1872    fn next(&mut self) -> Option<Self::Item> {
1873        // SAFETY: Entities are guaranteed to be unique, thus do not alias.
1874        unsafe {
1875            QueryManyIter::<'w, 's, D, F, I>::fetch_next_aliased_unchecked(
1876                &mut self.0.entity_iter,
1877                self.0.entities,
1878                self.0.tables,
1879                self.0.archetypes,
1880                &mut self.0.fetch,
1881                &mut self.0.filter,
1882                self.0.query_state,
1883            )
1884        }
1885    }
1886
1887    fn size_hint(&self) -> (usize, Option<usize>) {
1888        let (_, max_size) = self.0.entity_iter.size_hint();
1889        (0, max_size)
1890    }
1891}
1892
1893// This is correct as [`QueryManyIter`] always returns `None` once exhausted.
1894impl<'w, 's, D: QueryData, F: QueryFilter, I: EntitySetIterator> FusedIterator
1895    for QueryManyUniqueIter<'w, 's, D, F, I>
1896{
1897}
1898
1899// SAFETY: Fetching unique entities maintains uniqueness.
1900unsafe impl<'w, 's, F: QueryFilter, I: EntitySetIterator> EntitySetIterator
1901    for QueryManyUniqueIter<'w, 's, Entity, F, I>
1902{
1903}
1904
1905impl<'w, 's, D: QueryData, F: QueryFilter, I: EntitySetIterator> Debug
1906    for QueryManyUniqueIter<'w, 's, D, F, I>
1907{
1908    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1909        f.debug_struct("QueryManyUniqueIter").finish()
1910    }
1911}
1912
1913/// An [`Iterator`] over sorted query results of a [`QueryManyIter`].
1914///
1915/// This struct is created by the [`sort`](QueryManyIter), [`sort_unstable`](QueryManyIter),
1916/// [`sort_by`](QueryManyIter), [`sort_unstable_by`](QueryManyIter), [`sort_by_key`](QueryManyIter),
1917/// [`sort_unstable_by_key`](QueryManyIter), and [`sort_by_cached_key`](QueryManyIter) methods of [`QueryManyIter`].
1918pub struct QuerySortedManyIter<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item = Entity>> {
1919    entity_iter: I,
1920    entities: &'w Entities,
1921    tables: &'w Tables,
1922    archetypes: &'w Archetypes,
1923    fetch: D::Fetch<'w>,
1924    query_state: &'s QueryState<D, F>,
1925}
1926
1927impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item = Entity>>
1928    QuerySortedManyIter<'w, 's, D, F, I>
1929{
1930    /// # Safety
1931    /// - `world` must have permission to access any of the components registered in `query_state`.
1932    /// - `world` must be the same one used to initialize `query_state`.
1933    /// - `entity_list` must only contain unique entities or be empty.
1934    pub(crate) unsafe fn new<EntityList: IntoIterator<IntoIter = I>>(
1935        world: UnsafeWorldCell<'w>,
1936        query_state: &'s QueryState<D, F>,
1937        entity_list: EntityList,
1938        last_run: Tick,
1939        this_run: Tick,
1940    ) -> QuerySortedManyIter<'w, 's, D, F, I> {
1941        let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
1942        QuerySortedManyIter {
1943            query_state,
1944            entities: world.entities(),
1945            archetypes: world.archetypes(),
1946            // SAFETY: We only access table data that has been registered in `query_state`.
1947            // This means `world` has permission to access the data we use.
1948            tables: &world.storages().tables,
1949            fetch,
1950            entity_iter: entity_list.into_iter(),
1951        }
1952    }
1953
1954    /// # Safety
1955    /// The lifetime here is not restrictive enough for Fetch with &mut access,
1956    /// as calling `fetch_next_aliased_unchecked` multiple times can produce multiple
1957    /// references to the same component, leading to unique reference aliasing.
1958    ///
1959    /// It is always safe for shared access.
1960    /// `entity` must stem from `self.entity_iter`, and not have been passed before.
1961    #[inline(always)]
1962    unsafe fn fetch_next_aliased_unchecked(&mut self, entity: Entity) -> D::Item<'w> {
1963        let (location, archetype, table);
1964        // SAFETY:
1965        // `tables` and `archetypes` belong to the same world that the [`QueryIter`]
1966        // was initialized for.
1967        unsafe {
1968            location = self.entities.get(entity).debug_checked_unwrap();
1969            archetype = self
1970                .archetypes
1971                .get(location.archetype_id)
1972                .debug_checked_unwrap();
1973            table = self.tables.get(location.table_id).debug_checked_unwrap();
1974        }
1975
1976        // SAFETY: `archetype` is from the world that `fetch` was created for,
1977        // `fetch_state` is the state that `fetch` was initialized with
1978        unsafe {
1979            D::set_archetype(
1980                &mut self.fetch,
1981                &self.query_state.fetch_state,
1982                archetype,
1983                table,
1984            );
1985        }
1986
1987        // The entity list has already been filtered by the query lens, so we forego filtering here.
1988        // SAFETY:
1989        // - set_archetype was called prior, `location.archetype_row` is an archetype index in range of the current archetype
1990        // - fetch is only called once for each entity.
1991        unsafe { D::fetch(&mut self.fetch, entity, location.table_row) }
1992    }
1993
1994    /// Get next result from the query
1995    #[inline(always)]
1996    pub fn fetch_next(&mut self) -> Option<D::Item<'_>> {
1997        let entity = self.entity_iter.next()?;
1998
1999        // SAFETY:
2000        // We have collected the entity_iter once to drop all internal lens query item
2001        // references.
2002        // We are limiting the returned reference to self,
2003        // making sure this method cannot be called multiple times without getting rid
2004        // of any previously returned unique references first, thus preventing aliasing.
2005        // `entity` is passed from `entity_iter` the first time.
2006        unsafe { D::shrink(self.fetch_next_aliased_unchecked(entity)).into() }
2007    }
2008}
2009
2010impl<'w, 's, D: QueryData, F: QueryFilter, I: DoubleEndedIterator<Item = Entity>>
2011    QuerySortedManyIter<'w, 's, D, F, I>
2012{
2013    /// Get next result from the query
2014    #[inline(always)]
2015    pub fn fetch_next_back(&mut self) -> Option<D::Item<'_>> {
2016        let entity = self.entity_iter.next_back()?;
2017
2018        // SAFETY:
2019        // We have collected the entity_iter once to drop all internal lens query item
2020        // references.
2021        // We are limiting the returned reference to self,
2022        // making sure this method cannot be called multiple times without getting rid
2023        // of any previously returned unique references first, thus preventing aliasing.
2024        // `entity` is passed from `entity_iter` the first time.
2025        unsafe { D::shrink(self.fetch_next_aliased_unchecked(entity)).into() }
2026    }
2027}
2028
2029impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, I: Iterator<Item = Entity>> Iterator
2030    for QuerySortedManyIter<'w, 's, D, F, I>
2031{
2032    type Item = D::Item<'w>;
2033
2034    #[inline(always)]
2035    fn next(&mut self) -> Option<Self::Item> {
2036        let entity = self.entity_iter.next()?;
2037        // SAFETY:
2038        // It is safe to alias for ReadOnlyWorldQuery.
2039        // `entity` is passed from `entity_iter` the first time.
2040        unsafe { self.fetch_next_aliased_unchecked(entity).into() }
2041    }
2042
2043    fn size_hint(&self) -> (usize, Option<usize>) {
2044        self.entity_iter.size_hint()
2045    }
2046}
2047
2048impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, I: DoubleEndedIterator<Item = Entity>>
2049    DoubleEndedIterator for QuerySortedManyIter<'w, 's, D, F, I>
2050{
2051    #[inline(always)]
2052    fn next_back(&mut self) -> Option<Self::Item> {
2053        let entity = self.entity_iter.next_back()?;
2054        // SAFETY:
2055        // It is safe to alias for ReadOnlyWorldQuery.
2056        // `entity` is passed from `entity_iter` the first time.
2057        unsafe { self.fetch_next_aliased_unchecked(entity).into() }
2058    }
2059}
2060
2061impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, I: ExactSizeIterator<Item = Entity>>
2062    ExactSizeIterator for QuerySortedManyIter<'w, 's, D, F, I>
2063{
2064}
2065
2066impl<'w, 's, D: QueryData, F: QueryFilter, I: Iterator<Item = Entity>> Debug
2067    for QuerySortedManyIter<'w, 's, D, F, I>
2068{
2069    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2070        f.debug_struct("QuerySortedManyIter").finish()
2071    }
2072}
2073
2074/// An iterator over `K`-sized combinations of query items without repetition.
2075///
2076/// A combination is an arrangement of a collection of items where order does not matter.
2077///
2078/// `K` is the number of items that make up each subset, and the number of items returned by the iterator.
2079/// `N` is the number of total entities output by the query.
2080///
2081/// For example, given the list [1, 2, 3, 4], where `K` is 2, the combinations without repeats are
2082/// [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4].
2083/// And in this case, `N` would be defined as 4 since the size of the input list is 4.
2084///
2085/// The number of combinations depend on how `K` relates to the number of entities matching the [`Query`]:
2086/// - if `K = N`, only one combination exists,
2087/// - if `K < N`, there are <sub>N</sub>C<sub>K</sub> combinations (see the [performance section] of `Query`),
2088/// - if `K > N`, there are no combinations.
2089///
2090/// The output combination is not guaranteed to have any order of iteration.
2091///
2092/// # Usage
2093///
2094/// This type is returned by calling [`Query::iter_combinations`] or [`Query::iter_combinations_mut`].
2095///
2096/// It implements [`Iterator`] only if it iterates over read-only query items ([learn more]).
2097///
2098/// In the case of mutable query items, it can be iterated by calling [`fetch_next`] in a `while let` loop.
2099///
2100/// # Examples
2101///
2102/// The following example shows how to traverse the iterator when the query items are read-only.
2103///
2104/// ```
2105/// # use bevy_ecs::prelude::*;
2106/// # #[derive(Component)]
2107/// # struct ComponentA;
2108/// #
2109/// fn some_system(query: Query<&ComponentA>) {
2110///     for [a1, a2] in query.iter_combinations() {
2111///         // ...
2112///     }
2113/// }
2114/// ```
2115///
2116/// The following example shows how `fetch_next` should be called with a `while let` loop to traverse the iterator when the query items are mutable.
2117///
2118/// ```
2119/// # use bevy_ecs::prelude::*;
2120/// # #[derive(Component)]
2121/// # struct ComponentA;
2122/// #
2123/// fn some_system(mut query: Query<&mut ComponentA>) {
2124///     let mut combinations = query.iter_combinations_mut();
2125///     while let Some([a1, a2]) = combinations.fetch_next() {
2126///         // ...
2127///     }
2128/// }
2129/// ```
2130///
2131/// [`fetch_next`]: Self::fetch_next
2132/// [learn more]: Self#impl-Iterator
2133/// [performance section]: crate::system::Query#performance
2134/// [`Query`]: crate::system::Query
2135/// [`Query::iter_combinations`]: crate::system::Query::iter_combinations
2136/// [`Query::iter_combinations_mut`]: crate::system::Query::iter_combinations_mut
2137pub struct QueryCombinationIter<'w, 's, D: QueryData, F: QueryFilter, const K: usize> {
2138    tables: &'w Tables,
2139    archetypes: &'w Archetypes,
2140    query_state: &'s QueryState<D, F>,
2141    cursors: [QueryIterationCursor<'w, 's, D, F>; K],
2142}
2143
2144impl<'w, 's, D: QueryData, F: QueryFilter, const K: usize> QueryCombinationIter<'w, 's, D, F, K> {
2145    /// # Safety
2146    /// - `world` must have permission to access any of the components registered in `query_state`.
2147    /// - `world` must be the same one used to initialize `query_state`.
2148    pub(crate) unsafe fn new(
2149        world: UnsafeWorldCell<'w>,
2150        query_state: &'s QueryState<D, F>,
2151        last_run: Tick,
2152        this_run: Tick,
2153    ) -> Self {
2154        assert!(K != 0, "K should not equal to zero");
2155        // Initialize array with cursors.
2156        // There is no FromIterator on arrays, so instead initialize it manually with MaybeUninit
2157
2158        let mut array: MaybeUninit<[QueryIterationCursor<'w, 's, D, F>; K]> = MaybeUninit::uninit();
2159        let ptr = array
2160            .as_mut_ptr()
2161            .cast::<QueryIterationCursor<'w, 's, D, F>>();
2162        ptr.write(QueryIterationCursor::init(
2163            world,
2164            query_state,
2165            last_run,
2166            this_run,
2167        ));
2168        for slot in (1..K).map(|offset| ptr.add(offset)) {
2169            slot.write(QueryIterationCursor::init_empty(
2170                world,
2171                query_state,
2172                last_run,
2173                this_run,
2174            ));
2175        }
2176
2177        QueryCombinationIter {
2178            query_state,
2179            // SAFETY: We only access table data that has been registered in `query_state`.
2180            tables: unsafe { &world.storages().tables },
2181            archetypes: world.archetypes(),
2182            cursors: array.assume_init(),
2183        }
2184    }
2185
2186    /// # Safety
2187    /// The lifetime here is not restrictive enough for Fetch with &mut access,
2188    /// as calling `fetch_next_aliased_unchecked` multiple times can produce multiple
2189    /// references to the same component, leading to unique reference aliasing.
2190    /// .
2191    /// It is always safe for shared access.
2192    #[inline]
2193    unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option<[D::Item<'w>; K]> {
2194        // PERF: can speed up the following code using `cursor.remaining()` instead of `next_item.is_none()`
2195        // when D::IS_ARCHETYPAL && F::IS_ARCHETYPAL
2196        //
2197        // let `i` be the index of `c`, the last cursor in `self.cursors` that
2198        // returns `K-i` or more elements.
2199        // Make cursor in index `j` for all `j` in `[i, K)` a copy of `c` advanced `j-i+1` times.
2200        // If no such `c` exists, return `None`
2201        'outer: for i in (0..K).rev() {
2202            match self.cursors[i].next(self.tables, self.archetypes, self.query_state) {
2203                Some(_) => {
2204                    for j in (i + 1)..K {
2205                        self.cursors[j] = self.cursors[j - 1].clone();
2206                        match self.cursors[j].next(self.tables, self.archetypes, self.query_state) {
2207                            Some(_) => {}
2208                            None if i > 0 => continue 'outer,
2209                            None => return None,
2210                        }
2211                    }
2212                    break;
2213                }
2214                None if i > 0 => continue,
2215                None => return None,
2216            }
2217        }
2218
2219        let mut values = MaybeUninit::<[D::Item<'w>; K]>::uninit();
2220
2221        let ptr = values.as_mut_ptr().cast::<D::Item<'w>>();
2222        for (offset, cursor) in self.cursors.iter_mut().enumerate() {
2223            ptr.add(offset).write(cursor.peek_last().unwrap());
2224        }
2225
2226        Some(values.assume_init())
2227    }
2228
2229    /// Get next combination of queried components
2230    #[inline]
2231    pub fn fetch_next(&mut self) -> Option<[D::Item<'_>; K]> {
2232        // SAFETY: we are limiting the returned reference to self,
2233        // making sure this method cannot be called multiple times without getting rid
2234        // of any previously returned unique references first, thus preventing aliasing.
2235        unsafe {
2236            self.fetch_next_aliased_unchecked()
2237                .map(|array| array.map(D::shrink))
2238        }
2239    }
2240}
2241
2242// Iterator type is intentionally implemented only for read-only access.
2243// Doing so for mutable references would be unsound, because calling `next`
2244// multiple times would allow multiple owned references to the same data to exist.
2245impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, const K: usize> Iterator
2246    for QueryCombinationIter<'w, 's, D, F, K>
2247{
2248    type Item = [D::Item<'w>; K];
2249
2250    #[inline]
2251    fn next(&mut self) -> Option<Self::Item> {
2252        // Safety: it is safe to alias for ReadOnlyWorldQuery
2253        unsafe { QueryCombinationIter::fetch_next_aliased_unchecked(self) }
2254    }
2255
2256    fn size_hint(&self) -> (usize, Option<usize>) {
2257        // binomial coefficient: (n ; k) = n! / k!(n-k)! = (n*n-1*...*n-k+1) / k!
2258        // See https://en.wikipedia.org/wiki/Binomial_coefficient
2259        // See https://blog.plover.com/math/choose.html for implementation
2260        // It was chosen to reduce overflow potential.
2261        fn choose(n: usize, k: usize) -> Option<usize> {
2262            if k > n || n == 0 {
2263                return Some(0);
2264            }
2265            let k = k.min(n - k);
2266            let ks = 1..=k;
2267            let ns = (n - k + 1..=n).rev();
2268            ks.zip(ns)
2269                .try_fold(1_usize, |acc, (k, n)| Some(acc.checked_mul(n)? / k))
2270        }
2271        // sum_i=0..k choose(cursors[i].remaining, k-i)
2272        let max_combinations = self
2273            .cursors
2274            .iter()
2275            .enumerate()
2276            .try_fold(0, |acc, (i, cursor)| {
2277                let n = cursor.max_remaining(self.tables, self.archetypes);
2278                Some(acc + choose(n, K - i)?)
2279            });
2280
2281        let archetype_query = F::IS_ARCHETYPAL;
2282        let known_max = max_combinations.unwrap_or(usize::MAX);
2283        let min_combinations = if archetype_query { known_max } else { 0 };
2284        (min_combinations, max_combinations)
2285    }
2286}
2287
2288impl<'w, 's, D: QueryData, F: QueryFilter> ExactSizeIterator for QueryIter<'w, 's, D, F>
2289where
2290    F: ArchetypeFilter,
2291{
2292    fn len(&self) -> usize {
2293        self.size_hint().0
2294    }
2295}
2296
2297// This is correct as [`QueryCombinationIter`] always returns `None` once exhausted.
2298impl<'w, 's, D: ReadOnlyQueryData, F: QueryFilter, const K: usize> FusedIterator
2299    for QueryCombinationIter<'w, 's, D, F, K>
2300{
2301}
2302
2303impl<'w, 's, D: QueryData, F: QueryFilter, const K: usize> Debug
2304    for QueryCombinationIter<'w, 's, D, F, K>
2305{
2306    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2307        f.debug_struct("QueryCombinationIter").finish()
2308    }
2309}
2310
2311struct QueryIterationCursor<'w, 's, D: QueryData, F: QueryFilter> {
2312    // whether the query iteration is dense or not. Mirrors QueryState's `is_dense` field.
2313    is_dense: bool,
2314    storage_id_iter: core::slice::Iter<'s, StorageId>,
2315    table_entities: &'w [Entity],
2316    archetype_entities: &'w [ArchetypeEntity],
2317    fetch: D::Fetch<'w>,
2318    filter: F::Fetch<'w>,
2319    // length of the table or length of the archetype, depending on whether both `D`'s and `F`'s fetches are dense
2320    current_len: usize,
2321    // either table row or archetype index, depending on whether both `D`'s and `F`'s fetches are dense
2322    current_row: usize,
2323}
2324
2325impl<D: QueryData, F: QueryFilter> Clone for QueryIterationCursor<'_, '_, D, F> {
2326    fn clone(&self) -> Self {
2327        Self {
2328            is_dense: self.is_dense,
2329            storage_id_iter: self.storage_id_iter.clone(),
2330            table_entities: self.table_entities,
2331            archetype_entities: self.archetype_entities,
2332            fetch: self.fetch.clone(),
2333            filter: self.filter.clone(),
2334            current_len: self.current_len,
2335            current_row: self.current_row,
2336        }
2337    }
2338}
2339
2340impl<'w, 's, D: QueryData, F: QueryFilter> QueryIterationCursor<'w, 's, D, F> {
2341    /// # Safety
2342    /// - `world` must have permission to access any of the components registered in `query_state`.
2343    /// - `world` must be the same one used to initialize `query_state`.
2344    unsafe fn init_empty(
2345        world: UnsafeWorldCell<'w>,
2346        query_state: &'s QueryState<D, F>,
2347        last_run: Tick,
2348        this_run: Tick,
2349    ) -> Self {
2350        QueryIterationCursor {
2351            storage_id_iter: [].iter(),
2352            ..Self::init(world, query_state, last_run, this_run)
2353        }
2354    }
2355
2356    /// # Safety
2357    /// - `world` must have permission to access any of the components registered in `query_state`.
2358    /// - `world` must be the same one used to initialize `query_state`.
2359    unsafe fn init(
2360        world: UnsafeWorldCell<'w>,
2361        query_state: &'s QueryState<D, F>,
2362        last_run: Tick,
2363        this_run: Tick,
2364    ) -> Self {
2365        let fetch = D::init_fetch(world, &query_state.fetch_state, last_run, this_run);
2366        let filter = F::init_fetch(world, &query_state.filter_state, last_run, this_run);
2367        QueryIterationCursor {
2368            fetch,
2369            filter,
2370            table_entities: &[],
2371            archetype_entities: &[],
2372            storage_id_iter: query_state.matched_storage_ids.iter(),
2373            is_dense: query_state.is_dense,
2374            current_len: 0,
2375            current_row: 0,
2376        }
2377    }
2378
2379    fn reborrow(&mut self) -> QueryIterationCursor<'_, 's, D, F> {
2380        QueryIterationCursor {
2381            is_dense: self.is_dense,
2382            fetch: D::shrink_fetch(self.fetch.clone()),
2383            filter: F::shrink_fetch(self.filter.clone()),
2384            table_entities: self.table_entities,
2385            archetype_entities: self.archetype_entities,
2386            storage_id_iter: self.storage_id_iter.clone(),
2387            current_len: self.current_len,
2388            current_row: self.current_row,
2389        }
2390    }
2391
2392    /// Retrieve item returned from most recent `next` call again.
2393    ///
2394    /// # Safety
2395    /// The result of `next` and any previous calls to `peek_last` with this row must have been
2396    /// dropped to prevent aliasing mutable references.
2397    #[inline]
2398    unsafe fn peek_last(&mut self) -> Option<D::Item<'w>> {
2399        if self.current_row > 0 {
2400            let index = self.current_row - 1;
2401            if self.is_dense {
2402                // SAFETY: This must have been called previously in `next` as `current_row > 0`
2403                let entity = unsafe { self.table_entities.get_unchecked(index) };
2404                // SAFETY:
2405                //  - `set_table` must have been called previously either in `next` or before it.
2406                //  - `*entity` and `index` are in the current table.
2407                unsafe {
2408                    Some(D::fetch(
2409                        &mut self.fetch,
2410                        *entity,
2411                        TableRow::from_usize(index),
2412                    ))
2413                }
2414            } else {
2415                // SAFETY: This must have been called previously in `next` as `current_row > 0`
2416                let archetype_entity = unsafe { self.archetype_entities.get_unchecked(index) };
2417                // SAFETY:
2418                //  - `set_archetype` must have been called previously either in `next` or before it.
2419                //  - `archetype_entity.id()` and `archetype_entity.table_row()` are in the current archetype.
2420                unsafe {
2421                    Some(D::fetch(
2422                        &mut self.fetch,
2423                        archetype_entity.id(),
2424                        archetype_entity.table_row(),
2425                    ))
2426                }
2427            }
2428        } else {
2429            None
2430        }
2431    }
2432
2433    /// How many values will this cursor return at most?
2434    ///
2435    /// Note that if `F::IS_ARCHETYPAL`, the return value
2436    /// will be **the exact count of remaining values**.
2437    fn max_remaining(&self, tables: &'w Tables, archetypes: &'w Archetypes) -> usize {
2438        let ids = self.storage_id_iter.clone();
2439        let remaining_matched: usize = if self.is_dense {
2440            // SAFETY: The if check ensures that storage_id_iter stores TableIds
2441            unsafe { ids.map(|id| tables[id.table_id].entity_count()).sum() }
2442        } else {
2443            // SAFETY: The if check ensures that storage_id_iter stores ArchetypeIds
2444            unsafe { ids.map(|id| archetypes[id.archetype_id].len()).sum() }
2445        };
2446        remaining_matched + self.current_len - self.current_row
2447    }
2448
2449    // NOTE: If you are changing query iteration code, remember to update the following places, where relevant:
2450    // QueryIter, QueryIterationCursor, QuerySortedIter, QueryManyIter, QuerySortedManyIter, QueryCombinationIter,
2451    // QueryState::par_fold_init_unchecked_manual, QueryState::par_many_fold_init_unchecked_manual,
2452    // QueryState::par_many_unique_fold_init_unchecked_manual
2453    /// # Safety
2454    /// `tables` and `archetypes` must belong to the same world that the [`QueryIterationCursor`]
2455    /// was initialized for.
2456    /// `query_state` must be the same [`QueryState`] that was passed to `init` or `init_empty`.
2457    #[inline(always)]
2458    unsafe fn next(
2459        &mut self,
2460        tables: &'w Tables,
2461        archetypes: &'w Archetypes,
2462        query_state: &'s QueryState<D, F>,
2463    ) -> Option<D::Item<'w>> {
2464        if self.is_dense {
2465            loop {
2466                // we are on the beginning of the query, or finished processing a table, so skip to the next
2467                if self.current_row == self.current_len {
2468                    let table_id = self.storage_id_iter.next()?.table_id;
2469                    let table = tables.get(table_id).debug_checked_unwrap();
2470                    if table.is_empty() {
2471                        continue;
2472                    }
2473                    // SAFETY: `table` is from the world that `fetch/filter` were created for,
2474                    // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
2475                    unsafe {
2476                        D::set_table(&mut self.fetch, &query_state.fetch_state, table);
2477                        F::set_table(&mut self.filter, &query_state.filter_state, table);
2478                    }
2479                    self.table_entities = table.entities();
2480                    self.current_len = table.entity_count();
2481                    self.current_row = 0;
2482                }
2483
2484                // SAFETY: set_table was called prior.
2485                // `current_row` is a table row in range of the current table, because if it was not, then the above would have been executed.
2486                let entity = unsafe { self.table_entities.get_unchecked(self.current_row) };
2487                let row = TableRow::from_usize(self.current_row);
2488                if !F::filter_fetch(&mut self.filter, *entity, row) {
2489                    self.current_row += 1;
2490                    continue;
2491                }
2492
2493                // SAFETY:
2494                // - set_table was called prior.
2495                // - `current_row` must be a table row in range of the current table,
2496                //   because if it was not, then the above would have been executed.
2497                // - fetch is only called once for each `entity`.
2498                let item = unsafe { D::fetch(&mut self.fetch, *entity, row) };
2499
2500                self.current_row += 1;
2501                return Some(item);
2502            }
2503        } else {
2504            loop {
2505                if self.current_row == self.current_len {
2506                    let archetype_id = self.storage_id_iter.next()?.archetype_id;
2507                    let archetype = archetypes.get(archetype_id).debug_checked_unwrap();
2508                    if archetype.is_empty() {
2509                        continue;
2510                    }
2511                    let table = tables.get(archetype.table_id()).debug_checked_unwrap();
2512                    // SAFETY: `archetype` and `tables` are from the world that `fetch/filter` were created for,
2513                    // `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
2514                    unsafe {
2515                        D::set_archetype(
2516                            &mut self.fetch,
2517                            &query_state.fetch_state,
2518                            archetype,
2519                            table,
2520                        );
2521                        F::set_archetype(
2522                            &mut self.filter,
2523                            &query_state.filter_state,
2524                            archetype,
2525                            table,
2526                        );
2527                    }
2528                    self.archetype_entities = archetype.entities();
2529                    self.current_len = archetype.len();
2530                    self.current_row = 0;
2531                }
2532
2533                // SAFETY: set_archetype was called prior.
2534                // `current_row` is an archetype index row in range of the current archetype, because if it was not, then the if above would have been executed.
2535                let archetype_entity =
2536                    unsafe { self.archetype_entities.get_unchecked(self.current_row) };
2537                if !F::filter_fetch(
2538                    &mut self.filter,
2539                    archetype_entity.id(),
2540                    archetype_entity.table_row(),
2541                ) {
2542                    self.current_row += 1;
2543                    continue;
2544                }
2545
2546                // SAFETY:
2547                // - set_archetype was called prior.
2548                // - `current_row` must be an archetype index row in range of the current archetype,
2549                //   because if it was not, then the if above would have been executed.
2550                // - fetch is only called once for each `archetype_entity`.
2551                let item = unsafe {
2552                    D::fetch(
2553                        &mut self.fetch,
2554                        archetype_entity.id(),
2555                        archetype_entity.table_row(),
2556                    )
2557                };
2558                self.current_row += 1;
2559                return Some(item);
2560            }
2561        }
2562    }
2563}
2564
2565// A wrapper struct that gives its data a neutral ordering.
2566#[derive(Copy, Clone)]
2567struct NeutralOrd<T>(T);
2568
2569impl<T> PartialEq for NeutralOrd<T> {
2570    fn eq(&self, _other: &Self) -> bool {
2571        true
2572    }
2573}
2574
2575impl<T> Eq for NeutralOrd<T> {}
2576
2577#[expect(
2578    clippy::non_canonical_partial_ord_impl,
2579    reason = "`PartialOrd` and `Ord` on this struct must only ever return `Ordering::Equal`, so we prefer clarity"
2580)]
2581impl<T> PartialOrd for NeutralOrd<T> {
2582    fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
2583        Some(Ordering::Equal)
2584    }
2585}
2586
2587impl<T> Ord for NeutralOrd<T> {
2588    fn cmp(&self, _other: &Self) -> Ordering {
2589        Ordering::Equal
2590    }
2591}
2592
2593#[cfg(test)]
2594#[expect(clippy::print_stdout, reason = "Allowed in tests.")]
2595mod tests {
2596    use alloc::vec::Vec;
2597    use std::println;
2598
2599    use crate::component::Component;
2600    use crate::entity::Entity;
2601    use crate::prelude::World;
2602
2603    #[derive(Component, Debug, PartialEq, PartialOrd, Clone, Copy)]
2604    struct A(f32);
2605    #[derive(Component, Debug, Eq, PartialEq, Clone, Copy)]
2606    #[component(storage = "SparseSet")]
2607    struct Sparse(usize);
2608
2609    #[test]
2610    fn query_iter_sorts() {
2611        let mut world = World::new();
2612        for i in 0..100 {
2613            world.spawn(A(i as f32));
2614            world.spawn((A(i as f32), Sparse(i)));
2615            world.spawn(Sparse(i));
2616        }
2617
2618        let mut query = world.query::<Entity>();
2619
2620        let sort = query.iter(&world).sort::<Entity>().collect::<Vec<_>>();
2621        assert_eq!(sort.len(), 300);
2622
2623        let sort_unstable = query
2624            .iter(&world)
2625            .sort_unstable::<Entity>()
2626            .collect::<Vec<_>>();
2627
2628        let sort_by = query
2629            .iter(&world)
2630            .sort_by::<Entity>(Ord::cmp)
2631            .collect::<Vec<_>>();
2632
2633        let sort_unstable_by = query
2634            .iter(&world)
2635            .sort_unstable_by::<Entity>(Ord::cmp)
2636            .collect::<Vec<_>>();
2637
2638        let sort_by_key = query
2639            .iter(&world)
2640            .sort_by_key::<Entity, _>(|&e| e)
2641            .collect::<Vec<_>>();
2642
2643        let sort_unstable_by_key = query
2644            .iter(&world)
2645            .sort_unstable_by_key::<Entity, _>(|&e| e)
2646            .collect::<Vec<_>>();
2647
2648        let sort_by_cached_key = query
2649            .iter(&world)
2650            .sort_by_cached_key::<Entity, _>(|&e| e)
2651            .collect::<Vec<_>>();
2652
2653        let mut sort_v2 = query.iter(&world).collect::<Vec<_>>();
2654        sort_v2.sort();
2655
2656        let mut sort_unstable_v2 = query.iter(&world).collect::<Vec<_>>();
2657        sort_unstable_v2.sort_unstable();
2658
2659        let mut sort_by_v2 = query.iter(&world).collect::<Vec<_>>();
2660        sort_by_v2.sort_by(Ord::cmp);
2661
2662        let mut sort_unstable_by_v2 = query.iter(&world).collect::<Vec<_>>();
2663        sort_unstable_by_v2.sort_unstable_by(Ord::cmp);
2664
2665        let mut sort_by_key_v2 = query.iter(&world).collect::<Vec<_>>();
2666        sort_by_key_v2.sort_by_key(|&e| e);
2667
2668        let mut sort_unstable_by_key_v2 = query.iter(&world).collect::<Vec<_>>();
2669        sort_unstable_by_key_v2.sort_unstable_by_key(|&e| e);
2670
2671        let mut sort_by_cached_key_v2 = query.iter(&world).collect::<Vec<_>>();
2672        sort_by_cached_key_v2.sort_by_cached_key(|&e| e);
2673
2674        assert_eq!(sort, sort_v2);
2675        assert_eq!(sort_unstable, sort_unstable_v2);
2676        assert_eq!(sort_by, sort_by_v2);
2677        assert_eq!(sort_unstable_by, sort_unstable_by_v2);
2678        assert_eq!(sort_by_key, sort_by_key_v2);
2679        assert_eq!(sort_unstable_by_key, sort_unstable_by_key_v2);
2680        assert_eq!(sort_by_cached_key, sort_by_cached_key_v2);
2681    }
2682
2683    #[test]
2684    #[should_panic]
2685    fn query_iter_sort_after_next() {
2686        let mut world = World::new();
2687        world.spawn((A(0.),));
2688        world.spawn((A(1.1),));
2689        world.spawn((A(2.22),));
2690
2691        {
2692            let mut query = world.query::<&A>();
2693            let mut iter = query.iter(&world);
2694            println!(
2695                "archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2696                iter.cursor.archetype_entities.len(),
2697                iter.cursor.table_entities.len(),
2698                iter.cursor.current_len,
2699                iter.cursor.current_row
2700            );
2701            _ = iter.next();
2702            println!(
2703                "archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2704                iter.cursor.archetype_entities.len(),
2705                iter.cursor.table_entities.len(),
2706                iter.cursor.current_len,
2707                iter.cursor.current_row
2708            );
2709            println!("{}", iter.sort::<Entity>().len());
2710        }
2711    }
2712
2713    #[test]
2714    #[should_panic]
2715    fn query_iter_sort_after_next_dense() {
2716        let mut world = World::new();
2717        world.spawn((Sparse(11),));
2718        world.spawn((Sparse(22),));
2719        world.spawn((Sparse(33),));
2720
2721        {
2722            let mut query = world.query::<&Sparse>();
2723            let mut iter = query.iter(&world);
2724            println!(
2725                "before_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2726                iter.cursor.archetype_entities.len(),
2727                iter.cursor.table_entities.len(),
2728                iter.cursor.current_len,
2729                iter.cursor.current_row
2730            );
2731            _ = iter.next();
2732            println!(
2733                "after_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2734                iter.cursor.archetype_entities.len(),
2735                iter.cursor.table_entities.len(),
2736                iter.cursor.current_len,
2737                iter.cursor.current_row
2738            );
2739            println!("{}", iter.sort::<Entity>().len());
2740        }
2741    }
2742
2743    #[test]
2744    fn empty_query_iter_sort_after_next_does_not_panic() {
2745        let mut world = World::new();
2746        {
2747            let mut query = world.query::<(&A, &Sparse)>();
2748            let mut iter = query.iter(&world);
2749            println!(
2750                "before_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2751                iter.cursor.archetype_entities.len(),
2752                iter.cursor.table_entities.len(),
2753                iter.cursor.current_len,
2754                iter.cursor.current_row
2755            );
2756            _ = iter.next();
2757            println!(
2758                "after_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2759                iter.cursor.archetype_entities.len(),
2760                iter.cursor.table_entities.len(),
2761                iter.cursor.current_len,
2762                iter.cursor.current_row
2763            );
2764            println!("{}", iter.sort::<Entity>().len());
2765        }
2766    }
2767
2768    #[test]
2769    fn query_iter_cursor_state_non_empty_after_next() {
2770        let mut world = World::new();
2771        world.spawn((A(0.), Sparse(11)));
2772        world.spawn((A(1.1), Sparse(22)));
2773        world.spawn((A(2.22), Sparse(33)));
2774        {
2775            let mut query = world.query::<(&A, &Sparse)>();
2776            let mut iter = query.iter(&world);
2777            println!(
2778                "before_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2779                iter.cursor.archetype_entities.len(),
2780                iter.cursor.table_entities.len(),
2781                iter.cursor.current_len,
2782                iter.cursor.current_row
2783            );
2784            assert!(iter.cursor.table_entities.len() | iter.cursor.archetype_entities.len() == 0);
2785            _ = iter.next();
2786            println!(
2787                "after_next_call: archetype_entities: {} table_entities: {} current_len: {} current_row: {}",
2788                iter.cursor.archetype_entities.len(),
2789                iter.cursor.table_entities.len(),
2790                iter.cursor.current_len,
2791                iter.cursor.current_row
2792            );
2793            assert!(
2794                (
2795                    iter.cursor.table_entities.len(),
2796                    iter.cursor.archetype_entities.len()
2797                ) != (0, 0)
2798            );
2799        }
2800    }
2801
2802    #[test]
2803    fn query_iter_many_sorts() {
2804        let mut world = World::new();
2805
2806        let entity_list: &Vec<_> = &world
2807            .spawn_batch([A(0.), A(1.), A(2.), A(3.), A(4.)])
2808            .collect();
2809
2810        let mut query = world.query::<Entity>();
2811
2812        let sort = query
2813            .iter_many(&world, entity_list)
2814            .sort::<Entity>()
2815            .collect::<Vec<_>>();
2816
2817        let sort_unstable = query
2818            .iter_many(&world, entity_list)
2819            .sort_unstable::<Entity>()
2820            .collect::<Vec<_>>();
2821
2822        let sort_by = query
2823            .iter_many(&world, entity_list)
2824            .sort_by::<Entity>(Ord::cmp)
2825            .collect::<Vec<_>>();
2826
2827        let sort_unstable_by = query
2828            .iter_many(&world, entity_list)
2829            .sort_unstable_by::<Entity>(Ord::cmp)
2830            .collect::<Vec<_>>();
2831
2832        let sort_by_key = query
2833            .iter_many(&world, entity_list)
2834            .sort_by_key::<Entity, _>(|&e| e)
2835            .collect::<Vec<_>>();
2836
2837        let sort_unstable_by_key = query
2838            .iter_many(&world, entity_list)
2839            .sort_unstable_by_key::<Entity, _>(|&e| e)
2840            .collect::<Vec<_>>();
2841
2842        let sort_by_cached_key = query
2843            .iter_many(&world, entity_list)
2844            .sort_by_cached_key::<Entity, _>(|&e| e)
2845            .collect::<Vec<_>>();
2846
2847        let mut sort_v2 = query.iter_many(&world, entity_list).collect::<Vec<_>>();
2848        sort_v2.sort();
2849
2850        let mut sort_unstable_v2 = query.iter_many(&world, entity_list).collect::<Vec<_>>();
2851        sort_unstable_v2.sort_unstable();
2852
2853        let mut sort_by_v2 = query.iter_many(&world, entity_list).collect::<Vec<_>>();
2854        sort_by_v2.sort_by(Ord::cmp);
2855
2856        let mut sort_unstable_by_v2 = query.iter_many(&world, entity_list).collect::<Vec<_>>();
2857        sort_unstable_by_v2.sort_unstable_by(Ord::cmp);
2858
2859        let mut sort_by_key_v2 = query.iter_many(&world, entity_list).collect::<Vec<_>>();
2860        sort_by_key_v2.sort_by_key(|&e| e);
2861
2862        let mut sort_unstable_by_key_v2 = query.iter_many(&world, entity_list).collect::<Vec<_>>();
2863        sort_unstable_by_key_v2.sort_unstable_by_key(|&e| e);
2864
2865        let mut sort_by_cached_key_v2 = query.iter_many(&world, entity_list).collect::<Vec<_>>();
2866        sort_by_cached_key_v2.sort_by_cached_key(|&e| e);
2867
2868        assert_eq!(sort, sort_v2);
2869        assert_eq!(sort_unstable, sort_unstable_v2);
2870        assert_eq!(sort_by, sort_by_v2);
2871        assert_eq!(sort_unstable_by, sort_unstable_by_v2);
2872        assert_eq!(sort_by_key, sort_by_key_v2);
2873        assert_eq!(sort_unstable_by_key, sort_unstable_by_key_v2);
2874        assert_eq!(sort_by_cached_key, sort_by_cached_key_v2);
2875    }
2876
2877    #[test]
2878    fn query_iter_many_sort_doesnt_panic_after_next() {
2879        let mut world = World::new();
2880
2881        let entity_list: &Vec<_> = &world
2882            .spawn_batch([A(0.), A(1.), A(2.), A(3.), A(4.)])
2883            .collect();
2884
2885        let mut query = world.query::<Entity>();
2886        let mut iter = query.iter_many(&world, entity_list);
2887
2888        _ = iter.next();
2889
2890        iter.sort::<Entity>();
2891
2892        let mut query_2 = world.query::<&mut A>();
2893        let mut iter_2 = query_2.iter_many_mut(&mut world, entity_list);
2894
2895        _ = iter_2.fetch_next();
2896
2897        iter_2.sort::<Entity>();
2898    }
2899
2900    // This test should be run with miri to check for UB caused by aliasing.
2901    // The lens items created during the sort must not be live at the same time as the mutable references returned from the iterator.
2902    #[test]
2903    fn query_iter_many_sorts_duplicate_entities_no_ub() {
2904        #[derive(Component, Ord, PartialOrd, Eq, PartialEq)]
2905        struct C(usize);
2906
2907        let mut world = World::new();
2908        let id = world.spawn(C(10)).id();
2909        let mut query_state = world.query::<&mut C>();
2910
2911        {
2912            let mut query = query_state.iter_many_mut(&mut world, [id, id]).sort::<&C>();
2913            while query.fetch_next().is_some() {}
2914        }
2915        {
2916            let mut query = query_state
2917                .iter_many_mut(&mut world, [id, id])
2918                .sort_unstable::<&C>();
2919            while query.fetch_next().is_some() {}
2920        }
2921        {
2922            let mut query = query_state
2923                .iter_many_mut(&mut world, [id, id])
2924                .sort_by::<&C>(|l, r| Ord::cmp(l, r));
2925            while query.fetch_next().is_some() {}
2926        }
2927        {
2928            let mut query = query_state
2929                .iter_many_mut(&mut world, [id, id])
2930                .sort_unstable_by::<&C>(|l, r| Ord::cmp(l, r));
2931            while query.fetch_next().is_some() {}
2932        }
2933        {
2934            let mut query = query_state
2935                .iter_many_mut(&mut world, [id, id])
2936                .sort_by_key::<&C, _>(|d| d.0);
2937            while query.fetch_next().is_some() {}
2938        }
2939        {
2940            let mut query = query_state
2941                .iter_many_mut(&mut world, [id, id])
2942                .sort_unstable_by_key::<&C, _>(|d| d.0);
2943            while query.fetch_next().is_some() {}
2944        }
2945        {
2946            let mut query = query_state
2947                .iter_many_mut(&mut world, [id, id])
2948                .sort_by_cached_key::<&C, _>(|d| d.0);
2949            while query.fetch_next().is_some() {}
2950        }
2951    }
2952}