Skip to content

Commit

Permalink
make IndexExtension.GetEntitiesWithComponentValue() & GetAllIndexedCo…
Browse files Browse the repository at this point in the history
…mponentValues() obsolete - replaced with ComponentIndex<,> members
  • Loading branch information
friflo committed Dec 1, 2024
1 parent 9d007ca commit 279922e
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/ECS/Index/IIndexedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Friflo.Engine.ECS;
/// </item>
/// <item>
/// Return a collection of all unique component values.<br/>
/// See <see cref="IndexExtensions.GetAllIndexedComponentValues{TComponent,TValue}"/>.
/// See <see cref="ComponentIndex{TIndexedComponent,TValue}.Values"/>.
/// </item>
/// <item>
/// Filter entities in a query having a specific component value.<br/>
Expand Down
2 changes: 2 additions & 0 deletions src/ECS/Index/IndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static ComponentIndex<TIndexedComponent,TValue> ComponentIndex<TIndexedCo
/// Return the entities with the passed component value.<br/>
/// Executes in O(1) with default index.
/// </summary>
[Obsolete("replace with indexer: ComponentIndex<TIndexedComponent,TValue>()[TValue]")]
public static Entities GetEntitiesWithComponentValue<TComponent, TValue>(this EntityStore store, TValue value)
where TComponent: struct, IIndexedComponent<TValue>
{
Expand All @@ -72,6 +73,7 @@ public static Entities GetEntitiesWithComponentValue<TComponent, TValue>(this En
/// </item>
/// </list>
/// </remarks>
[Obsolete("replace with property: ComponentIndex<TIndexedComponent,TValue>().Values")]
public static IReadOnlyCollection<TValue> GetAllIndexedComponentValues<TComponent, TValue>(this EntityStore store)
where TComponent: struct, IIndexedComponent<TValue>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/ECS/Index/Query/Test_Index_Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private static IndexContext Query_Setup()
cx.entity2 = entities[2];
cx.entity3 = entities[3];

cx.nameValues = store.GetAllIndexedComponentValues<IndexedName, string>();
cx.intValues = store.GetAllIndexedComponentValues<IndexedInt, int>();
cx.nameValues = store.ComponentIndex<IndexedName, string>().Values;
cx.intValues = store.ComponentIndex<IndexedInt, int>().Values;

cx.entity0.AddComponent(new IndexedName { name = "find-me" }); AreEqual("{ find-me }", cx.nameValues.Debug());
cx.entity1.AddComponent(new IndexedInt { value = 123 }); AreEqual("{ 123 }", cx.intValues.Debug());
Expand Down
30 changes: 17 additions & 13 deletions src/Tests/ECS/Index/Test_Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class Test_Index
public static void Test_Index_ValueInRange_ValueStructIndex()
{
var store = new EntityStore();
var values = store.GetAllIndexedComponentValues<IndexedInt, int>();
var values = store.ComponentIndex<IndexedInt, int>().Values;
for (int n = 1; n <= 10; n++) {
var entity = store.CreateEntity(n);
entity.AddComponent(new IndexedInt { value = n });
Expand All @@ -33,7 +33,7 @@ public static void Test_Index_ValueInRange_ValueStructIndex()
public static void Test_Index_ValueInRange_ValueClassIndex()
{
var store = new EntityStore();
var values = store.GetAllIndexedComponentValues<IndexedName, string>();
var values = store.ComponentIndex<IndexedName, string>().Values;
for (int n = 1; n <= 10; n++) {
var entity = store.CreateEntity(n);
entity.AddComponent(new IndexedName { name = n.ToString() });
Expand All @@ -48,6 +48,8 @@ public static void Test_Index_ValueInRange_ValueClassIndex()
public static void Test_Index_Component_Update()
{
var store = new EntityStore();
var nameIndex = store.ComponentIndex<IndexedName, string>();
var intIndex = store.ComponentIndex<IndexedInt, int>();

var entities = new List<Entity>();
for (int n = 0; n < 10; n++) {
Expand All @@ -57,17 +59,17 @@ public static void Test_Index_Component_Update()
var entity1 = entities[0];
var entity2 = entities[1];
var entity3 = entities[2];
var nameValues = store.GetAllIndexedComponentValues<IndexedName, string>();
var intValues = store.GetAllIndexedComponentValues<IndexedInt, int>();
var nameValues = store.ComponentIndex<IndexedName, string>().Values;
var intValues = store.ComponentIndex<IndexedInt, int>().Values;

entity1.AddComponent(new IndexedName { name = "find-me1" }); AreEqual("{ find-me1 }", nameValues.Debug());
entity2.AddComponent(new IndexedInt { value = 123 }); AreEqual("{ 123 }", intValues.Debug());
entity3.AddComponent(new IndexedName { name = "find-me1" }); AreEqual("{ find-me1 }", nameValues.Debug());
entity3.AddComponent(new IndexedInt { value = 123 }); AreEqual("{ 123 }", intValues.Debug());

AreEqual("{ 1, 3 }",store.GetEntitiesWithComponentValue<IndexedName, string>("find-me1").Debug());
AreEqual("{ 2, 3 }",store.GetEntitiesWithComponentValue<IndexedInt, int>(123).Debug());
AreEqual("{ }", store.GetEntitiesWithComponentValue<IndexedInt, int>(42).Debug());
AreEqual("{ 1, 3 }",nameIndex["find-me1"].Debug());
AreEqual("{ 2, 3 }",intIndex[123].Debug());
AreEqual("{ }", intIndex[42].Debug());

var query1 = store.Query<Position, IndexedName>(). HasValue<IndexedName, string>("find-me1");
var query2 = store.Query<IndexedName, IndexedInt>(). HasValue<IndexedName, string>("find-me1");
Expand Down Expand Up @@ -150,38 +152,40 @@ public static void Test_Index_indexed_Entity()
public static void Test_Index_support_null()
{
var store = new EntityStore();
var index = store.ComponentIndex<IndexedName, string>();
var entity1 = store.CreateEntity();
var entity2 = store.CreateEntity();
entity1.AddComponent(new IndexedName { name = null });
entity2.AddComponent(new IndexedName { name = null });

var start = Mem.GetAllocatedBytes();
var result = store.GetEntitiesWithComponentValue<IndexedName, string>(null);
var result = index[null];
Mem.AssertNoAlloc(start);

AreEqual(2, result.Count);
AreEqual(1, result[0].Id);
AreEqual(2, result[1].Id);

entity2.RemoveComponent<IndexedName>();
result = store.GetEntitiesWithComponentValue<IndexedName, string>(null);
result = index[null];
AreEqual(1, result.Count);
}

[Test]
public static void Test_Index_ValueStructIndex()
{
var store = new EntityStore();
var index = store.ComponentIndex<IndexedInt, int>();
var entity1 = store.CreateEntity();
entity1.AddComponent(new IndexedInt { value = 123 });
entity1.AddComponent(new IndexedInt { value = 123 }); // add same component value again
var result = store.GetEntitiesWithComponentValue<IndexedInt, int>(123);
var result = index[123];
AreEqual(1, result.Count);

entity1.AddComponent(new IndexedInt { value = 456 });
result = store.GetEntitiesWithComponentValue<IndexedInt, int>(456);
result = index[456];
AreEqual(1, result.Count);
result = store.GetEntitiesWithComponentValue<IndexedInt, int>(123);
result = index[123];
AreEqual(0, result.Count);
}

Expand Down Expand Up @@ -263,7 +267,7 @@ public static void Test_Index_Perf()
// 1_000_000 #PC Test_Index_Perf - count: 1000000 duration: 176 ms
var store = new EntityStore();
var entities = new List<Entity>();
var values = store.GetAllIndexedComponentValues<IndexedInt, int>();
var values = store.ComponentIndex<IndexedInt, int>().Values;
for (int n = 1; n <= count; n++) {
entities.Add(store.CreateEntity());
}
Expand Down
12 changes: 6 additions & 6 deletions src/Tests/ECS/Index/Test_Index_Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void Test_Index_Delete_Value()
var entity1 = store.CreateEntity(1);
var entity2 = store.CreateEntity(2);
var entity3 = store.CreateEntity(3);
var values = store.GetAllIndexedComponentValues<IndexedInt, int>();
var values = store.ComponentIndex<IndexedInt, int>().Values;

entity1.AddComponent(new IndexedInt { value = 10 });
entity2.AddComponent(new IndexedInt { value = 10 });
Expand All @@ -40,7 +40,7 @@ public static void Test_Index_Delete_Class()
var entity1 = store.CreateEntity(1);
var entity2 = store.CreateEntity(2);
var entity3 = store.CreateEntity(3);
var values = store.GetAllIndexedComponentValues<IndexedName, string>();
var values = store.ComponentIndex<IndexedName, string>().Values;

entity1.AddComponent(new IndexedName { name = "abc" });
entity2.AddComponent(new IndexedName { name = "abc" });
Expand Down Expand Up @@ -69,7 +69,7 @@ public static void Test_Index_Delete_Entity()
var entity10 = store.CreateEntity(10);
var entity11 = store.CreateEntity(11);

var values = store.GetAllIndexedComponentValues<AttackComponent, Entity>();
var values = store.ComponentIndex<AttackComponent, Entity>().Values;

entity1.AddComponent(new AttackComponent { target = entity10 }); // 1 🡒 10 2
AreEqual("{ 1 }", entity10.GetIncomingLinks<AttackComponent>().Debug()); // 3 11
Expand Down Expand Up @@ -106,7 +106,7 @@ public static void Test_Index_Delete_linked_entities()
var target10 = store.CreateEntity(10);
var target11 = store.CreateEntity(11);

var targets = store.GetAllIndexedComponentValues<AttackComponent, Entity>();
var targets = store.ComponentIndex<AttackComponent, Entity>().Values;

entity1.AddComponent(new AttackComponent { target = target10 });
entity2.AddComponent(new AttackComponent { target = target10 });
Expand Down Expand Up @@ -135,7 +135,7 @@ public static void Test_Index_Delete_self_referencing_entity()
var entity2 = store.CreateEntity(2);
var entity3 = store.CreateEntity(3);

var targets = store.GetAllIndexedComponentValues<AttackComponent, Entity>();
var targets = store.ComponentIndex<AttackComponent, Entity>().Values;

entity1.AddComponent(new AttackComponent { target = entity1 });
entity2.AddComponent(new AttackComponent { target = entity1 });
Expand All @@ -161,7 +161,7 @@ public static void Test_Index_Delete_Range()
var entity3 = store.CreateEntity(3);


var values = store.GetAllIndexedComponentValues<IndexedIntRange, int>();
var values = store.ComponentIndex<IndexedIntRange, int>().Values;

entity1.AddComponent(new IndexedIntRange { value = 10 });
entity2.AddComponent(new IndexedIntRange { value = 10 });
Expand Down
13 changes: 7 additions & 6 deletions src/Tests/ECS/Index/Test_Index_Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class Test_Index_Range
public static void Test_Index_Range_Query_ValueInRange()
{
var store = new EntityStore();
var index = store.ComponentIndex<IndexedIntRange, int>();

var query0 = store.Query<IndexedIntRange, Position>().ValueInRange<IndexedIntRange, int>(0, 99);
var query1 = store.Query<IndexedIntRange, Position>().ValueInRange<IndexedIntRange, int>(100, 100);
Expand All @@ -29,15 +30,15 @@ public static void Test_Index_Range_Query_ValueInRange()
var entity2 = store.CreateEntity(new Position());
var entity3 = store.CreateEntity(new Position());

var values = store.GetAllIndexedComponentValues<IndexedIntRange, int>();
var values = index.Values;

entity1.AddComponent(new IndexedIntRange { value = 100 }); AreEqual("{ 100 }", values.Debug());
entity2.AddComponent(new IndexedIntRange { value = 200 }); AreEqual("{ 100, 200 }", values.Debug());
entity3.AddComponent(new IndexedIntRange { value = 300 }); AreEqual("{ 100, 200, 300 }", values.Debug());

var result = store.GetEntitiesWithComponentValue<IndexedIntRange, int>(100);
var result = index[100];
AreEqual(1, result.Count); AreEqual("{ 1 }", result.Ids.Debug());
result = store.GetEntitiesWithComponentValue<IndexedIntRange, int>(42);
result = index[42];
AreEqual(0, result.Count); AreEqual("{ }", result.Ids.Debug());
{
int count = 0;
Expand Down Expand Up @@ -100,7 +101,7 @@ public static void Test_Index_Range_Update_Remove()
var entity3 = store.CreateEntity(new Position());
var entity4 = store.CreateEntity(new Position());

var values = store.GetAllIndexedComponentValues<IndexedIntRange, int>();
var values = store.ComponentIndex<IndexedIntRange, int>().Values;
entity1.AddComponent(new IndexedIntRange { value = 100 }); AreEqual("{ 100 }", values.Debug());
entity2.AddComponent(new IndexedIntRange { value = 200 }); AreEqual("{ 100, 200 }", values.Debug());
entity3.AddComponent(new IndexedIntRange { value = 200 }); AreEqual("{ 100, 200 }", values.Debug());
Expand Down Expand Up @@ -139,7 +140,7 @@ public static void Test_Index_Range_Allocation()
var count = 100;
var store = new EntityStore();
var entities = new List<Entity>();
var values = store.GetAllIndexedComponentValues<IndexedStringRange, string>();
var values = store.ComponentIndex<IndexedStringRange, string>().Values;
var strings = new string[count];
for (int n = 1; n <= count; n++) {
entities.Add(store.CreateEntity());
Expand All @@ -165,7 +166,7 @@ public static void Test_Index_Allocation()
var count = 100;
var store = new EntityStore();
var entities = new List<Entity>();
var values = store.GetAllIndexedComponentValues<IndexedIntRange, int>();
var values = store.ComponentIndex<IndexedIntRange, int>().Values;
for (int n = 1; n <= count; n++) {
entities.Add(store.CreateEntity());
}
Expand Down
8 changes: 4 additions & 4 deletions src/Tests/ECS/Index/Test_Index_Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void Test_Index_Types_Guid()
var guid2 = Guid.Parse("10000000-0000-0000-0000-000000000000");

var store = new EntityStore();
var values = store.GetAllIndexedComponentValues<GuidComponent, Guid>();
var values = store.ComponentIndex<GuidComponent, Guid>().Values;

var query0 = store.Query().HasValue <GuidComponent, Guid>(guid1);
var query1 = store.Query().ValueInRange<GuidComponent, Guid>(guid1, guid1);
Expand All @@ -40,7 +40,7 @@ public static void Test_Index_Types_DateTime()
var date1 = new DateTime(2024, 6, 25);
var date2 = new DateTime(2024, 6, 26);
var store = new EntityStore();
var values = store.GetAllIndexedComponentValues<DateTimeComponent, DateTime>();
var values = store.ComponentIndex<DateTimeComponent, DateTime>().Values;

var query1 = store.Query().HasValue <DateTimeComponent, DateTime>(date1);
var query2 = store.Query().ValueInRange<DateTimeComponent, DateTime>(date1, date1);
Expand All @@ -63,7 +63,7 @@ public static void Test_Index_Types_DateTime()
public static void Test_Index_Types_enum()
{
var store = new EntityStore();
var values = store.GetAllIndexedComponentValues<EnumComponent, MyEnum>();
var values = store.ComponentIndex<EnumComponent, MyEnum>().Values;

var query1 = store.Query().HasValue <EnumComponent, MyEnum>(MyEnum.E1);

Expand All @@ -89,7 +89,7 @@ public static void Test_Index_Types_enum()
public static void Test_Index_Types_enum_comparable()
{
var store = new EntityStore();
var values = store.GetAllIndexedComponentValues<ComparableEnumComponent, ComparableEnum>();
var values = store.ComponentIndex<ComparableEnumComponent, ComparableEnum>().Values;

var query1 = store.Query().HasValue <ComparableEnumComponent, ComparableEnum>(MyEnum.E1);
var query2 = store.Query().ValueInRange<ComparableEnumComponent, ComparableEnum>(MyEnum.E1, MyEnum.E2);
Expand Down
Loading

0 comments on commit 279922e

Please sign in to comment.