Iterate backwards

This commit is contained in:
Ukendio 2024-07-05 14:52:58 +02:00
parent afc0c949f0
commit 086f7b5b40
2 changed files with 43 additions and 32 deletions

View file

@ -184,13 +184,9 @@ local function nextEntityId(entityIndex: EntityIndex, index: i24): i53
return id
end
local function transitionArchetype(
entityIndex: EntityIndex,
to: Archetype,
destinationRow: i24,
from: Archetype,
sourceRow: i24
)
local function transitionArchetype(entityIndex: EntityIndex, to: Archetype,
destinationRow: i24, from: Archetype, sourceRow: i24)
local columns = from.columns
local sourceEntities = from.entities
local destinationEntities = to.entities
@ -690,11 +686,11 @@ local function preparedQuery(compatibleArchetypes: { Archetype },
local queryOutput = {}
local i = 1
local entities = archetype.entities
local i = #entities
local function queryNext(): ...any
local entityId = archetype.entities[i]
local entityId = entities[i]
while entityId == nil do
lastArchetype += 1
archetype = compatibleArchetypes[lastArchetype]
@ -703,12 +699,13 @@ local function preparedQuery(compatibleArchetypes: { Archetype },
return
end
i = 1
entityId = archetype.entities[1]
entities = archetype.entities
i = #entities
entityId = entities[i]
end
local row = i
i+=1
i-=1
local columns = archetype.columns
local tr = indices[lastArchetype]
@ -788,9 +785,10 @@ local function preparedQuery(compatibleArchetypes: { Archetype },
local it = {
__iter = function()
i = 1
lastArchetype = 1
archetype = compatibleArchetypes[1]
entities = archetype.entities
i = #entities
return queryNext
end,

View file

@ -25,30 +25,23 @@ local N = 10
type World = jecs.WorldShim
TEST("world", function()
do CASE("should be iterable")
do CASE("should find every component id")
local world = jecs.World.new() :: World
local A = world:component()
local B = world:component()
local eA = world:entity()
world:set(eA, A, true)
local eB = world:entity()
world:set(eB, B, true)
local eAB = world:entity()
world:set(eAB, A, true)
world:set(eAB, B, true)
world:entity()
world:entity()
world:entity()
for id, data in world do
if id == eA then
CHECK(data[A] == true)
CHECK(data[B] == nil)
elseif id == eB then
CHECK(data[A] == nil)
CHECK(data[B] == true)
elseif id == eAB then
CHECK(data[A] == true)
CHECK(data[B] == true)
local count = 0
for componentId in world:query(jecs.Component) do
if componentId ~= A and componentId ~= B then
error("found entity")
end
count += 1
end
CHECK(count == 2)
end
do CASE("should remove its components")
@ -395,6 +388,26 @@ TEST("world", function()
end
CHECK(count == 2)
end
do CASE "infinite"
local world = jecs.World.new()
local Name = world:component()
for i = 1, 5 do
local e = world:entity()
world:set(e, Name, i)
end
local count = 0
for _ in world:query(Name) do
count += 1
print(count)
local e = world:entity()
world:set(e, Name, e)
end
print(count)
CHECK(count == 5)
end
end)
FINISH()