From 6291cecb7ee34bbcff3c3b9ba751eca170778238 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 18 Sep 2024 17:47:54 +0700 Subject: [PATCH] How i missed this i have no idea --- .../mc/otm/core/AbstractHistoryGraph.kt | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/AbstractHistoryGraph.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/AbstractHistoryGraph.kt index e4978664e..35d715784 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/AbstractHistoryGraph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/AbstractHistoryGraph.kt @@ -22,7 +22,7 @@ abstract class AbstractHistoryGraph( /** * Limit on how many graph values to store */ - val width: Int = 100, + val width: Int, ) : Iterable, INBTSerializable, ISynchable { constructor(ticks: Int) : this(1, ticks) @@ -36,8 +36,27 @@ abstract class AbstractHistoryGraph( protected abstract fun calculateAverage(input: List): V protected abstract fun sum(a: V, b: V): V + protected abstract fun sum(input: List): V protected abstract fun identity(): V + fun calculateAverage(width: Int = this.width): V { + require(width >= 0) { "Invalid time frame: $width" } + + if (width == 0 || this.values.isEmpty()) + return identity() + + return calculateAverage(this.values.subList(0, width.coerceAtMost(this.values.size))) + } + + fun calculateSum(width: Int = this.width): V { + require(width >= 0) { "Invalid time frame: $width" } + + if (width == 0 || this.values.isEmpty()) + return identity() + + return sum(this.values.subList(0, width.coerceAtMost(this.values.size))) + } + abstract val codec: Codec abstract val streamCodec: MatteryStreamCodec @@ -48,7 +67,7 @@ abstract class AbstractHistoryGraph( remotes.add(this) } - private var shouldFullyNetwork = false + private var shouldFullyNetwork = true private val networkValues = ArrayList() override fun write(stream: RegistryFriendlyByteBuf) {