1
0
Fork 0

Codechange: Base CargoArray off std::array.

This avoids needing to define array accessors and allows use of
default value initialization.
pull/10864/head
Peter Nelson 2023-05-23 12:23:50 +01:00 committed by PeterN
parent 74e42e39a8
commit f177ce7c9a
14 changed files with 26 additions and 68 deletions

View File

@ -139,7 +139,7 @@ static inline CargoTypes GetAvailableVehicleCargoTypes(EngineID engine, bool inc
*/
CargoArray GetCapacityOfArticulatedParts(EngineID engine)
{
CargoArray capacity;
CargoArray capacity{};
const Engine *e = Engine::Get(engine);
CargoID cargo_type;
@ -284,7 +284,7 @@ void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
CargoTypes real_refit_union = 0;
CargoTypes real_refit_intersection = ALL_CARGOTYPES;
CargoArray real_default_capacity;
CargoArray real_default_capacity{};
do {
CargoTypes refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);

View File

@ -79,41 +79,7 @@ typedef uint64 CargoTypes;
static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
/** Class for storing amounts of cargo */
struct CargoArray {
private:
uint amount[NUM_CARGO]; ///< Amount of each type of cargo.
public:
/** Default constructor. */
inline CargoArray()
{
this->Clear();
}
/** Reset all entries. */
inline void Clear()
{
memset(this->amount, 0, sizeof(this->amount));
}
/**
* Read/write access to an amount of a specific cargo type.
* @param cargo Cargo type to access.
*/
inline uint &operator[](CargoID cargo)
{
return this->amount[cargo];
}
/**
* Read-only access to an amount of a specific cargo type.
* @param cargo Cargo type to access.
*/
inline const uint &operator[](CargoID cargo) const
{
return this->amount[cargo];
}
struct CargoArray : std::array<uint, NUM_CARGO> {
/**
* Get the sum of all cargo amounts.
* @return The sum.
@ -121,24 +87,16 @@ public:
template <typename T>
inline const T GetSum() const
{
T ret = 0;
for (size_t i = 0; i < lengthof(this->amount); i++) {
ret += this->amount[i];
}
return ret;
return std::reduce(this->begin(), this->end(), T{});
}
/**
* Get the amount of cargos that have an amount.
* @return The amount.
*/
inline byte GetCount() const
inline uint GetCount() const
{
byte count = 0;
for (size_t i = 0; i < lengthof(this->amount); i++) {
if (this->amount[i] != 0) count++;
}
return count;
return std::count_if(this->begin(), this->end(), [](uint amount) { return amount != 0; });
}
};

View File

@ -22,7 +22,7 @@
struct CompanyEconomyEntry {
Money income; ///< The amount of income.
Money expenses; ///< The amount of expenses.
CargoArray delivered_cargo; ///< The amount of delivered cargo.
CargoArray delivered_cargo{}; ///< The amount of delivered cargo.
int32 performance_history; ///< Company score (scale 0-1000)
Money company_value; ///< The value of the company.
};

View File

@ -855,7 +855,7 @@ struct DepotWindow : Window {
if (v == nullptr || mode != MODE_DRAG_VEHICLE) return false;
CargoArray capacity, loaded;
CargoArray capacity{}, loaded{};
/* Display info for single (articulated) vehicle, or for whole chain starting with selected vehicle */
bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);

View File

@ -1588,7 +1588,7 @@ static void LoadUnloadVehicle(Vehicle *front)
StationIDStack next_station = front->GetNextStoppingStation();
bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CT_AUTO_REFIT;
CargoArray consist_capleft;
CargoArray consist_capleft{};
if (_settings_game.order.improved_load && use_autorefit ?
front->cargo_payment == nullptr : (front->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0) {
ReserveConsist(st, front,

View File

@ -167,7 +167,7 @@ public:
td.grf = nullptr;
CargoArray acceptance;
CargoArray acceptance{};
AddAcceptedCargo(tile, acceptance, nullptr);
GetTileDesc(tile, &td);

View File

@ -39,7 +39,7 @@ void DrawRoadVehDetails(const Vehicle *v, const Rect &r)
y += FONT_HEIGHT_NORMAL;
if (v->HasArticulatedPart()) {
CargoArray max_cargo;
CargoArray max_cargo{};
StringID subtype_text[NUM_CARGO];
char capacity[512];

View File

@ -513,7 +513,7 @@ static void ShowRejectOrAcceptNews(const Station *st, uint num_items, CargoID *c
*/
CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
{
CargoArray produced;
CargoArray produced{};
std::set<IndustryID> industries;
TileArea ta = TileArea(north_tile, w, h).Expand(rad);
@ -552,7 +552,7 @@ CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
*/
CargoArray GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad, CargoTypes *always_accepted)
{
CargoArray acceptance;
CargoArray acceptance{};
if (always_accepted != nullptr) *always_accepted = 0;
TileArea ta = TileArea(center_tile, w, h).Expand(rad);
@ -574,7 +574,7 @@ CargoArray GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad
*/
static CargoArray GetAcceptanceAroundStation(const Station *st, CargoTypes *always_accepted)
{
CargoArray acceptance;
CargoArray acceptance{};
if (always_accepted != nullptr) *always_accepted = 0;
BitmapTileIterator it(st->catchment_tiles);
@ -596,7 +596,7 @@ void UpdateStationAcceptance(Station *st, bool show_msg)
CargoTypes old_acc = GetAcceptanceMask(st);
/* And retrieve the acceptance. */
CargoArray acceptance;
CargoArray acceptance{};
if (!st->rect.IsEmpty()) {
acceptance = GetAcceptanceAroundStation(st, &st->always_accepted);
}

View File

@ -74,6 +74,7 @@
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <set>
#include <stdexcept>

View File

@ -323,7 +323,7 @@ bool FindSubsidyTownCargoRoute()
if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
/* Calculate the produced cargo of houses around town center. */
CargoArray town_cargo_produced;
CargoArray town_cargo_produced{};
TileArea ta = TileArea(src_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
for (TileIndex tile : ta) {
if (IsTileType(tile, MP_HOUSE)) {
@ -431,7 +431,7 @@ bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
const Town *dst_town = Town::GetRandom();
/* Calculate cargo acceptance of houses around town center. */
CargoArray town_cargo_accepted;
CargoArray town_cargo_accepted{};
TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
for (TileIndex tile : ta) {
if (IsTileType(tile, MP_HOUSE)) {

View File

@ -322,8 +322,8 @@ int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
int num = 0;
if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
CargoArray act_cargo;
CargoArray max_cargo;
CargoArray act_cargo{};
CargoArray max_cargo{};
for (const Vehicle *v = Vehicle::Get(veh_id); v != nullptr; v = v->Next()) {
act_cargo[v->cargo_type] += v->cargo.StoredCount();
max_cargo[v->cargo_type] += v->cargo_cap;
@ -435,8 +435,8 @@ void DrawTrainDetails(const Train *v, const Rect &r, int vscroll_pos, uint16 vsc
}
} else {
int y = r.top;
CargoArray act_cargo;
CargoArray max_cargo;
CargoArray act_cargo{};
CargoArray max_cargo{};
Money feeder_share = 0;
for (const Vehicle *u = v; u != nullptr; u = u->Next()) {

View File

@ -145,7 +145,7 @@ std::tuple<CommandCost, VehicleID, uint, uint16, CargoArray> CmdBuildVehicle(DoC
VehicleID veh_id = INVALID_VEHICLE;
uint refitted_capacity = 0;
uint16 refitted_mail_capacity = 0;
CargoArray cargo_capacities;
CargoArray cargo_capacities{};
if (value.Succeeded()) {
if (subflags & DC_EXEC) {
v->unitnumber = unit_num;
@ -166,7 +166,6 @@ std::tuple<CommandCost, VehicleID, uint, uint16, CargoArray> CmdBuildVehicle(DoC
refitted_mail_capacity = 0;
} else {
refitted_capacity = e->GetDisplayDefaultCapacity(&refitted_mail_capacity);
cargo_capacities.Clear();
cargo_capacities[default_cargo] = refitted_capacity;
cargo_capacities[CT_MAIL] = refitted_mail_capacity;
}
@ -355,7 +354,7 @@ static std::tuple<CommandCost, uint, uint16, CargoArray> RefitVehicle(Vehicle *v
uint total_capacity = 0;
uint total_mail_capacity = 0;
num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
CargoArray cargo_capacities;
CargoArray cargo_capacities{};
VehicleSet vehicles_to_refit;
if (!only_this) {

View File

@ -1375,7 +1375,7 @@ static bool VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle
static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b)
{
const Vehicle *v;
CargoArray diff;
CargoArray diff{};
/* Append the cargo of the connected waggons */
for (v = a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;

View File

@ -43,7 +43,7 @@ struct TestedEngineDetails {
CargoID cargo; ///< Cargo type
uint capacity; ///< Cargo capacity
uint16 mail_capacity; ///< Mail capacity if available
CargoArray all_capacities; ///< Capacities for all cargoes
CargoArray all_capacities{}; ///< Capacities for all cargoes
void FillDefaultCapacities(const Engine *e);
};