Skip to content

Commit

Permalink
renamed EntityMap.ContainsEntity method to Contains
Browse files Browse the repository at this point in the history
renamed EntityMultiMap.ContainsEntity method to Contains
added IEntityContainer type as common base interface for all entity container (EntitySet, EntitySortedSet, EntityMap, EntityMultiMap)
  • Loading branch information
Doraku committed Oct 16, 2021
1 parent c22eb4d commit c3dc2bb
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 191 deletions.
3 changes: 3 additions & 0 deletions documentation/NEXT_RELEASENOTES.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions source/DefaultEcs.Test/EntityMapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ 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();

Entity entity = world.CreateEntity();

using EntityMap<int> map = world.GetEntities().AsMap<int>();

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<int>();

Check.That(map.ContainsEntity(entity)).IsFalse();
Check.That(map.Contains(entity)).IsFalse();

entity.Enable<int>();

Check.That(map.ContainsEntity(entity)).IsTrue();
Check.That(map.Contains(entity)).IsTrue();

entity.Remove<int>();

Check.That(map.ContainsEntity(entity)).IsFalse();
Check.That(map.Contains(entity)).IsFalse();
}

[Fact]
Expand Down
12 changes: 6 additions & 6 deletions source/DefaultEcs.Test/EntityMultiMapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ 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();

Entity entity = world.CreateEntity();

using EntityMultiMap<int> map = world.GetEntities().AsMultiMap<int>();

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<int>();

Check.That(map.ContainsEntity(entity)).IsFalse();
Check.That(map.Contains(entity)).IsFalse();

entity.Enable<int>();

Check.That(map.ContainsEntity(entity)).IsTrue();
Check.That(map.Contains(entity)).IsTrue();

entity.Remove<int>();

Check.That(map.ContainsEntity(entity)).IsFalse();
Check.That(map.Contains(entity)).IsFalse();
}

[Fact]
Expand Down
2 changes: 0 additions & 2 deletions source/DefaultEcs.Test/System/AEntitySortedSetSystemTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using DefaultEcs.System;
using DefaultEcs.Threading;
using NFluent;
using NSubstitute;
using Xunit;

namespace DefaultEcs.Test.System
Expand Down
68 changes: 25 additions & 43 deletions source/DefaultEcs/EntityMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace DefaultEcs
/// <typeparam name="TKey">The type of the component used as key.</typeparam>
[DebuggerTypeProxy(typeof(EntityMapDebugView<>))]
[DebuggerDisplay("EntityMap[{_entities.Count}]")]
public sealed class EntityMap<TKey> : IEntityContainer, IDisposable
public sealed class EntityMap<TKey> : IEntityContainer
{
#region Types

Expand Down Expand Up @@ -133,20 +133,6 @@ internal KeyEnumerator(EntityMap<TKey> map)

#endregion

#region Events

/// <summary>
/// Occurs when an <see cref="Entity"/> is added in the current <see cref="EntityMap{TKey}"/>.
/// </summary>
public event EntityAddedHandler EntityAdded;

/// <summary>
/// Occurs when an <see cref="Entity"/> is removed from the current <see cref="EntityMap{TKey}"/>.
/// </summary>
public event EntityRemovedHandler EntityRemoved;

#endregion

#region Initialisation

internal EntityMap(
Expand Down Expand Up @@ -201,13 +187,6 @@ private void On(in ComponentChangedMessage<TKey> message)

#region Methods

/// <summary>
/// Determines whether the <see cref="EntityMap{TKey}"/> contains a specific <see cref="Entity"/>.
/// </summary>
/// <param name="entity">The <see cref="Entity"/> to locate in the <see cref="EntityMap{TKey}"/>.</param>
/// <returns>true if the <see cref="EntityMap{TKey}"/> contains the specified <see cref="Entity"/>; otherwise, false.</returns>
public bool ContainsEntity(Entity entity) => entity.EntityId < _entityIds.Length && _entityIds[entity.EntityId];

/// <summary>
/// Determines whether the <see cref="EntityMap{TKey}"/> contains the specified key.
/// </summary>
Expand All @@ -223,11 +202,20 @@ private void On(in ComponentChangedMessage<TKey> message)
/// <returns>true if the <see cref="EntityMap{TKey}"/> contains an <see cref="Entity"/> with the specified key; otherwise, false.</returns>
public bool TryGetEntity(TKey key, out Entity entity) => _entities.TryGetValue(key, out entity);

/// <summary>
/// Clears current instance of its entities if it was created with some reactive filter (<see cref="EntityQueryBuilder.WhenAdded{T}"/>, <see cref="EntityQueryBuilder.WhenChanged{T}"/> or <see cref="EntityQueryBuilder.WhenRemoved{T}"/>).
/// 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.
/// </summary>
#endregion

#region IEntityContainer

/// <inheritdoc/>
public event EntityAddedHandler EntityAdded;

/// <inheritdoc/>
public event EntityRemovedHandler EntityRemoved;

/// <inheritdoc/>
public bool Contains(in Entity entity) => entity.EntityId < _entityIds.Length && _entityIds[entity.EntityId];

/// <inheritdoc/>
public void Complete()
{
if (_needClearing)
Expand All @@ -237,11 +225,17 @@ public void Complete()
}
}

#endregion
/// <inheritdoc/>
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);

