Skip to content

Commit

Permalink
Archetype: Store relations as EntityType instead of List
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Dec 9, 2023
1 parent 243908c commit 04799b2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 25 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.mineinabyss.geary.datatypes.maps

import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.datatypes.*
import com.mineinabyss.geary.datatypes.family.Family
import com.mineinabyss.geary.helpers.hasRelationKind
import com.mineinabyss.geary.helpers.hasRelationTarget
import com.mineinabyss.geary.modules.geary

/**
* A map of [ComponentId]s to Arrays of objects with the ability to make fast queries based on component IDs.
Expand Down Expand Up @@ -32,7 +32,7 @@ internal class Family2ObjectArrayMap<T> {

// See componentMap definition for relations
if (id.isRelation()) {
val relation = id.toRelation()!!
val relation = Relation.of(id)
set(Relation.of(relation.kind, geary.components.any).id)
set(Relation.of(geary.components.any, relation.target).id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import com.mineinabyss.geary.systems.accessors.RelationWithData
* An example use case: If a query matches an archetype, it will also match all entities inside which
* gives a large performance boost to system iteration.
*/
data class Archetype(
data class Archetype internal constructor(
val type: EntityType,
val id: Int
) {
private val records get() = archetypes.records
private val archetypeProvider get() = archetypes.archetypeProvider
private val eventRunner get() = archetypes.eventRunner


val entities: Sequence<Entity> get() = ids.getEntities()

/** The entity ids in this archetype. Indices are the same as [componentData]'s sub-lists. */
Expand All @@ -47,15 +46,15 @@ data class Archetype(
/** Edges to other archetypes where a single component has been removed. */
internal val componentRemoveEdges = CompId2ArchetypeMap()

internal val relations = type.inner.mapNotNull { it.toRelation() }
internal val relationsWithData = relations.filter { it.id.holdsData() }
internal val relations: EntityType = type.filter { it.isRelation() }
internal val relationsWithData: EntityType = relations.filter { it.holdsData() }

fun getRelationsByTarget(target: EntityId): List<Relation> {
return relations.filter { it.target.toLong() == target.toLong() }
return relations.filter { Relation.of(it).target.toLong() == target.toLong() }.map { Relation.of(it) }
}

fun getRelationsByKind(kind: ComponentId): List<Relation> {
return relations.filter { it.kind.toLong() == kind.toLong() }
return relations.filter { Relation.of(it).kind.toLong() == kind.toLong() }.map { Relation.of(it) }
}

/** The amount of entities stored in this archetype. */
Expand Down Expand Up @@ -342,7 +341,7 @@ data class Archetype(
specificKind && specificTarget -> listOf(Relation.of(kind, target))
specificTarget -> getRelationsByTarget(target)
specificKind -> getRelationsByKind(kind)
else -> relations
else -> relations.map { Relation.of(it) }
}.run { //TODO this technically doesnt need to run when specificKind is set
if (kind.hasRole(HOLDS_DATA)) filter { it.hasRole(HOLDS_DATA) } else this
}.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ArchetypeReadOperations : EntityReadOperations {
override fun getComponentsFor(entity: Entity): Array<Component> {
val (archetype, row) = records[entity]
return archetype.getComponents(row).also { array ->
for (relation in archetype.relationsWithData) {
val i = archetype.indexOf(relation.id)
array[i] = RelationWithData(array[i], null, relation)
archetype.relationsWithData.forEach { relation ->
val i = archetype.indexOf(relation)
array[i] = RelationWithData(array[i], null, Relation.of(relation))
}
}
}
Expand Down

0 comments on commit 04799b2

Please sign in to comment.