Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: Use a few less NUM_CARGO loops. #11499

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/script/api/script_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,10 @@

CargoArray cap = ::GetCapacityOfArticulatedParts(engine_id);

CargoID most_cargo = CT_INVALID;
uint amount = 0;
for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
if (cap[cid] > amount) {
amount = cap[cid];
most_cargo = cid;
}
}
auto it = std::max_element(std::cbegin(cap), std::cend(cap));
if (*it == 0) return CT_INVALID;

return most_cargo;
return CargoID(std::distance(std::cbegin(cap), it));
}

/* static */ bool ScriptEngine::CanRefitCargo(EngineID engine_id, CargoID cargo_id)
Expand Down Expand Up @@ -94,9 +88,8 @@
case VEH_ROAD:
case VEH_TRAIN: {
CargoArray capacities = GetCapacityOfArticulatedParts(engine_id);
for (CargoID c = 0; c < NUM_CARGO; c++) {
if (capacities[c] == 0) continue;
return capacities[c];
for (uint &cap : capacities) {
if (cap != 0) return cap;
}
return -1;
}
Expand Down
17 changes: 5 additions & 12 deletions src/script/api/script_event_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,10 @@ CargoID ScriptEventEnginePreview::GetCargoType()
if (!this->IsEngineValid()) return CT_INVALID;
CargoArray cap = ::GetCapacityOfArticulatedParts(this->engine);

CargoID most_cargo = CT_INVALID;
uint amount = 0;
for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
if (cap[cid] > amount) {
amount = cap[cid];
most_cargo = cid;
}
}
auto it = std::max_element(std::cbegin(cap), std::cend(cap));
if (*it == 0) return CT_INVALID;

return most_cargo;
return CargoID(std::distance(std::cbegin(cap), it));
}

int32_t ScriptEventEnginePreview::GetCapacity()
Expand All @@ -62,9 +56,8 @@ int32_t ScriptEventEnginePreview::GetCapacity()
case VEH_ROAD:
case VEH_TRAIN: {
CargoArray capacities = GetCapacityOfArticulatedParts(this->engine);
for (CargoID c = 0; c < NUM_CARGO; c++) {
if (capacities[c] == 0) continue;
return capacities[c];
for (uint &cap : capacities) {
if (cap != 0) return cap;
}
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/vehicle_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,8 +1373,8 @@ static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &
for (v = b; v != nullptr; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap;

int r = 0;
for (CargoID i = 0; i < NUM_CARGO; i++) {
r = diff[i];
for (uint d : diff) {
r = d;
if (r != 0) break;
}
Comment on lines 1375 to 1379
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be something along the line of the following?

auto it = std::find_if(diff.begin(), diff.end(), [](auto d) { return d != 0; });
return it != diff.end() ? *it < 0 : VehicleNumberSorter(a, b);

It's sort of a recurring pattern with the capacities as well, though there the code is smaller than it's here.


Expand Down