Skip to content

Commit

Permalink
cleanup: renamed generic parameter used for relations: TComponent -> …
Browse files Browse the repository at this point in the history
…TRelation
  • Loading branch information
friflo committed Nov 22, 2024
1 parent 7f8c7cb commit 8a625a7
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 128 deletions.
2 changes: 1 addition & 1 deletion src/ECS/Relations/ILinkRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Friflo.Engine.ECS;
/// Add multiple link relations to an entity using <see cref="Entity.AddComponent{T}()"/>.
/// </item>
/// <item>
/// Return all links of an entity to other entities using <see cref="RelationExtensions.GetRelations{TComponent}"/>.
/// Return all links of an entity to other entities using <see cref="RelationExtensions.GetRelations{TRelation}"/>.
/// </item>
/// <item>
/// Remove a specific link to another entity with <see cref="RelationExtensions.RemoveRelation{T}"/>.
Expand Down
18 changes: 9 additions & 9 deletions src/ECS/Relations/IRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ namespace Friflo.Engine.ECS;
public interface IRelation { }

/// <summary>
/// A relation component enables adding multiple components of the same type to an entity.<br/>
/// A relation enables adding multiple components of the same type to an entity.<br/>
/// The components added to a single entity build a set of components using the relation <typeparamref name="TKey"/> as unique identifier.
/// </summary>
/// <typeparam name="TKey">The key defining a unique relation component.</typeparam>
/// <typeparam name="TKey">The key defining a unique relation.</typeparam>
/// <remarks>
/// A relation component enables:
/// A relation enables:
/// <list type="bullet">
/// <item>
/// Add multiple relation components to an entity using <see cref="RelationExtensions.AddRelation{TComponent}"/>.
/// Add multiple relations to an entity using <see cref="RelationExtensions.AddRelation{TRelation}"/>.
/// </item>
/// <item>
/// Return all relation components of an entity using <see cref="RelationExtensions.GetRelations{TComponent}"/>.
/// Return all relations of an entity using <see cref="RelationExtensions.GetRelations{TRelation}"/>.
/// </item>
/// <item>
/// Return a specific relation by key using <see cref="RelationExtensions.GetRelation{TComponent,TKey}"/><br/>
/// or <see cref="RelationExtensions.TryGetRelation{TComponent,TKey}"/>.
/// Return a specific relation by key using <see cref="RelationExtensions.GetRelation{TRelation,TKey}"/><br/>
/// or <see cref="RelationExtensions.TryGetRelation{TRelation,TKey}"/>.
/// </item>
/// <item>
/// Remove a specific relation component by key using <see cref="RelationExtensions.RemoveRelation{T,TKey}"/>.
/// Remove a specific relation by key using <see cref="RelationExtensions.RemoveRelation{T,TKey}"/>.
/// </item>
/// </list>
/// </remarks>
public interface IRelation<out TKey> : IRelation
{
/// <summary>
/// Returns the key of a unique relation component.
/// Returns the key of a unique relation.
/// </summary>
TKey GetRelationKey();
}
62 changes: 31 additions & 31 deletions src/ECS/Relations/Internal/EntityRelations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal abstract class EntityRelations
/// Single <see cref="StructHeap"/> stored in the <see cref="archetype"/>.
internal readonly StructHeap heap;

/// map: entity id -> relation component positions in <see cref="archetype"/>
/// map: entity id -> relation positions in <see cref="archetype"/>
internal readonly Dictionary<int, IdArray> positionMap = new();

internal readonly EntityStore store;
Expand All @@ -47,9 +47,9 @@ internal EntityRelations(ComponentType componentType, Archetype archetype, Struc
relationBit = (int)types.bitSet.l0;
}

internal abstract bool AddComponent<TComponent> (int id, in TComponent component) where TComponent : struct, IRelation;
internal abstract bool AddComponent<TRelation> (int id, in TRelation component) where TRelation : struct, IRelation;
internal abstract IRelation GetRelationAt (int id, int index);
internal virtual ref TComponent GetEntityRelation<TComponent>(int id, int target) where TComponent : struct => throw new InvalidOperationException($"type: {GetType().Name}");
internal virtual ref TRelation GetEntityRelation<TRelation >(int id, int target) where TRelation : struct => throw new InvalidOperationException($"type: {GetType().Name}");
internal virtual void AddIncomingRelations (int target, List<EntityLink> result) => throw new InvalidOperationException($"type: {GetType().Name}");
internal virtual void RemoveLinksWithTarget (int targetId) => throw new InvalidOperationException($"type: {GetType().Name}");

