From c694660fb3c299f33e97b2ab00ccdee53863fa6c Mon Sep 17 00:00:00 2001 From: Quillraven Date: Mon, 23 Dec 2024 00:52:11 +0100 Subject: [PATCH] add count with predicate to family --- src/commonMain/kotlin/com/github/quillraven/fleks/family.kt | 5 +++++ .../com/github/quillraven/fleks/FamilyBagFunctionsTest.kt | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/commonMain/kotlin/com/github/quillraven/fleks/family.kt b/src/commonMain/kotlin/com/github/quillraven/fleks/family.kt index a0e6a5b..737b2c2 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/family.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/family.kt @@ -254,6 +254,11 @@ data class Family( */ fun none(predicate: (Entity) -> Boolean): Boolean = mutableEntities.none(predicate) + /** + * Returns the number of [entities][Entity] matching the given [predicate]. + */ + fun count(predicate: (Entity) -> Boolean): Int = mutableEntities.count(predicate) + /** * Returns a [Map] containing key-value pairs provided by the [transform] function applied to * each [entity][Entity] of the family. diff --git a/src/commonTest/kotlin/com/github/quillraven/fleks/FamilyBagFunctionsTest.kt b/src/commonTest/kotlin/com/github/quillraven/fleks/FamilyBagFunctionsTest.kt index 1d4fc48..1f2ce4d 100644 --- a/src/commonTest/kotlin/com/github/quillraven/fleks/FamilyBagFunctionsTest.kt +++ b/src/commonTest/kotlin/com/github/quillraven/fleks/FamilyBagFunctionsTest.kt @@ -48,6 +48,11 @@ class FamilyBagFunctionsTest { assertFalse(testFamily.none { it.id == 0 }) } + @Test + fun testCount() { + assertEquals(1, testFamily.count { it.id == 0 }) + } + @Test fun testAssociate() { val expected = mapOf(testEntity1 to 0, testEntity2 to 1)