From 89cb4c68b9c8264a84261abc91020a4d693082b9 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 24 Dec 2023 18:05:30 +0800 Subject: [PATCH] fix EcsGroup --- src/Collections/EcsGroup.cs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Collections/EcsGroup.cs b/src/Collections/EcsGroup.cs index 463ccff..50d5fb7 100644 --- a/src/Collections/EcsGroup.cs +++ b/src/Collections/EcsGroup.cs @@ -10,6 +10,7 @@ using System.Runtime.InteropServices; namespace DCFApixels.DragonECS { //_dense заполняется с индекса 1 + //в операциях изменяющих состояние группы нельзя итерироваться по this, либо осторожно учитывать этот момент [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)] [DebuggerTypeProxy(typeof(DebuggerProxy))] public readonly ref struct EcsReadonlyGroup @@ -369,17 +370,39 @@ namespace DCFApixels.DragonECS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (_source != group.World) Throw.Group_ArgumentDifferentWorldsException(); #endif + //if (group.Count > Count) // вариант 1. есть итерация по this + //{ + // foreach (var item in this) + // if (group.Has(item)) + // RemoveInternal(item); + //} + //else + //{ + // foreach (var item in group) + // if (Has(item)) + // RemoveInternal(item); + //} + + //foreach (var item in group) // вариант 2 + // if (Has(item)) + // RemoveInternal(item); + if (group.Count > Count) { - foreach (var item in this) + for (int i = _count; i > 0; i--)//итерация в обратном порядке исключает ошибки при удалении элементов + { + int item = _dense[i]; if (group.Has(item)) RemoveInternal(item); + } } else { foreach (var item in group) + { if (Has(item)) RemoveInternal(item); + } } } public void ExceptWith(EcsSpan span) @@ -405,9 +428,12 @@ namespace DCFApixels.DragonECS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (World != group.World) Throw.Group_ArgumentDifferentWorldsException(); #endif - foreach (var item in this) + for (int i = _count; i > 0; i--)//итерация в обратном порядке исключает ошибки при удалении элементов + { + int item = _dense[i]; if (!group.Has(item)) RemoveInternal(item); + } } #endregion