@@ -214,6 +214,59 @@ where
214
214
{
215
215
}
216
216
217
+ /// An iterator over `K`-sized combinations of query items without repetition.
218
+ ///
219
+ /// In this context, a combination is an unordered subset of `K` elements.
220
+ /// The number of combinations depend on how `K` relates to the number of entities matching the [`Query`] (called `N`):
221
+ /// - if `K = N`, only one combination exists,
222
+ /// - if `K < N`, there are <sub>N</sub>C<sub>K</sub> combinations (see the [performance section] of `Query`),
223
+ /// - if `K > N`, there are no combinations.
224
+ ///
225
+ /// # Usage
226
+ ///
227
+ /// This type is returned by calling [`Query::iter_combinations`] or [`Query::iter_combinations_mut`].
228
+ ///
229
+ /// It implements [`Iterator`] only if it iterates over read-only query items ([learn more]).
230
+ ///
231
+ /// In the case of mutable query items, it can be iterated by calling [`fetch_next`] in a `while let` loop.
232
+ ///
233
+ /// # Examples
234
+ ///
235
+ /// The following example shows how to traverse the iterator when the query items are read-only.
236
+ ///
237
+ /// ```
238
+ /// # use bevy_ecs::prelude::*;
239
+ /// # #[derive(Component)]
240
+ /// # struct ComponentA;
241
+ /// #
242
+ /// fn some_system(query: Query<&ComponentA>) {
243
+ /// for [a1, a2] in query.iter_combinations() {
244
+ /// // ...
245
+ /// }
246
+ /// }
247
+ /// ```
248
+ ///
249
+ /// 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.
250
+ ///
251
+ /// ```
252
+ /// # use bevy_ecs::prelude::*;
253
+ /// # #[derive(Component)]
254
+ /// # struct ComponentA;
255
+ /// #
256
+ /// fn some_system(mut query: Query<&mut ComponentA>) {
257
+ /// let mut combinations = query.iter_combinations_mut();
258
+ /// while let Some([a1, a2]) = combinations.fetch_next() {
259
+ /// // ...
260
+ /// }
261
+ /// }
262
+ /// ```
263
+ ///
264
+ /// [`fetch_next`]: Self::fetch_next
265
+ /// [learn more]: Self#impl-Iterator
266
+ /// [performance section]: crate::system::Query#performance
267
+ /// [`Query`]: crate::system::Query
268
+ /// [`Query::iter_combinations`]: crate::system::Query::iter_combinations
269
+ /// [`Query::iter_combinations_mut`]: crate::system::Query::iter_combinations_mut
217
270
pub struct QueryCombinationIter < ' w , ' s , Q : WorldQuery , F : WorldQuery , const K : usize > {
218
271
tables : & ' w Tables ,
219
272
archetypes : & ' w Archetypes ,
0 commit comments