Expand All @@ -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])
{
Expand All @@ -266,18 +260,6 @@ void IEntityContainer.Remove(int entityId)
}
}

/// <summary>
/// Resizes inner storage to exactly the number of <see cref="Entity"/> this <see cref="EntityMap{TKey}"/> contains.
/// </summary>
public void TrimExcess()
{
ArrayExtension.Trim(ref _entityIds, Array.FindLastIndex(_entityIds, i => i) + 1);

#if NETSTANDARD2_1
_entities.TrimExcess();
#endif
}

#endregion

#region IDisposable
Expand Down
78 changes: 30 additions & 48 deletions source/DefaultEcs/EntityMultiMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace DefaultEcs
/// <typeparam name="TKey">The type of the component used as key.</typeparam>
[DebuggerTypeProxy(typeof(EntityMultiMapDebugView<>))]
[DebuggerDisplay("EntityMultiMap[{_entities.Count}]")]
public sealed class EntityMultiMap<TKey> : IEntityContainer, ISortable, IDisposable
public sealed class EntityMultiMap<TKey> : IEntityContainer, ISortable
{
#region Types

Expand Down Expand Up @@ -241,20 +241,6 @@ public bool MoveNext()

#endregion

#region Events

/// <summary>
/// Occurs when an <see cref="Entity"/> is added in the current <see cref="EntityMap{TKey}"/>.
/// </summary>
public event EntityAddedHandler EntityAdded;

/// <summary>
/// Occurs when an <see cref="Entity"/> is removed from the current <see cref="EntityMap{TKey}"/>.
/// </summary>
public event EntityRemovedHandler EntityRemoved;

#endregion

#region Initialisation

internal EntityMultiMap(
Expand Down Expand Up @@ -341,13 +327,6 @@ private void On(in ComponentChangedMessage<TKey> message)
/// <returns>The number of <see cref="Entity"/> in the current <see cref="EntityMultiMap{TKey}"/> for the given <typeparamref name="TKey"/>.</returns>
public int Count(TKey key) => _entities.TryGetValue(key, out Entities entities) ? entities.Count : 0;

/// <summary>
/// Determines whether the <see cref="EntityMultiMap{TKey}"/> contains a specific <see cref="Entity"/>.
/// </summary>
/// <param name="entity">The <see cref="Entity"/> to locate in the <see cref="EntityMultiMap{TKey}"/>.</param>
/// <returns>true if the <see cref="EntityMultiMap{TKey}"/> contains the specified <see cref="Entity"/>; otherwise, false.</returns>
public bool ContainsEntity(Entity entity) => entity.EntityId < _mapping.Length && _mapping[entity.EntityId].Entities != null;

/// <summary>
/// Determines whether the <see cref="EntityMultiMap{TKey}"/> contains the specified key.
/// </summary>
Expand All @@ -367,11 +346,20 @@ public bool TryGetEntities(TKey key, out ReadOnlySpan<Entity> entities)
return entities.Length > 0;
}

/// <summary>
/// Clears current instance of its entities if it was created with some reactive filter (<see cref="EntityQueryBuilder.WhenAdded{T}"/>, <see cref="EntityQueryBuilder.WhenChanged{T}"/> or <see cref="EntityQueryBuilder.WhenRemoved{T}"/>).
/// 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.
/// </summary>
#endregion

#region IEntityContainer

/// <inheritdoc/>
public event EntityAddedHandler EntityAdded;

/// <inheritdoc/>
public event EntityRemovedHandler EntityRemoved;

/// <inheritdoc/>
public bool Contains(in Entity entity) => entity.EntityId < _mapping.Length && _mapping[entity.EntityId].Entities != null;

/// <inheritdoc/>
public void Complete()
{
if (_needClearing)
Expand All @@ -384,11 +372,22 @@ public void Complete()
}
}

#endregion
/// <inheritdoc/>
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);

Expand All @@ -409,7 +408,7 @@ void IEntityContainer.Add(int entityId)
}
}

void IEntityContainer.Remove(int entityId)
void Internal.IEntityContainer.Remove(int entityId)
{
if (entityId < _mapping.Length)
{
Expand All @@ -428,23 +427,6 @@ void IEntityContainer.Remove(int entityId)
}
}

/// <summary>
/// Resizes inner storage to exactly the number of <see cref="Entity"/> this <see cref="EntityMultiMap{TKey}"/> contains.
/// </summary>
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
Expand Down
Loading

0 comments on commit c3dc2bb

Please sign in to comment.