From 1bdf87804d2d8e4f028d7c647f97e8dc083df445 Mon Sep 17 00:00:00 2001 From: Quillraven Date: Sun, 24 Nov 2024 23:43:57 +0100 Subject: [PATCH] add entityBagOf and mutableEntityBagOf functions --- .../quillraven/fleks/collection/entityBag.kt | 18 +++++++++++++++ .../fleks/collection/EntityBagTest.kt | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt b/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt index b1cf648..97b4c6c 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt @@ -7,6 +7,24 @@ import kotlin.math.max import kotlin.math.min import kotlin.random.Random +/** + * Returns a new read-only [EntityBag] of given [entities]. + */ +fun entityBagOf(vararg entities: Entity): EntityBag { + return MutableEntityBag(entities.size).apply { + entities.forEach { this += it } + } +} + +/** + * Returns a new [MutableEntityBag] with the given [entities]. + */ +fun mutableEntityBagOf(vararg entities: Entity): MutableEntityBag { + return MutableEntityBag(entities.size).apply { + entities.forEach { this += it } + } +} + interface EntityBag { /** * Returns the size of the [EntityBag]. diff --git a/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt b/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt index a1689c7..676440a 100644 --- a/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt +++ b/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt @@ -626,4 +626,26 @@ class EntityBagTest { assertEquals(bagOf(testEntity1, testEntity2), testBag.take(2)) assertEquals(bagOf(testEntity1, testEntity2), testBag.take(3)) } + + @Test + fun testEntityBagOf() { + var bag = entityBagOf(testEntity1, testEntity2) + assertEquals(2, bag.size) + assertTrue { testEntity1 in bag } + assertTrue { testEntity2 in bag } + + bag = entityBagOf() + assertEquals(0, bag.size) + } + + @Test + fun testMutableEntityBagOf() { + var bag = mutableEntityBagOf(testEntity1, testEntity2) + assertEquals(2, bag.size) + assertTrue { testEntity1 in bag } + assertTrue { testEntity2 in bag } + + bag = mutableEntityBagOf() + assertEquals(0, bag.size) + } }