mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-03 18:39:19 +00:00 
			
		
		
		
	Merge Archetype with its node interface
This commit is contained in:
		
							parent
							
								
									59abdcbe6f
								
							
						
					
					
						commit
						1dd108401f
					
				
					 2 changed files with 22 additions and 20 deletions
				
			
		| 
						 | 
					@ -16,9 +16,9 @@ type Map<K, V> = { [K]: V }
 | 
				
			||||||
type GraphEdge = {
 | 
					type GraphEdge = {
 | 
				
			||||||
	from: Archetype,
 | 
						from: Archetype,
 | 
				
			||||||
	to: Archetype?,
 | 
						to: Archetype?,
 | 
				
			||||||
	prev: GraphEdge?,
 | 
					 | 
				
			||||||
	next: GraphEdge?,
 | 
					 | 
				
			||||||
	id: number,
 | 
						id: number,
 | 
				
			||||||
 | 
						prev: GraphEdge?,
 | 
				
			||||||
 | 
						next: GraphEdge?
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type GraphEdges = Map<i53, GraphEdge>
 | 
					type GraphEdges = Map<i53, GraphEdge>
 | 
				
			||||||
| 
						 | 
					@ -31,13 +31,13 @@ type GraphNode = {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type Archetype = {
 | 
					export type Archetype = {
 | 
				
			||||||
	id: number,
 | 
						id: number,
 | 
				
			||||||
	node: GraphNode,
 | 
					 | 
				
			||||||
	types: Ty,
 | 
						types: Ty,
 | 
				
			||||||
	type: string,
 | 
						type: string,
 | 
				
			||||||
	entities: { number },
 | 
						entities: { number },
 | 
				
			||||||
	columns: { Column },
 | 
						columns: { Column },
 | 
				
			||||||
	records: { ArchetypeRecord },
 | 
						records: { ArchetypeRecord },
 | 
				
			||||||
}
 | 
					} & GraphNode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Record = {
 | 
					type Record = {
 | 
				
			||||||
	archetype: Archetype,
 | 
						archetype: Archetype,
 | 
				
			||||||
	row: number,
 | 
						row: number,
 | 
				
			||||||
| 
						 | 
					@ -576,12 +576,15 @@ local function archetype_create(world: World, types: { i24 }, ty, prev: i53?): A
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local archetype: Archetype = {
 | 
						local archetype: Archetype = {
 | 
				
			||||||
		columns = columns,
 | 
							columns = columns,
 | 
				
			||||||
		node = { add = {}, remove = {}, refs = {} :: GraphEdge },
 | 
					 | 
				
			||||||
		entities = {},
 | 
							entities = {},
 | 
				
			||||||
		id = archetype_id,
 | 
							id = archetype_id,
 | 
				
			||||||
		records = records,
 | 
							records = records,
 | 
				
			||||||
		type = ty,
 | 
							type = ty,
 | 
				
			||||||
		types = types,
 | 
							types = types,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						 	add = {},
 | 
				
			||||||
 | 
							remove = {},
 | 
				
			||||||
 | 
							refs = {} :: GraphEdge,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	world.archetypeIndex[ty] = archetype
 | 
						world.archetypeIndex[ty] = archetype
 | 
				
			||||||
| 
						 | 
					@ -671,11 +674,11 @@ local function archetype_ensure_edge(world, edges: GraphEdges, id): GraphEdge
 | 
				
			||||||
	return edge
 | 
						return edge
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function init_edge_for_add(world, archetype, edge: GraphEdge, id, to)
 | 
					local function init_edge_for_add(world, archetype: Archetype, edge: GraphEdge, id, to: Archetype)
 | 
				
			||||||
	archetype_init_edge(archetype, edge, id, to)
 | 
						archetype_init_edge(archetype, edge, id, to)
 | 
				
			||||||
	archetype_ensure_edge(world, archetype.node.add, id)
 | 
						archetype_ensure_edge(world, archetype.add, id)
 | 
				
			||||||
	if archetype ~= to then
 | 
						if archetype ~= to then
 | 
				
			||||||
		local to_refs = to.node.refs
 | 
							local to_refs = to.refs
 | 
				
			||||||
		local next_edge = to_refs.next
 | 
							local next_edge = to_refs.next
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		to_refs.next = edge
 | 
							to_refs.next = edge
 | 
				
			||||||
| 
						 | 
					@ -690,9 +693,9 @@ end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function init_edge_for_remove(world: World, archetype: Archetype, edge: GraphEdge, id: number, to: Archetype)
 | 
					local function init_edge_for_remove(world: World, archetype: Archetype, edge: GraphEdge, id: number, to: Archetype)
 | 
				
			||||||
	archetype_init_edge(archetype, edge, id, to)
 | 
						archetype_init_edge(archetype, edge, id, to)
 | 
				
			||||||
	archetype_ensure_edge(world, archetype.node.remove, id)
 | 
						archetype_ensure_edge(world, archetype.remove, id)
 | 
				
			||||||
	if archetype ~= to then
 | 
						if archetype ~= to then
 | 
				
			||||||
		local to_refs = to.node.refs
 | 
							local to_refs = to.refs
 | 
				
			||||||
		local prev_edge = to_refs.prev
 | 
							local prev_edge = to_refs.prev
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		to_refs.prev = edge
 | 
							to_refs.prev = edge
 | 
				
			||||||
| 
						 | 
					@ -719,7 +722,7 @@ end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function archetype_traverse_add(world: World, id: i53, from: Archetype?): Archetype
 | 
					local function archetype_traverse_add(world: World, id: i53, from: Archetype?): Archetype
 | 
				
			||||||
	from = from or world.ROOT_ARCHETYPE
 | 
						from = from or world.ROOT_ARCHETYPE
 | 
				
			||||||
	local edge = archetype_ensure_edge(world, from.node.add, id)
 | 
						local edge = archetype_ensure_edge(world, from.add, id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local to = edge.to
 | 
						local to = edge.to
 | 
				
			||||||
	if not to then
 | 
						if not to then
 | 
				
			||||||
| 
						 | 
					@ -732,7 +735,7 @@ end
 | 
				
			||||||
local function archetype_traverse_remove(world: World, id: i53, from: Archetype): Archetype
 | 
					local function archetype_traverse_remove(world: World, id: i53, from: Archetype): Archetype
 | 
				
			||||||
	from = from or world.ROOT_ARCHETYPE
 | 
						from = from or world.ROOT_ARCHETYPE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local edge = archetype_ensure_edge(world, from.node.remove, id)
 | 
						local edge = archetype_ensure_edge(world, from.remove, id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local to = edge.to
 | 
						local to = edge.to
 | 
				
			||||||
	if not to then
 | 
						if not to then
 | 
				
			||||||
| 
						 | 
					@ -961,10 +964,9 @@ local function archetype_remove_edge(edges: Map<i53, GraphEdge>, id: i53, edge:
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function archetype_clear_edges(archetype: Archetype)
 | 
					local function archetype_clear_edges(archetype: Archetype)
 | 
				
			||||||
	local node: GraphNode = archetype.node
 | 
						local add: GraphEdges = archetype.add
 | 
				
			||||||
	local add: GraphEdges = node.add
 | 
						local remove: GraphEdges = archetype.remove
 | 
				
			||||||
	local remove: GraphEdges = node.remove
 | 
						local node_refs = archetype.refs
 | 
				
			||||||
	local node_refs: GraphEdge = node.refs
 | 
					 | 
				
			||||||
	for id, edge in add do
 | 
						for id, edge in add do
 | 
				
			||||||
		archetype_disconnect_edge(edge)
 | 
							archetype_disconnect_edge(edge)
 | 
				
			||||||
		add[id] = nil :: any
 | 
							add[id] = nil :: any
 | 
				
			||||||
| 
						 | 
					@ -976,9 +978,9 @@ local function archetype_clear_edges(archetype: Archetype)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local cur = node_refs.next
 | 
						local cur = node_refs.next
 | 
				
			||||||
	while cur do
 | 
						while cur do
 | 
				
			||||||
		local edge: GraphEdge = cur
 | 
							local edge = cur :: GraphEdge
 | 
				
			||||||
		local next_edge = edge.next
 | 
							local next_edge = edge.next
 | 
				
			||||||
		archetype_remove_edge(edge.from.node.add, edge.id, edge)
 | 
							archetype_remove_edge(edge.from.add, edge.id, edge)
 | 
				
			||||||
		cur = next_edge
 | 
							cur = next_edge
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -986,7 +988,7 @@ local function archetype_clear_edges(archetype: Archetype)
 | 
				
			||||||
	while cur do
 | 
						while cur do
 | 
				
			||||||
		local edge: GraphEdge = cur
 | 
							local edge: GraphEdge = cur
 | 
				
			||||||
		local next_edge = edge.prev
 | 
							local next_edge = edge.prev
 | 
				
			||||||
		archetype_remove_edge(edge.from.node.remove, edge.id, edge)
 | 
							archetype_remove_edge(edge.from.remove, edge.id, edge)
 | 
				
			||||||
		cur = next_edge
 | 
							cur = next_edge
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -98,7 +98,7 @@ TEST("archetype", function()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local a1 = archetype_traverse_add(world, c1, nil)
 | 
						local a1 = archetype_traverse_add(world, c1, nil)
 | 
				
			||||||
	local a2 = archetype_traverse_remove(world, c1, a1)
 | 
						local a2 = archetype_traverse_remove(world, c1, a1)
 | 
				
			||||||
	CHECK(root.node.add[c1].to == a1)
 | 
						CHECK(root.add[c1].to == a1)
 | 
				
			||||||
	CHECK(root == a2)
 | 
						CHECK(root == a2)
 | 
				
			||||||
end)
 | 
					end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue