Skip to content

Commit

Permalink
Iter empty
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Sep 7, 2024
1 parent 06e5314 commit 3c7231b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,55 @@ public void Each(QueryFilterDelegateWithEntity fn)

World.EndDeferred();
}

public RefEnumerator Iter() => new(this.GetEnumerator());
}


[System.Runtime.CompilerServices.SkipLocalsInit]
public ref struct RefEnumerator
{
private QueryInternal _queryIt;

private QueryChunkIterator _chunkIt;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal RefEnumerator(QueryInternal queryIt)
{
_queryIt = queryIt;
}

[System.Diagnostics.CodeAnalysis.UnscopedRef]
public ReadOnlySpan<EntityView> Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ref var chunk = ref _chunkIt.Current;
return chunk.Entities.AsSpan(0, chunk.Count);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
while (true)
{
if (_chunkIt.MoveNext())
return true;
if (_queryIt.MoveNext())
{
_chunkIt = new QueryChunkIterator(_queryIt.Current.Chunks);
continue;
}

return false;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly RefEnumerator GetEnumerator() => this;
}

public ref struct QueryInternal
{
Expand Down

0 comments on commit 3c7231b

Please sign in to comment.