From 0e4b716815f43effe39968a7cac80cf112e7659c Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 23 Sep 2019 18:45:37 +0100 Subject: [PATCH] Fix: O(N^2) cost of Station::RecomputeCatchmentForAll Station::RemoveFromAllNearbyLists does not need to be called when all station nearby lists have been cleared and are being regenerated. --- src/station.cpp | 9 ++++++--- src/station_base.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/station.cpp b/src/station.cpp index a9ccfc4329..700ca7241d 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -455,11 +455,12 @@ bool Station::CatchmentCoversTown(TownID t) const /** * Recompute tiles covered in our catchment area. * This will additionally recompute nearby towns and industries. + * @param no_clear_nearby_lists If Station::RemoveFromAllNearbyLists does not need to be called. */ -void Station::RecomputeCatchment() +void Station::RecomputeCatchment(bool no_clear_nearby_lists) { this->industries_near.clear(); - this->RemoveFromAllNearbyLists(); + if (!no_clear_nearby_lists) this->RemoveFromAllNearbyLists(); if (this->rect.IsEmpty()) { this->catchment_tiles.Reset(); @@ -526,7 +527,9 @@ void Station::RecomputeCatchment() */ /* static */ void Station::RecomputeCatchmentForAll() { - for (Station *st : Station::Iterate()) { st->RecomputeCatchment(); } + for (Town *t : Town::Iterate()) { t->stations_near.clear(); } + for (Industry *i : Industry::Iterate()) { i->stations_near.clear(); } + for (Station *st : Station::Iterate()) { st->RecomputeCatchment(true); } } /************************************************************************/ diff --git a/src/station_base.h b/src/station_base.h index b2e31a29de..bd7f00b9f9 100644 --- a/src/station_base.h +++ b/src/station_base.h @@ -501,7 +501,7 @@ public: uint GetPlatformLength(TileIndex tile, DiagDirection dir) const override; uint GetPlatformLength(TileIndex tile) const override; - void RecomputeCatchment(); + void RecomputeCatchment(bool no_clear_nearby_lists = false); static void RecomputeCatchmentForAll(); uint GetCatchmentRadius() const;