Skip to content

Commit

Permalink
cleanup ComponentIndex creation
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Nov 30, 2024
1 parent b08324a commit 1ec638a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 17 deletions.
17 changes: 14 additions & 3 deletions src/ECS/Index/Indexes/ComponentIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public abstract class ComponentIndex

#region fields
internal readonly IdArrayHeap idHeap = new();
internal EntityStore store; // could be made readonly
internal ComponentType componentType;
internal int indexBit;
internal readonly EntityStore store;
internal readonly ComponentType componentType;
internal readonly int structIndex;
internal readonly int indexBit;
internal bool modified;
#endregion

Expand All @@ -38,6 +39,14 @@ public abstract class ComponentIndex
internal NotSupportedException NotSupportedException(string name) {
return new NotSupportedException($"{name} not supported by {GetType().Name}");
}

internal ComponentIndex(EntityStore store, ComponentType componentType) {
this.store = store;
this.componentType = componentType;
structIndex = componentType.StructIndex;
var types = new ComponentTypes(componentType);
indexBit = (int)types.bitSet.l0;
}
}

/// <summary>
Expand All @@ -50,4 +59,6 @@ public abstract class ComponentIndex<TValue> : ComponentIndex
internal abstract IReadOnlyCollection<TValue> IndexedComponentValues { get; }
internal abstract Entities GetHasValueEntities (TValue value);
internal virtual void AddValueInRangeEntities(TValue min, TValue max, HashSet<int> idSet) => throw NotSupportedException("ValueInRange()");

internal ComponentIndex(EntityStore store, ComponentType componentType) : base(store, componentType) { }
}
6 changes: 5 additions & 1 deletion src/ECS/Index/Indexes/EntityIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal abstract class EntityIndex : ComponentIndex<Entity>
private EntityIndexValues keyCollection;
#endregion

internal EntityIndex(EntityStore store, ComponentType componentType) : base(store, componentType) { }

