From 1b0f53e7caa27aabcf4bb4136a3211de6d0f9419 Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Tue, 13 Feb 2024 18:42:05 -0500 Subject: [PATCH] Chore: Some todos about matching entities as sequence --- .../com/mineinabyss/geary/datatypes/EntityType.kt | 2 -- .../com/mineinabyss/geary/engine/QueryManager.kt | 8 ++++++++ .../geary/engine/archetypes/ArchetypeQueryManager.kt | 11 +++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/EntityType.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/EntityType.kt index a908c3bba..c32773ad3 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/EntityType.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/EntityType.kt @@ -86,8 +86,6 @@ class EntityType private constructor( return EntityType(arr) } - //TODO intersection and union - override fun toString(): String = inner.joinToString(", ", prefix = "[", postfix = "]") { it.readableString() } } diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/QueryManager.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/QueryManager.kt index d7d73108e..6bc9b4260 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/QueryManager.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/QueryManager.kt @@ -9,5 +9,13 @@ interface QueryManager { fun trackEventListener(listener: Listener) fun trackQuery(query: GearyQuery) + /** Returns a list of entities matching the given family. */ fun getEntitiesMatching(family: Family): List + + /** + * Returns a sequence of entities matching the given family. + * This should be faster than [getEntitiesMatching] but will depend on impl. + * In an archetypal engine, this gets all matching archetypes first, then maps them to entities as a sequence. + */ + fun getEntitiesMatchingAsSequence(family: Family): Sequence } diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/ArchetypeQueryManager.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/ArchetypeQueryManager.kt index 2a525a0ca..a3df536c3 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/ArchetypeQueryManager.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/ArchetypeQueryManager.kt @@ -89,10 +89,13 @@ class ArchetypeQueryManager : QueryManager { return archetypes.match(family) } - //TODO convert to Sequence override fun getEntitiesMatching(family: Family): List { - return getArchetypesMatching(family).flatMap { arc -> - arc.entities - } + return getArchetypesMatching(family).flatMap(Archetype::entities) + } + + override fun getEntitiesMatchingAsSequence(family: Family): Sequence { + return getArchetypesMatching(family) + .asSequence() + .flatMap(Archetype::entities) } }