Skip to content

Commit

Permalink
replace RelationExtensions.GetAllEntityRelations() -> EntityRelation<…
Browse files Browse the repository at this point in the history
…TRelation>().Pairs
  • Loading branch information
friflo committed Dec 1, 2024
1 parent 6489ec2 commit eb41ae4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/ECS/Relations/EntityRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal EntityRelation(AbstractEntityRelations relations) {
}

/// <summary>
/// Returns a collection of entities having one or more relations of the specified <typeparamref name="TRelation"/> type.<br/>
/// Returns a collection of entities having one or more relation.<br/>
/// Executes in O(1).
/// </summary>
/// <remarks>
Expand All @@ -23,15 +23,21 @@ internal EntityRelation(AbstractEntityRelations relations) {
/// </item>
/// <item>
/// To get all entities including their relations (the cartesian product aka CROSS JOIN) use<br/>
/// <see cref="RelationExtensions.GetAllEntityRelations{TRelation}"/>
/// <see cref="EntityRelation{TRelation}.Pairs"/>
/// </item>
/// </list>
/// </remarks>
public EntityReadOnlyCollection Entities => new EntityReadOnlyCollection(relations.store, relations.positionMap.Keys);

/// <summary>
/// Iterates all entity relations of the specified <typeparamref name="TRelation"/> type.<br/>
/// Iterates all entity relations.<br/>
/// Executes in O(N) N: number of all entity relations.
/// </summary>
public void For(ForEachEntity<TRelation> lambda) => relations.ForAllEntityRelations(lambda);

/// <summary>
/// Return all entity relations as pairs. A pair is <c>(entities[i], relations[i])</c><br/>
/// Executes in O(1). Most efficient way to iterate all entity relations.
/// </summary>
public (Entities entities, Chunk<TRelation> relations) Pairs => relations.GetAllEntityRelations<TRelation>();
}
4 changes: 3 additions & 1 deletion src/ECS/Relations/RelationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static EntityRelation<TRelation> EntityRelation<TRelation>(this EntitySto
/// </item>
/// <item>
/// To get all entities including their relations (the cartesian product aka CROSS JOIN) use<br/>
/// <see cref="GetAllEntityRelations{TRelation}"/>
/// <see cref="EntityRelation{TRelation}.Pairs"/>
/// </item>
/// </list>
/// </remarks>
Expand All @@ -147,9 +147,11 @@ public static void ForAllEntityRelations<TRelation>(this EntityStore store, ForE
}

/// <summary>
/// Obsolete: Use <see cref="EntityRelation{TRelation}.Pairs"/><br/>
/// Return all entity relations of the specified <typeparamref name="TRelation"/> type.<br/>
/// Executes in O(1). Most efficient way to iterate all entity relations.
/// </summary>
[Obsolete("replace with property: EntityRelation<TRelation>().Pairs")]
public static (Entities entities, Chunk<TRelation> relations) GetAllEntityRelations<TRelation>(this EntityStore store)
where TRelation : struct, IRelation
{
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/ECS/Relations/Test_Relations_Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void Test_Relations_Delete_int_relations()
AreEqual(3, count);

// --- version: get all entity relations in O(1)
var (entities, relations) = store.GetAllEntityRelations<IntRelation>();
var (entities, relations) = store.EntityRelation<IntRelation>().Pairs;
AreEqual("{ 1, 1, 2 }", entities.Debug());
AreEqual("{ 10, 20, 30 }", relations.Debug());

Expand Down Expand Up @@ -86,7 +86,7 @@ public static void Test_Relations_Delete_Entity_relations()
AreEqual(3, count);

// --- version: get all entity relations in O(1)
var (entities, relations) = store.GetAllEntityRelations<AttackRelation>();
var (entities, relations) = store.EntityRelation<AttackRelation>().Pairs;
AreEqual("{ 1, 1, 2 }", entities.Debug());
AreEqual("{ 10, 11, 12 }", relations.Debug());

Expand Down
2 changes: 1 addition & 1 deletion src/Tests/ECS/Relations/Test_Relations_Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public static void Test_Relations_GetAllEntityRelations_Perf()
int count = 0;
var sw = new Stopwatch();
sw.Start();
var (entities, relations) = store.GetAllEntityRelations<IntRelation>();
var (entities, relations) = store.EntityRelation<IntRelation>().Pairs;
int length = entities.Count;
for (int n = 0; n < length; n++) {
count++;
Expand Down

0 comments on commit eb41ae4

Please sign in to comment.