Skip to content

Commit

Permalink
improved query iter
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed May 31, 2024
1 parent 7392d8f commit ad50586
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ namespace TinyEcs;

public ref struct QueryInternal
{
private readonly ReadOnlySpan<Archetype> _archetypes;
private int _index;
private ref Archetype _value;

Check failure on line 7 in src/Query.cs

View workflow job for this annotation

GitHub Actions / build

Target runtime doesn't support ref fields.

Check failure on line 7 in src/Query.cs

View workflow job for this annotation

GitHub Actions / build

Target runtime doesn't support ref fields.
private readonly ref Archetype _first, _last;

Check failure on line 8 in src/Query.cs

View workflow job for this annotation

GitHub Actions / build

Target runtime doesn't support ref fields.

Check failure on line 8 in src/Query.cs

View workflow job for this annotation

GitHub Actions / build

Target runtime doesn't support ref fields.

Check failure on line 8 in src/Query.cs

View workflow job for this annotation

GitHub Actions / build

Target runtime doesn't support ref fields.

Check failure on line 8 in src/Query.cs

View workflow job for this annotation

GitHub Actions / build

Target runtime doesn't support ref fields.

internal QueryInternal(ReadOnlySpan<Archetype> archetypes)
internal QueryInternal(Span<Archetype> archetypes)
{
_archetypes = archetypes;
_index = -1;
_first = ref MemoryMarshal.GetReference(archetypes);
_last = ref Unsafe.Add(ref _first, archetypes.Length);
_value = ref Unsafe.NullRef<Archetype>();
}

public readonly Archetype Current => _archetypes[_index];
public readonly ref Archetype Current => ref _value;

public bool MoveNext()
{
while (++_index < _archetypes.Length)
while (true)
{
var arch = _archetypes[_index];
if (arch.Count > 0)
_value = ref Unsafe.IsNullRef(ref _value) ? ref _first : ref Unsafe.Add(ref _value, 1);
if (!Unsafe.IsAddressLessThan(ref _value, ref _last))
break;

if (_value.Count > 0)
return true;
}

return false;
}

public void Reset() => _index = -1;

public readonly QueryInternal GetEnumerator() => this;
}

Expand Down

0 comments on commit ad50586

Please sign in to comment.