Skip to content

Commit

Permalink
Match
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed May 18, 2024
1 parent 42291f3 commit aca199a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
30 changes: 1 addition & 29 deletions src/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,35 +328,7 @@ private bool IsSuperset(ReadOnlySpan<ComponentInfo> other)

internal int FindMatch(ReadOnlySpan<Term> searching)
{
var currents = Components.AsSpan();
var i = 0;
var j = 0;

while (i < currents.Length && j < searching.Length)
{
ref readonly var current = ref currents[i];
ref readonly var search = ref searching[j];

if (_comparer.Compare(current.ID.Value, search.ID.Value) == 0)
{
if (search.Op == TermOp.Without)
return -1;

++j;
}
else if (current.ID > search.ID && search.Op != TermOp.With)
{
++j;
continue;
}

++i;
}

while (j < searching.Length && searching[j].Op != TermOp.With)
++j;

return j == searching.Length ? 0 : 1;
return Match.Validate(_comparer, Components.AsSpan(), searching);
}

public void Print()
Expand Down
56 changes: 56 additions & 0 deletions src/Match.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace TinyEcs;

static class Match
{
public static int Validate(IComparer<ulong> comparer, ReadOnlySpan<ComponentInfo> ids, ReadOnlySpan<Term> terms)
{
int idIndex = 0;
int termIndex = 0;

while (idIndex < ids.Length && termIndex < terms.Length)
{
var id = ids[idIndex].ID;
ref readonly var term = ref terms[termIndex];

if (comparer.Compare(id.Value, term.ID.Value) == 0)
{
switch (term.Op)
{
case TermOp.With:
termIndex++;
break;
case TermOp.Without:
return -1; // Forbidden ID found
case TermOp.Optional:
termIndex++;
break;
}
idIndex++;
}
else if (id < term.ID)
{
idIndex++;
}
else if (id > term.ID)
{
if (term.Op == TermOp.With)
{
return 1; // Required ID not found
}
termIndex++;
}
}

// Check any remaining required terms
while (termIndex < terms.Length)
{
if (terms[termIndex].Op == TermOp.With)
{
return 1; // Required ID not found
}
termIndex++;
}

return 0;
}
}
4 changes: 3 additions & 1 deletion src/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ static void ParseType<T>(SortedSet<Term> terms) where T : struct
EcsAssert.Panic(false, $"Type {type} is not registered. Register {type} using world.Entity<T>() or assign it to an entity.");
}

internal static class Query<TQuery, TFilter> where TQuery : struct where TFilter : struct
internal static class Query<TQuery, TFilter>
where TQuery : struct
where TFilter : struct
{
public static readonly ImmutableArray<Term> Terms;
public static readonly ImmutableArray<Term> Columns;
Expand Down

0 comments on commit aca199a

Please sign in to comment.