Optimize ActionResultFilter with Set and ListBuffer (#5672)

- Change UNIVERSALLY_VISIBLE_TYPES from Vector to Set for O(1) contains
- Replace O(n²) Vector concatenation with O(n) ListBuffer in filterForPlayer

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-28 11:19:37 -08:00
committed by GitHub
co-authored by Claude Opus 4.5
parent e99df3b900
commit 3b379e55bc
@@ -1,5 +1,7 @@
package net.eagle0.eagle.library
import scala.collection.mutable.ListBuffer
import net.eagle0.eagle.library.actions.applier.ActionResultWithResultingState
import net.eagle0.eagle.library.util.faction_utils.FactionUtils
import net.eagle0.eagle.library.util.view_filters.GameStateViewFilter
@@ -12,7 +14,7 @@ import net.eagle0.eagle.model.view.game_state.GameStateViewDiff
import net.eagle0.eagle.FactionId
object ActionResultFilter {
private val UNIVERSALLY_VISIBLE_TYPES: Vector[ActionResultType] = Vector(
private val UNIVERSALLY_VISIBLE_TYPES: Set[ActionResultType] = Set(
ActionResultType.GameStart,
ActionResultType.StartBattle,
ActionResultType.BattleEnded,
@@ -162,19 +164,22 @@ object ActionResultFilter {
startingState: GameState,
results: Vector[ActionResultWithResultingState],
factionId: FactionId
): Vector[ActionResultView] =
results
.foldLeft((startingState, Vector.empty[ActionResultView])) {
case ((gs, acc), res) =>
val maybeNewRes = filterOne(
startingState = gs,
result = res,
factionId = factionId
)
): Vector[ActionResultView] = {
val accumulator = ListBuffer.empty[ActionResultView]
var currentState = startingState
(res.resultingState, acc ++ maybeNewRes)
}
._2
results.foreach { res =>
filterOne(
startingState = currentState,
result = res,
factionId = factionId
).foreach(accumulator += _)
currentState = res.resultingState
}
accumulator.toVector
}
def filterForObserver(
startingState: GameState,