#region indexing
internal override void Add<TComponent>(int id, in TComponent component)
{
Expand Down Expand Up @@ -72,11 +74,13 @@ internal override Entities GetHasValueEntities(Entity value)
internal sealed class EntityIndex<TIndexedComponent> : EntityIndex
where TIndexedComponent : struct, IIndexedComponent<Entity>
{
internal EntityIndex(EntityStore store, ComponentType componentType) : base(store, componentType) { }

internal override void RemoveEntityFromIndex(int id, Archetype archetype, int compIndex)
{
var map = entityMap;
var heap = idHeap;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[componentType.StructIndex]).components;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[structIndex]).components;
int linkedEntity = components[compIndex].GetIndexedValue().Id;
map.TryGetValue(linkedEntity, out var idArray);
var idSpan = idArray.GetSpan(heap, store);
Expand Down
4 changes: 3 additions & 1 deletion src/ECS/Index/Indexes/RangeIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public sealed class RangeIndex<TIndexedComponent,TValue> : ComponentIndex<TValue
private ReadOnlyCollection<TValue> keyCollection;
#endregion

internal RangeIndex(EntityStore store, ComponentType componentType) : base(store, componentType) { }

#region indexing
internal override void Add<TComponent>(int id, in TComponent component)
{
Expand Down Expand Up @@ -58,7 +60,7 @@ internal override void RemoveEntityFromIndex(int id, Archetype archetype, int co
{
var map = entityMap;
var heap = idHeap;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[componentType.StructIndex]).components;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[structIndex]).components;
TValue value = components[compIndex].GetIndexedValue();
map.TryGetValue(value, out var idArray);
var idSpan = idArray.GetSpan(heap, store);
Expand Down
5 changes: 3 additions & 2 deletions src/ECS/Index/Indexes/ValueClassIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ internal sealed class ValueClassIndex<TIndexedComponent,TValue> : ComponentIndex
/// store entity ids for indexed value == null
private IdArray nullValue;
#endregion


internal ValueClassIndex(EntityStore store, ComponentType componentType) : base(store, componentType) { }

#region indexing
internal override void Add<TComponent>(int id, in TComponent component)
Expand Down Expand Up @@ -52,7 +53,7 @@ internal override void RemoveEntityFromIndex(int id, Archetype archetype, int co
{
var map = entityMap;
var heap = idHeap;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[componentType.StructIndex]).components;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[structIndex]).components;
TValue value = components[compIndex].GetIndexedValue();
map.TryGetValue(value, out var idArray);
var idSpan = idArray.GetSpan(heap, store);
Expand Down
4 changes: 3 additions & 1 deletion src/ECS/Index/Indexes/ValueStructIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal sealed class ValueStructIndex<TIndexedComponent,TValue> : ComponentInd
private readonly Dictionary<TValue, IdArray> entityMap = new();
#endregion

internal ValueStructIndex(EntityStore store, ComponentType componentType) : base(store, componentType) { }

#region indexing
internal override void Add<TComponent>(int id, in TComponent component)
{
Expand Down Expand Up @@ -49,7 +51,7 @@ internal override void RemoveEntityFromIndex(int id, Archetype archetype, int co
{
var map = entityMap;
var heap = idHeap;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[componentType.StructIndex]).components;
var components = ((StructHeap<TIndexedComponent>)archetype.heapMap[structIndex]).components;
TValue value = components[compIndex].GetIndexedValue();
map.TryGetValue(value, out var idArray);
var idSpan = idArray.GetSpan(heap, store);
Expand Down
11 changes: 5 additions & 6 deletions src/ECS/Index/Utils/ComponentIndexUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS.Index;
Expand All @@ -12,12 +13,10 @@ internal static class ComponentIndexUtils
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2077", Justification = "TODO")] // TODO
internal static ComponentIndex CreateComponentIndex(EntityStore store, ComponentType componentType)
{
var obj = Activator.CreateInstance(componentType.IndexType);
var index = (ComponentIndex)obj!;
index.store = store;
index.componentType = componentType;
var types = new ComponentTypes(componentType);
index.indexBit = (int)types.bitSet.l0;
var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance;
var args = new object[] { store, componentType };
var obj = Activator.CreateInstance(componentType.IndexType, flags, null, args, null);
var index = (ComponentIndex)obj!;
return index;
}

Expand Down
11 changes: 8 additions & 3 deletions src/Tests-internal/ECS/Test_Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public static class Test_Index
public static void Test_Index_ValueInRange_EntityIndex()
{
var store = new EntityStore();
var entityIndex = new EntityIndex<AttackComponent> { store = store };
var componentType = EntityStore.GetEntitySchema().ComponentTypeByType[typeof(AttackComponent)];
var entityIndex = new EntityIndex<AttackComponent> (store, componentType);
var e = Throws<NotSupportedException>(() => {
entityIndex.AddValueInRangeEntities(default, default, null);
});
Expand All @@ -47,7 +48,9 @@ public static void Test_Index_already_added()
[Test]
public static void Test_Index_already_removed()
{
var index = new ValueClassIndex<IndexedName,string>();
var store = new EntityStore();
var componentType = EntityStore.GetEntitySchema().ComponentTypeByType[typeof(IndexedName)];
var index = new ValueClassIndex<IndexedName,string>(store, componentType);
index.RemoveComponentValue(1, "missing"); // add key with default IdArray
AreEqual(0, index.Count);

Expand Down Expand Up @@ -91,7 +94,9 @@ public static void Test_Index_cover_AddComponentValue()
[Test]
public static void Test_Index_EntityIndexValue()
{
var index = new EntityIndex<AttackComponent>();
var store = new EntityStore();
var componentType = EntityStore.GetEntitySchema().ComponentTypeByType[typeof(AttackComponent)];
var index = new EntityIndex<AttackComponent>(store, componentType);
var values = new EntityIndexValues(index) as IEnumerable;

Throws<NotImplementedException>(() => {
Expand Down

0 comments on commit 1ec638a

Please sign in to comment.