Skip to content

Commit fd2dd43

Browse files
committed
Codechange: Use range-for and iterator to populate default cargo table.
1 parent 280dce9 commit fd2dd43

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/cargotype.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,33 @@ void SetupCargoForClimate(LandscapeID l)
4141
{
4242
assert(l < lengthof(_default_climate_cargo));
4343

44-
/* Reset and disable all cargo types */
45-
std::fill(std::begin(CargoSpec::array), std::end(CargoSpec::array), CargoSpec{});
46-
4744
_cargo_mask = 0;
4845

49-
for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
50-
CargoLabel cl = _default_climate_cargo[l][i];
46+
/* Copy from default cargo by label or index. */
47+
auto insert = std::begin(CargoSpec::array);
48+
for (const CargoLabel &cl : _default_climate_cargo[l]) {
5149

52-
/* Bzzt: check if cl is just an index into the cargo table */
50+
/* Check if value is an index into the cargo table */
5351
if (cl < lengthof(_default_cargo)) {
54-
/* Copy the indexed cargo */
55-
CargoSpec *cargo = CargoSpec::Get(i);
56-
*cargo = _default_cargo[cl];
57-
if (cargo->bitnum != INVALID_CARGO_BITNUM) SetBit(_cargo_mask, i);
58-
continue;
59-
}
60-
61-
/* Loop through each of the default cargo types to see if
62-
* the label matches */
63-
for (uint j = 0; j < lengthof(_default_cargo); j++) {
64-
if (_default_cargo[j].label == cl) {
65-
*CargoSpec::Get(i) = _default_cargo[j];
66-
67-
/* Populate the available cargo mask */
68-
SetBit(_cargo_mask, i);
69-
break;
52+
/* Copy the default cargo by index. */
53+
*insert = _default_cargo[cl];
54+
} else {
55+
/* Search for label in default cargo types and copy if found. */
56+
auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&cl](const CargoSpec &cs) { return cs.label == cl; });
57+
if (found != std::end(_default_cargo)) {
58+
*insert = *found;
59+
} else {
60+
/* Index or label is invalid, this should not happen. */
61+
NOT_REACHED();
7062
}
7163
}
64+
65+
if (insert->IsValid()) SetBit(_cargo_mask, insert->Index());
66+
++insert;
7267
}
68+
69+
/* Reset and disable remaining cargo types. */
70+
std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
7371
}
7472

7573
/**

0 commit comments

Comments
 (0)