Inline getSmallestMap

This commit is contained in:
Ukendio 2024-04-30 16:35:40 +02:00
parent cc13870fd9
commit b100cc2c2a
2 changed files with 18 additions and 47 deletions

View file

@ -4,11 +4,8 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local rgb = require(ReplicatedStorage.rgb)
local Matter = require(ReplicatedStorage.DevPackages.Matter)
local Rewrite = require(ReplicatedStorage.rewrite)
local ecr = require(ReplicatedStorage.DevPackages.ecr)
local newWorld = Matter.World.new()
local world = Rewrite.World.new()
local component = Rewrite.component
local jecs = require(ReplicatedStorage.Lib)
local mirror = require(ReplicatedStorage.mirror)
@ -33,15 +30,6 @@ local B6 = ecr.component()
local B7 = ecr.component()
local B8 = ecr.component()
local C1 = component()
local C2 = component()
local C3 = component()
local C4 = component()
local C5 = component()
local C6 = component()
local C7 = component()
local C8 = component()
local D1 = ecs:entity()
local D2 = ecs:entity()
local D3 = ecs:entity()
@ -78,13 +66,11 @@ for i = 1, N do
local combination = ""
local n = newWorld:spawn()
local entity = ecs:entity()
local e = world:spawn()
local m = mcs:entity()
if flip() then
combination ..= "B"
registry2:set(id, B2, {value = true})
world:insert(e, C2({ value = true}))
ecs:set(entity, D2, { value = true})
mcs:set(m, E2, { value = 2})
newWorld:insert(n, A2({value = true}))
@ -92,7 +78,6 @@ for i = 1, N do
if flip() then
combination ..= "C"
registry2:set(id, B3, {value = true})
world:insert(e, C3({ value = true}))
ecs:set(entity, D3, { value = true})
mcs:set(m, E3, { value = 2})
newWorld:insert(n, A3({value = true}))
@ -100,7 +85,6 @@ for i = 1, N do
if flip() then
combination ..= "D"
registry2:set(id, B4, {value = true})
world:insert(e, C4({ value = true}))
ecs:set(entity, D4, { value = true})
mcs:set(m, E4, { value = 2})
@ -109,7 +93,6 @@ for i = 1, N do
if flip() then
combination ..= "E"
registry2:set(id, B5, {value = true})
world:insert(e, C5({value = true}))
ecs:set(entity, D5, { value = true})
mcs:set(m, E5, { value = 2})
@ -118,26 +101,20 @@ for i = 1, N do
if flip() then
combination ..= "F"
registry2:set(id, B6, {value = true})
world:insert(e, C6({value = true}))
ecs:set(entity, D6, { value = true})
mcs:set(m, E6, { value = 2})
newWorld:insert(n, A6({value = true}))
end
if flip() then
combination ..= "G"
registry2:set(id, B7, {value = true})
world:insert(e, C7{ value = true})
ecs:set(entity, D7, { value = true})
mcs:set(m, E7, { value = 2})
newWorld:insert(n, A7({value = true}))
end
if flip() then
combination ..= "H"
registry2:set(id, B8, {value = true})
world:insert(e, C8{ value = true})
newWorld:insert(n, A8({value = true}))
ecs:set(entity, D8, { value = true})
mcs:set(m, E8, { value = 2})
@ -148,7 +125,6 @@ for i = 1, N do
combination = "A" .. combination
common += 1
registry2:set(id, B1, {value = true})
world:insert(e, C1{ value = true})
ecs:set(entity, D1, { value = true})
newWorld:insert(n, A1({value = true}))
mcs:set(m, E1, { value = 2})
@ -194,7 +170,7 @@ return {
end,
Functions = {
Mater = function()
Matter = function()
local matched = 0
for entityId, firstComponent in newWorld:query(A1, A4, A6, A8) do
matched += 1

View file

@ -350,36 +350,31 @@ local function noop(): any
end
end
local NO_POOLS = "No components provided"
local function getSmallestMap(componentIndex, components)
local s: any
for i, componentId in components do
local map = componentIndex[componentId]
if not map then
error(tostring(componentId).." has not been added to any entity")
end
if s == nil or map.size < s.size then
s = map
end
end
return if s then s.sparse else error(NO_POOLS)
end
function World.query(world: World, ...: i53): any
local compatibleArchetypes = {}
local components = { ... }
local archetypes = world.archetypes
local queryLength = #components
local firstArchetypeMap = getSmallestMap(world.componentIndex, components)
if not firstArchetypeMap then
return noop()
if queryLength == 0 then
error("Missing components")
end
for id in firstArchetypeMap do
local firstArchetypeMap
local componentIndex = world.componentIndex
for i, componentId in components do
local map = componentIndex[componentId]
if not map then
error(tostring(componentId).." has not been added to an entity")
end
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
firstArchetypeMap = map
end
end
for id in firstArchetypeMap.sparse do
local archetype = archetypes[id]
local columns = archetype.columns
local archetypeRecords = archetype.records