Skip to content

Commit

Permalink
rename: IRelationComponent<> -> IRelation<> and RelationComponents<> …
Browse files Browse the repository at this point in the history
…-> Relations<>
  • Loading branch information
friflo committed Nov 22, 2024
1 parent 6a5893d commit 7f8c7cb
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/ECS/Base/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ internal static void GetComponentTypes(Assembly assembly, int assemblyIndex, Lis
continue;
}
// COMP_TAG
if (typeof(IRelationComponent).IsAssignableFrom(type) ||
if (typeof(IRelation).IsAssignableFrom(type) ||
typeof(IComponent).IsAssignableFrom(type)) {
AddType(assemblyIndex, type, SchemaTypeKind.Component, componentTypes);
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/Base/SchemaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void OrderComponentTypes()
var type = componentTypes[n];
buffer[n] = type;
var isIndex = ComponentIndexUtils.GetIndexType(type.type, out _) != null ||
RelationComponentUtils.GetEntityRelationsType(type.type, out _) != null;
RelationTypeUtils.GetEntityRelationsType(type.type, out _) != null;
isIndexType[n] = isIndex;
if (isIndex) indexCount++;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ private SchemaType CreateComponentType(Type type)
private SchemaType CreateRelationType(Type type)
{
var structIndex = components.Count + 1;
var relationType = RelationComponentUtils.GetEntityRelationsType(type, out Type keyType);
var relationType = RelationTypeUtils.GetEntityRelationsType(type, out Type keyType);
var createParams = new object[] { structIndex, relationType, keyType };
var method = typeof(SchemaUtils).GetMethod(nameof(SchemaUtils.CreateRelationType), Flags);
var genericMethod = method!.MakeGenericMethod(type);
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Base/SchemaUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal static ComponentType CreateComponentType<T>(int structIndex, Type index
}

internal static ComponentType CreateRelationType<T>(int structIndex, Type relationType, Type keyType)
where T : struct, IRelationComponent
where T : struct, IRelation
{
string componentKey;
var type = typeof(T);
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Base/Types/ComponentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal override ComponentCommands CreateComponentCommands()
}

internal sealed class RelationType<T> : ComponentType
where T : struct, IRelationComponent
where T : struct, IRelation
{
#region properties
public override string ToString() => $"Component: [{typeof(T).Name}]";
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/Entity/EntityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public struct EntityNode
[Browse(Never)] internal int isOwner; // 4

/// <summary>
/// Bit mask for all <see cref="EntityIndex"/> and all <see cref="EntityRelationLinks{TRelationComponent}"/> instances.<br/>
/// Bit mask for all <see cref="EntityIndex"/> and all <see cref="EntityRelationLinks{TRelation}"/> instances.<br/>
/// A bit is set if the entity is linked by either a <see cref="ILinkComponent"/> or a <see cref="ILinkRelation"/>.
/// </summary>
/// <remarks>
/// Use <see cref="IsLinked"/> to see <see cref="ComponentTypes"/> by name.<br/>
/// This masks prevents the insane cost when deleting an entity.<br/>
/// Otherwise, all <see cref="EntityIndex"/> and <see cref="EntityRelationLinks{TRelationComponent}"/> instances need to be iterated
/// Otherwise, all <see cref="EntityIndex"/> and <see cref="EntityRelationLinks{TRelation}"/> instances need to be iterated
/// to check if the entity is a key in their Dictionary's and perform required cleanup.
/// </remarks>
[Browse(Never)] internal int isLinked; // 4
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/Query/EntityStore.Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class EntityStoreBase
/// Create a reusable <see cref="ArchetypeQuery"/> for the given relation type.<br/>
/// </summary>
public ArchetypeQuery<T1> QueryRelation<T1> ()
where T1 : struct, IRelationComponent
where T1 : struct, IRelation
{
return new ArchetypeQuery<T1>(this, Signature.GetRelation<T1>(), null);
}
Expand All @@ -27,7 +27,7 @@ public ArchetypeQuery<T1> QueryRelation<T1> ()
/// The filter attached to the query can be modified subsequently.
/// </summary>
public ArchetypeQuery<T1> QueryRelation<T1> (QueryFilter filter)
where T1 : struct, IRelationComponent
where T1 : struct, IRelation
{
return new ArchetypeQuery<T1>(this, Signature.GetRelation<T1>(), filter);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Query/Signature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Friflo.Engine.ECS;
public static class Signature
{
internal static Signature<T1> GetRelation<T1>()
where T1 : struct, IRelationComponent
where T1 : struct, IRelation
{
var schema = EntityStoreBase.Static.EntitySchema;
var indexes = new SignatureIndexes(
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Relations/ILinkRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ namespace Friflo.Engine.ECS;
/// </item>
/// </list>
/// </remarks>
public interface ILinkRelation : IRelationComponent<Entity> {
public interface ILinkRelation : IRelation<Entity> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS;

public interface IRelationComponent { }
public interface IRelation { }

/// <summary>
/// A relation component enables adding multiple components of the same type to an entity.<br/>
Expand All @@ -29,7 +29,7 @@ public interface IRelationComponent { }
/// </item>
/// </list>
/// </remarks>
public interface IRelationComponent<out TKey> : IRelationComponent
public interface IRelation<out TKey> : IRelation
{
/// <summary>
/// Returns the key of a unique relation component.
Expand Down
28 changes: 14 additions & 14 deletions src/ECS/Relations/Internal/EntityRelations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal abstract class EntityRelations
public override string ToString() => $"relation count: {archetype.Count}";

#region fields
/// Single <see cref="Archetype"/> containing all relations of a specific <see cref="IRelationComponent{TKey}"/>
/// Single <see cref="Archetype"/> containing all relations of a specific <see cref="IRelation{TKey}"/>
internal readonly Archetype archetype;

/// Single <see cref="StructHeap"/> stored in the <see cref="archetype"/>.
Expand Down Expand Up @@ -47,8 +47,8 @@ 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, IRelationComponent;
internal abstract IRelationComponent GetRelationAt (int id, int index);
internal abstract bool AddComponent<TComponent> (int id, in TComponent component) where TComponent : 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 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 Down Expand Up @@ -87,8 +87,8 @@ internal int GetRelationCount(Entity entity) {
return positions.count;
}

internal static RelationComponents<TComponent> GetRelations<TComponent>(EntityStore store, int id)
where TComponent : struct, IRelationComponent
internal static Relations<TComponent> GetRelations<TComponent>(EntityStore store, int id)
where TComponent : struct, IRelation
{
var relations = store.extension.relationsMap?[StructInfo<TComponent>.Index];
if (relations == null) {
Expand All @@ -98,15 +98,15 @@ internal static RelationComponents<TComponent> GetRelations<TComponent>(EntitySt
int count = positions.count;
var components = ((StructHeap<TComponent>)relations.heap).components;
switch (count) {
case 0: return new RelationComponents<TComponent>();
case 1: return new RelationComponents<TComponent>(components, positions.start);
case 0: return new Relations<TComponent>();
case 1: return new Relations<TComponent>(components, positions.start);
}
var poolPositions = IdArrayPool.GetIds(count, relations.idHeap);
return new RelationComponents<TComponent>(components, poolPositions, positions.start, positions.count);
return new Relations<TComponent>(components, poolPositions, positions.start, positions.count);
}

internal static ref TComponent GetRelation<TComponent, TKey>(EntityStore store, int id, TKey key)
where TComponent : struct, IRelationComponent<TKey>
where TComponent : struct, IRelation<TKey>
{
var relations = (EntityRelations<TComponent,TKey>)store.extension.relationsMap?[StructInfo<TComponent>.Index];
if (relations == null) {
Expand All @@ -116,7 +116,7 @@ internal static ref TComponent GetRelation<TComponent, TKey>(EntityStore store,
}

internal static bool TryGetRelation<TComponent, TKey>(EntityStore store, int id, TKey key, out TComponent value)
where TComponent : struct, IRelationComponent<TKey>
where TComponent : struct, IRelation<TKey>
{
var relations = (EntityRelations<TComponent,TKey>)store.extension.relationsMap?[StructInfo<TComponent>.Index];
if (relations == null) {
Expand All @@ -127,7 +127,7 @@ internal static bool TryGetRelation<TComponent, TKey>(EntityStore store, int id,
}

internal void ForAllEntityRelations<TComponent>(ForEachEntity<TComponent> lambda)
where TComponent : struct, IRelationComponent
where TComponent : struct, IRelation
{
var components = ((StructHeap<TComponent>)heap).components;
int count = archetype.Count;
Expand All @@ -139,7 +139,7 @@ internal void ForAllEntityRelations<TComponent>(ForEachEntity<TComponent> lambda
}

internal (Entities entities, Chunk<TComponent> relations) GetAllEntityRelations<TComponent>()
where TComponent : struct, IRelationComponent
where TComponent : struct, IRelation
{
int count = archetype.Count;
var entities = new Entities(store, archetype.entityIds, 0, count);
Expand Down Expand Up @@ -167,14 +167,14 @@ internal int CountIncomingLinkRelations(int target)

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

internal static bool RemoveRelation<TComponent, TKey>(EntityStoreBase store, int id, TKey key)
where TComponent : struct, IRelationComponent<TKey>
where TComponent : struct, IRelation<TKey>
{
var relations = (EntityRelations<TComponent,TKey>)GetEntityRelations(store, StructInfo<TComponent>.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 @@ -11,17 +11,17 @@ namespace Friflo.Engine.ECS.Relations;


/// Contains a single <see cref="Archetype"/> with a single <see cref="StructHeap{T}"/><br/>
internal class EntityRelations<TRelationComponent, TKey> : EntityRelations
where TRelationComponent : struct, IRelationComponent<TKey>
internal class EntityRelations<TRelation, TKey> : EntityRelations
where TRelation : struct, IRelation<TKey>
{
/// Single <see cref="StructHeap"/> stored in the <see cref="EntityRelations.archetype"/>.
internal readonly StructHeap<TRelationComponent> heapGeneric;
internal readonly StructHeap<TRelation> heapGeneric;

/// Instance created at <see cref="EntityRelations.GetEntityRelations"/>
public EntityRelations(ComponentType componentType, Archetype archetype, StructHeap heap)
: base(componentType, archetype, heap)
{
heapGeneric = (StructHeap<TRelationComponent>)heap;
heapGeneric = (StructHeap<TRelation>)heap;
}

/// Executes in O(M) M: number of entity relations
Expand All @@ -34,7 +34,7 @@ protected int FindRelationPosition(int id, TKey key, out IdArray positions, out
for (index = 0; index < positionCount; index++) {
int position = positionSpan[index];
TKey relation = components[position].GetRelationKey(); // no boxing
// var relation = RelationUtils<TRelationComponent, TKey>.GetRelationKey(components[position]);
// var relation = RelationUtils<TRelation, TKey>.GetRelationKey(components[position]);
if (EqualityComparer<TKey>.Default.Equals(relation, key)) {
return position;
}
Expand All @@ -43,7 +43,7 @@ protected int FindRelationPosition(int id, TKey key, out IdArray positions, out
}

#region query
internal override IRelationComponent GetRelationAt(int id, int index)
internal override IRelation GetRelationAt(int id, int index)
{
positionMap.TryGetValue(id, out var positions);
var count = positions.count;
Expand All @@ -57,7 +57,7 @@ internal override IRelationComponent GetRelationAt(int id, int index)
}

internal ref TComponent GetRelation<TComponent>(int id, TKey key)
where TComponent : struct, IRelationComponent<TKey>
where TComponent : struct, IRelation<TKey>
{
var position = FindRelationPosition(id, key, out _, out _);
if (position >= 0) {
Expand All @@ -67,7 +67,7 @@ internal ref TComponent GetRelation<TComponent>(int id, TKey key)
}

internal bool TryGetRelation<TComponent>(int id, TKey key, out TComponent value)
where TComponent : struct, IRelationComponent<TKey>
where TComponent : struct, IRelation<TKey>
{
var position = FindRelationPosition(id, key, out _, out _);
if (position >= 0) {
Expand All @@ -85,7 +85,7 @@ internal bool TryGetRelation<TComponent>(int id, TKey key, out TComponent value)
internal override bool AddComponent<TComponent>(int id, in TComponent component)
{
var relationKey = RelationUtils<TComponent, TKey>.GetRelationKey(component);
// var relationKey = ((IRelationComponent<TKey>)component).GetRelationKey(); // boxing version
// var relationKey = ((IRelation<TKey>)component).GetRelationKey(); // boxing version
var added = true;
var position = FindRelationPosition(id, relationKey, out var positions, out _);
if (position >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS.Relations;

internal static class RelationComponentUtils
internal static class RelationTypeUtils
{
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070", Justification = "TODO")] // TODO
internal static Type GetEntityRelationsType(Type componentType, out Type keyType)
Expand All @@ -17,7 +17,7 @@ internal static Type GetEntityRelationsType(Type componentType, out Type keyType
{
if (!i.IsGenericType) continue;
var genericType = i.GetGenericTypeDefinition();
if (genericType != typeof(IRelationComponent<>)) {
if (genericType != typeof(IRelation<>)) {
continue;
}
keyType = i.GenericTypeArguments[0];
Expand Down
12 changes: 6 additions & 6 deletions src/ECS/Relations/Internal/RelationUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ internal static class RelationUtils
{
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL3050", Justification = "TODO")] // TODO
internal static GetRelationKey<TComponent, TKey> CreateGetRelationKey<TComponent, TKey>()
where TComponent : struct, IRelationComponent
where TComponent : struct, IRelation
{
const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod;
var method = typeof(RelationUtils).GetMethod(nameof(GetRelationComponentKey), flags);
var method = typeof(RelationUtils).GetMethod(nameof(GetRelationKey), flags);
var genericMethod = method!.MakeGenericMethod(typeof(TComponent), typeof(TKey));

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

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

internal static class RelationUtils<TComponent, TKey>
where TComponent : struct, IRelationComponent
where TComponent : struct, IRelation
{
/// <summary> Returns the component value without boxing. </summary>
internal static readonly GetRelationKey<TComponent, TKey> GetRelationKey;
Expand All @@ -41,4 +41,4 @@ static RelationUtils() {
}

internal delegate TKey GetRelationKey<TComponent, out TKey>(in TComponent component)
where TComponent : struct, IRelationComponent;
where TComponent : struct, IRelation;
6 changes: 3 additions & 3 deletions src/ECS/Relations/Links/EntityRelationLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace Friflo.Engine.ECS.Relations;


/// Contains a single <see cref="Archetype"/> with a single <see cref="StructHeap{T}"/><br/>
internal class EntityRelationLinks<TRelationComponent> : EntityRelations<TRelationComponent, Entity>
where TRelationComponent : struct, ILinkRelation
internal class EntityRelationLinks<TRelation> : EntityRelations<TRelation, Entity>
where TRelation : struct, ILinkRelation
{
/// Instance created at <see cref="EntityRelations.GetEntityRelations"/>
public EntityRelationLinks(ComponentType componentType, Archetype archetype, StructHeap heap)
Expand Down Expand Up @@ -102,7 +102,7 @@ internal override void RemoveEntityRelations(int id)
private void RemoveIncomingLinks(IdArray positions, int id)
{
var positionsSpan = positions.GetSpan(idHeap, store);
var components = ((StructHeap<TRelationComponent>)heap).components;
var components = ((StructHeap<TRelation>)heap).components;
var linkMap = linkEntityMap;
foreach (var position in positionsSpan)
{
Expand Down
Loading

0 comments on commit 7f8c7cb

Please sign in to comment.