Skip to content

Commit 0256c76

Browse files
committed
Drainless iterators
1 parent 0ff2348 commit 0256c76

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

lib/init.luau

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ export type Query = typeof(EmptyQuery)
640640

641641
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
642642

643-
local function PreparedQuery(
644-
compatibleArchetypes: { Archetype } , components: { i53 }, indices: { [number]: number })
643+
local function preparedQuery(compatibleArchetypes: { Archetype },
644+
components: { i53? }, indices: { { number } })
645645

646646
local queryLength = #components
647647

@@ -652,7 +652,6 @@ local function PreparedQuery(
652652
return EmptyQuery
653653
end
654654

655-
656655
local queryOutput = {}
657656

658657
local i = 1
@@ -746,48 +745,47 @@ local function PreparedQuery(
746745
end
747746
end
748747

749-
lastArchetype, archetype = next(compatibleArchetypes)
750-
if not lastArchetype then
751-
return EmptyQuery
752-
end
753-
754748
return self
755749
end
756750

757-
local preparedQuery = {
751+
local query = {
758752
__iter = function()
753+
i = 1
754+
lastArchetype = 1
755+
archetype = compatibleArchetypes[1]
756+
759757
return queryNext
760758
end,
761759
next = queryNext,
762760
without = without
763761
}
764762

765-
return (setmetatable(preparedQuery, preparedQuery) :: any):: Query
763+
return setmetatable(query, query) :: any
766764
end
767765

768-
function World.query(world: World, ...: any): Query
766+
function World.query(world: World, ...: number?): Query
769767
-- breaking?
770768
if (...) == nil then
771769
error("Missing components")
772770
end
773771

774-
local columns = {}
775-
local compatibleArchetypes: { CompatibleArchetype } = {}
772+
local indices: { { number } } = {}
773+
local compatibleArchetypes: { Archetype } = {}
776774
local length = 0
777775

778-
local components = { ... } :: any
779-
local archetypes = world.archetypes
776+
local components: { number? } = { ... }
777+
local archetypes: { Archetype } = world.archetypes :: any
780778

781779
local firstArchetypeMap: ArchetypeMap
782780
local componentIndex = world.componentIndex
783781

784782
for _, componentId in components do
785-
local map = componentIndex[componentId]
783+
local map: ArchetypeMap = componentIndex[componentId] :: any
786784
if not map then
787785
return EmptyQuery
788786
end
789787

790-
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
788+
if (firstArchetypeMap :: any) == nil or firstArchetypeMap.size < map.size then
791789
firstArchetypeMap = map
792790
end
793791
end
@@ -796,7 +794,7 @@ function World.query(world: World, ...: any): Query
796794
local archetype = archetypes[id]
797795
local archetypeRecords = archetype.records
798796

799-
local records = {}
797+
local records: { number } = {}
800798
local skip = false
801799

802800
for i, componentId in components do
@@ -815,25 +813,27 @@ function World.query(world: World, ...: any): Query
815813

816814
length += 1
817815
compatibleArchetypes[length] = archetype
818-
columns[length] = records
816+
indices[length] = records
819817
end
820818

821-
return PreparedQuery(compatibleArchetypes, components, columns)
819+
return preparedQuery(compatibleArchetypes, components, indices)
822820
end
823821

824822
type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53)
825823

826824
function World.__iter(world: World): WorldIterator
827-
local dense = world.entityIndex.dense
828-
local sparse = world.entityIndex.sparse
825+
local entityIndex = world.entityIndex
826+
local dense = entityIndex.dense
827+
local sparse = entityIndex.sparse
829828
local last
830829

831830
-- new solver doesnt like the world iterator type even tho its correct
832831
-- so any cast here i come
832+
local i = 0
833833
local function iterator()
834-
local lastEntity: number?, entityId: number = next(dense, last)
835-
if not lastEntity then
836-
-- ignore type error
834+
i+=1
835+
local entityId = dense[i]
836+
if not entityId then
837837
return
838838
end
839839

0 commit comments

Comments
 (0)