< Summary

Information
Class: Lattice.ECS.Core.OwningGroup<T1, T2>
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/OwningGroup.cs
Line coverage
88%
Covered lines: 52
Uncovered lines: 7
Coverable lines: 59
Total lines: 335
Line coverage: 88.1%
Branch coverage
75%
Covered branches: 24
Total branches: 32
Branch coverage: 75%
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%22100%
Reinitialize(...)100%11100%
SyncEntity(...)71.42%151485%
Rebuild(...)75%4490%
ShouldInclude(...)75%4475%
GetDenseIndex(...)50%22100%
RemoveDenseIndex(...)75%4483.33%
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
 335        internal OwningGroup(Allocator* allocator, int entityCapacity, int typeId1, int typeId2)
 36        {
 337            _typeId1 = typeId1;
 338            _typeId2 = typeId2;
 339            Reinitialize(allocator, entityCapacity);
 340        }
 41
 742        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        {
 047            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        {
 1953            return _group.Next(globalIndex, out entity, out c1, out c2);
 54        }
 55
 56        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 57        public bool DependsOn(int typeId)
 58        {
 959            return typeId == _typeId1 || typeId == _typeId2;
 60        }
 61
 62        public void Reinitialize(Allocator* allocator, int entityCapacity)
 63        {
 464            _entityCapacity = entityCapacity;
 465            _entityToDenseIndex = (int*)allocator->Alloc(sizeof(int) * entityCapacity);
 466            _group = default;
 467            _group.Initialize(allocator);
 468            ResetDenseIndexMap();
 469        }
 70
 71        public void SyncEntity(Frame frame, EntityRef entity, int changedTypeId)
 72        {
 73            _ = changedTypeId;
 74
 975            int denseIndex = GetDenseIndex(entity.Index);
 976            if (!ShouldInclude(frame, entity))
 77            {
 578                if (denseIndex >= 0)
 79                {
 280                    RemoveDenseIndex(entity.Index, denseIndex);
 81                }
 82
 583                return;
 84            }
 85
 486            T1* component1 = frame.GetPointer<T1>(entity);
 487            T2* component2 = frame.GetPointer<T2>(entity);
 488            if (component1 == null || component2 == null)
 89            {
 090                if (denseIndex >= 0)
 91                {
 092                    RemoveDenseIndex(entity.Index, denseIndex);
 93                }
 94
 095                return;
 96            }
 97
 498            if (denseIndex >= 0)
 99            {
 1100                if (_group.GetAtLinearIndex(denseIndex, out EntityRef _, out T1* group1, out T2* group2))
 101                {
 1102                    *group1 = *component1;
 1103                    *group2 = *component2;
 104                }
 105
 1106                return;
 107            }
 108
 3109            int newIndex = _group.Count;
 3110            _group.Add(entity, *component1, *component2);
 3111            _entityToDenseIndex[entity.Index] = newIndex;
 3112        }
 113
 114        public void Rebuild(Frame frame)
 115        {
 4116            ResetDenseIndexMap();
 4117            while (_group.Count > 0)
 118            {
 0119                _group.RemoveAtLinearIndex(_group.Count - 1, out _);
 120            }
 121
 4122            var query = frame.Query<T1, T2>();
 4123            var enumerator = query.GetEnumerator();
 8124            while (enumerator.MoveNext())
 125            {
 4126                int denseIndex = _group.Count;
 4127                _group.Add(enumerator.Entity, *enumerator.Component1Ptr, *enumerator.Component2Ptr);
 4128                _entityToDenseIndex[enumerator.Entity.Index] = denseIndex;
 129            }
 4130        }
 131
 132        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 133        private bool ShouldInclude(Frame frame, EntityRef entity)
 134        {
 9135            if (!frame.IsValid(entity))
 136            {
 0137                return false;
 138            }
 139
 9140            return frame.HasComponentBit(entity.Index, _typeId1)
 9141                && frame.HasComponentBit(entity.Index, _typeId2);
 142        }
 143
 144        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 145        private int GetDenseIndex(int entityIndex)
 146        {
 9147            return (uint)entityIndex < (uint)_entityCapacity ? _entityToDenseIndex[entityIndex] : -1;
 148        }
 149
 150        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 151        private void RemoveDenseIndex(int entityIndex, int denseIndex)
 152        {
 2153            if (!_group.RemoveAtLinearIndex(denseIndex, out EntityRef movedEntity))
 154            {
 0155                return;
 156            }
 157
 2158            _entityToDenseIndex[entityIndex] = -1;
 2159            if (movedEntity != EntityRef.None)
 160            {
 1161                _entityToDenseIndex[movedEntity.Index] = denseIndex;
 162            }
 2163        }
 164
 165        private void ResetDenseIndexMap()
 166        {
 144167            for (int i = 0; i < _entityCapacity; i++)
 168            {
 64169                _entityToDenseIndex[i] = -1;
 170            }
 8171        }
 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
 189        internal OwningGroup(Allocator* allocator, int entityCapacity, int typeId1, int typeId2, int typeId3)
 190        {
 191            _typeId1 = typeId1;
 192            _typeId2 = typeId2;
 193            _typeId3 = typeId3;
 194            Reinitialize(allocator, entityCapacity);
 195        }
 196
 197        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        {
 202            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        {
 208            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        {
 214            return typeId == _typeId1 || typeId == _typeId2 || typeId == _typeId3;
 215        }
 216
 217        public void Reinitialize(Allocator* allocator, int entityCapacity)
 218        {
 219            _entityCapacity = entityCapacity;
 220            _entityToDenseIndex = (int*)allocator->Alloc(sizeof(int) * entityCapacity);
 221            _group = default;
 222            _group.Initialize(allocator);
 223            ResetDenseIndexMap();
 224        }
 225
 226        public void SyncEntity(Frame frame, EntityRef entity, int changedTypeId)
 227        {
 228            _ = changedTypeId;
 229
 230            int denseIndex = GetDenseIndex(entity.Index);
 231            if (!ShouldInclude(frame, entity))
 232            {
 233                if (denseIndex >= 0)
 234                {
 235                    RemoveDenseIndex(entity.Index, denseIndex);
 236                }
 237
 238                return;
 239            }
 240
 241            T1* component1 = frame.GetPointer<T1>(entity);
 242            T2* component2 = frame.GetPointer<T2>(entity);
 243            T3* component3 = frame.GetPointer<T3>(entity);
 244            if (component1 == null || component2 == null || component3 == null)
 245            {
 246                if (denseIndex >= 0)
 247                {
 248                    RemoveDenseIndex(entity.Index, denseIndex);
 249                }
 250
 251                return;
 252            }
 253
 254            if (denseIndex >= 0)
 255            {
 256                if (_group.GetAtLinearIndex(denseIndex, out EntityRef _, out T1* group1, out T2* group2, out T3* group3)
 257                {
 258                    *group1 = *component1;
 259                    *group2 = *component2;
 260                    *group3 = *component3;
 261                }
 262
 263                return;
 264            }
 265
 266            int newIndex = _group.Count;
 267            _group.Add(entity, *component1, *component2, *component3);
 268            _entityToDenseIndex[entity.Index] = newIndex;
 269        }
 270
 271        public void Rebuild(Frame frame)
 272        {
 273            ResetDenseIndexMap();
 274            while (_group.Count > 0)
 275            {
 276                _group.RemoveAtLinearIndex(_group.Count - 1, out _);
 277            }
 278
 279            var query = frame.Query<T1, T2, T3>();
 280            var enumerator = query.GetEnumerator();
 281            while (enumerator.MoveNext())
 282            {
 283                int denseIndex = _group.Count;
 284                _group.Add(
 285                    enumerator.Entity,
 286                    *enumerator.Component1Ptr,
 287                    *enumerator.Component2Ptr,
 288                    *enumerator.Component3Ptr);
 289                _entityToDenseIndex[enumerator.Entity.Index] = denseIndex;
 290            }
 291        }
 292
 293        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 294        private bool ShouldInclude(Frame frame, EntityRef entity)
 295        {
 296            if (!frame.IsValid(entity))
 297            {
 298                return false;
 299            }
 300
 301            return frame.HasComponentBit(entity.Index, _typeId1)
 302                && frame.HasComponentBit(entity.Index, _typeId2)
 303                && frame.HasComponentBit(entity.Index, _typeId3);
 304        }
 305
 306        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 307        private int GetDenseIndex(int entityIndex)
 308        {
 309            return (uint)entityIndex < (uint)_entityCapacity ? _entityToDenseIndex[entityIndex] : -1;
 310        }
 311
 312        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 313        private void RemoveDenseIndex(int entityIndex, int denseIndex)
 314        {
 315            if (!_group.RemoveAtLinearIndex(denseIndex, out EntityRef movedEntity))
 316            {
 317                return;
 318            }
 319
 320            _entityToDenseIndex[entityIndex] = -1;
 321            if (movedEntity != EntityRef.None)
 322            {
 323                _entityToDenseIndex[movedEntity.Index] = denseIndex;
 324            }
 325        }
 326
 327        private void ResetDenseIndexMap()
 328        {
 329            for (int i = 0; i < _entityCapacity; i++)
 330            {
 331                _entityToDenseIndex[i] = -1;
 332            }
 333        }
 334    }
 335}