From fd2dd4397feaca12a57046290caa4bfd0f0e2a7f Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 17 Sep 2023 17:53:13 +0100 Subject: [PATCH] Codechange: Use range-for and iterator to populate default cargo table. --- src/cargotype.cpp | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 885f96a49b..f764ad2dd7 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -41,35 +41,33 @@ void SetupCargoForClimate(LandscapeID l) { assert(l < lengthof(_default_climate_cargo)); - /* Reset and disable all cargo types */ - std::fill(std::begin(CargoSpec::array), std::end(CargoSpec::array), CargoSpec{}); - _cargo_mask = 0; - for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) { - CargoLabel cl = _default_climate_cargo[l][i]; + /* Copy from default cargo by label or index. */ + auto insert = std::begin(CargoSpec::array); + for (const CargoLabel &cl : _default_climate_cargo[l]) { - /* Bzzt: check if cl is just an index into the cargo table */ + /* Check if value is an index into the cargo table */ if (cl < lengthof(_default_cargo)) { - /* Copy the indexed cargo */ - CargoSpec *cargo = CargoSpec::Get(i); - *cargo = _default_cargo[cl]; - if (cargo->bitnum != INVALID_CARGO_BITNUM) SetBit(_cargo_mask, i); - continue; - } - - /* Loop through each of the default cargo types to see if - * the label matches */ - for (uint j = 0; j < lengthof(_default_cargo); j++) { - if (_default_cargo[j].label == cl) { - *CargoSpec::Get(i) = _default_cargo[j]; - - /* Populate the available cargo mask */ - SetBit(_cargo_mask, i); - break; + /* Copy the default cargo by index. */ + *insert = _default_cargo[cl]; + } else { + /* Search for label in default cargo types and copy if found. */ + auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&cl](const CargoSpec &cs) { return cs.label == cl; }); + if (found != std::end(_default_cargo)) { + *insert = *found; + } else { + /* Index or label is invalid, this should not happen. */ + NOT_REACHED(); } } + + if (insert->IsValid()) SetBit(_cargo_mask, insert->Index()); + ++insert; } + + /* Reset and disable remaining cargo types. */ + std::fill(insert, std::end(CargoSpec::array), CargoSpec{}); } /**