From 3eb662851bfc829ff6696f07d14f89c070c6dcf5 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 31 Oct 2025 18:36:05 -0400 Subject: [PATCH] Adds a flatten to set utility --- .../amethyst/ui/feeds/ChannelFeedContentState.kt | 3 ++- .../vitorpamplona/amethyst/ui/feeds/FeedContentState.kt | 3 ++- .../screen/loggedIn/notifications/CardFeedContentState.kt | 3 ++- .../apps/UserProfileAppRecommendationsFeedFilter.kt | 3 ++- .../quartz/benchmark/LargeDBInsertBenchmark.kt | 5 +++-- .../quartz/nip01Core/tags/hashtags/MipMapHashTags.kt | 6 ++++-- .../kotlin/com/vitorpamplona/quartz/utils/IterableExt.kt | 8 ++++++++ 7 files changed, 23 insertions(+), 8 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/ChannelFeedContentState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/ChannelFeedContentState.kt index f615ccbe7..8f7cedc71 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/ChannelFeedContentState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/ChannelFeedContentState.kt @@ -31,6 +31,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications.equalImmutabl import com.vitorpamplona.ammolite.relays.BundledInsert import com.vitorpamplona.ammolite.relays.BundledUpdate import com.vitorpamplona.quartz.utils.Log +import com.vitorpamplona.quartz.utils.flattenToSet import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.CoroutineScope @@ -160,7 +161,7 @@ class ChannelFeedContentState( } fun invalidateInsertData(newItems: Set) { - bundlerInsert.invalidateList(newItems) { refreshFromOldState(it.flatten().toSet()) } + bundlerInsert.invalidateList(newItems) { refreshFromOldState(it.flattenToSet()) } } fun updateFeedWith(newNotes: Set) { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt index 5aad2eff9..b1a12702a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt @@ -32,6 +32,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications.equalImmutabl import com.vitorpamplona.ammolite.relays.BasicBundledInsert import com.vitorpamplona.ammolite.relays.BasicBundledUpdate import com.vitorpamplona.quartz.nip09Deletions.DeletionEvent +import com.vitorpamplona.quartz.utils.flattenToSet import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.CoroutineScope @@ -212,7 +213,7 @@ class FeedContentState( fun invalidateInsertData(newItems: Set) { bundlerInsert.invalidateList(newItems) { - refreshFromOldState(it.flatten().toSet()) + refreshFromOldState(it.flattenToSet()) } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/CardFeedContentState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/CardFeedContentState.kt index e2e59f680..e2ed1d22f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/CardFeedContentState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/CardFeedContentState.kt @@ -48,6 +48,7 @@ import com.vitorpamplona.quartz.nip28PublicChat.admin.ChannelMetadataEvent import com.vitorpamplona.quartz.nip57Zaps.LnZapEvent import com.vitorpamplona.quartz.nip58Badges.BadgeAwardEvent import com.vitorpamplona.quartz.utils.Log +import com.vitorpamplona.quartz.utils.flattenToSet import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.CoroutineScope @@ -426,7 +427,7 @@ class CardFeedContentState( fun invalidateInsertData(newItems: Set) { bundlerInsert.invalidateList(newItems) { - val newObjects = it.flatten().toSet() + val newObjects = it.flattenToSet() logTime("${this.javaClass.simpleName} Card additive receiving ${newObjects.size} items into ${it.size} items") { if (newObjects.isNotEmpty()) { refreshFromOldState(newObjects) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/apps/UserProfileAppRecommendationsFeedFilter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/apps/UserProfileAppRecommendationsFeedFilter.kt index 805d6cbfe..9bfda48aa 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/apps/UserProfileAppRecommendationsFeedFilter.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/header/apps/UserProfileAppRecommendationsFeedFilter.kt @@ -26,6 +26,7 @@ import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter import com.vitorpamplona.amethyst.ui.dal.DefaultFeedOrder import com.vitorpamplona.quartz.nip89AppHandlers.recommendation.AppRecommendationEvent +import com.vitorpamplona.quartz.utils.flattenToSet class UserProfileAppRecommendationsFeedFilter( val user: User, @@ -43,7 +44,7 @@ class UserProfileAppRecommendationsFeedFilter( override fun applyFilter(newItems: Set): Set = innerApplyFilter(newItems) - private fun innerApplyFilter(collection: Collection): Set = collection.mapNotNull { filterMap(it) }.flatten().toSet() + private fun innerApplyFilter(collection: Collection): Set = collection.mapNotNull { filterMap(it) }.flattenToSet() fun filterMap(it: Note): List? { val noteEvent = it.event diff --git a/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/LargeDBInsertBenchmark.kt b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/LargeDBInsertBenchmark.kt index 36f2dee27..ff3c55a3b 100644 --- a/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/LargeDBInsertBenchmark.kt +++ b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/LargeDBInsertBenchmark.kt @@ -31,6 +31,7 @@ import com.vitorpamplona.quartz.nip01Core.store.sqlite.EventStore import com.vitorpamplona.quartz.nip09Deletions.DeletionEvent import com.vitorpamplona.quartz.nip40Expiration.isExpired import com.vitorpamplona.quartz.utils.Log +import com.vitorpamplona.quartz.utils.flattenToSet import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -72,8 +73,8 @@ class LargeDBInsertBenchmark : BaseLargeCacheBenchmark() { fun bench40DeletionRequestsEvents() { val context = ApplicationProvider.getApplicationContext() val deletions = allEvents.filterIsInstance() - val deletionIds = deletions.map { it.deleteEventIds() }.flatten().toSet() - val deletionAddresses = deletions.map { it.deleteAddressIds() }.flatten().toSet() + val deletionIds = deletions.map { it.deleteEventIds() }.flattenToSet() + val deletionAddresses = deletions.map { it.deleteAddressIds() }.flattenToSet() val toBeDeletedEvents = allEvents.filter { diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/hashtags/MipMapHashTags.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/hashtags/MipMapHashTags.kt index 4e14f78bf..e795aa333 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/hashtags/MipMapHashTags.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/hashtags/MipMapHashTags.kt @@ -20,6 +20,8 @@ */ package com.vitorpamplona.quartz.nip01Core.tags.hashtags +import com.vitorpamplona.quartz.utils.flattenToSet + fun hashtagAlts(tag: String): Set = setOf( tag, @@ -30,6 +32,6 @@ fun hashtagAlts(tag: String): Set = }, ) -fun hashtagAlts(tags: List): Set = tags.map { hashtagAlts(it) }.flatten().toSet() +fun hashtagAlts(tags: List): Set = tags.map { hashtagAlts(it) }.flattenToSet() -fun hashtagAlts(tags: Set): Set = tags.map { hashtagAlts(it) }.flatten().toSet() +fun hashtagAlts(tags: Set): Set = tags.map { hashtagAlts(it) }.flattenToSet() diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/IterableExt.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/IterableExt.kt index b3f624e6d..e41a58575 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/IterableExt.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/IterableExt.kt @@ -46,3 +46,11 @@ fun Iterable.joinToStringLimited( buffer.append(postfix) return buffer.toString() } + +public fun Iterable>.flattenToSet(): Set { + val result = mutableSetOf() + for (element in this) { + result.addAll(element) + } + return result +}