< Summary

Information
Class: Lattice.ECS.Core.OwningGroup<T1, T2, T3>
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/OwningGroup.cs
Line coverage
77%
Covered lines: 52
Uncovered lines: 15
Coverable lines: 67
Total lines: 335
Line coverage: 77.6%
Branch coverage
71%
Covered branches: 27
Total branches: 38
Branch coverage: 71%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Count()100%11100%
NextBlock(...)100%210%
Next(...)100%11100%
DependsOn(...)100%44100%
Reinitialize(...)100%11100%
SyncEntity(...)68.75%171686.36%
Rebuild(...)50%7442.85%
ShouldInclude(...)83.33%6680%
GetDenseIndex(...)50%22100%
RemoveDenseIndex(...)50%5466.66%
ResetDenseIndexMap()100%22100%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/OwningGroup.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4using System;
 5using System.Runtime.CompilerServices;
 6using Lattice.Core;
 7
 8namespace Lattice.ECS.Core
 9{
 10    internal unsafe interface IFrameOwningGroup
 11    {
 12        bool DependsOn(int typeId);
 13
 14        void Reinitialize(Allocator* allocator, int entityCapacity);
 15
 16        void SyncEntity(Frame frame, EntityRef entity, int changedTypeId);
 17
 18        void Rebuild(Frame frame);
 19    }
 20
 21    /// <summary>
 22    /// 显式注册的双组件 Owning Group。
 23    /// 该结构由 Frame 自动维护,适合极少数固定热点系统的高频遍历。
 24    /// </summary>
 25    public unsafe sealed class OwningGroup<T1, T2> : IFrameOwningGroup
 26        where T1 : unmanaged, IComponent
 27        where T2 : unmanaged, IComponent
 28    {
 29        private readonly int _typeId1;
 30        private readonly int _typeId2;
 31        private int _entityCapacity;
 32        private int* _entityToDenseIndex;
 33        private FullOwningGroup<T1, T2> _group;
 34
 35        internal OwningGroup(Allocator* allocator, int entityCapacity, int typeId1, int typeId2)
 36        {
 37            _typeId1 = typeId1;
 38            _typeId2 = typeId2;
 39            Reinitialize(allocator, entityCapacity);
 40        }
 41
 42        public int Count => _group.Count;
 43
 44        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 45        public bool NextBlock(int* blockIndex, out EntityRef* entities, out T1* data1, out T2* data2, out int count)
 46        {
 47            return _group.NextBlock(blockIndex, out entities, out data1, out data2, out count);
 48        }
 49
 50        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 51        public bool Next(int* globalIndex, out EntityRef entity, out T1* c1, out T2* c2)
 52        {
 53            return _group.Next(globalIndex, out entity, out c1, out c2);
 54        }
 55
 56        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 57        public bool DependsOn(int typeId)
 58        {
 59            return typeId == _typeId1 || typeId == _typeId2;
 60        }
 61
 62        public void Reinitialize(Allocator* allocator, int entityCapacity)
 63        {
 64            _entityCapacity = entityCapacity;
 65            _entityToDenseIndex = (int*)allocator->Alloc(sizeof(int) * entityCapacity);
 66            _group = default;
 67            _group.Initialize(allocator);
 68            ResetDenseIndexMap();
 69        }
 70
 71        public void SyncEntity(Frame frame, EntityRef entity, int changedTypeId)
 72        {
 73            _ = changedTypeId;
 74
 75            int denseIndex = GetDenseIndex(entity.Index);
 76            if (!ShouldInclude(frame, entity))
 77            {
 78                if (denseIndex >= 0)
 79                {
 80                    RemoveDenseIndex(entity.Index, denseIndex);
 81                }
 82
 83                return;
 84            }
 85
 86            T1* component1 = frame.GetPointer<T1>(entity);
 87            T2* component2 = frame.GetPointer<T2>(entity);
 88            if (component1 == null || component2 == null)
 89            {
 90                if (denseIndex >= 0)
 91                {
 92                    RemoveDenseIndex(entity.Index, denseIndex);
 93                }
 94
 95                return;
 96            }
 97
 98            if (denseIndex >= 0)
 99            {
 100                if (_group.GetAtLinearIndex(denseIndex, out EntityRef _, out T1* group1, out T2* group2))
 101                {
 102                    *group1 = *component1;
 103                    *group2 = *component2;
 104                }
 105
 106                return;
 107            }
 108
 109            int newIndex = _group.Count;
 110            _group.Add(entity, *component1, *component2);
 111            _entityToDenseIndex[entity.Index] = newIndex;
 112        }
 113
 114        public void Rebuild(Frame frame)
 115        {
 116            ResetDenseIndexMap();
 117            while (_group.Count > 0)
 118            {
 119                _group.RemoveAtLinearIndex(_group.Count - 1, out _);
 120            }
 121
 122            var query = frame.Query<T1, T2>();
 123            var enumerator = query.GetEnumerator();
 124            while (enumerator.MoveNext())
 125            {
 126                int denseIndex = _group.Count;
 127                _group.Add(enumerator.Entity, *enumerator.Component1Ptr, *enumerator.Component2Ptr);
 128                _entityToDenseIndex[enumerator.Entity.Index] = denseIndex;
 129            }
 130        }
 131
 132        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 133        private bool ShouldInclude(Frame frame, EntityRef entity)
 134        {
 135            if (!frame.IsValid(entity))
 136            {
 137                return false;
 138            }
 139
 140            return frame.HasComponentBit(entity.Index, _typeId1)
 141                && frame.HasComponentBit(entity.Index, _typeId2);
 142        }
 143
 144        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 145        private int GetDenseIndex(int entityIndex)
 146        {
 147            return (uint)entityIndex < (uint)_entityCapacity ? _entityToDenseIndex[entityIndex] : -1;
 148        }
 149
 150        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 151        private void RemoveDenseIndex(int entityIndex, int denseIndex)
 152        {
 153            if (!_group.RemoveAtLinearIndex(denseIndex, out EntityRef movedEntity))
 154            {
 155                return;
 156            }
 157
 158            _entityToDenseIndex[entityIndex] = -1;
 159            if (movedEntity != EntityRef.None)
 160            {
 161                _entityToDenseIndex[movedEntity.Index] = denseIndex;
 162            }
 163        }
 164
 165        private void ResetDenseIndexMap()
 166        {
 167            for (int i = 0; i < _entityCapacity; i++)
 168            {
 169                _entityToDenseIndex[i] = -1;
 170            }
 171        }
 172    }
 173
 174    /// <summary>
 175    /// 显式注册的三组件 Owning Group。
 176    /// </summary>
 177    public unsafe sealed class OwningGroup<T1, T2, T3> : IFrameOwningGroup
 178        where T1 : unmanaged, IComponent
 179        where T2 : unmanaged, IComponent
 180        where T3 : unmanaged, IComponent
 181    {
 182        private readonly int _typeId1;
 183        private readonly int _typeId2;
 184        private readonly int _typeId3;
 185        private int _entityCapacity;
 186        private int* _entityToDenseIndex;
 187        private FullOwningGroup<T1, T2, T3> _group;
 188
 1189        internal OwningGroup(Allocator* allocator, int entityCapacity, int typeId1, int typeId2, int typeId3)
 190        {
 1191            _typeId1 = typeId1;
 1192            _typeId2 = typeId2;
 1193            _typeId3 = typeId3;
 1194            Reinitialize(allocator, entityCapacity);
 1195        }
 196
 3197        public int Count => _group.Count;
 198
 199        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 200        public bool NextBlock(int* blockIndex, out EntityRef* entities, out T1* data1, out T2* data2, out T3* data3, out
 201        {
 0202            return _group.NextBlock(blockIndex, out entities, out data1, out data2, out data3, out count);
 203        }
 204
 205        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 206        public bool Next(int* globalIndex, out EntityRef entity, out T1* c1, out T2* c2, out T3* c3)
 207        {
 4208            return _group.Next(globalIndex, out entity, out c1, out c2, out c3);
 209        }
 210
 211        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 212        public bool DependsOn(int typeId)
 213        {
 3214            return typeId == _typeId1 || typeId == _typeId2 || typeId == _typeId3;
 215        }
 216
 217        public void Reinitialize(Allocator* allocator, int entityCapacity)
 218        {
 1219            _entityCapacity = entityCapacity;
 1220            _entityToDenseIndex = (int*)allocator->Alloc(sizeof(int) * entityCapacity);
 1221            _group = default;
 1222            _group.Initialize(allocator);
 1223            ResetDenseIndexMap();
 1224        }
 225
 226        public void SyncEntity(Frame frame, EntityRef entity, int changedTypeId)
 227        {
 228            _ = changedTypeId;
 229
 3230            int denseIndex = GetDenseIndex(entity.Index);
 3231            if (!ShouldInclude(frame, entity))
 232            {
 1233                if (denseIndex >= 0)
 234                {
 1235                    RemoveDenseIndex(entity.Index, denseIndex);
 236                }
 237
 1238                return;
 239            }
 240
 2241            T1* component1 = frame.GetPointer<T1>(entity);
 2242            T2* component2 = frame.GetPointer<T2>(entity);
 2243            T3* component3 = frame.GetPointer<T3>(entity);
 2244            if (component1 == null || component2 == null || component3 == null)
 245            {
 0246                if (denseIndex >= 0)
 247                {
 0248                    RemoveDenseIndex(entity.Index, denseIndex);
 249                }
 250
 0251                return;
 252            }
 253
 2254            if (denseIndex >= 0)
 255            {
 1256                if (_group.GetAtLinearIndex(denseIndex, out EntityRef _, out T1* group1, out T2* group2, out T3* group3)
 257                {
 1258                    *group1 = *component1;
 1259                    *group2 = *component2;
 1260                    *group3 = *component3;
 261                }
 262
 1263                return;
 264            }
 265
 1266            int newIndex = _group.Count;
 1267            _group.Add(entity, *component1, *component2, *component3);
 1268            _entityToDenseIndex[entity.Index] = newIndex;
 1269        }
 270
 271        public void Rebuild(Frame frame)
 272        {
 1273            ResetDenseIndexMap();
 1274            while (_group.Count > 0)
 275            {
 0276                _group.RemoveAtLinearIndex(_group.Count - 1, out _);
 277            }
 278
 1279            var query = frame.Query<T1, T2, T3>();
 1280            var enumerator = query.GetEnumerator();
 1281            while (enumerator.MoveNext())
 282            {
 0283                int denseIndex = _group.Count;
 0284                _group.Add(
 0285                    enumerator.Entity,
 0286                    *enumerator.Component1Ptr,
 0287                    *enumerator.Component2Ptr,
 0288                    *enumerator.Component3Ptr);
 0289                _entityToDenseIndex[enumerator.Entity.Index] = denseIndex;
 290            }
 1291        }
 292
 293        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 294        private bool ShouldInclude(Frame frame, EntityRef entity)
 295        {
 3296            if (!frame.IsValid(entity))
 297            {
 0298                return false;
 299            }
 300
 3301            return frame.HasComponentBit(entity.Index, _typeId1)
 3302                && frame.HasComponentBit(entity.Index, _typeId2)
 3303                && frame.HasComponentBit(entity.Index, _typeId3);
 304        }
 305
 306        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 307        private int GetDenseIndex(int entityIndex)
 308        {
 3309            return (uint)entityIndex < (uint)_entityCapacity ? _entityToDenseIndex[entityIndex] : -1;
 310        }
 311
 312        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 313        private void RemoveDenseIndex(int entityIndex, int denseIndex)
 314        {
 1315            if (!_group.RemoveAtLinearIndex(denseIndex, out EntityRef movedEntity))
 316            {
 0317                return;
 318            }
 319
 1320            _entityToDenseIndex[entityIndex] = -1;
 1321            if (movedEntity != EntityRef.None)
 322            {
 0323                _entityToDenseIndex[movedEntity.Index] = denseIndex;
 324            }
 1325        }
 326
 327        private void ResetDenseIndexMap()
 328        {
 36329            for (int i = 0; i < _entityCapacity; i++)
 330            {
 16331                _entityToDenseIndex[i] = -1;
 332            }
 2333        }
 334    }
 335}