Expand All @@ -72,7 +72,7 @@ internal static EntityRelations GetEntityRelations(EntityStoreBase store, int st
var archetype = new Archetype(config, heap);
var obj = Activator.CreateInstance(componentType.RelationType, componentType, archetype, heap);
return relationsMap[structIndex] = (EntityRelations)obj;
// return store.relationsMap[structIndex] = new RelationArchetype<TComponent, TKey>(archetype, heap);
// return store.relationsMap[structIndex] = new RelationArchetype<TRelation, TKey>(archetype, heap);
}

private static EntityRelations[] CreateRelationsMap() {
Expand All @@ -87,49 +87,49 @@ internal int GetRelationCount(Entity entity) {
return positions.count;
}

internal static Relations<TComponent> GetRelations<TComponent>(EntityStore store, int id)
where TComponent : struct, IRelation
internal static Relations<TRelation> GetRelations<TRelation>(EntityStore store, int id)
where TRelation : struct, IRelation
{
var relations = store.extension.relationsMap?[StructInfo<TComponent>.Index];
var relations = store.extension.relationsMap?[StructInfo<TRelation>.Index];
if (relations == null) {
return default;
}
relations.positionMap.TryGetValue(id, out var positions);
int count = positions.count;
var components = ((StructHeap<TComponent>)relations.heap).components;
var components = ((StructHeap<TRelation>)relations.heap).components;
switch (count) {
case 0: return new Relations<TComponent>();
case 1: return new Relations<TComponent>(components, positions.start);
case 0: return new Relations<TRelation>();
case 1: return new Relations<TRelation>(components, positions.start);
}
var poolPositions = IdArrayPool.GetIds(count, relations.idHeap);
return new Relations<TComponent>(components, poolPositions, positions.start, positions.count);
return new Relations<TRelation>(components, poolPositions, positions.start, positions.count);
}

internal static ref TComponent GetRelation<TComponent, TKey>(EntityStore store, int id, TKey key)
where TComponent : struct, IRelation<TKey>
internal static ref TRelation GetRelation<TRelation, TKey>(EntityStore store, int id, TKey key)
where TRelation : struct, IRelation<TKey>
{
var relations = (EntityRelations<TComponent,TKey>)store.extension.relationsMap?[StructInfo<TComponent>.Index];
var relations = (EntityRelations<TRelation,TKey>)store.extension.relationsMap?[StructInfo<TRelation>.Index];
if (relations == null) {
throw KeyNotFoundException(id, key);
}
return ref relations.GetRelation<TComponent>(id, key);
return ref relations.GetRelation<TRelation>(id, key);
}

internal static bool TryGetRelation<TComponent, TKey>(EntityStore store, int id, TKey key, out TComponent value)
where TComponent : struct, IRelation<TKey>
internal static bool TryGetRelation<TRelation, TKey>(EntityStore store, int id, TKey key, out TRelation value)
where TRelation : struct, IRelation<TKey>
{
var relations = (EntityRelations<TComponent,TKey>)store.extension.relationsMap?[StructInfo<TComponent>.Index];
var relations = (EntityRelations<TRelation,TKey>)store.extension.relationsMap?[StructInfo<TRelation>.Index];
if (relations == null) {
value = default;
return false;
}
return relations.TryGetRelation(id, key, out value);
}

internal void ForAllEntityRelations<TComponent>(ForEachEntity<TComponent> lambda)
where TComponent : struct, IRelation
internal void ForAllEntityRelations<TRelation>(ForEachEntity<TRelation> lambda)
where TRelation : struct, IRelation
{
var components = ((StructHeap<TComponent>)heap).components;
var components = ((StructHeap<TRelation>)heap).components;
int count = archetype.Count;
var ids = archetype.entityIds;
var entityStore = store;
Expand All @@ -138,13 +138,13 @@ internal void ForAllEntityRelations<TComponent>(ForEachEntity<TComponent> lambda
}
}

internal (Entities entities, Chunk<TComponent> relations) GetAllEntityRelations<TComponent>()
where TComponent : struct, IRelation
internal (Entities entities, Chunk<TRelation> relations) GetAllEntityRelations<TRelation>()
where TRelation : struct, IRelation
{
int count = archetype.Count;
var entities = new Entities(store, archetype.entityIds, 0, count);
var components = ((StructHeap<TComponent>)heap).components;
var chunk = new Chunk<TComponent>(components, count, 0);
var components = ((StructHeap<TRelation>)heap).components;
var chunk = new Chunk<TRelation>(components, count, 0);
return (entities, chunk);
}

Expand All @@ -166,17 +166,17 @@ internal int CountIncomingLinkRelations(int target)
#endregion

#region mutation
internal static bool AddRelation<TComponent>(EntityStoreBase store, int id, in TComponent component)
where TComponent : struct, IRelation
internal static bool AddRelation<TRelation>(EntityStoreBase store, int id, in TRelation component)
where TRelation : struct, IRelation
{
var relations = GetEntityRelations(store, StructInfo<TComponent>.Index);
var relations = GetEntityRelations(store, StructInfo<TRelation>.Index);
return relations.AddComponent(id, component);
}

internal static bool RemoveRelation<TComponent, TKey>(EntityStoreBase store, int id, TKey key)
where TComponent : struct, IRelation<TKey>
internal static bool RemoveRelation<TRelation, TKey>(EntityStoreBase store, int id, TKey key)
where TRelation : struct, IRelation<TKey>
{
var relations = (EntityRelations<TComponent,TKey>)GetEntityRelations(store, StructInfo<TComponent>.Index);
var relations = (EntityRelations<TRelation,TKey>)GetEntityRelations(store, StructInfo<TRelation>.Index);
return relations.RemoveRelation(id, key);
}

Expand Down
18 changes: 9 additions & 9 deletions src/ECS/Relations/Internal/EntityRelations.generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ internal override IRelation GetRelationAt(int id, int index)
return components[poolPositions[start + index]];
}

