From c3dc2bb089a438e1c3a2dbcce53f7271772874d3 Mon Sep 17 00:00:00 2001 From: Laszlo Paillat Date: Sat, 16 Oct 2021 20:05:32 +0200 Subject: [PATCH] renamed EntityMap.ContainsEntity method to Contains renamed EntityMultiMap.ContainsEntity method to Contains added IEntityContainer type as common base interface for all entity container (EntitySet, EntitySortedSet, EntityMap, EntityMultiMap) --- documentation/NEXT_RELEASENOTES.txt | 3 + source/DefaultEcs.Test/EntityMapTest.cs | 12 +-- source/DefaultEcs.Test/EntityMultiMapTest.cs | 12 +-- .../System/AEntitySortedSetSystemTest.cs | 2 - source/DefaultEcs/EntityMap.cs | 68 ++++++---------- source/DefaultEcs/EntityMultiMap.cs | 78 +++++++------------ source/DefaultEcs/EntitySet.cs | 64 ++++++--------- source/DefaultEcs/EntitySortedSet.cs | 64 ++++++--------- source/DefaultEcs/IEntityContainer.cs | 36 +++++++++ .../Internal/EntityContainerWatcher.cs | 4 +- .../DefaultEcs/Internal/IEntityContainer.cs | 2 - 11 files changed, 154 insertions(+), 191 deletions(-) create mode 100644 source/DefaultEcs/IEntityContainer.cs diff --git a/documentation/NEXT_RELEASENOTES.txt b/documentation/NEXT_RELEASENOTES.txt index ef32cde7..7b6d6cde 100644 --- a/documentation/NEXT_RELEASENOTES.txt +++ b/documentation/NEXT_RELEASENOTES.txt @@ -1,8 +1,11 @@ breaking changes: remove EntityCommandRecorder.CreateEntity method, see new WorldRecord type +renamed EntityMap.ContainsEntity method to Contains +renamed EntityMultiMap.ContainsEntity method to Contains --- +added IEntityContainer type as common base interface for all entity container (EntitySet, EntitySortedSet, EntityMap, EntityMultiMap) added EntityCommandRecorder.Record(World) method added WorldRecord type to record action on World added EntitySet.EntityAdded event diff --git a/source/DefaultEcs.Test/EntityMapTest.cs b/source/DefaultEcs.Test/EntityMapTest.cs index c3782e7d..a04735ba 100644 --- a/source/DefaultEcs.Test/EntityMapTest.cs +++ b/source/DefaultEcs.Test/EntityMapTest.cs @@ -21,7 +21,7 @@ public void World_Should_return_world() } [Fact] - public void ContainsEntity_Should_return_weither_an_entity_is_in_or_not() + public void Contains_Should_return_weither_an_entity_is_in_or_not() { using World world = new(); @@ -29,23 +29,23 @@ public void ContainsEntity_Should_return_weither_an_entity_is_in_or_not() using EntityMap map = world.GetEntities().AsMap(); - Check.That(map.ContainsEntity(entity)).IsFalse(); + Check.That(map.Contains(entity)).IsFalse(); entity.Set(42); - Check.That(map.ContainsEntity(entity)).IsTrue(); + Check.That(map.Contains(entity)).IsTrue(); entity.Disable(); - Check.That(map.ContainsEntity(entity)).IsFalse(); + Check.That(map.Contains(entity)).IsFalse(); entity.Enable(); - Check.That(map.ContainsEntity(entity)).IsTrue(); + Check.That(map.Contains(entity)).IsTrue(); entity.Remove(); - Check.That(map.ContainsEntity(entity)).IsFalse(); + Check.That(map.Contains(entity)).IsFalse(); } [Fact] diff --git a/source/DefaultEcs.Test/EntityMultiMapTest.cs b/source/DefaultEcs.Test/EntityMultiMapTest.cs index 0b917313..e9753a8e 100644 --- a/source/DefaultEcs.Test/EntityMultiMapTest.cs +++ b/source/DefaultEcs.Test/EntityMultiMapTest.cs @@ -20,7 +20,7 @@ public void World_Should_return_world() } [Fact] - public void ContainsEntity_Should_return_weither_an_entity_is_in_or_not() + public void Contains_Should_return_weither_an_entity_is_in_or_not() { using World world = new(); @@ -28,24 +28,24 @@ public void ContainsEntity_Should_return_weither_an_entity_is_in_or_not() using EntityMultiMap map = world.GetEntities().AsMultiMap(); - Check.That(map.ContainsEntity(entity)).IsFalse(); + Check.That(map.Contains(entity)).IsFalse(); entity.Set(42); world.CreateEntity().Set(42); - Check.That(map.ContainsEntity(entity)).IsTrue(); + Check.That(map.Contains(entity)).IsTrue(); entity.Disable(); - Check.That(map.ContainsEntity(entity)).IsFalse(); + Check.That(map.Contains(entity)).IsFalse(); entity.Enable(); - Check.That(map.ContainsEntity(entity)).IsTrue(); + Check.That(map.Contains(entity)).IsTrue(); entity.Remove(); - Check.That(map.ContainsEntity(entity)).IsFalse(); + Check.That(map.Contains(entity)).IsFalse(); } [Fact] diff --git a/source/DefaultEcs.Test/System/AEntitySortedSetSystemTest.cs b/source/DefaultEcs.Test/System/AEntitySortedSetSystemTest.cs index be71bccf..c3dad9e8 100644 --- a/source/DefaultEcs.Test/System/AEntitySortedSetSystemTest.cs +++ b/source/DefaultEcs.Test/System/AEntitySortedSetSystemTest.cs @@ -1,8 +1,6 @@ using System; using DefaultEcs.System; -using DefaultEcs.Threading; using NFluent; -using NSubstitute; using Xunit; namespace DefaultEcs.Test.System diff --git a/source/DefaultEcs/EntityMap.cs b/source/DefaultEcs/EntityMap.cs index 24a102a6..12579a5e 100644 --- a/source/DefaultEcs/EntityMap.cs +++ b/source/DefaultEcs/EntityMap.cs @@ -16,7 +16,7 @@ namespace DefaultEcs /// The type of the component used as key. [DebuggerTypeProxy(typeof(EntityMapDebugView<>))] [DebuggerDisplay("EntityMap[{_entities.Count}]")] - public sealed class EntityMap : IEntityContainer, IDisposable + public sealed class EntityMap : IEntityContainer { #region Types @@ -133,20 +133,6 @@ internal KeyEnumerator(EntityMap map) #endregion - #region Events - - /// - /// Occurs when an is added in the current . - /// - public event EntityAddedHandler EntityAdded; - - /// - /// Occurs when an is removed from the current . - /// - public event EntityRemovedHandler EntityRemoved; - - #endregion - #region Initialisation internal EntityMap( @@ -201,13 +187,6 @@ private void On(in ComponentChangedMessage message) #region Methods - /// - /// Determines whether the contains a specific . - /// - /// The to locate in the . - /// true if the contains the specified ; otherwise, false. - public bool ContainsEntity(Entity entity) => entity.EntityId < _entityIds.Length && _entityIds[entity.EntityId]; - /// /// Determines whether the contains the specified key. /// @@ -223,11 +202,20 @@ private void On(in ComponentChangedMessage message) /// true if the contains an with the specified key; otherwise, false. public bool TryGetEntity(TKey key, out Entity entity) => _entities.TryGetValue(key, out entity); - /// - /// Clears current instance of its entities if it was created with some reactive filter (, or ). - /// Does nothing if it was created from a static filter. - /// This method need to be called after current instance content has been processed in a update cycle. - /// + #endregion + + #region IEntityContainer + + /// + public event EntityAddedHandler EntityAdded; + + /// + public event EntityRemovedHandler EntityRemoved; + + /// + public bool Contains(in Entity entity) => entity.EntityId < _entityIds.Length && _entityIds[entity.EntityId]; + + /// public void Complete() { if (_needClearing) @@ -237,11 +225,17 @@ public void Complete() } } - #endregion + /// + public void TrimExcess() + { + ArrayExtension.Trim(ref _entityIds, Array.FindLastIndex(_entityIds, i => i) + 1); - #region IEntityContainer +#if NETSTANDARD2_1 + _entities.TrimExcess(); +#endif + } - void IEntityContainer.Add(int entityId) + void Internal.IEntityContainer.Add(int entityId) { ArrayExtension.EnsureLength(ref _entityIds, entityId, _worldMaxCapacity); @@ -255,7 +249,7 @@ void IEntityContainer.Add(int entityId) } } - void IEntityContainer.Remove(int entityId) + void Internal.IEntityContainer.Remove(int entityId) { if (entityId < _entityIds.Length && _entityIds[entityId]) { @@ -266,18 +260,6 @@ void IEntityContainer.Remove(int entityId) } } - /// - /// Resizes inner storage to exactly the number of this contains. - /// - public void TrimExcess() - { - ArrayExtension.Trim(ref _entityIds, Array.FindLastIndex(_entityIds, i => i) + 1); - -#if NETSTANDARD2_1 - _entities.TrimExcess(); -#endif - } - #endregion #region IDisposable diff --git a/source/DefaultEcs/EntityMultiMap.cs b/source/DefaultEcs/EntityMultiMap.cs index 8580e41d..4aff4730 100644 --- a/source/DefaultEcs/EntityMultiMap.cs +++ b/source/DefaultEcs/EntityMultiMap.cs @@ -18,7 +18,7 @@ namespace DefaultEcs /// The type of the component used as key. [DebuggerTypeProxy(typeof(EntityMultiMapDebugView<>))] [DebuggerDisplay("EntityMultiMap[{_entities.Count}]")] - public sealed class EntityMultiMap : IEntityContainer, ISortable, IDisposable + public sealed class EntityMultiMap : IEntityContainer, ISortable { #region Types @@ -241,20 +241,6 @@ public bool MoveNext() #endregion - #region Events - - /// - /// Occurs when an is added in the current . - /// - public event EntityAddedHandler EntityAdded; - - /// - /// Occurs when an is removed from the current . - /// - public event EntityRemovedHandler EntityRemoved; - - #endregion - #region Initialisation internal EntityMultiMap( @@ -341,13 +327,6 @@ private void On(in ComponentChangedMessage message) /// The number of in the current for the given . public int Count(TKey key) => _entities.TryGetValue(key, out Entities entities) ? entities.Count : 0; - /// - /// Determines whether the contains a specific . - /// - /// The to locate in the . - /// true if the contains the specified ; otherwise, false. - public bool ContainsEntity(Entity entity) => entity.EntityId < _mapping.Length && _mapping[entity.EntityId].Entities != null; - /// /// Determines whether the contains the specified key. /// @@ -367,11 +346,20 @@ public bool TryGetEntities(TKey key, out ReadOnlySpan entities) return entities.Length > 0; } - /// - /// Clears current instance of its entities if it was created with some reactive filter (, or ). - /// Does nothing if it was created from a static filter. - /// This method need to be called after current instance content has been processed in a update cycle. - /// + #endregion + + #region IEntityContainer + + /// + public event EntityAddedHandler EntityAdded; + + /// + public event EntityRemovedHandler EntityRemoved; + + /// + public bool Contains(in Entity entity) => entity.EntityId < _mapping.Length && _mapping[entity.EntityId].Entities != null; + + /// public void Complete() { if (_needClearing) @@ -384,11 +372,22 @@ public void Complete() } } - #endregion + /// + public void TrimExcess() + { +#if NETSTANDARD2_1 + _entities.TrimExcess(); +#endif - #region IEntityContainer + ArrayExtension.Trim(ref _mapping, Array.FindLastIndex(_mapping, i => i.Entities != null) + 1); + + foreach (Entities entities in _entities.Values) + { + entities.TrimExcess(); + } + } - void IEntityContainer.Add(int entityId) + void Internal.IEntityContainer.Add(int entityId) { ArrayExtension.EnsureLength(ref _mapping, entityId, _worldMaxCapacity); @@ -409,7 +408,7 @@ void IEntityContainer.Add(int entityId) } } - void IEntityContainer.Remove(int entityId) + void Internal.IEntityContainer.Remove(int entityId) { if (entityId < _mapping.Length) { @@ -428,23 +427,6 @@ void IEntityContainer.Remove(int entityId) } } - /// - /// Resizes inner storage to exactly the number of this contains. - /// - public void TrimExcess() - { -#if NETSTANDARD2_1 - _entities.TrimExcess(); -#endif - - ArrayExtension.Trim(ref _mapping, Array.FindLastIndex(_mapping, i => i.Entities != null) + 1); - - foreach (Entities entities in _entities.Values) - { - entities.TrimExcess(); - } - } - #endregion #region ISortable diff --git a/source/DefaultEcs/EntitySet.cs b/source/DefaultEcs/EntitySet.cs index 229eda11..2e1abe30 100644 --- a/source/DefaultEcs/EntitySet.cs +++ b/source/DefaultEcs/EntitySet.cs @@ -15,7 +15,7 @@ namespace DefaultEcs /// [DebuggerTypeProxy(typeof(EntitySetDebugView))] [DebuggerDisplay("EntitySet[{Count}]")] - public sealed class EntitySet : IEntityContainer, ISortable, IDisposable + public sealed class EntitySet : IEntityContainer, ISortable { #region Fields @@ -49,20 +49,6 @@ public int Count #endregion - #region Events - - /// - /// Occurs when an is added in the current . - /// - public event EntityAddedHandler EntityAdded; - - /// - /// Occurs when an is removed from the current . - /// - public event EntityRemovedHandler EntityRemoved; - - #endregion - #region Initialisation internal EntitySet( @@ -114,11 +100,20 @@ internal EntitySet( [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlySpan GetEntities() => GetEntities(0, Count); - /// - /// Clears current instance of its entities if it was created with some reactive filter (, or ). - /// Does nothing if it was created from a static filter. - /// This method need to be called after current instance content has been processed in a update cycle. - /// + #endregion + + #region IEntityContainer + + /// + public event EntityAddedHandler EntityAdded; + + /// + public event EntityRemovedHandler EntityRemoved; + + /// + public bool Contains(in Entity entity) => entity.WorldId == _worldId && entity.EntityId < _mapping.Length && _mapping[entity.EntityId] != -1; + + /// public void Complete() { if (_needClearing && Count > 0) @@ -129,18 +124,14 @@ public void Complete() } } - /// - /// Determines whether an is in the . - /// - /// The to locate in the . - /// true if is found in the ; otherwise, false. - public bool Contains(in Entity entity) => entity.WorldId == _worldId && entity.EntityId < _mapping.Length && _mapping[entity.EntityId] != -1; - - #endregion - - #region IEntityContainer + /// + public void TrimExcess() + { + ArrayExtension.Trim(ref _entities, Count); + ArrayExtension.Trim(ref _mapping, Array.FindLastIndex(_mapping, i => i != -1) + 1); + } - void IEntityContainer.Add(int entityId) + void Internal.IEntityContainer.Add(int entityId) { ArrayExtension.EnsureLength(ref _mapping, entityId, _worldMaxCapacity, -1); @@ -162,7 +153,7 @@ void IEntityContainer.Add(int entityId) } } - void IEntityContainer.Remove(int entityId) + void Internal.IEntityContainer.Remove(int entityId) { if (entityId < _mapping.Length) { @@ -186,15 +177,6 @@ void IEntityContainer.Remove(int entityId) } } - /// - /// Resizes inner storage to exactly the number of this contains. - /// - public void TrimExcess() - { - ArrayExtension.Trim(ref _entities, Count); - ArrayExtension.Trim(ref _mapping, Array.FindLastIndex(_mapping, i => i != -1) + 1); - } - #endregion #region ISortable diff --git a/source/DefaultEcs/EntitySortedSet.cs b/source/DefaultEcs/EntitySortedSet.cs index 92e91ed7..487298b7 100644 --- a/source/DefaultEcs/EntitySortedSet.cs +++ b/source/DefaultEcs/EntitySortedSet.cs @@ -16,7 +16,7 @@ namespace DefaultEcs /// The type of the component to sort by. [DebuggerTypeProxy(typeof(EntitySortedSetDebugView<>))] [DebuggerDisplay("EntitySortedSet[{Count}]")] - public sealed class EntitySortedSet : IEntityContainer, IDisposable + public sealed class EntitySortedSet : IEntityContainer { #region Fields @@ -50,20 +50,6 @@ public int Count #endregion - #region Events - - /// - /// Occurs when an is added in the current . - /// - public event EntityAddedHandler EntityAdded; - - /// - /// Occurs when an is removed from the current . - /// - public event EntityRemovedHandler EntityRemoved; - - #endregion - #region Initialisation internal EntitySortedSet( @@ -130,11 +116,20 @@ private void On(in ComponentChangedMessage message) [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlySpan GetEntities() => new(_entities, 0, Count); - /// - /// Clears current instance of its entities if it was created with some reactive filter (, or ). - /// Does nothing if it was created from a static filter. - /// This method need to be called after current instance content has been processed in a update cycle. - /// + #endregion + + #region IEntityContainer + + /// + public event EntityAddedHandler EntityAdded; + + /// + public event EntityRemovedHandler EntityRemoved; + + /// + public bool Contains(in Entity entity) => entity.WorldId == _worldId && entity.EntityId < _mapping.Length && _mapping[entity.EntityId] != -1; + + /// public void Complete() { if (_needClearing && Count > 0) @@ -144,18 +139,14 @@ public void Complete() } } - /// - /// Determines whether an is in the . - /// - /// The to locate in the . - /// true if is found in the ; otherwise, false. - public bool Contains(in Entity entity) => entity.WorldId == _worldId && entity.EntityId < _mapping.Length && _mapping[entity.EntityId] != -1; - - #endregion - - #region IEntityContainer + /// + public void TrimExcess() + { + ArrayExtension.Trim(ref _entities, Count); + ArrayExtension.Trim(ref _mapping, Array.FindLastIndex(_mapping, i => i != -1) + 1); + } - void IEntityContainer.Add(int entityId) + void Internal.IEntityContainer.Add(int entityId) { ArrayExtension.EnsureLength(ref _mapping, entityId, _worldMaxCapacity, -1); @@ -180,7 +171,7 @@ void IEntityContainer.Add(int entityId) } } - void IEntityContainer.Remove(int entityId) + void Internal.IEntityContainer.Remove(int entityId) { if (entityId < _mapping.Length) { @@ -206,15 +197,6 @@ void IEntityContainer.Remove(int entityId) } } - /// - /// Resizes inner storage to exactly the number of this contains. - /// - public void TrimExcess() - { - ArrayExtension.Trim(ref _entities, Count); - ArrayExtension.Trim(ref _mapping, Array.FindLastIndex(_mapping, i => i != -1) + 1); - } - #endregion #region IDisposable diff --git a/source/DefaultEcs/IEntityContainer.cs b/source/DefaultEcs/IEntityContainer.cs new file mode 100644 index 00000000..0946f940 --- /dev/null +++ b/source/DefaultEcs/IEntityContainer.cs @@ -0,0 +1,36 @@ +using System; + +namespace DefaultEcs +{ + internal interface IEntityContainer : Internal.IEntityContainer, IDisposable + { + /// + /// Occurs when an is added in the current . + /// + event EntityAddedHandler EntityAdded; + + /// + /// Occurs when an is removed from the current . + /// + event EntityRemovedHandler EntityRemoved; + + /// + /// Determines whether the contains a specific . + /// + /// The to locate in the . + /// true if the contains the specified ; otherwise, false. + bool Contains(in Entity entity); + + /// + /// Clears current instance of its entities if it was created with some reactive filter (, or ). + /// Does nothing if it was created from a static filter. + /// This method need to be called after current instance content has been processed in a update cycle. + /// + void Complete(); + + /// + /// Resizes inner storage to exactly the number of this contains. + /// + void TrimExcess(); + } +} diff --git a/source/DefaultEcs/Internal/EntityContainerWatcher.cs b/source/DefaultEcs/Internal/EntityContainerWatcher.cs index 99280b4a..1b5d2ce2 100644 --- a/source/DefaultEcs/Internal/EntityContainerWatcher.cs +++ b/source/DefaultEcs/Internal/EntityContainerWatcher.cs @@ -7,7 +7,7 @@ internal sealed class EntityContainerWatcher { #region Fields - private readonly IEntityContainer _container; + private readonly DefaultEcs.IEntityContainer _container; private readonly Predicate _filter; private readonly Predicate _predicate; @@ -15,7 +15,7 @@ internal sealed class EntityContainerWatcher #region Initialisation - public EntityContainerWatcher(IEntityContainer container, Predicate filter, Predicate predicate) + public EntityContainerWatcher(DefaultEcs.IEntityContainer container, Predicate filter, Predicate predicate) { _container = container; _filter = filter; diff --git a/source/DefaultEcs/Internal/IEntityContainer.cs b/source/DefaultEcs/Internal/IEntityContainer.cs index 04b7f8cb..945c9e8d 100644 --- a/source/DefaultEcs/Internal/IEntityContainer.cs +++ b/source/DefaultEcs/Internal/IEntityContainer.cs @@ -5,7 +5,5 @@ internal interface IEntityContainer void Add(int entityId); void Remove(int entityId); - - void TrimExcess(); } }