using System.Runtime.CompilerServices; namespace OECS; /// /// Provides query execution logic for . /// Iterates one sparse set as the driver and probes the remaining sets /// for membership. The "without" filter is checked after all "with" probes. /// /// The singleton entity (ID 1) is automatically excluded from all queries. /// internal static class QueryExecutor { private const uint SingletonId = 1; [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool PassesWithoutFilter(ComponentStore store, Entity entity, IReadOnlySet withoutTypes) { foreach (var type in withoutTypes) { var set = store.GetSet(type); if (set != null && set.Contains(entity)) return false; } return true; } // ── ForEach overloads ────────────────────────────────────────────── public static void ForEach( ComponentStore store, QueryDescriptor query, ForEachAction action) where T1 : struct { var set = store.GetSet(typeof(T1)) as SparseSet; if (set == null) return; var dense = set.Dense; var denseEntities = set.DenseEntities; var count = set.Count; for (int i = 0; i < count; i++) { var entity = denseEntities[i]; if (entity.Id == SingletonId) continue; if (!PassesWithoutFilter(store, entity, query.Without)) continue; action(entity, ref dense[i]); } } public static void ForEach( ComponentStore store, QueryDescriptor query, ForEachAction action) where T1 : struct where T2 : struct { var set1 = store.GetSet(typeof(T1)) as SparseSet; if (set1 == null) return; var set2 = store.GetSet(typeof(T2)) as SparseSet; if (set2 == null) return; if (set1.Count <= set2.Count) { IterateTwo(set1, set2, store, query.Without, action); } else { IterateTwoSwapped(set2, set1, store, query.Without, action); } } private static void IterateTwo( SparseSet driveSet, SparseSet otherSet, ComponentStore store, IReadOnlySet withoutTypes, ForEachAction action) where TDriver : struct where TOther : struct { var dense = driveSet.Dense; var denseEntities = driveSet.DenseEntities; var count = driveSet.Count; for (int i = 0; i < count; i++) { var entity = denseEntities[i]; if (entity.Id == SingletonId) continue; if (!otherSet.Contains(entity)) continue; if (!PassesWithoutFilter(store, entity, withoutTypes)) continue; action(entity, ref dense[i], ref otherSet.Get(entity)); } } private static void IterateTwoSwapped( SparseSet driveSet, SparseSet otherSet, ComponentStore store, IReadOnlySet withoutTypes, ForEachAction action) where TDriver : struct where TOther : struct { var dense = driveSet.Dense; var denseEntities = driveSet.DenseEntities; var count = driveSet.Count; for (int i = 0; i < count; i++) { var entity = denseEntities[i]; if (entity.Id == SingletonId) continue; if (!otherSet.Contains(entity)) continue; if (!PassesWithoutFilter(store, entity, withoutTypes)) continue; action(entity, ref otherSet.Get(entity), ref dense[i]); } } public static void ForEach( ComponentStore store, QueryDescriptor query, ForEachAction action) where T1 : struct where T2 : struct where T3 : struct { var set1 = store.GetSet(typeof(T1)) as SparseSet; if (set1 == null) return; var set2 = store.GetSet(typeof(T2)) as SparseSet; if (set2 == null) return; var set3 = store.GetSet(typeof(T3)) as SparseSet; if (set3 == null) return; var dense = set1.Dense; var denseEntities = set1.DenseEntities; var count = set1.Count; for (int i = 0; i < count; i++) { var entity = denseEntities[i]; if (entity.Id == SingletonId) continue; if (!set2.Contains(entity)) continue; if (!set3.Contains(entity)) continue; if (!PassesWithoutFilter(store, entity, query.Without)) continue; action(entity, ref dense[i], ref set2.Get(entity), ref set3.Get(entity)); } } public static void ForEach( ComponentStore store, QueryDescriptor query, ForEachAction action) where T1 : struct where T2 : struct where T3 : struct where T4 : struct { var set1 = store.GetSet(typeof(T1)) as SparseSet; if (set1 == null) return; var set2 = store.GetSet(typeof(T2)) as SparseSet; if (set2 == null) return; var set3 = store.GetSet(typeof(T3)) as SparseSet; if (set3 == null) return; var set4 = store.GetSet(typeof(T4)) as SparseSet; if (set4 == null) return; var dense = set1.Dense; var denseEntities = set1.DenseEntities; var count = set1.Count; for (int i = 0; i < count; i++) { var entity = denseEntities[i]; if (entity.Id == SingletonId) continue; if (!set2.Contains(entity)) continue; if (!set3.Contains(entity)) continue; if (!set4.Contains(entity)) continue; if (!PassesWithoutFilter(store, entity, query.Without)) continue; action(entity, ref dense[i], ref set2.Get(entity), ref set3.Get(entity), ref set4.Get(entity)); } } public static void ForEach( ComponentStore store, QueryDescriptor query, ForEachAction action) where T1 : struct where T2 : struct where T3 : struct where T4 : struct where T5 : struct { var set1 = store.GetSet(typeof(T1)) as SparseSet; if (set1 == null) return; var set2 = store.GetSet(typeof(T2)) as SparseSet; if (set2 == null) return; var set3 = store.GetSet(typeof(T3)) as SparseSet; if (set3 == null) return; var set4 = store.GetSet(typeof(T4)) as SparseSet; if (set4 == null) return; var set5 = store.GetSet(typeof(T5)) as SparseSet; if (set5 == null) return; var dense = set1.Dense; var denseEntities = set1.DenseEntities; var count = set1.Count; for (int i = 0; i < count; i++) { var entity = denseEntities[i]; if (entity.Id == SingletonId) continue; if (!set2.Contains(entity)) continue; if (!set3.Contains(entity)) continue; if (!set4.Contains(entity)) continue; if (!set5.Contains(entity)) continue; if (!PassesWithoutFilter(store, entity, query.Without)) continue; action(entity, ref dense[i], ref set2.Get(entity), ref set3.Get(entity), ref set4.Get(entity), ref set5.Get(entity)); } } public static void ForEach( ComponentStore store, QueryDescriptor query, ForEachAction action) where T1 : struct where T2 : struct where T3 : struct where T4 : struct where T5 : struct where T6 : struct { var set1 = store.GetSet(typeof(T1)) as SparseSet; if (set1 == null) return; var set2 = store.GetSet(typeof(T2)) as SparseSet; if (set2 == null) return; var set3 = store.GetSet(typeof(T3)) as SparseSet; if (set3 == null) return; var set4 = store.GetSet(typeof(T4)) as SparseSet; if (set4 == null) return; var set5 = store.GetSet(typeof(T5)) as SparseSet; if (set5 == null) return; var set6 = store.GetSet(typeof(T6)) as SparseSet; if (set6 == null) return; var dense = set1.Dense; var denseEntities = set1.DenseEntities; var count = set1.Count; for (int i = 0; i < count; i++) { var entity = denseEntities[i]; if (entity.Id == SingletonId) continue; if (!set2.Contains(entity)) continue; if (!set3.Contains(entity)) continue; if (!set4.Contains(entity)) continue; if (!set5.Contains(entity)) continue; if (!set6.Contains(entity)) continue; if (!PassesWithoutFilter(store, entity, query.Without)) continue; action(entity, ref dense[i], ref set2.Get(entity), ref set3.Get(entity), ref set4.Get(entity), ref set5.Get(entity), ref set6.Get(entity)); } } }