internal ref TComponent GetRelation<TComponent>(int id, TKey key)
where TComponent : struct, IRelation<TKey>
internal ref T GetRelation<T>(int id, TKey key)
where T : struct, IRelation<TKey>
{
var position = FindRelationPosition(id, key, out _, out _);
if (position >= 0) {
return ref ((StructHeap<TComponent>)heap).components[position];
return ref ((StructHeap<T>)heap).components[position];
}
throw KeyNotFoundException(id, key);
}

internal bool TryGetRelation<TComponent>(int id, TKey key, out TComponent value)
where TComponent : struct, IRelation<TKey>
internal bool TryGetRelation<T>(int id, TKey key, out T value)
where T : struct, IRelation<TKey>
{
var position = FindRelationPosition(id, key, out _, out _);
if (position >= 0) {
value = ((StructHeap<TComponent>)heap).components[position];
value = ((StructHeap<T>)heap).components[position];
return true;
}
value = default;
Expand All @@ -82,9 +82,9 @@ internal bool TryGetRelation<TComponent>(int id, TKey key, out TComponent value)
#region mutation

/// <returns>true - component is newly added to the entity.<br/> false - component is updated.</returns>
internal override bool AddComponent<TComponent>(int id, in TComponent component)
internal override bool AddComponent<T>(int id, in T component)
{
var relationKey = RelationUtils<TComponent, TKey>.GetRelationKey(component);
var relationKey = RelationUtils<T, TKey>.GetRelationKey(component);
// var relationKey = ((IRelation<TKey>)component).GetRelationKey(); // boxing version
var added = true;
var position = FindRelationPosition(id, relationKey, out var positions, out _);
Expand All @@ -94,7 +94,7 @@ internal override bool AddComponent<TComponent>(int id, in TComponent component)
}
position = AddEntityRelation(id, positions);
AssignComponent:
((StructHeap<TComponent>)heap).components[position] = component;
((StructHeap<T>)heap).components[position] = component;
return added;
}

