From 1119d593a22287a42eb3fed1dbb15e82a1825b36 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 8 Jul 2024 14:29:43 +0600 Subject: [PATCH] Revert "extract GroupAttributesCalculator" This reverts commit bf128a3eb9520c6076634f7fec4075f7bc97d1f5. --- .../maps/features/FeatureDrawScope.kt | 16 +++++++++++++--- .../maps/utils/GroupAttributesCalculator.kt | 19 ------------------- .../space/kscience/maps/svg/exportToSvg.kt | 14 +++++++++++--- 3 files changed, 24 insertions(+), 25 deletions(-) delete mode 100644 maps-kt-features/src/commonMain/kotlin/space/kscience/maps/utils/GroupAttributesCalculator.kt diff --git a/maps-kt-features/src/commonMain/kotlin/space/kscience/maps/features/FeatureDrawScope.kt b/maps-kt-features/src/commonMain/kotlin/space/kscience/maps/features/FeatureDrawScope.kt index d5d7ce0..028fd9f 100644 --- a/maps-kt-features/src/commonMain/kotlin/space/kscience/maps/features/FeatureDrawScope.kt +++ b/maps-kt-features/src/commonMain/kotlin/space/kscience/maps/features/FeatureDrawScope.kt @@ -23,7 +23,6 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.sample import space.kscience.attributes.Attributes import space.kscience.attributes.plus -import space.kscience.maps.utils.GroupAttributesCalculator import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds @@ -104,13 +103,24 @@ public fun FeatureCanvas( } clipRect { ComposeFeatureDrawScope(this, state, painterCache, textMeasurer).apply(draw).apply { - val attributesCalculator = GroupAttributesCalculator(features) + + val attributesCache = mutableMapOf, Attributes>() + + fun computeGroupAttributes(path: List): Attributes = attributesCache.getOrPut(path) { + if (path.isEmpty()) return Attributes.EMPTY + else if (path.size == 1) { + features[path.first()]?.attributes ?: Attributes.EMPTY + } else { + computeGroupAttributes(path.dropLast(1)) + (features[path.first()]?.attributes + ?: Attributes.EMPTY) + } + } features.entries.sortedBy { it.value.z } .filter { state.viewPoint.zoom in it.value.zoomRange } .forEach { (id, feature) -> val path = id.split("/") - drawFeature(feature, attributesCalculator.computeGroupAttributes(path.dropLast(1))) + drawFeature(feature, computeGroupAttributes(path.dropLast(1))) } } } diff --git a/maps-kt-features/src/commonMain/kotlin/space/kscience/maps/utils/GroupAttributesCalculator.kt b/maps-kt-features/src/commonMain/kotlin/space/kscience/maps/utils/GroupAttributesCalculator.kt deleted file mode 100644 index 4b9d35e..0000000 --- a/maps-kt-features/src/commonMain/kotlin/space/kscience/maps/utils/GroupAttributesCalculator.kt +++ /dev/null @@ -1,19 +0,0 @@ -package space.kscience.maps.utils - -import space.kscience.attributes.Attributes -import space.kscience.attributes.plus -import space.kscience.maps.features.Feature - -public class GroupAttributesCalculator( - private val features: Map>, - private val attributesCache: MutableMap, Attributes> = mutableMapOf() -) { - public fun computeGroupAttributes(path: List): Attributes = attributesCache.getOrPut(path){ - if (path.isEmpty()) return Attributes.EMPTY - else if (path.size == 1) { - features[path.first()]?.attributes ?: Attributes.EMPTY - } else { - computeGroupAttributes(path.dropLast(1)) + (features[path.first()]?.attributes ?: Attributes.EMPTY) - } - } -} diff --git a/maps-kt-scheme/src/jvmMain/kotlin/space/kscience/maps/svg/exportToSvg.kt b/maps-kt-scheme/src/jvmMain/kotlin/space/kscience/maps/svg/exportToSvg.kt index 420a819..3f6993f 100644 --- a/maps-kt-scheme/src/jvmMain/kotlin/space/kscience/maps/svg/exportToSvg.kt +++ b/maps-kt-scheme/src/jvmMain/kotlin/space/kscience/maps/svg/exportToSvg.kt @@ -11,7 +11,6 @@ import space.kscience.attributes.plus import space.kscience.maps.features.* import space.kscience.maps.scheme.XY import space.kscience.maps.scheme.XYCanvasState -import space.kscience.maps.utils.GroupAttributesCalculator public class FeatureStateSnapshot( @@ -166,10 +165,19 @@ public fun FeatureStateSnapshot.generateSvg( features.entries.sortedBy { it.value.z } .filter { state.viewPoint.zoom in it.value.zoomRange } .forEach { (id, feature) -> - val attributesCalculator = GroupAttributesCalculator(features) + val attributesCache = mutableMapOf, Attributes>() + + fun computeGroupAttributes(path: List): Attributes = attributesCache.getOrPut(path){ + if (path.isEmpty()) return Attributes.EMPTY + else if (path.size == 1) { + features[path.first()]?.attributes ?: Attributes.EMPTY + } else { + computeGroupAttributes(path.dropLast(1)) + (features[path.first()]?.attributes ?: Attributes.EMPTY) + } + } val path = id.split("/") - drawFeature(feature, attributesCalculator.computeGroupAttributes(path.dropLast(1))) + drawFeature(feature, computeGroupAttributes(path.dropLast(1))) } } return svgGraphics2D.getSVGElement(id)