From 5844027eb8588197d82fe896f027182621c4f923 Mon Sep 17 00:00:00 2001 From: glx22 Date: Sun, 13 Jun 2021 02:41:41 +0200 Subject: Codechange: Remove FOR_ALL_SORTED_STANDARD_CARGOSPECS --- src/build_vehicle_gui.cpp | 3 +-- src/cargotype.cpp | 13 ++++++++----- src/cargotype.h | 10 ++-------- src/graph_gui.cpp | 20 +++++++------------- src/industry_gui.cpp | 6 ++---- src/station_gui.cpp | 30 +++++++++++++++--------------- 6 files changed, 35 insertions(+), 47 deletions(-) diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index bdd32dc22..f88a7ee14 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1186,8 +1186,7 @@ struct BuildVehicleWindow : Window { } /* Collect available cargo types for filtering. */ - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { this->cargo_filter[filter_items] = cs->Index(); this->cargo_filter_texts[filter_items] = cs->name; filter_items++; diff --git a/src/cargotype.cpp b/src/cargotype.cpp index a6ea680d8..e35c94d2f 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -149,8 +149,8 @@ SpriteID CargoSpec::GetCargoIcon() const return sprite; } -std::vector _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name. -uint8 _sorted_standard_cargo_specs_size; ///< Number of standard cargo specifications stored in the _sorted_cargo_specs array. +std::vector _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name. +span _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name. /** Sort cargo specifications by their name. */ static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b) @@ -196,13 +196,16 @@ void InitializeSortedCargoSpecs() /* Sort cargo specifications by cargo class and name. */ std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter); + /* Count the number of standard cargos and fill the mask. */ _standard_cargo_mask = 0; - - _sorted_standard_cargo_specs_size = 0; + uint8 nb_standard_cargo = 0; for (const auto &cargo : _sorted_cargo_specs) { if (cargo->classes & CC_SPECIAL) break; - _sorted_standard_cargo_specs_size++; + nb_standard_cargo++; SetBit(_standard_cargo_mask, cargo->Index()); } + + /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */ + _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo }; } diff --git a/src/cargotype.h b/src/cargotype.h index e411d5312..c93feea75 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -15,6 +15,7 @@ #include "gfx_type.h" #include "strings_type.h" #include "landscape_type.h" +#include "core/span_type.hpp" #include /** Globally unique label of a cargo type. */ @@ -181,7 +182,7 @@ CargoID GetDefaultCargoID(LandscapeID l, CargoType ct); void InitializeSortedCargoSpecs(); extern std::vector _sorted_cargo_specs; -extern uint8 _sorted_standard_cargo_specs_size; +extern span _sorted_standard_cargo_specs; /** * Does cargo \a c have cargo class \a cc? @@ -196,11 +197,4 @@ static inline bool IsCargoInClass(CargoID c, CargoClass cc) #define FOR_EACH_SET_CARGO_ID(var, cargo_bits) FOR_EACH_SET_BIT_EX(CargoID, var, CargoTypes, cargo_bits) -/** - * Loop header for iterating over 'real' cargoes, sorted by name. Phony cargoes like regearing cargoes are skipped. - * @param var Reference getting the cargospec. - * @see CargoSpec - */ -#define FOR_ALL_SORTED_STANDARD_CARGOSPECS(var) for (uint8 index = 0; index < _sorted_standard_cargo_specs_size && (var = _sorted_cargo_specs[index], true); index++) - #endif /* CARGOTYPE_H */ diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index 10433fc09..00e1efd90 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -892,7 +892,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow { this->CreateNestedTree(); this->vscroll = this->GetScrollbar(WID_CPR_MATRIX_SCROLLBAR); - this->vscroll->SetCount(_sorted_standard_cargo_specs_size); + this->vscroll->SetCount(static_cast(_sorted_standard_cargo_specs.size())); /* Initialise the dataset */ this->OnHundredthTick(); @@ -911,8 +911,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow { this->excluded_data = 0; int i = 0; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { if (HasBit(_legend_excluded_cargo, cs->Index())) SetBit(this->excluded_data, i); i++; } @@ -925,8 +924,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow { return; } - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { SetDParam(0, cs->name); Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO); d.width += this->legend_width + 4; // colour field @@ -958,8 +956,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow { int pos = this->vscroll->GetPosition(); int max = pos + this->vscroll->GetCapacity(); - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { if (pos-- > 0) continue; if (--max < 0) break; @@ -993,8 +990,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow { case WID_CPR_DISABLE_CARGOES: { /* Add all cargoes to the excluded lists. */ int i = 0; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { SetBit(_legend_excluded_cargo, cs->Index()); SetBit(this->excluded_data, i); i++; @@ -1007,8 +1003,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow { uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_CPR_MATRIX); if (row >= this->vscroll->GetCount()) return; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { if (row-- > 0) continue; ToggleBit(_legend_excluded_cargo, cs->Index()); @@ -1047,8 +1042,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow { this->UpdateExcludedData(); int i = 0; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { this->colours[i] = cs->legend_colour; for (uint j = 0; j != 20; j++) { this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index()); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 648939975..5757a3048 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1375,8 +1375,7 @@ protected: filter_items++; /* Collect available cargo types for filtering. */ - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { this->cargo_filter[filter_items] = cs->Index(); this->cargo_filter_texts[filter_items] = cs->name; filter_items++; @@ -2995,8 +2994,7 @@ struct IndustryCargoesWindow : public Window { case WID_IC_CARGO_DROPDOWN: { DropDownList lst; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { lst.emplace_back(new DropDownListStringItem(cs->name, cs->Index(), false)); } if (!lst.empty()) { diff --git a/src/station_gui.cpp b/src/station_gui.cpp index a43cc1707..1b64d8aee 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -347,10 +347,12 @@ public: this->FinishInitNested(window_number); this->owner = (Owner)this->window_number; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { - if (!HasBit(this->cargo_filter, cs->Index())) continue; - this->LowerWidget(WID_STL_CARGOSTART + index); + uint8 index = 0; + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { + if (HasBit(this->cargo_filter, cs->Index())) { + this->LowerWidget(WID_STL_CARGOSTART + index); + } + index++; } if (this->cargo_filter == this->cargo_filter_max) this->cargo_filter = _cargo_mask; @@ -396,8 +398,7 @@ public: /* Determine appropriate width for mini station rating graph */ this->rating_width = 0; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { this->rating_width = std::max(this->rating_width, GetStringBoundingBox(cs->abbrev).width); } /* Approximately match original 16 pixel wide rating bars by multiplying string width by 1.6 */ @@ -465,8 +466,8 @@ public: x += rtl ? -text_spacing : text_spacing; /* show cargo waiting and station ratings */ - for (uint j = 0; j < _sorted_standard_cargo_specs_size; j++) { - CargoID cid = _sorted_cargo_specs[j]->Index(); + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { + CargoID cid = cs->Index(); if (st->goods[cid].cargo.TotalCount() > 0) { /* For RTL we work in exactly the opposite direction. So * decrement the space needed first, then draw to the left @@ -581,7 +582,7 @@ public: break; case WID_STL_CARGOALL: { - for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) { + for (uint i = 0; i < _sorted_standard_cargo_specs.size(); i++) { this->LowerWidget(WID_STL_CARGOSTART + i); } this->LowerWidget(WID_STL_NOCARGOWAITING); @@ -607,7 +608,7 @@ public: this->include_empty = !this->include_empty; this->ToggleWidgetLoweredState(WID_STL_NOCARGOWAITING); } else { - for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) { + for (uint i = 0; i < _sorted_standard_cargo_specs.size(); i++) { this->RaiseWidget(WID_STL_CARGOSTART + i); } @@ -629,7 +630,7 @@ public: ToggleBit(this->cargo_filter, cs->Index()); this->ToggleWidgetLoweredState(widget); } else { - for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) { + for (uint i = 0; i < _sorted_standard_cargo_specs.size(); i++) { this->RaiseWidget(WID_STL_CARGOSTART + i); } this->RaiseWidget(WID_STL_NOCARGOWAITING); @@ -724,7 +725,7 @@ static NWidgetBase *CargoWidgets(int *biggest_index) { NWidgetHorizontal *container = new NWidgetHorizontal(); - for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) { + for (uint i = 0; i < _sorted_standard_cargo_specs.size(); i++) { NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, WID_STL_CARGOSTART + i); panel->SetMinimalSize(14, 0); panel->SetMinimalTextLines(1, 0, FS_NORMAL); @@ -733,7 +734,7 @@ static NWidgetBase *CargoWidgets(int *biggest_index) panel->SetDataTip(0, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE); container->Add(panel); } - *biggest_index = WID_STL_CARGOSTART + _sorted_standard_cargo_specs_size; + *biggest_index = WID_STL_CARGOSTART + static_cast(_sorted_standard_cargo_specs.size()); return container; } @@ -1859,8 +1860,7 @@ struct StationViewWindow : public Window { DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_SUPPLY_RATINGS_TITLE); y += FONT_HEIGHT_NORMAL; - const CargoSpec *cs; - FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) { + for (const CargoSpec *cs : _sorted_standard_cargo_specs) { const GoodsEntry *ge = &st->goods[cs->Index()]; if (!ge->HasRating()) continue; -- cgit v1.2.3-70-g09d2