Expand Down
26 changes: 13 additions & 13 deletions src/ECS/Relations/Internal/RelationUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ namespace Friflo.Engine.ECS.Relations;
internal static class RelationUtils
{
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL3050", Justification = "TODO")] // TODO
internal static GetRelationKey<TComponent, TKey> CreateGetRelationKey<TComponent, TKey>()
where TComponent : struct, IRelation
internal static GetRelationKey<TRelation, TKey> CreateGetRelationKey<TRelation, TKey>()
where TRelation : struct, IRelation
{
const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod;
var method = typeof(RelationUtils).GetMethod(nameof(GetRelationKey), flags);
var genericMethod = method!.MakeGenericMethod(typeof(TComponent), typeof(TKey));
var genericMethod = method!.MakeGenericMethod(typeof(TRelation), typeof(TKey));

var genericDelegate = Delegate.CreateDelegate(typeof(GetRelationKey<TComponent,TKey>), genericMethod);
return (GetRelationKey<TComponent,TKey>)genericDelegate;
var genericDelegate = Delegate.CreateDelegate(typeof(GetRelationKey<TRelation,TKey>), genericMethod);
return (GetRelationKey<TRelation,TKey>)genericDelegate;
}

private static TKey GetRelationKey<TComponent,TKey>(in TComponent component)
where TComponent : struct, IRelation<TKey>
private static TKey GetRelationKey<TRelation,TKey>(in TRelation component)
where TRelation : struct, IRelation<TKey>
{
return component.GetRelationKey();
}
}

internal static class RelationUtils<TComponent, TKey>
where TComponent : struct, IRelation
internal static class RelationUtils<TRelation, TKey>
where TRelation : struct, IRelation
{
/// <summary> Returns the component value without boxing. </summary>
internal static readonly GetRelationKey<TComponent, TKey> GetRelationKey;
internal static readonly GetRelationKey<TRelation, TKey> GetRelationKey;

static RelationUtils() {
GetRelationKey = RelationUtils.CreateGetRelationKey<TComponent,TKey>();
GetRelationKey = RelationUtils.CreateGetRelationKey<TRelation,TKey>();
}
}

internal delegate TKey GetRelationKey<TComponent, out TKey>(in TComponent component)
where TComponent : struct, IRelation;
internal delegate TKey GetRelationKey<TRelation, out TKey>(in TRelation component)
where TRelation : struct, IRelation;
10 changes: 5 additions & 5 deletions src/ECS/Relations/Links/EntityRelationLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public EntityRelationLinks(ComponentType componentType, Archetype archetype, Str
}

/// Expect: component is present
internal override ref TComponent GetEntityRelation<TComponent>(int id, int targetId)
internal override ref T GetEntityRelation<T>(int id, int targetId)
{
Entity target = new Entity(store, targetId);
int position = FindRelationPosition(id, target, out _, out _);
return ref ((StructHeap<TComponent>)heap).components[position];
return ref ((StructHeap<T>)heap).components[position];
}

internal override void AddIncomingRelations(int target, List<EntityLink> result)
Expand All @@ -47,9 +47,9 @@ internal override void AddIncomingRelations(int target, List<EntityLink> result)
#region mutation

/// <returns>true - component is newly added to the entity.<br/> false - component is updated.</returns>
internal override bool AddComponent<TComponent>(int id, in TComponent component)
internal override bool AddComponent<T>(int id, in T component)
{
Entity target = RelationUtils<TComponent, Entity>.GetRelationKey(component);
Entity target = RelationUtils<T, Entity>.GetRelationKey(component);
bool added = true;
int position = FindRelationPosition(id, target, out var positions, out _);
if (position >= 0) {
Expand All @@ -59,7 +59,7 @@ internal override bool AddComponent<TComponent>(int id, in TComponent component)
position = AddEntityRelation(id, positions);
LinkRelationUtils.AddComponentValue(id, target.Id, this);
AssignComponent:
((StructHeap<TComponent>)heap).components[position] = component;
((StructHeap<T>)heap).components[position] = component;
return added;
}

Expand Down
Loading

0 comments on commit 8a625a7

Please sign in to comment.