From 7c834921b215cc00c05d2815f251b04448b17e6c Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 5 May 2025 00:49:13 +0100 Subject: [PATCH] Codechange: Use FlatSet for per-industry type industry lists. --- src/industry.h | 3 ++- src/industry_cmd.cpp | 7 +++---- src/saveload/industry_sl.cpp | 2 +- src/saveload/oldloader_sl.cpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/industry.h b/src/industry.h index d7683d0c41..50e669ce4d 100644 --- a/src/industry.h +++ b/src/industry.h @@ -10,6 +10,7 @@ #ifndef INDUSTRY_H #define INDUSTRY_H +#include "core/flatset_type.hpp" #include "newgrf_storage.h" #include "subsidy_type.h" #include "industry_map.h" @@ -257,7 +258,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { return this->cached_name; } - static std::array, NUM_INDUSTRYTYPES> industries; ///< List of industries of each type. + static std::array, NUM_INDUSTRYTYPES> industries; ///< List of industries of each type. private: void FillCachedName() const; diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 8f6892ef4b..771408c3d1 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -63,7 +63,7 @@ void BuildOilRig(TileIndex tile); static uint8_t _industry_sound_ctr; static TileIndex _industry_sound_tile; -std::array, NUM_INDUSTRYTYPES> Industry::industries; +std::array, NUM_INDUSTRYTYPES> Industry::industries; IndustrySpec _industry_specs[NUM_INDUSTRYTYPES]; IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES]; @@ -190,8 +190,7 @@ Industry::~Industry() delete this->psa; auto &industries = Industry::industries[type]; - auto it = std::ranges::lower_bound(industries, this->index); - industries.erase(it); + industries.erase(this->index); DeleteIndustryNews(this->index); CloseWindowById(WC_INDUSTRY_VIEW, this->index); @@ -1760,7 +1759,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, i->type = type; auto &industries = Industry::industries[type]; - industries.emplace(std::ranges::lower_bound(industries, i->index), i->index); + industries.insert(i->index); for (size_t index = 0; index < std::size(indspec->produced_cargo); ++index) { if (!IsValidCargoType(indspec->produced_cargo[index])) break; diff --git a/src/saveload/industry_sl.cpp b/src/saveload/industry_sl.cpp index 17b61c5b8b..0b37f7e1cf 100644 --- a/src/saveload/industry_sl.cpp +++ b/src/saveload/industry_sl.cpp @@ -228,7 +228,7 @@ struct INDYChunkHandler : ChunkHandler { } else if (IsSavegameVersionBefore(SLV_INDUSTRY_CARGO_REORGANISE)) { LoadMoveAcceptsProduced(i, INDUSTRY_NUM_INPUTS, INDUSTRY_NUM_OUTPUTS); } - Industry::industries[i->type].push_back(i->index); // Assume savegame indices are sorted. + Industry::industries[i->type].insert(i->index); } } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 6f35e13e79..bd08924953 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -875,7 +875,7 @@ static bool LoadOldIndustry(LoadgameState &ls, int num) i->random_colour = RemapTTOColour(i->random_colour); } - Industry::industries[i->type].push_back(i->index); // Assume savegame indices are sorted. + Industry::industries[i->type].insert(i->index); } else { delete i; }