From 6b5b1f938acf24437caa308e3a7f77be8d6ecbff Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 6 Jul 2023 01:09:39 +0700 Subject: [PATCH] =?UTF-8?q?YuRaNnNzZZ=20=E2=80=94=20=D0=A1=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=D0=B4=D0=BD=D1=8F,=20=D0=B2=200:22=20=D0=B2=D1=80=D0=B5?= =?UTF-8?q?=D0=BC=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B2=D0=BE=D0=B4=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B4=D0=B5=D1=81=D1=82=D1=80=D0=B8=D0=BC=D0=B8=D1=84?= =?UTF-8?q?=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mc/otm/container/UpgradeContainer.kt | 22 +++--- .../mc/otm/core/collect/StreamyIterator.kt | 72 +++++++++++++++++++ 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt index d93c2c282..521a25a0a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt @@ -5,6 +5,12 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.UpgradeType import ru.dbotthepony.mc.otm.config.ConciseBalanceValues import ru.dbotthepony.mc.otm.config.VerboseBalanceValues +import ru.dbotthepony.mc.otm.core.collect.filter +import ru.dbotthepony.mc.otm.core.collect.map +import ru.dbotthepony.mc.otm.core.collect.mapToDouble +import ru.dbotthepony.mc.otm.core.collect.mapToInt +import ru.dbotthepony.mc.otm.core.collect.reduce +import ru.dbotthepony.mc.otm.core.collect.sum import ru.dbotthepony.mc.otm.core.isNotEmpty import ru.dbotthepony.mc.otm.core.math.Decimal import kotlin.math.pow @@ -16,21 +22,21 @@ open class UpgradeContainer(slotCount: Int, open val allowedUpgrades: Set a + b } override val energyStorageFlat: Decimal - get() = stream().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyStorageFlat }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) + get() = iterator().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyStorageFlat }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) override val energyStorage: Decimal - get() = stream().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyStorage }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) + get() = iterator().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyStorage }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) override val energyConsumed: Decimal - get() = stream().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyConsumed }.orElse(Decimal.ZERO) * it.count }.reduce(Decimal.ZERO, Decimal::plus) + get() = iterator().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyConsumed }.orElse(Decimal.ZERO) * it.count }.reduce(Decimal.ZERO, Decimal::plus) override val failureMultiplier: Double - get() = stream().filter { it.isNotEmpty }.mapToDouble { it.getCapability(MatteryCapability.UPGRADE).map { it.failureMultiplier }.orElse(1.0).coerceAtLeast(0.0).pow(it.count.toDouble()) }.reduce(1.0) { a, b -> a * b } + get() = iterator().filter { it.isNotEmpty }.mapToDouble { it.getCapability(MatteryCapability.UPGRADE).map { it.failureMultiplier }.orElse(1.0).coerceAtLeast(0.0).pow(it.count.toDouble()) }.reduce(1.0) { a, b -> a * b } override val energyThroughputFlat: Decimal - get() = stream().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyThroughputFlat }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) + get() = iterator().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyThroughputFlat }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) override val energyThroughput: Decimal - get() = stream().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyThroughput }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) + get() = iterator().filter { it.isNotEmpty }.map { it.getCapability(MatteryCapability.UPGRADE).map { it.energyThroughput }.orElse(Decimal.ZERO).moreThanZero() * it.count }.reduce(Decimal.ZERO, Decimal::plus) fun transform(values: ConciseBalanceValues): ConciseBalanceValues { return object : ConciseBalanceValues { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt index ddd5193d7..53cda603a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt @@ -135,6 +135,78 @@ fun Iterator.map(mapper: java.util.function.Function): Iterator< fun Iterator.flatMap(mapper: (T) -> Iterator): Iterator = FlatMappingIterator(this, mapper) fun Iterator.flatMap(mapper: java.util.function.Function>): Iterator = FlatMappingIterator(this, mapper::apply) +fun interface O2DFunction { + fun apply(value: T): Double +} + +fun interface O2IFunction { + fun apply(value: T): Int +} + +fun Iterator.mapToDouble(mapper: O2DFunction): it.unimi.dsi.fastutil.doubles.DoubleIterator { + return object : it.unimi.dsi.fastutil.doubles.DoubleIterator { + override fun hasNext(): Boolean { + return this@mapToDouble.hasNext() + } + + override fun remove() { + throw UnsupportedOperationException() + } + + override fun nextDouble(): Double { + return mapper.apply(this@mapToDouble.next()) + } + } +} + +fun Iterator.mapToInt(mapper: O2IFunction): it.unimi.dsi.fastutil.ints.IntIterator { + return object : it.unimi.dsi.fastutil.ints.IntIterator { + override fun hasNext(): Boolean { + return this@mapToInt.hasNext() + } + + override fun remove() { + throw UnsupportedOperationException() + } + + override fun nextInt(): Int { + return mapper.apply(this@mapToInt.next()) + } + } +} + +fun it.unimi.dsi.fastutil.doubles.DoubleIterator.sum(): Double { + var value = 0.0 + while (hasNext()) value += nextDouble() + return value +} + +fun it.unimi.dsi.fastutil.ints.IntIterator.sum(): Int { + var value = 0 + while (hasNext()) value += nextInt() + return value +} + +fun interface DD2DFunction { + fun apply(a: Double, b: Double): Double +} + +fun interface II2IFunction { + fun apply(a: Int, b: Int): Int +} + +fun it.unimi.dsi.fastutil.doubles.DoubleIterator.reduce(identity: Double, reducer: DD2DFunction): Double { + var result = identity + while (hasNext()) result = reducer.apply(result, nextDouble()) + return result +} + +fun it.unimi.dsi.fastutil.ints.IntIterator.reduce(identity: Int, reducer: II2IFunction): Int { + var result = identity + while (hasNext()) result = reducer.apply(result, nextInt()) + return result +} + fun Iterator.reduce(identity: T, reducer: (T, T) -> T): T { var result = identity