Skip to content

Commit

Permalink
ECS - rename IdRevision -> Ident
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Aug 3, 2024
1 parent aea8eaf commit cdbd8ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
13 changes: 6 additions & 7 deletions src/ECS/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,10 @@ public ref readonly Tags Tags { get {
/// <summary>Unique entity id.<br/>
/// Uniqueness relates to the <see cref="Entity"/>'s stored in its <see cref="EntityStore"/></summary>
[Browse(Never)]
[FieldOffset(8)] internal readonly IdRevision idRevision; // (8)
// ReSharper disable once InconsistentNaming
[FieldOffset(8)] public readonly int Id; // 4
[FieldOffset(8)] internal readonly Ident ident; // (8)
[FieldOffset(8)] public readonly int Id; // 4
[Browse(Never)]
[FieldOffset(12)] public readonly short Revision; // 2
[FieldOffset(12)] public readonly short Revision; // 2
#endregion


Expand Down Expand Up @@ -658,13 +657,13 @@ internal bool TryGetTreeNode(out TreeNode treeNode)
// ------------------------------------ general methods ---------------------------------------
#region general - methods
/// <summary> Return true if the passed entities have the same <see cref="Entity.Id"/>'s. </summary>
public static bool operator == (Entity a, Entity b) => a.idRevision == b.idRevision && a.store == b.store;
public static bool operator == (Entity a, Entity b) => a.ident == b.ident && a.store == b.store;

/// <summary> Return true if the passed entities have the different <see cref="Entity.Id"/>'s. </summary>
public static bool operator != (Entity a, Entity b) => a.idRevision != b.idRevision || a.store != b.store;
public static bool operator != (Entity a, Entity b) => a.ident != b.ident || a.store != b.store;

// --- IEquatable<T>
public bool Equals(Entity other) => idRevision == other.idRevision && store == other.store;
public bool Equals(Entity other) => ident == other.ident && store == other.store;

// --- object
/// <summary> Note: Not implemented to avoid excessive boxing. </summary>
Expand Down
12 changes: 6 additions & 6 deletions src/ECS/Entity/IdRevision.cs → src/ECS/Entity/Ident.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
namespace Friflo.Engine.ECS;

[StructLayout(LayoutKind.Explicit)]
internal readonly struct IdRevision : IEquatable<IdRevision>
internal readonly struct Ident : IEquatable<Ident>
{
[FieldOffset(0)] public readonly int Id; // 4
[FieldOffset(4)] public readonly short Revision; // 2

[Browse(Never)]
[FieldOffset(0)] internal readonly long value; // (8) - 4 (Id) + 2 (Revision) + 2 (padding)

public bool Equals (IdRevision other) => value == other.value;
public static bool operator == (IdRevision a, IdRevision b) => a.value == b.value;
public static bool operator != (IdRevision a, IdRevision b) => a.value != b.value;
public bool Equals (Ident other) => value == other.value;
public static bool operator == (Ident a, Ident b) => a.value == b.value;
public static bool operator != (Ident a, Ident b) => a.value != b.value;

public override bool Equals(object obj) => throw new NotImplementedException("by intenetion to avoid boxing");
public override bool Equals(object obj) => throw new NotImplementedException("by intention to avoid boxing");
public override int GetHashCode() => Id ^ Revision;

internal IdRevision(int id, short revision) {
internal Ident(int id, short revision) {
Id = id;
Revision = revision;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Tests-internal/ECS/Test_Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public static class Test_Entity
[Test]
public static void Test_Entity_IdRevision()
{
var dict = new Dictionary<IdRevision, int>();
var idRef11 = new IdRevision(1,1);
var idRef21 = new IdRevision(2,1);
var dict = new Dictionary<Ident, int>();
var idRef11 = new Ident(1,1);
var idRef21 = new Ident(2,1);

IsTrue(idRef11 == idRef11);
IsTrue(idRef11 == new Ident(1,1));
IsTrue(idRef11 != idRef21);

dict.Add(idRef11, 11);
dict.Add(idRef21, 21);

AreEqual(2, dict.Count);
AreEqual(11, dict[new IdRevision(1,1)]);
AreEqual(11, dict[new Ident(1,1)]);

var start = Mem.GetAllocatedBytes();
_ = dict[new IdRevision(1,1)];
_ = dict[new Ident(1,1)];
Mem.AssertNoAlloc(start);

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

0 comments on commit cdbd8ff

Please sign in to comment.