diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/IdList.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/IdList.kt index 5a67166d8..1fa7a9fb4 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/IdList.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/IdList.kt @@ -1,9 +1,11 @@ package com.mineinabyss.geary.datatypes +import com.mineinabyss.geary.helpers.toGeary + private const val initialSize: Int = 16 private const val growFactor: Int = 2 -class IdList : Iterable { +class IdList { var backingArr = ULongArray(initialSize) var size = 0 val lastIndex get() = size - 1 @@ -25,7 +27,7 @@ class IdList : Iterable { return backingArr[--size] } - override fun iterator(): Iterator { - return backingArr.iterator() + fun getEntities(): Sequence { + return backingArr.asSequence().take(size).map { it.toGeary() } } } diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt index 740796e5a..ca366251c 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt @@ -26,7 +26,7 @@ data class Archetype( private val eventRunner get() = archetypes.eventRunner - val entities: List get() = ids.map { it.toGeary() } + val entities: Sequence get() = ids.getEntities() /** The entity ids in this archetype. Indices are the same as [componentData]'s sub-lists. */ private val ids: IdList = IdList() diff --git a/geary-core/src/jvmTest/kotlin/com/mineinabyss/geary/datatypes/GearyEntityTests.kt b/geary-core/src/jvmTest/kotlin/com/mineinabyss/geary/datatypes/GearyEntityTests.kt index 812f35726..09a9d9c1b 100644 --- a/geary-core/src/jvmTest/kotlin/com/mineinabyss/geary/datatypes/GearyEntityTests.kt +++ b/geary-core/src/jvmTest/kotlin/com/mineinabyss/geary/datatypes/GearyEntityTests.kt @@ -2,9 +2,7 @@ package com.mineinabyss.geary.datatypes import com.mineinabyss.geary.components.relations.InstanceOf import com.mineinabyss.geary.components.relations.Persists -import com.mineinabyss.geary.helpers.componentId -import com.mineinabyss.geary.helpers.entity -import com.mineinabyss.geary.helpers.getArchetype +import com.mineinabyss.geary.helpers.* import com.mineinabyss.geary.helpers.tests.GearyTest import com.mineinabyss.geary.modules.archetypes import com.mineinabyss.geary.systems.accessors.RelationWithData @@ -136,6 +134,20 @@ internal class GearyEntityTests : GearyTest() { entity.getAllPersisting().shouldContainExactly("Test") } + @Nested + inner class ChildTest { + @Test + fun `should handle single parent child relation correctly when using addParent`() { + val parent = entity() + val child = entity { + addParent(parent) + } + + parent.children.shouldContainExactly(child) + child.parents.shouldContainExactly(parent) + } + } + @Nested inner class RelationTest { @Test