From c4f1e0f15d5bb39434f19c2d5395048497bb9e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A4in=C3=B6=20M=C3=A4kel=C3=A4?= Date: Mon, 27 Apr 2020 22:57:32 +0300 Subject: [PATCH] Use ArrayDeque for BFS implementation (#2529) --- core/src/com/unciv/logic/map/BFS.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core/src/com/unciv/logic/map/BFS.kt b/core/src/com/unciv/logic/map/BFS.kt index d07c226193..39b8328087 100644 --- a/core/src/com/unciv/logic/map/BFS.kt +++ b/core/src/com/unciv/logic/map/BFS.kt @@ -1,10 +1,13 @@ package com.unciv.logic.map +// Kotlin's ArrayDeque is experimental +import java.util.ArrayDeque + /** * Defines intermediate steps of a breadth-first search, for use in either get shortest path or get onnected tiles. */ class BFS(val startingPoint: TileInfo, val predicate : (TileInfo) -> Boolean){ - var tilesToCheck = ArrayList() + var tilesToCheck = ArrayDeque() /** each tile reached points to its parent tile, where we got to it from */ val tilesReached = HashMap() @@ -25,16 +28,13 @@ class BFS(val startingPoint: TileInfo, val predicate : (TileInfo) -> Boolean){ } fun nextStep(){ - val newTilesToCheck = ArrayList() - for(tileInfo in tilesToCheck){ - for(neighbor in tileInfo.neighbors){ - if(predicate(neighbor) && !tilesReached.containsKey(neighbor)){ - tilesReached[neighbor] = tileInfo - newTilesToCheck.add(neighbor) - } + val current = tilesToCheck.remove() + for(neighbor in current.neighbors){ + if(predicate(neighbor) && !tilesReached.containsKey(neighbor)){ + tilesReached[neighbor] = current + tilesToCheck.add(neighbor) } } - tilesToCheck = newTilesToCheck } fun getPathTo(destination: TileInfo): ArrayList {