From 8dcd4fd6a69722393554ddc87d4e83e0cb5d95d4 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sun, 4 Apr 2021 17:26:59 +0200 Subject: [PATCH 1/9] Added example of entity sorting by components --- crates/bevy_ecs/src/world/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index bd6c10e6a9079..4d99992f0d92e 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -444,6 +444,25 @@ impl World { /// assert_eq!(world.get::(entities[0]).unwrap(), &Position { x: 1.0, y: 0.0 }); /// assert_eq!(world.get::(entities[1]).unwrap(), &Position { x: 0.0, y: 1.0 }); /// ``` + /// + /// To iterate over entities in a deterministic order sort results using the required component as a key. + /// + /// ``` + /// use bevy_ecs::world::World; + /// let mut world = World::new(); + /// let a = world.spawn((2, "abc")); + /// let b = world.spawn((3, "xyz")); + /// let c = world.spawn((1, "def")); + /// let mut entities = world.query::<(Entity, &i32, &str)>() + /// .map(|(e, &i &s)| (e, i, &s)) // Copy out of the world + /// .collect::>(); + /// // Sort by `i32` component + /// entities.sort_by(|x, y| x.1.cmp(&y.1)); + /// assert_eq!(entities.len(), 3); + /// assert!(entities[0] == (c, 1, "def")); + /// assert!(entities[1] == (a, 2, "abc")); + /// assert!(entities[2] == (b, 3, "xyz")); + /// ``` #[inline] pub fn query(&mut self) -> QueryState { QueryState::new(self) From 575868f0a00843189fc6e06359c6fca70c8d2e6b Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Fri, 16 Apr 2021 23:12:03 +0200 Subject: [PATCH 2/9] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/world/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 4d99992f0d92e..2130be6a38d00 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -445,7 +445,8 @@ impl World { /// assert_eq!(world.get::(entities[1]).unwrap(), &Position { x: 0.0, y: 1.0 }); /// ``` /// - /// To iterate over entities in a deterministic order sort results using the required component as a key. + /// To iterate over entities in a deterministic order, + /// sort the results of the query using the desired component as a key. /// /// ``` /// use bevy_ecs::world::World; From 4a9d89b6cfb67f84b0290735a396dd3b4b24b550 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sat, 17 Apr 2021 18:12:03 +0200 Subject: [PATCH 3/9] Added loop into example --- crates/bevy_ecs/src/world/mod.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 2130be6a38d00..0c294b2f2032e 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -459,10 +459,13 @@ impl World { /// .collect::>(); /// // Sort by `i32` component /// entities.sort_by(|x, y| x.1.cmp(&y.1)); - /// assert_eq!(entities.len(), 3); - /// assert!(entities[0] == (c, 1, "def")); - /// assert!(entities[1] == (a, 2, "abc")); - /// assert!(entities[2] == (b, 3, "xyz")); + /// for (index, entity) in entities.iter().enumerate() { + /// match index { + /// 0 => assert!(entity == (c, 1, "def")), + /// 1 => assert!(entity == (a, 2, "abc")), + /// 2 => assert!(entity == (b, 3, "xyz")), + /// } + /// } /// ``` #[inline] pub fn query(&mut self) -> QueryState { From 650ec48fcb2170d0c2c652d33f0c6920054f3b49 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sat, 17 Apr 2021 19:05:49 +0200 Subject: [PATCH 4/9] Fixed rebase issues --- crates/bevy_ecs/src/world/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 0c294b2f2032e..e4fcf7823caed 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -449,11 +449,11 @@ impl World { /// sort the results of the query using the desired component as a key. /// /// ``` - /// use bevy_ecs::world::World; + /// use bevy_ecs{entity::Entity, world::World}; /// let mut world = World::new(); - /// let a = world.spawn((2, "abc")); - /// let b = world.spawn((3, "xyz")); - /// let c = world.spawn((1, "def")); + /// let a = world.spawn().insert_bundle((2, "abc")).id(); + /// let b = world.spawn().insert_bundle((3, "xyz")).id(); + /// let c = world.spawn().insert_bundle((1, "def")).id(); /// let mut entities = world.query::<(Entity, &i32, &str)>() /// .map(|(e, &i &s)| (e, i, &s)) // Copy out of the world /// .collect::>(); From d6d0ee37b1d3d7dfda30832ff53d015a765e2d5f Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sat, 17 Apr 2021 19:22:31 +0200 Subject: [PATCH 5/9] Fixup --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index e4fcf7823caed..e4bd397d718d1 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -449,7 +449,7 @@ impl World { /// sort the results of the query using the desired component as a key. /// /// ``` - /// use bevy_ecs{entity::Entity, world::World}; + /// use bevy_ecs::{entity::Entity, world::World}; /// let mut world = World::new(); /// let a = world.spawn().insert_bundle((2, "abc")).id(); /// let b = world.spawn().insert_bundle((3, "xyz")).id(); From 7d8ef4e6a51cce52b1cd9cf5d03f48a4bc49aebd Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sat, 17 Apr 2021 21:44:29 +0200 Subject: [PATCH 6/9] Fixes and review issues --- crates/bevy_ecs/src/world/mod.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index e4bd397d718d1..4e8cded9d7087 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -451,19 +451,20 @@ impl World { /// ``` /// use bevy_ecs::{entity::Entity, world::World}; /// let mut world = World::new(); - /// let a = world.spawn().insert_bundle((2, "abc")).id(); - /// let b = world.spawn().insert_bundle((3, "xyz")).id(); - /// let c = world.spawn().insert_bundle((1, "def")).id(); - /// let mut entities = world.query::<(Entity, &i32, &str)>() - /// .map(|(e, &i &s)| (e, i, &s)) // Copy out of the world + /// let a = world.spawn().insert_bundle((2, 4.0)).id(); + /// let b = world.spawn().insert_bundle((3, 5.0)).id(); + /// let c = world.spawn().insert_bundle((1, 6.0)).id(); + /// let mut entities = world.query::<(Entity, &i32, &f32)>() + /// .iter(&world) /// .collect::>(); /// // Sort by `i32` component /// entities.sort_by(|x, y| x.1.cmp(&y.1)); /// for (index, entity) in entities.iter().enumerate() { /// match index { - /// 0 => assert!(entity == (c, 1, "def")), - /// 1 => assert!(entity == (a, 2, "abc")), - /// 2 => assert!(entity == (b, 3, "xyz")), + /// 0 => assert_eq!(entity, &(c, &1, &6.0)), + /// 1 => assert_eq!(entity, &(a, &2, &4.0)), + /// 2 => assert_eq!(entity, &(b, &3, &5.0)), + /// _ => panic!("not expected"), /// } /// } /// ``` From 8f3ad20a2568e9ad4f775ff7b74885f1295592e1 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sat, 17 Apr 2021 22:28:56 +0200 Subject: [PATCH 7/9] Note on vec allocation --- crates/bevy_ecs/src/world/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 4e8cded9d7087..fcb746732e404 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -447,6 +447,8 @@ impl World { /// /// To iterate over entities in a deterministic order, /// sort the results of the query using the desired component as a key. + /// Note that it requires fetching the whole result set from the query + /// and allocation of a [Vec] to store it. /// /// ``` /// use bevy_ecs::{entity::Entity, world::World}; From 0be8174f3ce31bd7c7c26062daf7bb33227370b1 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sat, 17 Apr 2021 22:36:38 +0200 Subject: [PATCH 8/9] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index fcb746732e404..2d6cc09de1bd7 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -447,7 +447,7 @@ impl World { /// /// To iterate over entities in a deterministic order, /// sort the results of the query using the desired component as a key. - /// Note that it requires fetching the whole result set from the query + /// Note that this requires fetching the whole result set from the query /// and allocation of a [Vec] to store it. /// /// ``` From 7559068fdaa61721c01e36b8a89becf481e85046 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 19 Apr 2021 13:26:51 -0700 Subject: [PATCH 9/9] Use sort_by_key, simplify doctest assertion, and add note on query order --- crates/bevy_ecs/src/world/mod.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 2d6cc09de1bd7..2646f58a1dbfa 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -456,19 +456,13 @@ impl World { /// let a = world.spawn().insert_bundle((2, 4.0)).id(); /// let b = world.spawn().insert_bundle((3, 5.0)).id(); /// let c = world.spawn().insert_bundle((1, 6.0)).id(); - /// let mut entities = world.query::<(Entity, &i32, &f32)>() + /// let mut entities = world.query::<(Entity, &i32, &f64)>() /// .iter(&world) /// .collect::>(); - /// // Sort by `i32` component - /// entities.sort_by(|x, y| x.1.cmp(&y.1)); - /// for (index, entity) in entities.iter().enumerate() { - /// match index { - /// 0 => assert_eq!(entity, &(c, &1, &6.0)), - /// 1 => assert_eq!(entity, &(a, &2, &4.0)), - /// 2 => assert_eq!(entity, &(b, &3, &5.0)), - /// _ => panic!("not expected"), - /// } - /// } + /// // Sort the query results by their `i32` component before comparing + /// // to expected results. Query iteration order should not be relied on. + /// entities.sort_by_key(|e| e.1); + /// assert_eq!(entities, vec![(c, &1, &6.0), (a, &2, &4.0), (b, &3, &5.0)]); /// ``` #[inline] pub fn query(&mut self) -> QueryState {