add replace method

This commit is contained in:
Ukendio 2024-07-07 00:08:47 +02:00
parent ea89be96c2
commit 5e3f0485d3
2 changed files with 94 additions and 42 deletions

View file

@ -8,7 +8,7 @@ local function TITLE(title: string)
print(testkit.color.white(title))
end
local jecs = require("../lib/init")
local jecs = require("@jecs")
local mirror = require("../mirror/init")
type i53 = number

View file

@ -662,16 +662,24 @@ local function noop(_self: Query, ...): () -> ()
end
local EmptyQuery = {
__iter = noop,
without = noop,
__iter = iterNoop,
next = noop,
replace = noop,
without = function()
return EmptyQuery
end
}
EmptyQuery.__index = EmptyQuery
setmetatable(EmptyQuery, EmptyQuery)
export type Query = typeof(EmptyQuery)
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
local function replaceColumns(row, columns, ...)
for i, column in columns do
column[row] = select(i, ...)
end
end
local function preparedQuery(compatibleArchetypes: { Archetype },
components: { i53? }, indices: { { number } })
@ -783,20 +791,64 @@ local function preparedQuery(compatibleArchetypes: { Archetype },
return self
end
local it = {
__iter = function()
local function iter()
lastArchetype = 1
archetype = compatibleArchetypes[1]
entities = archetype.entities
i = #entities
return queryNext
end,
end
local function replace()
for _, compatibleArchetype in compatibleArchetypes do
local archetype = compatibleArchetype.archetype
local tr = compatibleArchetype.indices
local columns = archetype.columns
for row in archetype.entities do
if queryLength == 1 then
local a = columns[tr[1]]
local pa = fn(a[row])
a[row] = pa
elseif queryLength == 2 then
local a = columns[tr[1]]
local b = columns[tr[2]]
a[row], b[row] = fn(a[row], b[row])
elseif queryLength == 3 then
local a = columns[tr[1]]
local b = columns[tr[2]]
local c = columns[tr[3]]
a[row], b[row], c[row] = fn(a[row], b[row], c[row])
elseif queryLength == 4 then
local a = columns[tr[1]]
local b = columns[tr[2]]
local c = columns[tr[3]]
local d = columns[tr[4]]
a[row], b[row], c[row], d[row] = fn(
a[row], b[row], c[row], d[row])
else
for i = 1, queryLength do
queryOutput[i] = columns[tr[i]][row]
end
replace(row, columns, fn(unpack(queryOutput)))
end
end
end
end
local it = {
__iter = iter,
next = queryNext,
without = without
without = without,
replace = replace
}
return setmetatable(it, it) :: any
return it
end
local function query(world: World, ...: number): Query