From 66cbb5d207094799740228c4c913218ddca3a19d Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 22 Jul 2026 16:59:48 +0200 Subject: refactor(engine-ecs): clean up query terms fns & fields --- engine-ecs/src/query.rs | 82 ++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 55 deletions(-) (limited to 'engine-ecs/src/query.rs') diff --git a/engine-ecs/src/query.rs b/engine-ecs/src/query.rs index 16e7b99..1b7bf43 100644 --- a/engine-ecs/src/query.rs +++ b/engine-ecs/src/query.rs @@ -11,7 +11,7 @@ use crate::component::{ use crate::entity::Handle as EntityHandle; use crate::query::flexible::{Iter as FlexibleQueryIter, Query as FlexibleQuery}; use crate::system::{Metadata as SystemMetadata, Param as SystemParam}; -use crate::uid::{Uid, With as WithUid}; +use crate::uid::Uid; use crate::util::array_vec::ArrayVec; use crate::util::Array; use crate::World; @@ -168,8 +168,8 @@ where #[derive(Debug)] pub struct Terms { - required_components: ArrayVec, - excluded_components: ArrayVec, + present: ArrayVec, + absent: ArrayVec, } impl Terms @@ -184,20 +184,16 @@ impl Terms #[must_use] pub struct TermsBuilder { - required_components: ArrayVec, - excluded_components: ArrayVec, + present: ArrayVec, + absent: ArrayVec, } #[allow(clippy::return_self_not_must_use)] pub trait TermsBuilderInterface { - fn with(self) -> Self; + fn present(self, ids: impl Array) -> Self; - fn without(self) -> Self; - - fn with_required(self, ids: impl Array) -> Self; - - fn without_ids(self, ids: impl Array) -> Self; + fn absent(self, ids: impl Array) -> Self; } macro_rules! impl_terms_builder { @@ -218,54 +214,30 @@ macro_rules! impl_terms_builder { impl_terms_builder! { #[allow(unused_mut)] - fn with(mut self) -> Self - { - let insert_index = self.required_components - .partition_point(|id| *id <= WithUidT::uid()); - - self.required_components - .insert(insert_index, WithUidT::uid()); - - self - } - - #[allow(unused_mut)] - fn without(mut self) -> Self - { - let insert_index = self.excluded_components - .partition_point(|id| *id <= WithUidT::uid()); - - self.excluded_components - .insert(insert_index, WithUidT::uid()); - - self - } - - #[allow(unused_mut)] - fn with_required(mut self, mut ids: impl Array) -> Self + fn present(mut self, mut ids: impl Array) -> Self { if !ids.as_ref().is_sorted() { ids.as_mut().sort(); } - if self.required_components.is_empty() { - self.required_components.extend(ids); + if self.present.is_empty() { + self.present.extend(ids); return self; } let mut id_iter = ids.into_iter(); while let Some(id) = id_iter.next() { - let insert_index = self.required_components + let insert_index = self.present .partition_point(|other_id| *other_id <= id); - if insert_index == self.required_components.len() { - self.required_components.extend([id].into_iter().chain(id_iter)); + if insert_index == self.present.len() { + self.present.extend([id].into_iter().chain(id_iter)); return self; } - self.required_components + self.present .insert(insert_index, id); } @@ -274,30 +246,30 @@ impl_terms_builder! { } #[allow(unused_mut)] - fn without_ids(mut self, mut ids: impl Array) -> Self + fn absent(mut self, mut ids: impl Array) -> Self { if !ids.as_ref().is_sorted() { ids.as_mut().sort(); } - if self.excluded_components.is_empty() { - self.excluded_components.extend(ids); + if self.absent.is_empty() { + self.absent.extend(ids); return self; } let mut id_iter = ids.into_iter(); while let Some(id) = id_iter.next() { - let insert_index = self.excluded_components + let insert_index = self.absent .partition_point(|other_id| *other_id <= id); - if insert_index == self.excluded_components.len() { - self.excluded_components.extend([id].into_iter().chain(id_iter)); + if insert_index == self.absent.len() { + self.absent.extend([id].into_iter().chain(id_iter)); return self; } - self.excluded_components + self.absent .insert(insert_index, id); } @@ -311,12 +283,12 @@ impl TermsBuilder #[must_use] pub fn build(self) -> Terms { - debug_assert!(self.required_components.is_sorted()); - debug_assert!(self.excluded_components.is_sorted()); + debug_assert!(self.present.is_sorted()); + debug_assert!(self.absent.is_sorted()); Terms { - required_components: self.required_components, - excluded_components: self.excluded_components, + present: self.present, + absent: self.absent, } } } @@ -350,7 +322,7 @@ impl TermWithField for &ComponentT terms_builder: &mut TermsBuilder, ) { - terms_builder.with::(); + terms_builder.present([ComponentT::id()]); } fn get_field<'world>( @@ -391,7 +363,7 @@ impl TermWithField for &mut ComponentT terms_builder: &mut TermsBuilder, ) { - terms_builder.with::(); + terms_builder.present([ComponentT::id()]); } fn get_field<'world>( -- cgit v1.2.3-18-g5258