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

View file

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